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(); }
220 template <
typename KeyType,
typename ValueType>
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()
249 int i = r.nextInt (keys.size() - 1);
250 return keys.getReference (i);
253 static KeyType generateRandomKey (
Random&);
268 for (
int i = 0; i < len; ++i)
269 str += static_cast<char> (rnd.
nextInt (95) + 32);
Holds a set of mappings between some key/value pairs.
void remove(KeyTypeParameter keyToRemove)
Removes an item with the given key.
int nextInt() noexcept
Returns the next random 32 bit integer.
int size() const noexcept
Returns the current number of items in the map.
UnitTest(const String &name, const String &category=String())
Creates a test with the given name and optionally places it in a category.
void expectEquals(ValueType actual, ValueType expected, String failureMessage=String())
Compares a value to an expected value.
int64 nextInt64() noexcept
Returns the next 64-bit random number.
This is a base class for classes that perform a unit test.
void set(KeyTypeParameter newKey, ValueTypeParameter newValue)
Adds or replaces an element in the hash-map.
void beginTest(const String &testName)
Tells the system that a new subsection of tests is beginning.
void runTest() override
Implement this method in your subclass to actually run your tests.
Holds a resizable array of primitive or copy-by-value objects.
void expect(bool testResult, const String &failureMessage=String())
Checks that the result of a test is true, and logs this result.
A random number generator.
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.