aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Horn2009-04-11 00:29:34 +0000
committerMax Horn2009-04-11 00:29:34 +0000
commit7db2648ba00137d5cc5b7fc26f1ab54584dd3ad0 (patch)
tree71ae2477e398ddcff1d9f5fba2553715d3467793
parent7412b0f8e87a0139ff26309bf9db8024a63b4fb9 (diff)
downloadscummvm-rg350-7db2648ba00137d5cc5b7fc26f1ab54584dd3ad0.tar.gz
scummvm-rg350-7db2648ba00137d5cc5b7fc26f1ab54584dd3ad0.tar.bz2
scummvm-rg350-7db2648ba00137d5cc5b7fc26f1ab54584dd3ad0.zip
COMMON: Added unit test for Common::List::size(); made List::size() slightly more efficient; same for remove() and operator=
svn-id: r39921
-rw-r--r--common/list.h21
-rw-r--r--test/common/list.h13
2 files changed, 25 insertions, 9 deletions
diff --git a/common/list.h b/common/list.h
index 325c8e8bd5..b2ad3fbc18 100644
--- a/common/list.h
+++ b/common/list.h
@@ -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;
}
diff --git a/test/common/list.h b/test/common/list.h
index 356693c33e..75a961a604 100644
--- a/test/common/list.h
+++ b/test/common/list.h
@@ -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;