aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/array.h14
-rw-r--r--test/common/array.h42
2 files changed, 56 insertions, 0 deletions
diff --git a/common/array.h b/common/array.h
index f240a9c2f5..db1a62ba34 100644
--- a/common/array.h
+++ b/common/array.h
@@ -141,6 +141,12 @@ public:
insert_aux(_storage + idx, array.begin(), array.end());
}
+ /**
+ * Inserts element before pos.
+ */
+ void insert(iterator pos, const T &element) {
+ insert_aux(pos, &element, &element + 1);
+ }
T remove_at(size_type idx) {
assert(idx < _size);
@@ -187,6 +193,14 @@ public:
_capacity = 0;
}
+ iterator erase(iterator pos) {
+ copy(pos + 1, _storage + _size, pos);
+ _size--;
+ // We also need to destroy the last object properly here.
+ _storage[_size].~T();
+ return pos;
+ }
+
bool empty() const {
return (_size == 0);
}
diff --git a/test/common/array.h b/test/common/array.h
index f0027ec201..64354abc00 100644
--- a/test/common/array.h
+++ b/test/common/array.h
@@ -44,6 +44,48 @@ class ArrayTestSuite : public CxxTest::TestSuite
TS_ASSERT_EQUALS(iter, array.end());
}
+ void test_erase_iterator() {
+ Common::Array<int> array;
+ Common::Array<int>::iterator iter;
+
+ // Fill the array with some random data
+ array.push_back(17);
+ array.push_back(33);
+ array.push_back(-11);
+
+ iter = array.begin();
+ ++iter;
+
+ iter = array.erase(iter);
+ TS_ASSERT_DIFFERS(iter, array.end());
+ TS_ASSERT_EQUALS(*iter, -11);
+ TS_ASSERT_EQUALS(array.size(), (unsigned int)2);
+ TS_ASSERT_EQUALS(array[0], 17);
+ TS_ASSERT_EQUALS(array[1], -11);
+ }
+
+ void test_insert_iterator() {
+ Common::Array<int> array;
+ Common::Array<int>::iterator iter;
+
+ // Fill the array with some random data
+ array.push_back(17);
+ array.push_back(33);
+ array.push_back(-11);
+
+ iter = array.begin();
+ ++iter;
+
+ array.insert(iter, 99);
+
+ TS_ASSERT_EQUALS(*iter, 99);
+ TS_ASSERT_EQUALS(array.size(), (unsigned int)4);
+ TS_ASSERT_EQUALS(array[0], 17);
+ TS_ASSERT_EQUALS(array[1], 99);
+ TS_ASSERT_EQUALS(array[2], 33);
+ TS_ASSERT_EQUALS(array[3], -11);
+ }
+
void test_direct_access() {
Common::Array<int> array;