Fixed a bug in Common::Array (including a unit test for it), and changed the way the internal storage growth over time.
The bug could result in incorrect results when using push_back (or insert_at) to insert data from an array into itself if this insertions would cause the internal array storage to grow. Also added a unit test for this bug. Furthermore, if the internal storage needs to grow, it will now be resized to the next power of two, instead of being increased by 32. svn-id: r40907
This commit is contained in:
parent
ae378632cd
commit
94dfc8f638
2 changed files with 124 additions and 23 deletions
|
@ -124,6 +124,29 @@ class ArrayTestSuite : public CxxTest::TestSuite
|
|||
TS_ASSERT_EQUALS(array2.size(), (unsigned int)3);
|
||||
}
|
||||
|
||||
struct SafeInt {
|
||||
int val;
|
||||
SafeInt() : val(0) {}
|
||||
SafeInt(int v) : val(v) {}
|
||||
~SafeInt() { val = -1; }
|
||||
bool operator==(int v) {
|
||||
return val == v;
|
||||
}
|
||||
};
|
||||
|
||||
void test_push_back_ex() {
|
||||
// This test makes sure that inserting an element invalidates
|
||||
// references/iterators/pointers to elements in the array itself
|
||||
// only *after* their value has been copied.
|
||||
Common::Array<SafeInt> array;
|
||||
|
||||
array.push_back(42);
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
array.push_back(array[0]);
|
||||
TS_ASSERT_EQUALS(array[i], 42);
|
||||
}
|
||||
}
|
||||
|
||||
void test_copy_constructor() {
|
||||
Common::Array<int> array1;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue