feat(Core): detach Uuid from Math, add CheckedBits and portable Hash (M-CORE-02, M-CORE-03)
This commit is contained in:
1 parent
8d28c7e3c4
commit
cbc900412b
9 files changed
+555
-117
No files matched your search
@@ -2,6 +2,26 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct ThrowingUrbg {
|
||||
using result_type = std::uint32_t;
|
||||
static constexpr result_type min() { return 0; }
|
||||
static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
|
||||
result_type operator()()
|
||||
{
|
||||
throw std::runtime_error{"entropy failed"};
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// ── v4 ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test v4_not_nil()
|
||||
@@ -111,3 +131,93 @@ Void::Testing::Test v4_not_nil_check()
|
||||
{
|
||||
Void::Testing::expect(!Void::Uuid::v4().is_nil());
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_injected_urbg_deterministic()
|
||||
{
|
||||
std::mt19937_64 g1{12345ULL};
|
||||
std::mt19937_64 g2{12345ULL};
|
||||
const auto a = Void::Uuid::v4(g1);
|
||||
const auto b = Void::Uuid::v4(g2);
|
||||
Void::Testing::expect_eq(a, b);
|
||||
Void::Testing::expect_eq(a.bytes[6] >> 4, std::uint8_t{4});
|
||||
Void::Testing::expect_eq(a.bytes[8] >> 6, std::uint8_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_injected_urbg_propagates_throw()
|
||||
{
|
||||
ThrowingUrbg bad;
|
||||
bool threw = false;
|
||||
try {
|
||||
(void)Void::Uuid::v4(bad);
|
||||
} catch (const std::runtime_error&) {
|
||||
threw = true;
|
||||
}
|
||||
Void::Testing::expect(threw);
|
||||
}
|
||||
|
||||
Void::Testing::Test try_v4_succeeds()
|
||||
{
|
||||
const auto r = Void::Uuid::try_v4();
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect(!r->is_nil());
|
||||
Void::Testing::expect_eq(r->bytes[6] >> 4, std::uint8_t{4});
|
||||
Void::Testing::expect_eq(r->bytes[8] >> 6, std::uint8_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test constexpr_format_and_parse()
|
||||
{
|
||||
constexpr auto u = Void::Uuid::Namespace::Dns;
|
||||
constexpr auto chars = Void::Uuid::format_chars(u);
|
||||
static_assert(std::string_view{chars.data(), chars.size()} ==
|
||||
"6ba7b810-9dad-11d1-80b4-00c04fd430c8");
|
||||
|
||||
constexpr auto parsed =
|
||||
Void::Uuid::parse("6BA7B810-9DAD-11D1-80B4-00C04FD430C8");
|
||||
static_assert(parsed.has_value() && *parsed == u);
|
||||
|
||||
constexpr auto compact = Void::Uuid::parse("6ba7b8109dad11d180b400c04fd430c8");
|
||||
static_assert(compact.has_value() && *compact == u);
|
||||
|
||||
static_assert(!Void::Uuid::parse("6ba7b810_9dad_11d1_80b4_00c04fd430c8").has_value());
|
||||
static_assert(!Void::Uuid::parse("6ba7b810-9dad-11d1-80b4-00c04fd430cz").has_value());
|
||||
static_assert(!Void::Uuid::parse("6ba7b810-9dad-11d1-80b4-00c04fd430c80").has_value());
|
||||
|
||||
Void::Testing::expect_eq(u.to_string(),
|
||||
std::string{"6ba7b810-9dad-11d1-80b4-00c04fd430c8"});
|
||||
Void::Testing::expect_eq(*parsed, u);
|
||||
Void::Testing::expect_eq(*compact, u);
|
||||
}
|
||||
|
||||
Void::Testing::Test v5_lite_matches_1x_blob_golden()
|
||||
{
|
||||
const auto ns = Void::Uuid::Namespace::Dns;
|
||||
constexpr std::string_view name = "example.com";
|
||||
const auto actual = Void::Uuid::v5_lite(ns, name);
|
||||
|
||||
std::string blob;
|
||||
for (auto b : ns.bytes) blob += static_cast<char>(b);
|
||||
blob += name;
|
||||
const auto h64 = Void::Hash::fnv1a64(blob);
|
||||
const auto h32 = Void::Hash::crc32(blob);
|
||||
const auto h32b = Void::Hash::murmur3_32(name, static_cast<std::uint32_t>(h64));
|
||||
|
||||
Void::Uuid::Uuid expected{};
|
||||
expected.bytes[0] = static_cast<std::uint8_t>(h64 >> 56);
|
||||
expected.bytes[1] = static_cast<std::uint8_t>(h64 >> 48);
|
||||
expected.bytes[2] = static_cast<std::uint8_t>(h64 >> 40);
|
||||
expected.bytes[3] = static_cast<std::uint8_t>(h64 >> 32);
|
||||
expected.bytes[4] = static_cast<std::uint8_t>(h64 >> 24);
|
||||
expected.bytes[5] = static_cast<std::uint8_t>(h64 >> 16);
|
||||
expected.bytes[6] = static_cast<std::uint8_t>(((h64 >> 8) & 0x0Fu) | 0x50u);
|
||||
expected.bytes[7] = static_cast<std::uint8_t>(h64);
|
||||
expected.bytes[8] = static_cast<std::uint8_t>(((h32 >> 24) & 0x3Fu) | 0x80u);
|
||||
expected.bytes[9] = static_cast<std::uint8_t>(h32 >> 16);
|
||||
expected.bytes[10] = static_cast<std::uint8_t>(h32 >> 8);
|
||||
expected.bytes[11] = static_cast<std::uint8_t>(h32);
|
||||
expected.bytes[12] = static_cast<std::uint8_t>(h32b >> 24);
|
||||
expected.bytes[13] = static_cast<std::uint8_t>(h32b >> 16);
|
||||
expected.bytes[14] = static_cast<std::uint8_t>(h32b >> 8);
|
||||
expected.bytes[15] = static_cast<std::uint8_t>(h32b);
|
||||
|
||||
Void::Testing::expect_eq(actual, expected);
|
||||
}
|
||||
Reference in new issue
Block a user