summaryrefslogtreecommitdiffhomepage
path: root/test/hash.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/hash.cpp')
-rw-r--r--test/hash.cpp34
1 files changed, 31 insertions, 3 deletions
diff --git a/test/hash.cpp b/test/hash.cpp
index 3a8af600..4e8d9d81 100644
--- a/test/hash.cpp
+++ b/test/hash.cpp
@@ -1,11 +1,31 @@
#include "app.hpp"
#include "compat/int-hash.hpp"
+#include <Corrade/Containers/StringView.h>
#include <bitset>
#include <memory>
namespace floormat {
-void test_app::test_hash()
+namespace {
+
+void test_simple()
+{
+ constexpr StringView list[] = { "foo"_s, "bar"_s, "bar\0"_s, "bar2"_s, "baz"_s, };
+ constexpr auto size = arraySize(list);
+
+ size_t hashes[size] = {};
+ for (auto i = 0uz; i < size; i++)
+ hashes[i] = hash_64(list[i].data(), list[i].size());
+
+ for (auto i = 0uz; i < size; i++)
+ for (auto j = i+1; j < size; j++)
+ {
+ if (hashes[i] == hashes[j])
+ fm_abort("hash collision between '%s' and '%s': 0x%p", list[i].data(), list[j].data(), (void*)hashes[i]);
+ }
+}
+
+void test_collisions()
{
constexpr int max = 8;
constexpr size_t size = (2*max+1)*(2*max+1)*4 * 10;
@@ -25,7 +45,7 @@ void test_app::test_hash()
value |= (uint64_t)(uint16_t)(int16_t)i << 0;
value |= (uint64_t)(uint16_t)(int16_t)j << 16;
value |= (uint64_t)(uint16_t)(int16_t)k << 32;
- auto x = (size_t)int_hash(value);
+ auto x = (size_t)hash_int(value);
//Debug {} << "bitset:" << i << j << k << "=" << x % size << "/" << x;
x %= size;
#if 1
@@ -33,7 +53,7 @@ void test_app::test_hash()
fm_abort("test/bitset: %zu collisions at iter %zu id %zu (%d;%d:%d)", num_collisions, iter, x, i, j, k);
#else
if ((void)max_collisions, bitset.test(x))
- fm_warn("test/bitset: %zu collisions at iter %zu id %zu (%d;%d:%d)", ++num_collisions, iter, x, i, j, k);
+ fm_warn("test/bitset: %zu collisions at iter %zu id %zu (%d;%d:%d)", ++num_collisions, iter, x, i, j, k);
#endif
bitset.set(x);
}
@@ -41,4 +61,12 @@ void test_app::test_hash()
}
}
+} // namespace
+
+void test_app::test_hash()
+{
+ test_simple();
+ test_collisions();
+}
+
} // namespace floormat