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