/Users/deen/code/yugabyte-db/src/yb/util/strongly_typed_uuid-test.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | #include <gtest/gtest.h> |
14 | | |
15 | | #include "yb/util/result.h" |
16 | | #include "yb/util/strongly_typed_uuid.h" |
17 | | #include "yb/util/test_macros.h" |
18 | | |
19 | | namespace yb { |
20 | | namespace util { |
21 | | |
22 | | YB_STRONGLY_TYPED_UUID(TestUuid); |
23 | | YB_STRONGLY_TYPED_UUID_IMPL(TestUuid); |
24 | | |
25 | 1 | TEST(TestStronglyTypedUuid, TestBasic) { |
26 | | // Assert that constant kUndefined.IsValid() is false and that undefined == undefined. |
27 | 1 | ASSERT_TRUE(TestUuid::Nil().IsNil()); |
28 | 1 | ASSERT_TRUE(TestUuid::Nil() == TestUuid::Nil()); |
29 | 1 | ASSERT_EQ(TestUuid::Nil(), TestUuid::Nil()); |
30 | | |
31 | 1 | TestUuid strongly_typed_uuid_0 = TestUuid::GenerateRandom(); |
32 | 1 | TestUuid strongly_typed_uuid_0_copy = strongly_typed_uuid_0; |
33 | 1 | TestUuid strongly_typed_uuid_1 = TestUuid::GenerateRandom(); |
34 | | |
35 | | // Assert two strongly typed uuids created from the same uuid are equal. |
36 | 1 | ASSERT_TRUE(strongly_typed_uuid_0 == strongly_typed_uuid_0_copy); |
37 | 1 | ASSERT_EQ(strongly_typed_uuid_0, strongly_typed_uuid_0_copy); |
38 | | |
39 | | // Assert two strongly typed uuids created from different uuids are not equal. |
40 | 1 | ASSERT_TRUE(strongly_typed_uuid_0 != strongly_typed_uuid_1); |
41 | 1 | ASSERT_NE(strongly_typed_uuid_0, strongly_typed_uuid_1); |
42 | | |
43 | | // Assert that GenerateUuidFromString and ToString are inverses. |
44 | 1 | auto strong_typed_uuid_0_from_string = |
45 | 1 | TestUuidFromString(strongly_typed_uuid_0.ToString()); |
46 | 1 | ASSERT_TRUE(strong_typed_uuid_0_from_string.ok()); |
47 | 1 | ASSERT_EQ(strongly_typed_uuid_0, *strong_typed_uuid_0_from_string); |
48 | | |
49 | | // Assert that generating a uuid from "" is undefined. |
50 | 1 | auto uuid_from_string_empty = ASSERT_RESULT(TestUuidFromString("")); |
51 | 1 | ASSERT_TRUE(uuid_from_string_empty.IsNil()); |
52 | | |
53 | | // Assert that uuid from invalid string returns result not okay. |
54 | 1 | auto uuid_from_string_invalid = TestUuidFromString("invalid_string"); |
55 | 1 | LOG(INFO) << "uuid_from_string_invalid: " << uuid_from_string_invalid; |
56 | 1 | ASSERT_FALSE(uuid_from_string_invalid.ok()); |
57 | | |
58 | | // Assert that ToString of undefined uuid returns "<Uuid undefined>". |
59 | 1 | ASSERT_EQ(TestUuid::Nil().ToString(), "00000000-0000-0000-0000-000000000000"); |
60 | | |
61 | | // Assert that * operator and constructor are inverses. |
62 | 1 | ASSERT_EQ(strongly_typed_uuid_0, TestUuid(Uuid(*strongly_typed_uuid_0))); |
63 | | |
64 | | // Assert that a defined uuid is valid. |
65 | 1 | ASSERT_FALSE(strongly_typed_uuid_0.IsNil()); |
66 | 1 | } |
67 | | |
68 | | } // namespace util |
69 | | } // namespace yb |
70 | | |