COMMON: Added unit test for Common::List::size(); made List::size() slightly more efficient; same for remove() and operator=

svn-id: r39921
This commit is contained in:
Max Horn 2009-04-11 00:29:34 +00:00
parent 7412b0f8e8
commit 7db2648ba0
2 changed files with 25 additions and 9 deletions

View File

@ -119,8 +119,9 @@ public:
void remove(const t_T &val) {
iterator i = begin();
while (i != end())
if (val == i.operator*())
const iterator e = end();
while (i != e)
if (val == *i)
i = erase(i);
else
++i;
@ -135,16 +136,18 @@ public:
List<t_T> &operator=(const List<t_T> &list) {
if (this != &list) {
iterator i;
const_iterator j;
const iterator e = end();
const_iterator i2;
const_iterator e2 = list.end();
for (i = begin(), j = list.begin(); (i != end()) && (j != list.end()) ; ++i, ++j) {
static_cast<Node *>(i._node)->_data = static_cast<const Node *>(j._node)->_data;
for (i = begin(), i2 = list.begin(); (i != e) && (i2 != e2) ; ++i, ++i2) {
static_cast<Node *>(i._node)->_data = static_cast<const Node *>(i2._node)->_data;
}
if (i == end())
insert(i, j, list.end());
if (i == e)
insert(i, i2, e2);
else
erase(i, end());
erase(i, e);
}
return *this;
@ -152,7 +155,7 @@ public:
uint size() const {
int n = 0;
for (const_iterator i = begin(); i != end(); ++i)
for (const NodeBase *cur = _anchor._next; cur != &_anchor; cur = cur->_next)
++n;
return n;
}

View File

@ -16,6 +16,19 @@ class ListTestSuite : public CxxTest::TestSuite
TS_ASSERT( container.empty() );
}
public:
void test_size( void )
{
Common::List<int> container;
TS_ASSERT( container.size() == 0 );
container.push_back(17);
TS_ASSERT( container.size() == 1 );
container.push_back(33);
TS_ASSERT( container.size() == 2 );
container.clear();
TS_ASSERT( container.size() == 0 );
}
void test_iterator_begin_end( void )
{
Common::List<int> container;