From 110d1df50da5f28dff128f0ab42096ef0d2611c5 Mon Sep 17 00:00:00 2001 From: Levente Batuska Date: Thu, 29 Jan 2026 23:20:41 +0100 Subject: [PATCH] chore(uuid): add more tests --- tests/test_uuid.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/tests/test_uuid.cpp b/tests/test_uuid.cpp index 0d35a1c..f90ddb2 100644 --- a/tests/test_uuid.cpp +++ b/tests/test_uuid.cpp @@ -2,8 +2,15 @@ #include #include +#include +#include +#include +#include +#include -TEST_CASE("uuidToString formats UUIDs correctly") { +using namespace cpputils::uuid; + +TEST_CASE("uuidToString formats UUIDs correctly", "[uuid]") { struct TestVector { std::array uuid; @@ -38,6 +45,101 @@ TEST_CASE("uuidToString formats UUIDs correctly") { }; for (auto const& t : tests) { - REQUIRE(cpputils::uuid::uuidToString(t.uuid) == t.expected); + REQUIRE(uuidToString(t.uuid) == t.expected); } } + +TEST_CASE("V4 generates unique UUIDs", "[uuid][v4]") { + V4 generator; + + constexpr int N = 1000; + std::unordered_set uuids; + + for (int i = 0; i < N; ++i) { + auto uuid = generator.generate(); + auto str = uuidToString(uuid); + REQUIRE(uuids.find(str) == uuids.end()); + uuids.insert(str); + } +} + +TEST_CASE("V7 generates increasing timestamps", "[uuid][v7]") { + V7 generator; + + constexpr int N = 1000; + int64_t prev_ts = 0; + + for (int i = 0; i < N; ++i) { + auto uuid = generator.generate(); + auto ts = V7::get_timestamp_from_uuid(uuid); + REQUIRE(ts >= prev_ts); + prev_ts = ts; + } +} + +TEST_CASE("V7 sequence increments on same timestamp", "[uuid][v7][!mayfail]") { + INFO("info message"); + V7 generator; + + size_t count = 0; + size_t i = 1000; + for (; i > 0;) { + auto uuid1 = generator.generate(); + auto uuid2 = generator.generate(); + + auto ts1 = V7::get_timestamp_from_uuid(uuid1); + auto ts2 = V7::get_timestamp_from_uuid(uuid2); + + if (ts1 == ts2) { + REQUIRE(uuid1 != uuid2); + count++; + if (count >= 10) { + break; + } + } else { + i--; + } + } + INFO( + "Testing that V7 UUIDs with the same timestamp produce unique UUIDs. " + "Tries up to 1000 pairs to avoid long runtime if UUID generation is " + "slow. " + "(Test fails with 0 > 0 if no same-timestamp UUIDs could be generated)"); + REQUIRE(i > 0); +} + +TEST_CASE("UUID string formatting consistency", "[uuid]") { + V4 v4; + V7 v7; + + auto uuid4 = v4.generate(); + auto uuid7 = v7.generate(); + + auto str4 = uuidToString(uuid4); + auto str7 = uuidToString(uuid7); + + REQUIRE(str4.size() == 36); + REQUIRE(str7.size() == 36); + + auto is_hex_or_dash = [](char c) { + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c == '-'); + }; + REQUIRE(std::all_of(str4.begin(), str4.end(), is_hex_or_dash)); + REQUIRE(std::all_of(str7.begin(), str7.end(), is_hex_or_dash)); +} + +TEST_CASE("V4 and V7 generate large number of UUIDs without collision", + "[uuid][stress]") { + V4 v4; + V7 v7; + + constexpr int N = 5000; + std::unordered_set uuids; + + for (int i = 0; i < N; ++i) { + uuids.insert(uuidToString(v4.generate())); + uuids.insert(uuidToString(v7.generate())); + } + + REQUIRE(uuids.size() == 2 * N); +}