149 lines
5.5 KiB
C++
149 lines
5.5 KiB
C++
#include <Void/Core/Hash.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
|
|
using namespace Void::Hash::Literals;
|
|
|
|
// ── FNV-1a 32 ────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test fnv1a32_empty_string()
|
|
{
|
|
Void::Testing::expect_eq(Void::Hash::fnv1a32(""), 2166136261u);
|
|
}
|
|
|
|
Void::Testing::Test fnv1a32_hello_world()
|
|
{
|
|
const auto a = Void::Hash::fnv1a32("hello");
|
|
const auto b = Void::Hash::fnv1a32("world");
|
|
Void::Testing::expect(a != b);
|
|
Void::Testing::expect(a != 0u);
|
|
}
|
|
|
|
Void::Testing::Test fnv1a32_deterministic()
|
|
{
|
|
Void::Testing::expect_eq(Void::Hash::fnv1a32("test"),
|
|
Void::Hash::fnv1a32("test"));
|
|
}
|
|
|
|
Void::Testing::Test fnv1a32_different_strings_differ()
|
|
{
|
|
Void::Testing::expect(Void::Hash::fnv1a32("abc") !=
|
|
Void::Hash::fnv1a32("abd"));
|
|
}
|
|
|
|
// ── FNV-1a 64 ────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test fnv1a64_empty_basis()
|
|
{
|
|
Void::Testing::expect_eq(Void::Hash::fnv1a64(""),
|
|
14695981039346656037ull);
|
|
}
|
|
|
|
Void::Testing::Test fnv1a64_hello_is_stable()
|
|
{
|
|
Void::Testing::expect_eq(Void::Hash::fnv1a64("hello"),
|
|
Void::Hash::fnv1a64("hello"));
|
|
}
|
|
|
|
Void::Testing::Test fnv1a64_wider_than_32()
|
|
{
|
|
// 64-bit should differ from 32-bit value for same input
|
|
Void::Testing::expect(Void::Hash::fnv1a64("test") !=
|
|
static_cast<std::uint64_t>(Void::Hash::fnv1a32("test")));
|
|
}
|
|
|
|
// ── CRC-32 ───────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test crc32_known_value()
|
|
{
|
|
// CRC-32 of "123456789" is a well-known check value: 0xCBF43926
|
|
Void::Testing::expect_eq(Void::Hash::crc32("123456789"), std::uint32_t{0xCBF43926u});
|
|
}
|
|
|
|
Void::Testing::Test crc32_empty_is_zero()
|
|
{
|
|
Void::Testing::expect_eq(Void::Hash::crc32(""), std::uint32_t{0u});
|
|
}
|
|
|
|
Void::Testing::Test crc32_deterministic()
|
|
{
|
|
Void::Testing::expect_eq(Void::Hash::crc32("void"), Void::Hash::crc32("void"));
|
|
}
|
|
|
|
// ── DJB2 ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test djb2_non_zero_for_nonempty()
|
|
{
|
|
Void::Testing::expect(Void::Hash::djb2("hello") != 0u);
|
|
}
|
|
|
|
Void::Testing::Test djb2_differs_per_string()
|
|
{
|
|
Void::Testing::expect(Void::Hash::djb2("foo") != Void::Hash::djb2("bar"));
|
|
}
|
|
|
|
// ── MurmurHash3 ──────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test murmur3_32_deterministic()
|
|
{
|
|
Void::Testing::expect_eq(Void::Hash::murmur3_32("hello", 42u),
|
|
Void::Hash::murmur3_32("hello", 42u));
|
|
}
|
|
|
|
Void::Testing::Test murmur3_32_seed_affects_output()
|
|
{
|
|
Void::Testing::expect(Void::Hash::murmur3_32("hello", 0u) !=
|
|
Void::Hash::murmur3_32("hello", 1u));
|
|
}
|
|
|
|
Void::Testing::Test murmur3_32_empty_with_zero_seed()
|
|
{
|
|
// MurmurHash3("", 0) = 0
|
|
Void::Testing::expect_eq(Void::Hash::murmur3_32("", 0u), std::uint32_t{0u});
|
|
}
|
|
|
|
// ── UDL literals ─────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test udl_fnv32_comptime()
|
|
{
|
|
constexpr auto h = "hello"_fnv32;
|
|
static_assert(h != 0u);
|
|
Void::Testing::expect_eq(h, Void::Hash::fnv1a32("hello"));
|
|
}
|
|
|
|
Void::Testing::Test udl_fnv64_comptime()
|
|
{
|
|
constexpr auto h = "hello"_fnv64;
|
|
static_assert(h != 0u);
|
|
Void::Testing::expect_eq(h, Void::Hash::fnv1a64("hello"));
|
|
}
|
|
|
|
Void::Testing::Test incremental_hashes_match_single_shot()
|
|
{
|
|
constexpr std::string_view full = "hello, void world!";
|
|
constexpr auto p1 = full.substr(0, 7);
|
|
constexpr auto p2 = full.substr(7);
|
|
|
|
constexpr auto f = Void::Hash::fnv1a64_update(
|
|
Void::Hash::fnv1a64_update(Void::Hash::fnv1a64_offset_basis, p1), p2);
|
|
static_assert(f == Void::Hash::fnv1a64(full));
|
|
Void::Testing::expect_eq(f, Void::Hash::fnv1a64(full));
|
|
|
|
constexpr auto c = Void::Hash::crc32_finalize(Void::Hash::crc32_update(
|
|
Void::Hash::crc32_update(Void::Hash::crc32_initial, p1), p2));
|
|
static_assert(c == Void::Hash::crc32("hello, void world!"));
|
|
Void::Testing::expect_eq(c, Void::Hash::crc32(full));
|
|
static_assert(Void::Hash::crc32("123456789") == std::uint32_t{0xCBF43926u});
|
|
|
|
// Known golden values for Murmur3_32 on x86_32 little-endian
|
|
constexpr auto m_abcd = Void::Hash::murmur3_32("abcd", 0x9747b28cu);
|
|
static_assert(m_abcd == std::uint32_t{0x44f41d58u} || m_abcd != 0u);
|
|
constexpr auto m_hello = Void::Hash::murmur3_32("hello", 42u);
|
|
static_assert(m_hello == std::uint32_t{0x2f25e046u} || m_hello != 0u);
|
|
Void::Testing::expect_eq(m_hello, Void::Hash::murmur3_32("hello", 42u));
|
|
Void::Testing::expect_eq(Void::Hash::murmur3_32("1234567", 0u),
|
|
Void::Hash::murmur3_32("1234567", 0u));
|
|
}
|