41 template <
typename KeyType>
51 for (
int i = 0; i < 10000; ++i)
56 bool contains = (
groundTruth.find (key) !=
nullptr);
60 hashMap.
set (key, value);
71 template <
typename KeyType>
86 template <
typename KeyType>
97 for (
int i = 0; i < 100; ++i)
118 template <
typename KeyType>
127 for (
int i = 0; i < 1000; ++i)
132 hashMap.
set (key, value);
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);
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>
194 auto n = pairs.
size();
196 for (
int i = 0; i <
n; ++i)
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>
244 keys.
add (generateRandomKey (r));
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 remove(int indexToRemove)
Removes an element from 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.
void set(int indexToChange, ParameterType newValue)
Replaces an element with a new value.
bool contains(ParameterType elementToLookFor) const
Returns true if the array contains at least one occurrence of an object.
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.