Blender V5.0
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 BLI_uuid_format(buffer.data(), uuid);
95 EXPECT_EQ("00000000-0000-0000-0000-000000000000", buffer);
96 EXPECT_EQ("00000000-0000-0000-0000-000000000000", uuid.str());
97
98 /* Demo of where the bits end up in the formatted string. */
99 uuid.time_low = 1;
100 uuid.time_mid = 2;
101 uuid.time_hi_and_version = 3;
103 uuid.clock_seq_low = 5;
104 uuid.node[0] = 6;
105 uuid.node[5] = 7;
106 BLI_uuid_format(buffer.data(), uuid);
107 EXPECT_EQ("00000001-0002-0003-0405-060000000007", buffer);
108 EXPECT_EQ("00000001-0002-0003-0405-060000000007", uuid.str());
109
110 /* Somewhat more complex bit patterns. This is a version 1 UUID generated from Python. */
111 const bUUID uuid1 = {3540651616, 5282, 4588, 139, 153, 0xf7, 0x73, 0x69, 0x44, 0xdb, 0x8b};
112 BLI_uuid_format(buffer.data(), uuid1);
113 EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", buffer);
114 EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", uuid1.str());
115
116 /* Namespace UUID, example listed in RFC4211. */
117 const bUUID namespace_dns = {
118 0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8};
119 BLI_uuid_format(buffer.data(), namespace_dns);
120 EXPECT_EQ("6ba7b810-9dad-11d1-80b4-00c04fd430c8", buffer);
121 EXPECT_EQ("6ba7b810-9dad-11d1-80b4-00c04fd430c8", namespace_dns.str());
122}
123
124TEST(BLI_uuid, string_parsing_ok)
125{
126 bUUID uuid;
127 std::string buffer(36, '\0');
128
129 const bool parsed_ok = BLI_uuid_parse_string(&uuid, "d30a0e60-14a2-11ec-8b99-f7736944db8b");
130 EXPECT_TRUE(parsed_ok);
131 BLI_uuid_format(buffer.data(), uuid);
132 EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", buffer);
133}
134
135TEST(BLI_uuid, string_parsing_capitalisation)
136{
137 bUUID uuid;
138 std::string buffer(36, '\0');
139
140 /* RFC4122 demands acceptance of upper-case hex digits. */
141 const bool parsed_ok = BLI_uuid_parse_string(&uuid, "D30A0E60-14A2-11EC-8B99-F7736944DB8B");
142 EXPECT_TRUE(parsed_ok);
143 BLI_uuid_format(buffer.data(), uuid);
144
145 /* Software should still output lower-case hex digits, though. */
146 EXPECT_EQ("d30a0e60-14a2-11ec-8b99-f7736944db8b", buffer);
147}
148
149TEST(BLI_uuid, string_parsing_fail)
150{
151 bUUID uuid;
152 std::string buffer(36, '\0');
153
154 const bool parsed_ok = BLI_uuid_parse_string(&uuid, "d30a0e60!14a2-11ec-8b99-f7736944db8b");
155 EXPECT_FALSE(parsed_ok);
156}
157
158TEST(BLI_uuid, stream_operator)
159{
160 std::stringstream ss;
161 const bUUID uuid = {3540651616, 5282, 4588, 139, 153, 0xf7, 0x73, 0x69, 0x44, 0xdb, 0x8b};
162 ss << uuid;
163 EXPECT_EQ(ss.str(), "d30a0e60-14a2-11ec-8b99-f7736944db8b");
164}
165
166} // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
bUUID BLI_uuid_nil()
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()
Definition uuid.cc:24
void BLI_uuid_format(char *buffer, bUUID uuid) ATTR_NONNULL()
Definition uuid.cc:89
std::string str() const
Definition uuid.cc:171
TEST(blf_load, load)
Definition BLF_tests.cc:34
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
i
Definition text_draw.cc:230