1 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 #include <map> 6 7 #include <TestUtils.h> 8 #include <cppunit/Test.h> 9 #include <cppunit/TestCaller.h> 10 #include <cppunit/TestSuite.h> 11 12 #include <VectorMap.h> 13 14 #include "common.h" 15 #include "OrderedMapTest.h" 16 #include "VectorMapTest.h" 17 18 // That's how it should work, if we had a working compiler. 19 /* 20 template<typename Key, typename Value> 21 struct PairTestBase { 22 typedef SimpleValueStrategy<Key> KeyStrategy; 23 typedef SimpleValueStrategy<Value> ValueStrategy; 24 typedef PairEntryStrategy<KeyStrategy, ValueStrategy> EntryStrategy; 25 26 template<typename CompareStrategy> 27 struct MyMap 28 : public VectorMap<Key, Value, VectorMapEntryStrategy::Pair<Key, Value, 29 CompareStrategy> > { 30 }; 31 32 template<class CompareStrategy> 33 struct Strategy : public TestStrategy<MyMap, EntryStrategy, 34 CompareStrategy> { 35 }; 36 }; 37 typedef typename PairTestBase<int, int>::Strategy IntIntTestStrategy; 38 // ... 39 */ 40 41 // ugly work-around: 42 43 template<typename Key, typename Value> 44 struct PairTestBase { 45 typedef SimpleValueStrategy<Key> KeyStrategy; 46 typedef SimpleValueStrategy<Value> ValueStrategy; 47 typedef PairEntryStrategy<KeyStrategy, ValueStrategy> EntryStrategy; 48 49 template<typename CompareStrategy> 50 struct MyMap 51 : public VectorMap<Key, Value, VectorMapEntryStrategy::Pair<Key, Value, 52 CompareStrategy> > { 53 }; 54 }; 55 56 #define DECLARE_TEST_STRATEGY(Key, Value, Map, Strategy, ClassName) \ 57 template<typename CS> struct Map : PairTestBase<Key, Value>::MyMap<CS> {};\ 58 template<typename CS> \ 59 struct Strategy \ 60 : public TestStrategy<Map, PairTestBase<Key, Value>::EntryStrategy, \ 61 CS> { \ 62 static const char *kClassName; \ 63 }; \ 64 template<typename CS> const char *Strategy<CS>::kClassName = ClassName; 65 66 DECLARE_TEST_STRATEGY(int, int, IntIntMap, IntIntTestStrategy, 67 "VectorMap<int, int, Pair>") 68 DECLARE_TEST_STRATEGY(int, string, IntStringMap, IntStringTestStrategy, 69 "VectorMap<int, string, Pair>") 70 DECLARE_TEST_STRATEGY(string, int, StringIntMap, StringIntTestStrategy, 71 "VectorMap<string, int, Pair>") 72 DECLARE_TEST_STRATEGY(string, string, StringStringMap, 73 StringStringTestStrategy, 74 "VectorMap<string, string, Pair>") 75 76 77 // TestStrategy for the ImplicitKey entry strategy 78 79 // string_hash (from the Dragon Book: a slightly modified hashpjw()) 80 static inline 81 int 82 string_hash(const char *name) 83 { 84 uint32 h = 0; 85 for (; *name; name++) { 86 if (uint32 g = h & 0xf0000000) 87 h ^= g >> 24; 88 h = (h << 4) + *name; 89 } 90 return (int)h; 91 } 92 93 struct ImplicitKeyTestGetKey { 94 int operator()(string value) const 95 { 96 return string_hash(value.c_str()); 97 } 98 }; 99 100 template<typename CompareStrategy> 101 struct ImplicitKeyTestMap 102 : public VectorMap<int, string, 103 VectorMapEntryStrategy::ImplicitKey<int, string, ImplicitKeyTestGetKey, 104 CompareStrategy> > { 105 }; 106 107 typedef ImplicitKeyStrategy<SimpleValueStrategy<int>, 108 SimpleValueStrategy<string>, 109 ImplicitKeyTestGetKey> 110 ImplicitKeyTestEntryStrategy; 111 112 template<class CompareStrategy> 113 struct ImplicitKeyTestStrategy : public TestStrategy< 114 ImplicitKeyTestMap, ImplicitKeyTestEntryStrategy, CompareStrategy> { 115 static const char *kClassName; 116 }; 117 template<class CompareStrategy> 118 const char *ImplicitKeyTestStrategy<CompareStrategy>::kClassName 119 = "VectorMap<int, string, ImplicitKey>"; 120 121 122 // constructor 123 VectorMapTest::VectorMapTest(std::string name) 124 : BTestCase(name) 125 { 126 } 127 128 // Suite 129 CppUnit::Test* 130 VectorMapTest::Suite() 131 { 132 CppUnit::TestSuite *suite = new CppUnit::TestSuite("VectorMap"); 133 134 suite->addTest(OrderedMapTest<IntIntTestStrategy>::Suite()); 135 suite->addTest(OrderedMapTest<IntStringTestStrategy>::Suite()); 136 suite->addTest(OrderedMapTest<StringIntTestStrategy>::Suite()); 137 suite->addTest(OrderedMapTest<StringStringTestStrategy>::Suite()); 138 suite->addTest(OrderedMapTest<ImplicitKeyTestStrategy>::Suite()); 139 140 return suite; 141 } 142 143