diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-10-04 23:11:51 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-10-05 14:16:50 +0200 |
commit | f46004b68ba6a2edf701a960c30b8f9d2955a210 (patch) | |
tree | 0f9c5114ccc0c5f983803cb58e48669d474fc8b9 /test | |
parent | d1e3660288b33421456bcf459a03d7619aa3a0c4 (diff) |
test: add hash test
Diffstat (limited to 'test')
-rw-r--r-- | test/app.hpp | 1 | ||||
-rw-r--r-- | test/hash.cpp | 44 | ||||
-rw-r--r-- | test/main.cpp | 1 |
3 files changed, 46 insertions, 0 deletions
diff --git a/test/app.hpp b/test/app.hpp index db1d11a9..2a2e39c8 100644 --- a/test/app.hpp +++ b/test/app.hpp @@ -41,6 +41,7 @@ struct test_app final : private FM_APPLICATION static void test_loader(); static void test_bitmask(); static void test_path_search(); + static void test_hash(); static void zzz_test_misc(); }; } // namespace floormat diff --git a/test/hash.cpp b/test/hash.cpp new file mode 100644 index 00000000..3a8af600 --- /dev/null +++ b/test/hash.cpp @@ -0,0 +1,44 @@ +#include "app.hpp" +#include "compat/int-hash.hpp" +#include <bitset> +#include <memory> + +namespace floormat { + +void test_app::test_hash() +{ + constexpr int max = 8; + constexpr size_t size = (2*max+1)*(2*max+1)*4 * 10; + constexpr size_t max_collisions = size*2/100; + size_t iter = 0, num_collisions = 0; + auto bitset_ = std::make_unique<std::bitset<size>>(false); + auto& bitset = *bitset_; + + for (int j = -max; j <= max; j++) + { + for (int i = -max; i <= max; i++) + { + for (int k = -2; k <= 1; k++) + { + ++iter; + uint64_t value = 0; + 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); + //Debug {} << "bitset:" << i << j << k << "=" << x % size << "/" << x; + x %= size; +#if 1 + if (bitset.test(x) && ++num_collisions >= max_collisions) + 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); +#endif + bitset.set(x); + } + } + } +} + +} // namespace floormat diff --git a/test/main.cpp b/test/main.cpp index 131e8452..4ff866bf 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -31,6 +31,7 @@ int test_app::exec() test_serializer_2(); test_path_search(); test_math(); + test_hash(); zzz_test_misc(); return 0; |