chore(uuid): add more tests
This commit is contained in:
@@ -2,8 +2,15 @@
|
||||
|
||||
#include <cpputils/uuid.h>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unordered_set>
|
||||
|
||||
TEST_CASE("uuidToString formats UUIDs correctly") {
|
||||
using namespace cpputils::uuid;
|
||||
|
||||
TEST_CASE("uuidToString formats UUIDs correctly", "[uuid]") {
|
||||
|
||||
struct TestVector {
|
||||
std::array<uint8_t, 16> 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<std::string> 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<std::string> uuids;
|
||||
|
||||
for (int i = 0; i < N; ++i) {
|
||||
uuids.insert(uuidToString(v4.generate()));
|
||||
uuids.insert(uuidToString(v7.generate()));
|
||||
}
|
||||
|
||||
REQUIRE(uuids.size() == 2 * N);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user