From ffe8ccde2157ef802b7876cef9489904c02bcece Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 26 Jul 2007 08:16:44 +0000 Subject: Add Table unit test. Match std::map interface for empty and insert (more powerful insert interface). git-svn-id: http://svn.drobilla.net/lad/raul@631 a436a847-0d15-0410-975c-d299462d15a1 --- tests/table_test.cpp | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/table_test.cpp (limited to 'tests/table_test.cpp') diff --git a/tests/table_test.cpp b/tests/table_test.cpp new file mode 100644 index 0000000..06bf489 --- /dev/null +++ b/tests/table_test.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + +using namespace Raul; +using namespace std; + +int +main() +{ + Table t; + for (size_t i=0; i < 20; ++i) { + int val = rand()%10; + t.insert(make_pair(val, val)); + } + + for (Table::const_iterator i = t.begin(); i != t.end(); ++i) + cout << i->first << " "; + cout << endl; + + Table::iterator first = t.begin(); + ++first; + Table::iterator last = first; + ++last; ++last; ++last; + + cout << "Erasing elements 1..3:" << endl; + t.erase(first, last); + + for (Table::const_iterator i = t.begin(); i != t.end(); ++i) + cout << i->first << " "; + cout << endl; + + cout << "Erasing elements 0..3" << endl; + first = t.begin(); + last = first; + ++last; ++last; ++last; + t.erase(first, last); + + for (Table::const_iterator i = t.begin(); i != t.end(); ++i) + cout << i->first << " "; + cout << endl; + + cout << "Erasing elements end()-2..end()" << endl; + last = t.end(); + first = last; + --first; --first; + t.erase(first, last); + + for (Table::const_iterator i = t.begin(); i != t.end(); ++i) + cout << i->first << " "; + cout << endl; + + cout << "Erasing everything" << endl; + first = t.begin(); + last = t.end(); + t.erase(first, last); + + for (Table::const_iterator i = t.begin(); i != t.end(); ++i) + cout << i->first << " "; + cout << endl; + + /* **** */ + + cout << "Assuming you built with debugging, if this continues to run " + << "and chews your CPU without dying, everything's good." << endl; + srand(time(NULL)); + + Table st; + + st.insert(make_pair("apple", "core")); + st.insert(make_pair("candy", "cane")); + st.insert(make_pair("banana", "peel")); + + st.erase("apple"); + + while (true) { + Table t; + + size_t table_size = (rand() % 1000) + 1; + + for (size_t i=0; i < table_size; ++i) { + int val = rand()%100; + t.insert(make_pair(val, ((val + 3) * 17))); + } + + for (int i=0; i < (int)table_size; ++i) { + int val = rand()%100; + Table::iterator iter = t.find(val); + assert(iter == t.end() || iter->second == (val + 3) * 17); + } + + /*cout << "CONTENTS:" << endl; + + for (Table::const_iterator i = t.begin(); i != t.end(); ++i) { + cout << i->first << ": " << i->second << endl; + } + + Table::iterator i = t.find(7); + if (i != t.end()) + cout << "Find: 7: " << i->second << endl; + */ + } + + return 0; +} + -- cgit v1.2.1