From 7bd86a11e3b0b3ebd92f92608750355e780ae1f7 Mon Sep 17 00:00:00 2001 From: Marius Bancila Date: Fri, 17 May 2019 15:55:43 +0300 Subject: [PATCH] test time gen only on Windows --- include/uuid.h | 19 +++++++++---------- test/test_generators.cpp | 4 +++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/include/uuid.h b/include/uuid.h index 7686362..34f59e5 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #ifdef _WIN32 @@ -242,6 +243,10 @@ namespace uuids size_t m_blockByteIndex; size_t m_byteCount; }; + + static std::mt19937 clock_gen(std::random_device{}()); + static std::uniform_int_distribution clock_dis{ -32768, 32767 }; + static std::atomic_short clock_sequence = clock_dis(clock_gen); } // -------------------------------------------------------------------------------------------------------------------------- @@ -805,14 +810,13 @@ namespace uuids detail::sha1 hasher; }; - // this implementation is currently unreliable for good uuids + // !!! DO NOT USE THIS IN PRODUCTION + // this implementation is unreliable for good uuids class uuid_time_generator { using mac_address = std::array; std::optional device_address; - std::mt19937 generator; - std::uniform_int_distribution dis; bool get_mac_address() { @@ -846,13 +850,8 @@ namespace uuids } public: - uuid_time_generator():dis(-32768, 32767) + uuid_time_generator() { - std::random_device rd; - auto seed_data = std::array {}; - std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd)); - std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - generator.seed(seq); } uuid operator()() @@ -863,7 +862,7 @@ namespace uuids auto tm = get_time_intervals(); - short clock_seq = dis(generator); + short clock_seq = detail::clock_sequence++; clock_seq &= 0x3FFF; diff --git a/test/test_generators.cpp b/test/test_generators.cpp index 16ed5f6..923f2fb 100644 --- a/test/test_generators.cpp +++ b/test/test_generators.cpp @@ -220,6 +220,7 @@ TEST_CASE("Test name generator (std::string)", "[gen][name]") REQUIRE(id3 != id4); } +#ifdef _WIN32 TEST_CASE("Test time generator", "[gen][time]") { uuid_time_generator gen; @@ -240,4 +241,5 @@ TEST_CASE("Test time generator", "[gen][time]") ids.insert(gen()); REQUIRE(ids.size() == 100); -} \ No newline at end of file +} +#endif \ No newline at end of file