text
stringlengths 0
2.2M
|
---|
JUCE is an open source library subject to commercial or open-source
|
licensing.
|
The code included in this file is provided under the terms of the ISC license
|
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
|
To use, copy, modify, and/or distribute this software for any purpose with or
|
without fee is hereby granted provided that the above copyright notice and
|
this permission notice appear in all copies.
|
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
DISCLAIMED.
|
==============================================================================
|
*/
|
namespace juce
|
{
|
struct HashMapTest : public UnitTest
|
{
|
HashMapTest()
|
: UnitTest ("HashMap", UnitTestCategories::containers)
|
{}
|
void runTest() override
|
{
|
doTest<AddElementsTest> ("AddElementsTest");
|
doTest<AccessTest> ("AccessTest");
|
doTest<RemoveTest> ("RemoveTest");
|
doTest<PersistantMemoryLocationOfValues> ("PersistantMemoryLocationOfValues");
|
}
|
//==============================================================================
|
struct AddElementsTest
|
{
|
template <typename KeyType>
|
static void run (UnitTest& u)
|
{
|
AssociativeMap<KeyType, int> groundTruth;
|
HashMap<KeyType, int> hashMap;
|
RandomKeys<KeyType> keyOracle (300, 3827829);
|
Random valueOracle (48735);
|
int totalValues = 0;
|
for (int i = 0; i < 10000; ++i)
|
{
|
auto key = keyOracle.next();
|
auto value = valueOracle.nextInt();
|
bool contains = (groundTruth.find (key) != nullptr);
|
u.expectEquals ((int) contains, (int) hashMap.contains (key));
|
groundTruth.add (key, value);
|
hashMap.set (key, value);
|
if (! contains) totalValues++;
|
u.expectEquals (hashMap.size(), totalValues);
|
}
|
}
|
};
|
struct AccessTest
|
{
|
template <typename KeyType>
|
static void run (UnitTest& u)
|
{
|
AssociativeMap<KeyType, int> groundTruth;
|
HashMap<KeyType, int> hashMap;
|
fillWithRandomValues (hashMap, groundTruth);
|
for (auto pair : groundTruth.pairs)
|
u.expectEquals (hashMap[pair.key], pair.value);
|
}
|
};
|
struct RemoveTest
|
{
|
template <typename KeyType>
|
static void run (UnitTest& u)
|
{
|
AssociativeMap<KeyType, int> groundTruth;
|
HashMap<KeyType, int> hashMap;
|
fillWithRandomValues (hashMap, groundTruth);
|
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);
|
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.