blob: 99b870bc9e2c8b82150b6db8ac64d0494da880cc [file] [log] [blame]
James Kuszmaul05ccb272023-07-13 10:58:14 -07001#include <set>
2#include <unordered_set>
3
4#include "glog/logging.h"
5#include "gtest/gtest.h"
6
7#include "aos/uuid.h"
8
Stephan Pleinesf63bde82024-01-13 15:59:33 -08009namespace aos::testing {
James Kuszmaul05ccb272023-07-13 10:58:14 -070010
11// Tests that modest numbers of UUID::Random() calls cannot create UUID
12// collisions (to test that we have not *completely* messed up the random number
13// generation).
14TEST(UUIDTest, CollisionTest) {
15 std::set<UUID> uuids;
16 // When we only had ~32 bits of randomness in our UUIDs, we could generate
17 // issues with only ~sqrt(2 ** 32) (aka 2 ** 16) UUIDs.
18 // Just go up to 2 ** 22, since too much longer just makes this test take
19 // obnoxiously long.
20 for (size_t ii = 0; ii < (1UL << 22); ++ii) {
21 UUID uuid = UUID::Random();
22 ASSERT_FALSE(uuids.count(uuid) > 0) << ii;
23 uuids.insert(uuid);
24 }
25}
James Kuszmaula791b762023-07-13 14:56:21 -070026
27// Tests that our random seed generation for the mt19937 does not trivially
28// collide.
29TEST(UUIDTest, SeedInitializationTest) {
30 std::uniform_int_distribution<uint64_t> distribution(0);
31 std::set<uint64_t> values;
32 // This test takes significantly longer than the above due to needing to query
33 // std::random_device substantially. However, covering a range of 2 ** 18
34 // should readily catch things if we are accidentally using 32-bit seeds.
35 for (size_t ii = 0; ii < (1UL << 18); ++ii) {
36 std::mt19937 twister = internal::FullySeededRandomGenerator();
37 const uint64_t value = distribution(twister);
38 ASSERT_FALSE(values.count(value) > 0) << ii;
39 values.insert(value);
40 }
41}
Stephan Pleinesf63bde82024-01-13 15:59:33 -080042} // namespace aos::testing