diff options
-rw-r--r-- | common/array.h | 12 | ||||
-rw-r--r-- | test/common/array.h | 21 |
2 files changed, 31 insertions, 2 deletions
diff --git a/common/array.h b/common/array.h index 693c024d11..2db0ed2a8b 100644 --- a/common/array.h +++ b/common/array.h @@ -46,12 +46,20 @@ public: public: Array() : _capacity(0), _size(0), _storage(0) {} Array(const Array<T> &array) : _capacity(0), _size(0), _storage(0) { - _size = array._size; - _capacity = _size + 32; + _capacity = _size = array._size; _storage = new T[_capacity]; copy(array._storage, array._storage + _size, _storage); } + /** + * Construct an array by copying data from a regular array. + */ + Array(const T *data, int n) { + _capacity = _size = n; + _storage = new T[_capacity]; + copy(data, data + _size, _storage); + } + ~Array() { delete[] _storage; } diff --git a/test/common/array.h b/test/common/array.h index 2a41cd5b6a..ad92acaf3a 100644 --- a/test/common/array.h +++ b/test/common/array.h @@ -73,6 +73,8 @@ class ArrayTestSuite : public CxxTest::TestSuite TS_ASSERT_EQUALS( array[2], 33 ); TS_ASSERT_EQUALS( array[3], 25 ); TS_ASSERT_EQUALS( array[4], -11 ); + + TS_ASSERT_EQUALS( array.size(), 5 ); } void test_remove_at() { @@ -92,6 +94,8 @@ class ArrayTestSuite : public CxxTest::TestSuite TS_ASSERT_EQUALS( array[1], 33 ); TS_ASSERT_EQUALS( array[2], 25 ); TS_ASSERT_EQUALS( array[3], -11 ); + + TS_ASSERT_EQUALS( array.size(), 4 ); } void test_push_back() { @@ -114,6 +118,9 @@ class ArrayTestSuite : public CxxTest::TestSuite TS_ASSERT_EQUALS( array1[3], 3 ); TS_ASSERT_EQUALS( array1[4], -2 ); TS_ASSERT_EQUALS( array1[5], -131 ); + + TS_ASSERT_EQUALS( array1.size(), 6 ); + TS_ASSERT_EQUALS( array2.size(), 3 ); } void test_copy_constructor() { @@ -129,5 +136,19 @@ class ArrayTestSuite : public CxxTest::TestSuite TS_ASSERT_EQUALS( array2[0], -3 ); TS_ASSERT_EQUALS( array2[1], 5 ); TS_ASSERT_EQUALS( array2[2], 9 ); + + TS_ASSERT_EQUALS( array2.size(), 3 ); + } + + void test_array_constructor() { + const int array1[] = { -3, 5, 9 }; + + Common::Array<int> array2(array1, 3); + + TS_ASSERT_EQUALS( array2[0], -3 ); + TS_ASSERT_EQUALS( array2[1], 5 ); + TS_ASSERT_EQUALS( array2[2], 9 ); + + TS_ASSERT_EQUALS( array2.size(), 3 ); } }; |