summaryrefslogtreecommitdiffstats
path: root/tests/UUID/UUIDTest.cpp
blob: 1912da8fa1da01034de02c700d614acf88c633b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

// UUIDTest.cpp

#include "Globals.h"
#include "../TestHelpers.h"
#include "UUID.h"

#include <numeric>  // for std::iota





/** Test that FromString -> ToShortString preserves the original value for short UUIDs. */
static void UUIDFromStringToShortString()
{
	const char TestStrings[][33]
	{
		"0123456789abcdef0123456789ABCDEF",
		"d188b2648cc311e7bb31be2e44b06b34",
		"e760d270d8b34288b895d9f78a31e083",
		"7052f2f2594246abb8e3fed602158870",
		"7f14d4b60cd84ba7885c8301b67ce891",
		"57be7039250548b590af272291fabcfa"
	};

	for (auto TestString : TestStrings)
	{
		cUUID UUID;
		TEST_TRUE(UUID.FromString(TestString));
		auto ResultString = UUID.ToShortString();
		// Result should be equivalent to original
		TEST_EQUAL(NoCaseCompare(ResultString, TestString), 0);
		// And should be all lower case
		TEST_EQUAL(ResultString, StrToLower(ResultString));
	}
}





/** Test that FromString -> ToLongString preserves the original value for long UUIDs. */
static void UUIDFromStringToLongString()
{
	const char TestStrings[][37]
	{
		"01234567-89ab-cdef-0123-456789ABCDEF",
		"d188b264-8cc3-11e7-bb31-be2e44b06b34",
		"e760d270-d8b3-4288-b895-d9f78a31e083",
		"7052f2f2-5942-46ab-b8e3-fed602158870",
		"7f14d4b6-0cd8-4ba7-885c-8301b67ce891",
		"57be7039-2505-48b5-90af-272291fabcfa"
	};

	for (auto TestString : TestStrings)
	{
		cUUID UUID;
		TEST_TRUE(UUID.FromString(TestString));
		auto ResultString = UUID.ToLongString();
		// Result should be equivalent to original
		TEST_EQUAL(NoCaseCompare(ResultString, TestString), 0);
		// And should be all lower case
		TEST_EQUAL(ResultString, StrToLower(ResultString));
	}
}





/** Test that FromRaw -> ToRaw preserves the original value. */
static void UUIDFromRawToRaw()
{
	std::array<Byte, 16> TestData[16]{};
	// Fill test data with all values 0 - 255
	for (int i = 0; i != 16; ++i)
	{
		std::iota(begin(TestData[i]), end(TestData[i]), i * 16);
	}

	for (const auto & TestRaw : TestData)
	{
		cUUID UUID;
		UUID.FromRaw(TestRaw);
		auto ResultRaw = UUID.ToRaw();
		TEST_EQUAL(ResultRaw, TestRaw);
	}
}





/** Test that IsNil correctly identifies nil UUIDs. */
static void UUIDNil()
{
	const auto NilString    = "00000000-0000-0000-0000-000000000000";
	const auto NonNilString = "e760d270-d8b3-4288-b895-d9f78a31e083";

	{
		cUUID UUID;
		TEST_TRUE(UUID.FromString(NilString));
		TEST_TRUE(UUID.IsNil());
	}
	{
		cUUID UUID;
		TEST_TRUE(UUID.FromString(NonNilString));
		TEST_TRUE(!UUID.IsNil());
	}
}





/** Test that variant and version numbers are read correctly. */
static void UUIDType()
{
	// From issue #4209
	const char TestStrings[][33]
	{
		"31cfcbeea8f2324a86dbccb1d5aaa6f8",
		"3f2e1dd234a03b6b824c5a0e74083b1e"
	};

	for (const auto & String : TestStrings)
	{
		cUUID UUID;
		TEST_TRUE(UUID.FromString(String));
		TEST_EQUAL(UUID.Variant(), 1);
		TEST_EQUAL(UUID.Version(), 3);
	}

}





IMPLEMENT_TEST_MAIN("UUID",
	UUIDFromStringToShortString();
	UUIDFromStringToLongString();
	UUIDFromRawToRaw();
	UUIDNil();
	UUIDType();
)