Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 1 | #include "aos/uuid.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 2 | |
| 3 | #include "glog/logging.h" |
| 4 | #include "gtest/gtest.h" |
| 5 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 6 | namespace aos::testing { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 7 | |
| 8 | // Tests that random UUIDs are actually random, and we can convert them to a |
| 9 | // string. Not very exhaustive, but it is a good smoke test. |
| 10 | TEST(UUIDTest, GetOne) { |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 11 | LOG(INFO) << UUID::Random(); |
| 12 | |
| 13 | UUID r = UUID::Random(); |
| 14 | |
| 15 | std::stringstream ss; |
| 16 | ss << r; |
| 17 | |
| 18 | UUID r2 = UUID::FromString(ss.str()); |
| 19 | EXPECT_EQ(r2, r); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 20 | |
| 21 | EXPECT_NE(UUID::Random(), UUID::Random()); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 22 | EXPECT_NE(UUID::Random(), UUID::Zero()); |
| 23 | EXPECT_EQ(UUID::Zero(), UUID::Zero()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 26 | // Tests that converting to and from various formats produces the same UUID. |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 27 | TEST(UUIDTest, FromStringOrSpan) { |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 28 | std::string_view str = "4b88ab00-556a-455b-a395-17d1a0c6f906"; |
| 29 | std::array<uint8_t, UUID::kDataSize> data = { |
| 30 | 0x4b, 0x88, 0xab, 0x00, 0x55, 0x6a, 0x45, 0x5b, |
| 31 | 0xa3, 0x95, 0x17, 0xd1, 0xa0, 0xc6, 0xf9, 0x06}; |
| 32 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 33 | const UUID u_str = UUID::FromString(str); |
| 34 | const UUID u_span = UUID::FromSpan({data.data(), data.size()}); |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 35 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 36 | EXPECT_EQ(u_str.span(), absl::Span<uint8_t>(data.data(), data.size())); |
| 37 | EXPECT_EQ(u_span.span(), absl::Span<uint8_t>(data.data(), data.size())); |
| 38 | EXPECT_EQ(u_str.ToString(), str); |
| 39 | EXPECT_EQ(u_span.ToString(), str); |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 40 | |
| 41 | flatbuffers::FlatBufferBuilder fbb; |
| 42 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset = |
| 43 | fbb.CreateVector(data.data(), data.size()); |
| 44 | |
| 45 | const flatbuffers::Vector<uint8_t> *data_vector = |
| 46 | flatbuffers::GetTemporaryPointer(fbb, data_offset); |
| 47 | |
| 48 | const UUID u2 = UUID::FromVector(data_vector); |
| 49 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 50 | EXPECT_EQ(u_str, u2); |
| 51 | EXPECT_EQ(u_span, u2); |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 54 | } // namespace aos::testing |