test time gen only on Windows
This commit is contained in:
parent
b74a5eb7e9
commit
7bd86a11e3
@ -14,6 +14,7 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
#include <atomic>
|
||||||
#include <gsl/span>
|
#include <gsl/span>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -242,6 +243,10 @@ namespace uuids
|
|||||||
size_t m_blockByteIndex;
|
size_t m_blockByteIndex;
|
||||||
size_t m_byteCount;
|
size_t m_byteCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static std::mt19937 clock_gen(std::random_device{}());
|
||||||
|
static std::uniform_int_distribution<short> clock_dis{ -32768, 32767 };
|
||||||
|
static std::atomic_short clock_sequence = clock_dis(clock_gen);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------------------------------------------------
|
// --------------------------------------------------------------------------------------------------------------------------
|
||||||
@ -805,14 +810,13 @@ namespace uuids
|
|||||||
detail::sha1 hasher;
|
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
|
class uuid_time_generator
|
||||||
{
|
{
|
||||||
using mac_address = std::array<unsigned char, 6>;
|
using mac_address = std::array<unsigned char, 6>;
|
||||||
|
|
||||||
std::optional<mac_address> device_address;
|
std::optional<mac_address> device_address;
|
||||||
std::mt19937 generator;
|
|
||||||
std::uniform_int_distribution<short> dis;
|
|
||||||
|
|
||||||
bool get_mac_address()
|
bool get_mac_address()
|
||||||
{
|
{
|
||||||
@ -846,13 +850,8 @@ namespace uuids
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
uuid_time_generator():dis(-32768, 32767)
|
uuid_time_generator()
|
||||||
{
|
{
|
||||||
std::random_device rd;
|
|
||||||
auto seed_data = std::array<int, std::mt19937::state_size> {};
|
|
||||||
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()()
|
uuid operator()()
|
||||||
@ -863,7 +862,7 @@ namespace uuids
|
|||||||
|
|
||||||
auto tm = get_time_intervals();
|
auto tm = get_time_intervals();
|
||||||
|
|
||||||
short clock_seq = dis(generator);
|
short clock_seq = detail::clock_sequence++;
|
||||||
|
|
||||||
clock_seq &= 0x3FFF;
|
clock_seq &= 0x3FFF;
|
||||||
|
|
||||||
|
|||||||
@ -220,6 +220,7 @@ TEST_CASE("Test name generator (std::string)", "[gen][name]")
|
|||||||
REQUIRE(id3 != id4);
|
REQUIRE(id3 != id4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
TEST_CASE("Test time generator", "[gen][time]")
|
TEST_CASE("Test time generator", "[gen][time]")
|
||||||
{
|
{
|
||||||
uuid_time_generator gen;
|
uuid_time_generator gen;
|
||||||
@ -241,3 +242,4 @@ TEST_CASE("Test time generator", "[gen][time]")
|
|||||||
|
|
||||||
REQUIRE(ids.size() == 100);
|
REQUIRE(ids.size() == 100);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user