text
stringlengths
0
2.2M
u.expect (! hashMap.contains (key));
for (auto pair : groundTruth.pairs)
u.expectEquals (hashMap[pair.key], pair.value);
}
}
};
// ensure that the addresses of object references don't change
struct PersistantMemoryLocationOfValues
{
struct AddressAndValue { int value; const int* valueAddress; };
template <typename KeyType>
static void run (UnitTest& u)
{
AssociativeMap<KeyType, AddressAndValue> groundTruth;
HashMap<KeyType, int> hashMap;
RandomKeys<KeyType> keyOracle (300, 3827829);
Random valueOracle (48735);
for (int i = 0; i < 1000; ++i)
{
auto key = keyOracle.next();
auto value = valueOracle.nextInt();
hashMap.set (key, value);
if (auto* existing = groundTruth.find (key))
{
// don't change the address: only the value
existing->value = value;
}
else
{
groundTruth.add (key, { value, &hashMap.getReference (key) });
}
for (auto pair : groundTruth.pairs)
{
const auto& hashMapValue = hashMap.getReference (pair.key);
u.expectEquals (hashMapValue, pair.value.value);
u.expect (&hashMapValue == pair.value.valueAddress);
}
}
auto n = groundTruth.size();
Random r (3827387);
for (int i = 0; i < 100; ++i)
{
auto idx = r.nextInt (n-- - 1);
auto key = groundTruth.pairs.getReference (idx).key;
groundTruth.pairs.remove (idx);
hashMap.remove (key);
for (auto pair : groundTruth.pairs)
{
const auto& hashMapValue = hashMap.getReference (pair.key);
u.expectEquals (hashMapValue, pair.value.value);
u.expect (&hashMapValue == pair.value.valueAddress);
}
}
}
};
//==============================================================================
template <class Test>
void doTest (const String& testName)
{
beginTest (testName);
Test::template run<int> (*this);
Test::template run<void*> (*this);
Test::template run<String> (*this);
}
//==============================================================================
template <typename KeyType, typename ValueType>
struct AssociativeMap
{
struct KeyValuePair { KeyType key; ValueType value; };
ValueType* find (KeyType key)
{
auto n = pairs.size();
for (int i = 0; i < n; ++i)
{
auto& pair = pairs.getReference (i);
if (pair.key == key)
return &pair.value;
}