diff options
author | Max Horn | 2009-04-20 19:27:11 +0000 |
---|---|---|
committer | Max Horn | 2009-04-20 19:27:11 +0000 |
commit | 78cf5a4ccfe4e36f33230bb196aad34eb3cf89f7 (patch) | |
tree | 15f038e525bc9a7d0900f339e558ea43e53e1856 | |
parent | faa911794953679a1fff24999878f79612dfc7f3 (diff) | |
download | scummvm-rg350-78cf5a4ccfe4e36f33230bb196aad34eb3cf89f7.tar.gz scummvm-rg350-78cf5a4ccfe4e36f33230bb196aad34eb3cf89f7.tar.bz2 scummvm-rg350-78cf5a4ccfe4e36f33230bb196aad34eb3cf89f7.zip |
COMMON & TESTS: Added new constructor to Array<T>, namely Array(const T* data, int n), which makes it possible to clone a regular array into a Common::Array; added a unit test for that and slightly extended existing Common::Array unit tests
svn-id: r40027
-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 ); } }; |