Blender  V3.3
BLI_uuid_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #include "testing/testing.h"
3 #include <cstring>
4 
5 #include "BLI_uuid.h"
6 
7 namespace blender::tests {
8 
9 TEST(BLI_uuid, generate_random)
10 {
11  const bUUID uuid = BLI_uuid_generate_random();
12 
13  /* The 4 MSbits represent the "version" of the UUID. */
14  const uint16_t version = uuid.time_hi_and_version >> 12;
15  EXPECT_EQ(version, 4);
16 
17  /* The 2 MSbits should be 0b10, indicating compliance with RFC4122. */
18  const uint8_t reserved = uuid.clock_seq_hi_and_reserved >> 6;
19  EXPECT_EQ(reserved, 0b10);
20 }
21 
22 TEST(BLI_uuid, generate_many_random)
23 {
24  const bUUID first_uuid = BLI_uuid_generate_random();
25 
26  /* Generate lots of UUIDs to get some indication that the randomness is okay. */
27  for (int i = 0; i < 1000000; ++i) {
28  const bUUID uuid = BLI_uuid_generate_random();
29  EXPECT_NE(first_uuid, uuid);
30 
31  /* Check that the non-random bits are set according to RFC4122. */
32  const uint16_t version = uuid.time_hi_and_version >> 12;
33  EXPECT_EQ(version, 4);
34  const uint8_t reserved = uuid.clock_seq_hi_and_reserved >> 6;
35  EXPECT_EQ(reserved, 0b10);
36  }
37 }
38 
39 TEST(BLI_uuid, nil_value)
40 {
41  const bUUID nil_uuid = BLI_uuid_nil();
42  const bUUID zeroes_uuid{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
43  const bUUID default_constructed{};
44 
45  EXPECT_EQ(nil_uuid, zeroes_uuid);
46  EXPECT_TRUE(BLI_uuid_is_nil(nil_uuid));
47  EXPECT_TRUE(BLI_uuid_is_nil(default_constructed))
48  << "Default constructor should produce the nil value.";
49 
50  std::string buffer(36, '\0');
51  BLI_uuid_format(buffer.data(), nil_uuid);
52  EXPECT_EQ("00000000-0000-0000-0000-000000000000", buffer);
53 }
54 
55 TEST(BLI_uuid, equality)
56 {
57  const bUUID uuid1 = BLI_uuid_generate_random();
58  const bUUID uuid2 = BLI_uuid_generate_random();
59 
60  EXPECT_EQ(uuid1, uuid1);
61  EXPECT_NE(uuid1, uuid2);
62 }
63 
64 TEST(BLI_uuid, comparison_trivial)
65 {
66  const bUUID uuid0{};
67  const bUUID uuid1("11111111-1111-1111-1111-111111111111");
68  const bUUID uuid2("22222222-2222-2222-2222-222222222222");
69 
70  EXPECT_LT(uuid0, uuid1);
71  EXPECT_LT(uuid0, uuid2);
72  EXPECT_LT(uuid1, uuid2);
73 }
74 
75 TEST(BLI_uuid, comparison_byte_order_check)
76 {
77  const bUUID uuid0{};
78  /* Chosen to test byte ordering is taken into account correctly when comparing. */
79  const bUUID uuid12("12222222-2222-2222-2222-222222222222");
80  const bUUID uuid21("21111111-1111-1111-1111-111111111111");
81 
82  EXPECT_LT(uuid0, uuid12);
83  EXPECT_LT(uuid0, uuid21);
84  EXPECT_LT(uuid12, uuid21);
85 }
86 
87 TEST(BLI_uuid, string_formatting)
88 {
89  bUUID uuid;
90  std::string buffer(36, '\0');
91 
92  memset(&uuid, 0, sizeof(uuid));
93  BLI_uuid_format(buffer.data(), uuid);
94  EXPECT_EQ("00000000-0000-0000-0000-000000000000", buffer);
95 
96  /* Demo of where the bits end up in the formatted string. */
97  uuid.time_low = 1;
98  uuid.time_mid = 2;
99  uuid.time_hi_and_version = 3;
100  uuid.clock_seq_hi_and_reserved = 4;
101  uuid.clock_seq_low = 5;
102  uuid.node[0] = 6;
103  uuid.node[5] = 7;
104  BLI_uuid_format(buffer.data(), uuid);
105  EXPECT_EQ("00000001-0002-0003-0405-060000000007", buffer);
106 
107  /* Somewhat more complex bit patterns. This is a version 1 UUID generated from Python. */
108  const bUUID uuid1 = {3540651616, 5282, 4588, 139, 153, 0xf7, 0x73, 0x69, 0x44, 0xdb, 0x8b};
109  BLI_uuid_format(buffer.data(), uuid1);
110  EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", buffer);
111 
112  /* Namespace UUID, example listed in RFC4211. */
113  const bUUID namespace_dns = {
114  0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8};
115  BLI_uuid_format(buffer.data(), namespace_dns);
116  EXPECT_EQ("6ba7b810-9dad-11d1-80b4-00c04fd430c8", buffer);
117 }
118 
119 TEST(BLI_uuid, string_parsing_ok)
120 {
121  bUUID uuid;
122  std::string buffer(36, '\0');
123 
124  const bool parsed_ok = BLI_uuid_parse_string(&uuid, "d30a0e60-14a2-11ec-8b99-f7736944db8b");
125  EXPECT_TRUE(parsed_ok);
126  BLI_uuid_format(buffer.data(), uuid);
127  EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", buffer);
128 }
129 
130 TEST(BLI_uuid, string_parsing_capitalisation)
131 {
132  bUUID uuid;
133  std::string buffer(36, '\0');
134 
135  /* RFC4122 demands acceptance of upper-case hex digits. */
136  const bool parsed_ok = BLI_uuid_parse_string(&uuid, "D30A0E60-14A2-11EC-8B99-F7736944DB8B");
137  EXPECT_TRUE(parsed_ok);
138  BLI_uuid_format(buffer.data(), uuid);
139 
140  /* Software should still output lower-case hex digits, though. */
141  EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", buffer);
142 }
143 
144 TEST(BLI_uuid, string_parsing_fail)
145 {
146  bUUID uuid;
147  std::string buffer(36, '\0');
148 
149  const bool parsed_ok = BLI_uuid_parse_string(&uuid, "d30a0e60!14a2-11ec-8b99-f7736944db8b");
150  EXPECT_FALSE(parsed_ok);
151 }
152 
153 TEST(BLI_uuid, stream_operator)
154 {
155  std::stringstream ss;
156  const bUUID uuid = {3540651616, 5282, 4588, 139, 153, 0xf7, 0x73, 0x69, 0x44, 0xdb, 0x8b};
157  ss << uuid;
158  EXPECT_EQ(ss.str(), "d30a0e60-14a2-11ec-8b99-f7736944db8b");
159 }
160 
161 } // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
bUUID BLI_uuid_nil(void)
Definition: uuid.cc:70
bool BLI_uuid_parse_string(bUUID *uuid, const char *buffer) ATTR_NONNULL()
Definition: uuid.cc:103
bool BLI_uuid_is_nil(bUUID uuid)
Definition: uuid.cc:76
bUUID BLI_uuid_generate_random(void)
Definition: uuid.cc:21
void BLI_uuid_format(char *buffer, bUUID uuid) ATTR_NONNULL()
Definition: uuid.cc:86
ccl_global float * buffer
TEST(any, DefaultConstructor)
Definition: BLI_any_test.cc:10
unsigned short uint16_t
Definition: stdint.h:79
unsigned char uint8_t
Definition: stdint.h:78
Universally Unique Identifier according to RFC4122.
uint8_t clock_seq_hi_and_reserved
uint8_t clock_seq_low
uint16_t time_mid
uint32_t time_low
uint8_t node[6]
uint16_t time_hi_and_version