summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-11-05 15:53:03 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-11-05 15:53:03 +0100
commiteb60ef73b0af4ebbf35a67226edf1d8e61d972ae (patch)
treef9f002cf20cc62f831e996d360acdfd0806f746f /test
parent9c7e2fd21b6a39ad9f1eb0f699fc054559d2a6b9 (diff)
a
Diffstat (limited to 'test')
-rw-r--r--test/hash.cpp34
-rw-r--r--test/json/frameset-header2.json9
-rw-r--r--test/wall-atlas.cpp12
3 files changed, 48 insertions, 7 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
diff --git a/test/json/frameset-header2.json b/test/json/frameset-header2.json
new file mode 100644
index 00000000..8b32cabc
--- /dev/null
+++ b/test/json/frameset-header2.json
@@ -0,0 +1,9 @@
+{
+ "name": "foo",
+ "depth": 42,
+ "framesets": {
+ "w": {},
+ "n": {}
+ },
+ "frames": []
+}
diff --git a/test/wall-atlas.cpp b/test/wall-atlas.cpp
index b1cdd8d6..720c118f 100644
--- a/test/wall-atlas.cpp
+++ b/test/wall-atlas.cpp
@@ -4,18 +4,20 @@
#include "serialize/json-helper.hpp"
#include "loader/loader.hpp"
#include <algorithm>
+#include <Corrade/Containers/PairStl.h>
#include <Corrade/Utility/Path.h>
namespace floormat {
namespace ranges = std::ranges;
namespace test = floormat::Serialize::wall_test;
+using nlohmann::json;
namespace {
-void test_atlas_header(StringView path)
+Pair<wall_atlas_def, json> test_atlas_header(StringView path, StringView filename)
{
- auto j = json_helper::from_json_(Path::join(path, "frameset-header.json"_s));
+ auto j = json_helper::from_json_(Path::join(path, filename));
wall_atlas_def def;
test::read_atlas_header(j, def);
@@ -34,8 +36,10 @@ void test_atlas_header(StringView path)
fm_assert(def.frameset_indexes[W] == 1);
else if (def.frameset_indexes[N] == 1)
fm_assert(def.frameset_indexes[W] == 0);
+ else
+ fm_assert(false);
- std::fputs("", stdout);
+ return {std::move(def), std::move(j)};
}
} // namespace
@@ -46,7 +50,7 @@ void test_app::test_wall_atlas()
const auto path = Path::join(loader.TEMP_PATH, "test/json"_s);
fm_assert(Path::isDirectory(path));
- test_atlas_header(path);
+ test_atlas_header(path, "frameset-header.json"_s);
}