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