32 doTest<AddElementsTest> (
"AddElementsTest");
33 doTest<AccessTest> (
"AccessTest");
34 doTest<RemoveTest> (
"RemoveTest");
35 doTest<PersistantMemoryLocationOfValues> (
"PersistantMemoryLocationOfValues");
41 template <
typename KeyType>
48 Random valueOracle (48735);
51 for (
int i = 0; i < 10000; ++i)
53 auto key = keyOracle.next();
54 auto value = valueOracle.
nextInt();
56 bool contains = (groundTruth.find (key) !=
nullptr);
59 groundTruth.add (key, value);
60 hashMap.
set (key, value);
62 if (! contains) totalValues++;
71 template <
typename KeyType>
77 fillWithRandomValues (hashMap, groundTruth);
79 for (
auto pair : groundTruth.pairs)
86 template <
typename KeyType>
92 fillWithRandomValues (hashMap, groundTruth);
93 auto n = groundTruth.size();
97 for (
int i = 0; i < 100; ++i)
100 auto key = groundTruth.pairs.getReference (idx).key;
102 groundTruth.pairs.remove (idx);
107 for (
auto pair : groundTruth.pairs)
118 template <
typename KeyType>
125 Random valueOracle (48735);
127 for (
int i = 0; i < 1000; ++i)
129 auto key = keyOracle.next();
130 auto value = valueOracle.nextInt();
132 hashMap.
set (key, value);
134 if (
auto* existing = groundTruth.find (key))
137 existing->value = value;
141 groundTruth.add (key, { value, &hashMap.
getReference (key) });
144 for (
auto pair : groundTruth.pairs)
146 const auto& hashMapValue = hashMap.
getReference (pair.key);
149 u.
expect (&hashMapValue == pair.value.valueAddress);
153 auto n = groundTruth.size();
156 for (
int i = 0; i < 100; ++i)
158 auto idx = r.nextInt (n-- - 1);
159 auto key = groundTruth.pairs.getReference (idx).key;
161 groundTruth.pairs.remove (idx);
164 for (
auto pair : groundTruth.pairs)
166 const auto& hashMapValue = hashMap.
getReference (pair.key);
169 u.
expect (&hashMapValue == pair.value.valueAddress);
176 template <
class Test>
177 void doTest (
const String& testName)
181 Test::template run<int> (*
this);
182 Test::template run<void*> (*
this);
183 Test::template run<String> (*
this);
187 template <
typename KeyType,
typename ValueType>
192 ValueType* find (KeyType key)
194 auto n = pairs.size();
196 for (
int i = 0; i < n; ++i)
198 auto& pair = pairs.getReference (i);
207 void add (KeyType key, ValueType value)
209 if (ValueType* v = find (key))
212 pairs.add ({key, value});
215 int size()
const {
return pairs.size(); }
217 Array<KeyValuePair> pairs;
220 template <
typename KeyType,
typename ValueType>
221 static void fillWithRandomValues (HashMap<KeyType, int>& hashMap, AssociativeMap<KeyType, ValueType>& groundTruth)
223 RandomKeys<KeyType> keyOracle (300, 3827829);
224 Random valueOracle (48735);
226 for (
int i = 0; i < 10000; ++i)
228 auto key = keyOracle.next();
229 auto value = valueOracle.nextInt();
231 groundTruth.add (key, value);
232 hashMap.set (key, value);
237 template <
typename KeyType>
241 RandomKeys (
int maxUniqueKeys,
int seed) : r (seed)
243 for (
int i = 0; i < maxUniqueKeys; ++i)
244 keys.
add (generateRandomKey (r));
247 const KeyType& next()
253 static KeyType generateRandomKey (
Random&);
261template <>
void* HashMapTest::RandomKeys<void*>::generateRandomKey (Random& rnd) {
return reinterpret_cast<void*
> (rnd.nextInt64()); }
263template <> String HashMapTest::RandomKeys<String>::generateRandomKey (Random& rnd)
267 int len = rnd.nextInt (8)+1;
268 for (
int i = 0; i < len; ++i)
269 str +=
static_cast<char> (rnd.nextInt (95) + 32);
274static HashMapTest hashMapTest;
Holds a resizable array of primitive or copy-by-value objects.
int size() const noexcept
Returns the current number of elements in the array.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
ElementType & getReference(int index) const noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Holds a set of mappings between some key/value pairs.
void remove(KeyTypeParameter keyToRemove)
Removes an item with the given key.
void set(KeyTypeParameter newKey, ValueTypeParameter newValue)
Adds or replaces an element in the hash-map.
bool contains(KeyTypeParameter keyToLookFor) const
Returns true if the map contains an item with the specied key.
ValueType & getReference(KeyTypeParameter keyToLookFor)
Returns a reference to the value corresponding to a given key.
int size() const noexcept
Returns the current number of items in the map.
A random number generator.
int nextInt() noexcept
Returns the next random 32 bit integer.
This is a base class for classes that perform a unit test.
void expectEquals(ValueType actual, ValueType expected, String failureMessage=String())
Compares a value to an expected value.
void beginTest(const String &testName)
Tells the system that a new subsection of tests is beginning.
void expect(bool testResult, const String &failureMessage=String())
Checks that the result of a test is true, and logs this result.
void runTest() override
Implement this method in your subclass to actually run your tests.