James Kuszmaul | 05ccb27 | 2023-07-13 10:58:14 -0700 | [diff] [blame] | 1 | #include <set> |
| 2 | #include <unordered_set> |
| 3 | |
| 4 | #include "glog/logging.h" |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | #include "aos/uuid.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
| 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). |
| 15 | TEST(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 Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame^] | 27 | |
| 28 | // Tests that our random seed generation for the mt19937 does not trivially |
| 29 | // collide. |
| 30 | TEST(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 | } |
James Kuszmaul | 05ccb27 | 2023-07-13 10:58:14 -0700 | [diff] [blame] | 43 | } // namespace testing |
| 44 | } // namespace aos |