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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
@safe unittest
{
import std.uuid;
import std.uuid;
UUID[] ids;
ids ~= randomUUID();
ids ~= md5UUID("test.name.123");
ids ~= sha1UUID("test.name.123");
foreach (entry; ids)
{
assert(entry.variant == UUID.Variant.rfc4122);
}
assert(ids[0].uuidVersion == UUID.Version.randomNumberBased);
assert(ids[1].toString() == "22390768-cced-325f-8f0f-cfeaa19d0ccd");
assert(ids[1].data == [34, 57, 7, 104, 204, 237, 50, 95, 143, 15, 207,
234, 161, 157, 12, 205]);
UUID id;
assert(id.empty);
}
@safe pure unittest
{
import std.uuid;
enum ubyte[16] data = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
auto uuid = UUID(data);
enum ctfe = UUID(data);
assert(uuid.data == data);
assert(ctfe.data == data);
}
@safe unittest
{
import std.uuid;
auto tmp = UUID(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
assert(tmp.data == cast(ubyte[16])[0,1,2,3,4,5,6,7,8,9,10,11,
12,13,14,15]);
}
@safe pure unittest
{
import std.uuid;
auto id = UUID("8AB3060E-2cba-4f23-b74c-b52db3bdfb46");
assert(id.data == [138, 179, 6, 14, 44, 186, 79, 35, 183, 76,
181, 45, 179, 189, 251, 70]);
assert(id.toString() == "8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
//Can also be used in CTFE, for example as UUID literals:
enum ctfeID = UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
//here parsing is done at compile time, no runtime overhead!
}
@safe pure unittest
{
import std.uuid;
UUID id;
assert(id.empty);
id = UUID("00000000-0000-0000-0000-000000000001");
assert(!id.empty);
}
@safe pure unittest
{
import std.uuid;
assert(UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46").variant
== UUID.Variant.rfc4122);
}
@safe unittest
{
import std.uuid;
assert(UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46").uuidVersion
== UUID.Version.randomNumberBased);
}
@safe unittest
{
import std.uuid;
immutable ubyte[16] data = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
UUID u1;
UUID u2 = UUID(data);
u1.swap(u2);
assert(u1 == UUID(data));
assert(u2 == UUID.init);
}
@safe pure unittest
{
import std.uuid;
//compare UUIDs
assert(UUID("00000000-0000-0000-0000-000000000000") == UUID.init);
//UUIDs in associative arrays:
int[UUID] test = [UUID("8a94f585-d180-44f7-8929-6fca0189c7d0") : 1,
UUID("7c351fd4-b860-4ee3-bbdc-7f79f3dfb00a") : 2,
UUID("9ac0a4e5-10ee-493a-86fc-d29eeb82ecc1") : 3];
assert(test[UUID("9ac0a4e5-10ee-493a-86fc-d29eeb82ecc1")] == 3);
//UUIDS can be sorted:
import std.algorithm;
UUID[] ids = [UUID("8a94f585-d180-44f7-8929-6fca0189c7d0"),
UUID("7c351fd4-b860-4ee3-bbdc-7f79f3dfb00a"),
UUID("9ac0a4e5-10ee-493a-86fc-d29eeb82ecc1")];
sort(ids);
}
@safe pure unittest
{
import std.uuid;
immutable str = "8ab3060e-2cba-4f23-b74c-b52db3bdfb46";
auto id = UUID(str);
assert(id.toString() == str);
}
@safe unittest
{
import std.uuid;
UUID id;
assert(id.empty);
id = randomUUID;
assert(!id.empty);
id = UUID(cast(ubyte[16]) [138, 179, 6, 14, 44, 186, 79,
35, 183, 76, 181, 45, 179, 189, 251, 70]);
assert(id.toString() == "8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
}
@safe unittest
{
import std.uuid;
//Use default UUID.init namespace
auto simpleID = md5UUID("test.uuid.any.string");
//use a name-based id as namespace
auto namespace = md5UUID("my.app");
auto id = md5UUID("some-description", namespace);
}
@safe unittest
{
import std.uuid;
//Use default UUID.init namespace
auto simpleID = sha1UUID("test.uuid.any.string");
//use a name-based id as namespace
auto namespace = sha1UUID("my.app");
auto id = sha1UUID("some-description", namespace);
}
@safe unittest
{
import std.uuid;
import std.random : Xorshift192, unpredictableSeed;
//simple call
auto uuid = randomUUID();
//provide a custom RNG. Must be seeded manually.
Xorshift192 gen;
gen.seed(unpredictableSeed);
auto uuid3 = randomUUID(gen);
}
@safe unittest
{
import std.uuid;
auto id = parseUUID("8AB3060E-2CBA-4F23-b74c-B52Db3BDFB46");
//no dashes
id = parseUUID("8ab3060e2cba4f23b74cb52db3bdfb46");
//dashes at different positions
id = parseUUID("8a-b3-06-0e2cba4f23b74c-b52db3bdfb-46");
//leading / trailing characters
id = parseUUID("{8ab3060e-2cba-4f23-b74c-b52db3bdfb46}");
//unicode
id = parseUUID("ü8ab3060e2cba4f23b74cb52db3bdfb46ü");
//multiple trailing/leading characters
id = parseUUID("///8ab3060e2cba4f23b74cb52db3bdfb46||");
//Can also be used in CTFE, for example as UUID literals:
enum ctfeID = parseUUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
//here parsing is done at compile time, no runtime overhead!
}
@safe unittest
{
import std.uuid;
import std.algorithm;
import std.regex;
string test = "Lorem ipsum dolor sit amet, consetetur "~
"6ba7b814-9dad-11d1-80b4-00c04fd430c8 sadipscing \n"~
"elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore \r\n"~
"magna aliquyam erat, sed diam voluptua. "~
"8ab3060e-2cba-4f23-b74c-b52db3bdfb46 At vero eos et accusam et "~
"justo duo dolores et ea rebum.";
auto r = regex(uuidRegex, "g");
UUID[] found;
foreach (c; match(test, r))
{
found ~= UUID(c.hit);
}
assert(found == [
UUID("6ba7b814-9dad-11d1-80b4-00c04fd430c8"),
UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46"),
]);
}
@safe unittest
{
import std.uuid;
import std.exception : collectException;
const inputUUID = "this-is-an-invalid-uuid";
auto ex = collectException!UUIDParsingException(UUID(inputUUID));
assert(ex !is null); // check that exception was thrown
assert(ex.input == inputUUID);
assert(ex.position == 0);
assert(ex.reason == UUIDParsingException.Reason.tooLittle);
}
|