blob: 4a521cf50f9495ac54ae7a4b5a52ef5ad03d2e15 [file] [log] [blame]
CJCombrink4b909092024-04-27 19:51:39 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20
21#include <boost/test/unit_test.hpp>
22#include <boost/uuid/uuid.hpp>
23#include <boost/uuid/uuid_io.hpp>
24#include <boost/uuid/string_generator.hpp>
25
26#include <thrift/TUuid.h>
27
28using apache::thrift::TUuid;
29
30BOOST_AUTO_TEST_SUITE(TUuidTest)
31
32BOOST_AUTO_TEST_CASE(construction) {
33 BOOST_TEST(TUuid().is_nil());
34}
35
36BOOST_AUTO_TEST_CASE(construction_string_valid) {
37 const std::string expected_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
38
39 BOOST_TEST(to_string(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")) == expected_1);
40 BOOST_TEST(to_string(TUuid("{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}")) == expected_1);
41 BOOST_TEST(to_string(TUuid("{5e2ab18817264e75a04f1ed9a6a89c4c}")) == expected_1);
42 BOOST_TEST(to_string(TUuid("5e2ab18817264e75a04f1ed9a6a89c4c")) == expected_1);
43}
44
45BOOST_AUTO_TEST_CASE(construction_string_invalid) {
46 // This test also ensures that the constructor does not throw
47 const std::string expected{"00000000-0000-0000-0000-000000000000"};
48
49 BOOST_TEST(to_string(TUuid("5e2ab188-1726-4e75-a04f")) == expected);
50 BOOST_TEST(to_string(TUuid("{}")) == expected);
51 BOOST_TEST(to_string(TUuid("{5e2ab18817264e75a04f1ed9a6a89c4c")) == expected);
52 BOOST_TEST(to_string(TUuid("5e2ab18817264e75a04f1ed9a689c4c")) == expected);
53}
54
55BOOST_AUTO_TEST_CASE(compare) {
56 BOOST_TEST(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")
57 == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
58 BOOST_TEST(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")
59 != TUuid("00000000-1726-4e75-a04f-1ed9a6a89c4c"));
60 BOOST_TEST(TUuid("{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}")
61 == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
62
63 // This comparison is expected to fail if strcmp is used
64 TUuid uuid_1{};
65 TUuid uuid_2{};
66 uuid_2.data()[15] = 0x64;
67 BOOST_TEST(uuid_1 != uuid_2);
68}
69
70BOOST_AUTO_TEST_CASE(assign_valid) {
71 TUuid uuid_1{};
72 BOOST_TEST(uuid_1.is_nil());
73 uuid_1 = TUuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
74 BOOST_TEST(!uuid_1.is_nil());
75
76 BOOST_TEST(uuid_1 == TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
77
78 uuid_1 = TUuid{"{12345678-1726-4e75-a04f-1ed9a6a89c4c}"};
79 BOOST_TEST(uuid_1 != TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
80 BOOST_TEST(uuid_1 == TUuid("{12345678-1726-4e75-a04f-1ed9a6a89c4c}"));
81}
82
83BOOST_AUTO_TEST_CASE(assign_invalid) {
84 TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
85 BOOST_TEST(!uuid_1.is_nil());
86
87 BOOST_CHECK_NO_THROW(uuid_1 = TUuid{"123"});
88 BOOST_TEST(uuid_1.is_nil());
89 BOOST_TEST(to_string(uuid_1) == std::string{"00000000-0000-0000-0000-000000000000"});
90}
91
92BOOST_AUTO_TEST_CASE(swap) {
93 TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
94 TUuid uuid_2{};
95 BOOST_TEST(!uuid_1.is_nil());
96 BOOST_TEST(uuid_2.is_nil());
97
98 using std::swap;
99 swap(uuid_1, uuid_2);
100
101 BOOST_TEST(uuid_1.is_nil());
102 BOOST_TEST(!uuid_2.is_nil());
103
104 BOOST_TEST(to_string(uuid_1) == std::string{"00000000-0000-0000-0000-000000000000"});
105 BOOST_TEST(to_string(uuid_2) == std::string{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"});
106}
107
108BOOST_AUTO_TEST_CASE(begin_end) {
109 TUuid uuid_1{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
110 BOOST_TEST(std::distance(std::begin(uuid_1), std::end(uuid_1)) == uuid_1.size());
111}
112
113BOOST_AUTO_TEST_CASE(into_boost_uuid) {
114 TUuid uuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
115 boost::uuids::uuid boost_uuid{};
116 BOOST_TEST(boost_uuid.is_nil());
117 std::copy(std::begin(uuid), std::end(uuid), boost_uuid.begin());
118 BOOST_TEST(!boost_uuid.is_nil());
119 BOOST_TEST(boost::uuids::to_string(boost_uuid) == "5e2ab188-1726-4e75-a04f-1ed9a6a89c4c");
120 BOOST_TEST(boost::uuids::to_string(boost_uuid) == to_string(uuid));
121}
122
123BOOST_AUTO_TEST_CASE(from_boost_uuid) {
124 static boost::uuids::string_generator gen;
125 boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
126 BOOST_TEST(!boost_uuid.is_nil());
127 TUuid uuid;
128 BOOST_TEST(uuid.is_nil());
129
130 std::copy(std::begin(boost_uuid), std::end(boost_uuid), uuid.begin());
131 BOOST_TEST(!uuid.is_nil());
132
133 BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
134}
135
136BOOST_AUTO_TEST_CASE(test_byte_order_variant) {
137 TUuid uuid{"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"};
138 boost::uuids::uuid boost_uuid{};
139 BOOST_TEST(boost_uuid.is_nil());
140 std::copy(std::begin(uuid), std::end(uuid), boost_uuid.begin());
141 BOOST_TEST(!boost_uuid.is_nil());
142 BOOST_TEST(boost_uuid.variant() == boost::uuids::uuid::variant_rfc_4122);
143}
144
145BOOST_AUTO_TEST_CASE(test_byte_order_verify_network) {
146 const TUuid uuid{"{00112233-4455-6677-8899-aabbccddeeff}"};
147
148 for (uint8_t idx = 0; idx < uuid.size(); ++idx) {
149 const uint8_t expected = idx * 0x11;
150 BOOST_TEST(*(std::begin(uuid) + idx) == expected);
151 }
152
153 const uint8_t test[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
154 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
155
156 TUuid new_uuid;
157 std::copy(std::begin(test), std::end(test), std::begin(new_uuid));
158
159 BOOST_TEST(!new_uuid.is_nil());
160 BOOST_TEST(to_string(new_uuid) == std::string{"00112233-4455-6677-8899-aabbccddeeff"});
161
162 BOOST_TEST(new_uuid == uuid);
163}
164
165BOOST_AUTO_TEST_CASE(test_character_buffer) {
166
167 const uint8_t test[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
168 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
169
170 const TUuid uuid{test};
171
172 BOOST_TEST(to_string(uuid) == std::string{"00112233-4455-6677-8899-aabbccddeeff"});
173}
174
175BOOST_AUTO_TEST_CASE(test_boost_buffer) {
176
177 static boost::uuids::string_generator gen;
178 boost::uuids::uuid boost_uuid{gen("1f610073-db33-4d21-adf2-75460d4955cc")};
179 BOOST_TEST(!boost_uuid.is_nil());
180
181 const TUuid uuid{boost_uuid.data};
182
183 BOOST_TEST(to_string(boost_uuid) == to_string(uuid));
184}
185
186BOOST_AUTO_TEST_SUITE_END()