tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

422 lignes · 11154 octetsBrut · Historique
1// Expected encodings come from a separate model (Python's `uuid` module and
2// a base conversion), not from this package.
3
4import std/bytes
5import std/list
6import std/random.{type random}
7import std/test.{assert, assert_equal}
8import std/time.{type clock}
9import uuid.
10{ type Alphabet
11, type Uuid
12, InvalidChar
13, NotAscending
14, NotUrlSafe
15, Repeated
16, TooLarge
17, TooShort
18, WrongLength
19}
20
21/// RFC 9562, appendix A.6.
22fn example :: -> Uuid
23{
24 uuid.v7_from
25 0x017F22E279B0
26 ( bytes.from_list
27 [0x0C, 0xC3, 0x18, 0xC4, 0xDC, 0x0C, 0x0C, 0x07, 0x39, 0x8F]
28 )
29}
30
31fn presets :: -> List<(String, Alphabet)>
32{
33 [ ("hex", uuid.hex ())
34 , ("crockford", uuid.crockford ())
35 , ("base58", uuid.base58 ())
36 , ("base62", uuid.base62 ())
37 ]
38}
39
40/// 50 IDs of random bytes, the same on every run.
41fn random_ids :: -> List<Uuid>
42{
43 random.with_seed
44 5
45 fn
46 {
47 list.map
48 (list.range 1 50)
49 fn _
50 {
51 match uuid.from_bytes (random.bytes 16)
52 { Some(id) -> id, None -> panic "16 bytes" }
53 }
54 }
55}
56
57@test
58fn the_rfc_example ::
59{
60 let id = example ()
61 assert_equal (uuid.to_rfc id) "017f22e2-79b0-7cc3-98c4-dc0c0c07398f"
62 assert_equal (uuid.version id) 7
63 assert_equal (uuid.timestamp id) Some(1645557742000)
64}
65
66@test
67fn nil_and_max ::
68{
69 assert_equal
70 (uuid.to_rfc (uuid.nil ()))
71 "00000000-0000-0000-0000-000000000000"
72 assert_equal
73 (uuid.to_rfc (uuid.max ()))
74 "ffffffff-ffff-ffff-ffff-ffffffffffff"
75 assert_equal (uuid.timestamp (uuid.nil ())) None
76 // Version 15 and the wrong variant: not a v7 timestamp.
77 assert_equal (uuid.timestamp (uuid.max ())) None
78 assert
79 (uuid.nil () < example () && example () < uuid.max ())
80 "ordered by bytes"
81}
82
83@test
84fn bytes_round_trip ::
85{
86 assert_equal (uuid.from_bytes (uuid.to_bytes (example ()))) Some(example ())
87 assert_equal (uuid.from_bytes (bytes.from_list [1, 2, 3])) None
88}
89
90@test
91fn the_rfc_form_round_trips_and_ignores_case ::
92{
93 assert_equal
94 (uuid.from_rfc "017f22e2-79b0-7cc3-98c4-dc0c0c07398f")
95 Some(example ())
96 assert_equal
97 (uuid.from_rfc "017F22E2-79B0-7CC3-98C4-DC0C0C07398F")
98 Some(example ())
99 list.each
100 (random_ids ())
101 fn id { assert_equal (uuid.from_rfc (uuid.to_rfc id)) Some(id) }
102}
103
104@test
105fn the_rfc_form_is_strict ::
106{
107 let refused =
108 [ ""
109 , "017f22e279b07cc398c4dc0c0c07398f"
110 , "\{017f22e2-79b0-7cc3-98c4-dc0c0c07398f\}"
111 , "017f22e2-79b0-7cc3-98c4-dc0c0c07398"
112 , "017f22e2-79b0-7cc3-98c4-dc0c0c07398f0"
113 , "017f22e2_79b0-7cc3-98c4-dc0c0c07398f"
114 , "017f22e2-79b0-7cc3-98c4-dc0c0c07398g"
115 , "017f22e-279b0-7cc3-98c4-dc0c0c07398f"
116 ]
117 list.each refused fn text { assert_equal (uuid.from_rfc text) None }
118}
119
120@test
121fn each_preset_has_its_width ::
122{
123 let widths =
124 list.map (presets ()) fn p { match p { (name, a) -> (name, uuid.width a) } }
125 assert_equal
126 widths
127 [("hex", 32), ("crockford", 26), ("base58", 22), ("base62", 22)]
128}
129
130@test
131fn the_example_and_max_in_each_preset ::
132{
133 assert_equal
134 (uuid.encode (example ()) (uuid.hex ()))
135 "017f22e279b07cc398c4dc0c0c07398f"
136 assert_equal
137 (uuid.encode (example ()) (uuid.crockford ()))
138 "01FWHE4YDGFK1SHH6W1G60EECF"
139 assert_equal
140 (uuid.encode (example ()) (uuid.base58 ()))
141 "1BihbxwwQ4NZZpKRH9JDCz"
142 assert_equal
143 (uuid.encode (example ()) (uuid.base62 ()))
144 "02p5oQZoHTv0zeY5yG21K3"
145 assert_equal
146 (uuid.encode (uuid.max ()) (uuid.crockford ()))
147 "7ZZZZZZZZZZZZZZZZZZZZZZZZZ"
148 assert_equal
149 (uuid.encode (uuid.max ()) (uuid.base58 ()))
150 "YcVfxkQb6JRzqk5kF2tNLv"
151 assert_equal
152 (uuid.encode (uuid.max ()) (uuid.base62 ()))
153 "7n42DGM5Tflk9n8mt7Fhc7"
154 assert_equal
155 (uuid.encode (uuid.nil ()) (uuid.base58 ()))
156 "1111111111111111111111"
157 assert_equal (show (example ())) "01FWHE4YDGFK1SHH6W1G60EECF"
158 assert_equal
159 (debug (example ()))
160 "uuid.Uuid(017f22e2-79b0-7cc3-98c4-dc0c0c07398f)"
161}
162
163@test
164fn every_preset_round_trips ::
165{
166 let ids = [uuid.nil (), uuid.max (), example (), ..random_ids ()]
167 list.each
168 (presets ())
169 fn p
170 {
171 match p
172 { (_, a) ->
173 list.each
174 ids
175 fn id { assert_equal (uuid.decode (uuid.encode id a) a) Ok(id) }
176 }
177 }
178}
179
180@test
181fn decode_refuses_what_no_uuid_encodes ::
182{
183 let c = uuid.crockford ()
184 assert_equal
185 (uuid.decode "01FWHE4YDGFK1SHH6W1G60EEC" c)
186 Error(WrongLength(25))
187 assert_equal
188 (uuid.decode "01FWHE4YDGFK1SHH6W1G60EECF0" c)
189 Error(WrongLength(27))
190 assert_equal
191 (uuid.decode "01FWHE4YDGFK1SHH6W1G60EECU" c)
192 Error(InvalidChar(25))
193 assert_equal
194 (uuid.decode "0-FWHE4YDGFK1SHH6W1G60EECF" c)
195 Error(InvalidChar(1))
196 // 8 * 32^25 = 2^128.
197 assert_equal (uuid.decode "80000000000000000000000000" c) Error(TooLarge)
198 assert_equal
199 (uuid.decode "zzzzzzzzzzzzzzzzzzzzzz" (uuid.base62 ()))
200 Error(TooLarge)
201 // Base 58 is case-sensitive and has no 0.
202 assert_equal
203 (uuid.decode "0111111111111111111111" (uuid.base58 ()))
204 Error(InvalidChar(0))
205}
206
207@test
208fn crockford_reads_lowercase_and_look_alikes ::
209{
210 let c = uuid.crockford ()
211 assert_equal (uuid.decode "01fwhe4ydgfk1shh6w1g60eecf" c) Ok(example ())
212 // I and L read as 1, O as 0.
213 assert_equal (uuid.decode "O1FWHE4YDGFKISHH6W1G6OEECF" c) Ok(example ())
214 assert_equal (uuid.decode "o1FWHE4YDGFKlSHH6W1G6oEECF" c) Ok(example ())
215 assert_equal
216 (uuid.decode "017F22E279B07CC398C4DC0C0C07398F" (uuid.hex ()))
217 Ok(example ())
218}
219
220@test
221fn alphabets_are_checked ::
222{
223 let error = fn chars: String
224 {
225 match uuid.alphabet chars { Ok(_) -> None, Error(e) -> Some(e) }
226 }
227 assert_equal (error "") Some(TooShort)
228 assert_equal (error "a") Some(TooShort)
229 assert_equal (error "ab a") Some(NotUrlSafe(" "))
230 assert_equal (error "ab/") Some(NotUrlSafe("/"))
231 assert_equal (error "abca") Some(Repeated("a"))
232 assert_equal (error "ba") Some(NotAscending("a"))
233 assert_equal (error "-._~09AZaz") Some(NotAscending("0"))
234 assert_equal (error "-.09AZ_az~") None
235}
236
237@test
238fn a_custom_alphabet_round_trips ::
239{
240 match uuid.alphabet "01"
241 { Ok(binary) ->
242 {
243 assert_equal (uuid.width binary) 128
244 list.each
245 [example (), uuid.max (), ..random_ids ()]
246 fn id
247 {
248 assert_equal (uuid.decode (uuid.encode id binary) binary) Ok(id)
249 }
250 }
251 , Error(e) -> panic "{e}"
252 }
253}
254
255/// The presets skip `alphabet`'s checks and carry their width: they must
256/// agree with the alphabet built from the same characters.
257@test
258fn presets_agree_with_their_alphabet ::
259{
260 list.each
261 [ ("0123456789abcdef", uuid.hex ())
262 , ("0123456789ABCDEFGHJKMNPQRSTVWXYZ", uuid.crockford ())
263 , ( "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
264 , uuid.base58 ()
265 )
266 , ( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
267 , uuid.base62 ()
268 )
269 ]
270 fn pair
271 {
272 let (chars, preset) = pair
273 match uuid.alphabet chars
274 { Ok(built) ->
275 {
276 assert_equal (uuid.width preset) (uuid.width built)
277 list.each
278 [example (), uuid.nil (), uuid.max (), ..random_ids ()]
279 fn id
280 {
281 assert_equal (uuid.encode id preset) (uuid.encode id built)
282 }
283 }
284 , Error(e) -> panic "{chars}: {e}"
285 }
286 }
287}
288
289/// Alphabets of 2^k characters are read and written k bits at a time. When
290/// k does not divide 128, the first digit only uses its low bits.
291@test
292fn power_of_two_alphabets ::
293{
294 let octal = alphabet_of "01234567"
295 assert_equal (uuid.width octal) 43
296 assert_equal
297 (uuid.encode (example ()) octal)
298 "0013762134236330174607143046700601401634617"
299 assert_equal
300 (uuid.encode (uuid.max ()) octal)
301 "3777777777777777777777777777777777777777777"
302 assert_equal
303 (uuid.decode "4000000000000000000000000000000000000000000" octal)
304 Error(TooLarge)
305 let quaternary = alphabet_of "0123"
306 assert_equal
307 (uuid.encode (example ()) quaternary)
308 "0001133302023202132123001330300321203010313000300030001303212033"
309 let sixty_four =
310 alphabet_of
311 "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
312 assert_equal (uuid.width sixty_four) 22
313 assert_equal (uuid.encode (example ()) sixty_four) "-0UmAXTQ0wktY3r-kB0naE"
314 assert_equal (uuid.encode (uuid.max ()) sixty_four) "2zzzzzzzzzzzzzzzzzzzzz"
315 assert_equal (uuid.decode "3---------------------" sixty_four) Error(TooLarge)
316 list.each
317 [octal, quaternary, sixty_four]
318 fn a
319 {
320 list.each
321 [example (), uuid.nil (), uuid.max (), ..random_ids ()]
322 fn id { assert_equal (uuid.decode (uuid.encode id a) a) Ok(id) }
323 }
324}
325
326/// An invalid character is reported before a value too large, as when the
327/// whole value was computed first.
328@test
329fn an_invalid_character_comes_before_too_large ::
330{
331 assert_equal
332 (uuid.decode "8000000000000000000000000U" (uuid.crockford ()))
333 Error(InvalidChar(25))
334}
335
336fn alphabet_of :: chars: String -> Alphabet
337{
338 match uuid.alphabet chars { Ok(a) -> a, Error(e) -> panic "{chars}: {e}" }
339}
340
341/// Whether text compares like the IDs, for each consecutive pair.
342fn sorts_alike :: ids: List<Uuid> a: Alphabet -> Bool
343{
344 match ids
345 { [x, y, ..rest] ->
346 (x < y) == (uuid.encode x a < uuid.encode y a)
347 && sorts_alike [y, ..rest] a
348 , _ -> True
349 }
350}
351
352@test
353fn text_sorts_like_the_ids ::
354{
355 let ids = random_ids ()
356 list.each
357 (presets ())
358 fn p { match p { (name, a) -> assert (sorts_alike ids a) name } }
359}
360
361@test
362fn v7_ids_sort_by_time ::
363{
364 let r = bytes.from_list [255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
365 let zero = bytes.from_list [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
366 // A later millisecond sorts after, whatever the random bits.
367 assert (uuid.v7_from 1000 r < uuid.v7_from 1001 zero) "by time"
368 list.each
369 (presets ())
370 fn p
371 {
372 match p
373 { (name, a) ->
374 assert
375 ( uuid.encode (uuid.v7_from 1000 r) a
376 < uuid.encode (uuid.v7_from 1001 zero) a
377 )
378 name
379 }
380 }
381 assert_equal
382 (uuid.timestamp (uuid.v7_from 281474976710655 r))
383 Some(281474976710655)
384}
385
386@test
387fn v7_reads_the_clock_and_randomness ::
388{
389 let id =
390 time.with_fixed_clock
391 (time.from_unix_ms 1700000000000)
392 fn { random.with_seed 9 fn { uuid.v7 () } }
393 assert_equal (uuid.to_rfc id) "018bcfe5-6800-7460-b0be-fe52afae62ea"
394 assert_equal (show id) "01HF7YAT00EHGB1FQYAAQTWRQA"
395 assert_equal (uuid.timestamp id) Some(1700000000000)
396}
397
398@test
399fn real_v7_ids_carry_the_time :: -> () / <clock, random>
400{
401 let before = time.unix_ms (time.now ())
402 let id = uuid.v7 ()
403 assert_equal (uuid.version id) 7
404 match uuid.timestamp id
405 { Some(ms) -> assert (ms >= before) "not before the clock"
406 , None -> panic "a v7 UUID has a timestamp"
407 }
408}
409
410@test
411@should_panic "not in [0, 2^48)"
412fn v7_from_refuses_times_before_1970 ::
413{
414 let _ = uuid.v7_from (-1) (bytes.from_list [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
415}
416
417@test
418@should_panic "expected 10 random bytes, got 9"
419fn v7_from_refuses_other_amounts_of_randomness ::
420{
421 let _ = uuid.v7_from 0 (bytes.from_list [0, 0, 0, 0, 0, 0, 0, 0, 0])
422}