aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/common/hashmap.h93
-rw-r--r--test/common/str.h8
-rw-r--r--test/module.mk2
3 files changed, 102 insertions, 1 deletions
diff --git a/test/common/hashmap.h b/test/common/hashmap.h
index c62f909f95..b90d91467c 100644
--- a/test/common/hashmap.h
+++ b/test/common/hashmap.h
@@ -86,6 +86,24 @@ class HashMapTestSuite : public CxxTest::TestSuite
TS_ASSERT_EQUALS(container[4], 96);
}
+ void test_lookup_with_default() {
+ Common::HashMap<int, int> container;
+ container[0] = 17;
+ container[1] = -1;
+ container[2] = 45;
+ container[3] = 12;
+ container[4] = 96;
+
+ // We take a const ref now to ensure that the map
+ // is not modified by getVal.
+ const Common::HashMap<int, int> &containerRef = container;
+
+ TS_ASSERT_EQUALS(containerRef.getVal(0), 17);
+ TS_ASSERT_EQUALS(containerRef.getVal(17), 0);
+ TS_ASSERT_EQUALS(containerRef.getVal(0, -10), 17);
+ TS_ASSERT_EQUALS(containerRef.getVal(17, -10), -10);
+ }
+
void test_iterator_begin_end() {
Common::HashMap<int, int> container;
@@ -108,5 +126,80 @@ class HashMapTestSuite : public CxxTest::TestSuite
TS_ASSERT_EQUALS(container2[323], 32);
}
+ void test_collision() {
+ // NB: The usefulness of this example depends strongly on the
+ // specific hashmap implementation.
+ // It is constructed to insert multiple colliding elements.
+ Common::HashMap<int, int> h;
+ h[5] = 1;
+ h[32+5] = 1;
+ h[64+5] = 1;
+ h[128+5] = 1;
+ TS_ASSERT(h.contains(5));
+ TS_ASSERT(h.contains(32+5));
+ TS_ASSERT(h.contains(64+5));
+ TS_ASSERT(h.contains(128+5));
+ h.erase(32+5);
+ TS_ASSERT(h.contains(5));
+ TS_ASSERT(h.contains(64+5));
+ TS_ASSERT(h.contains(128+5));
+ h.erase(5);
+ TS_ASSERT(h.contains(64+5));
+ TS_ASSERT(h.contains(128+5));
+ h[32+5] = 1;
+ TS_ASSERT(h.contains(32+5));
+ TS_ASSERT(h.contains(64+5));
+ TS_ASSERT(h.contains(128+5));
+ h[5] = 1;
+ TS_ASSERT(h.contains(5));
+ TS_ASSERT(h.contains(32+5));
+ TS_ASSERT(h.contains(64+5));
+ TS_ASSERT(h.contains(128+5));
+ h.erase(5);
+ TS_ASSERT(h.contains(32+5));
+ TS_ASSERT(h.contains(64+5));
+ TS_ASSERT(h.contains(128+5));
+ h.erase(64+5);
+ TS_ASSERT(h.contains(32+5));
+ TS_ASSERT(h.contains(128+5));
+ h.erase(128+5);
+ TS_ASSERT(h.contains(32+5));
+ h.erase(32+5);
+ TS_ASSERT(h.empty());
+ }
+
+ void test_iterator() {
+ Common::HashMap<int, int> container;
+ container[0] = 17;
+ container[1] = 33;
+ container[2] = 45;
+ container[3] = 12;
+ container[4] = 96;
+ container.erase(1);
+ container[1] = 42;
+ container.erase(0);
+ container.erase(1);
+
+ int found = 0;
+ Common::HashMap<int, int>::iterator i;
+ for (i = container.begin(); i != container.end(); ++i) {
+ int key = i->_key;
+ TS_ASSERT(key >= 0 && key <= 4);
+ TS_ASSERT(!(found & (1 << key)));
+ found |= 1 << key;
+ }
+ TS_ASSERT(found == 16+8+4);
+
+ found = 0;
+ Common::HashMap<int, int>::const_iterator j;
+ for (j = container.begin(); j != container.end(); ++j) {
+ int key = j->_key;
+ TS_ASSERT(key >= 0 && key <= 4);
+ TS_ASSERT(!(found & (1 << key)));
+ found |= 1 << key;
+ }
+ TS_ASSERT(found == 16+8+4);
+}
+
// TODO: Add test cases for iterators, find, ...
};
diff --git a/test/common/str.h b/test/common/str.h
index bf0db98b09..f65b078bdc 100644
--- a/test/common/str.h
+++ b/test/common/str.h
@@ -285,4 +285,12 @@ class StringTestSuite : public CxxTest::TestSuite
TS_ASSERT(!Common::matchString("monkey.s99", "monkey.s*1"));
TS_ASSERT(Common::matchString("monkey.s101", "monkey.s*1"));
}
+
+ void test_string_printf() {
+ TS_ASSERT( Common::String::printf("") == "" );
+ TS_ASSERT( Common::String::printf("%s", "test") == "test" );
+ TS_ASSERT( Common::String::printf("%s.s%.02d", "monkey", 1) == "monkey.s01" );
+ TS_ASSERT( Common::String::printf("%s%X", "test", 1234) == "test4D2" );
+ TS_ASSERT( Common::String::printf("Some %s to make this string longer than the default built-in %s %d", "text", "capacity", 123456) == "Some text to make this string longer than the default built-in capacity 123456" );
+ }
};
diff --git a/test/module.mk b/test/module.mk
index 64cb34c7ac..79a1a12023 100644
--- a/test/module.mk
+++ b/test/module.mk
@@ -22,7 +22,7 @@ TEST_LDFLAGS :=
test: test/runner
./test/runner
test/runner: test/runner.cpp $(TEST_LIBS)
- $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TEST_LDFLAGS) $(TEST_CFLAGS) -o $@ $+
+ $(QUIET_LINK)$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TEST_LDFLAGS) $(TEST_CFLAGS) -o $@ $+
test/runner.cpp: $(TESTS)
@mkdir -p test
$(srcdir)/test/cxxtest/cxxtestgen.py $(TEST_FLAGS) -o $@ $+