v / vlib / rand / random_identifiers_test.v
136 lines · 127 sloc · 3.09 KB · 2332ecff4811b8c97dfda8e825170e9397962519
Raw
1import time
2import rand
3
4fn validate_separators(uuid string) {
5 assert uuid[8] == `-`
6 assert uuid[13] == `-`
7 assert uuid[18] == `-`
8 assert uuid[23] == `-`
9 assert uuid.len == 36
10}
11
12// uuid_v4:
13fn test_rand_uuid_v4() {
14 uuid1 := rand.uuid_v4()
15 uuid2 := rand.uuid_v4()
16 uuid3 := rand.uuid_v4()
17 validate_separators(uuid1)
18 validate_separators(uuid2)
19 validate_separators(uuid3)
20 assert uuid1 != uuid2
21 assert uuid1 != uuid3
22 assert uuid2 != uuid3
23 for i in 0 .. 1000 {
24 x := rand.uuid_v4()
25 // check the version field is always 4:
26 assert x[14] == `4`
27 // and the clock_seq_hi_and_reserved field is valid too:
28 assert x[19] in [`8`, `9`, `a`, `b`]
29 validate_separators(x)
30 }
31}
32
33// uuid_v7:
34fn test_rand_uuid_v7() {
35 uuid1 := rand.uuid_v7()
36 uuid2 := rand.uuid_v7()
37 uuid3 := rand.uuid_v7()
38 validate_separators(uuid1)
39 validate_separators(uuid2)
40 validate_separators(uuid3)
41 assert uuid1 != uuid2
42 assert uuid1 != uuid3
43 assert uuid2 != uuid3
44 for i in 0 .. 1000 {
45 x := rand.uuid_v7()
46 // check the version field is always 7:
47 assert x[14] == `7`
48 // and variant field is always 0b10:
49 assert x[19] in [`8`, `9`, `a`, `b`]
50 validate_separators(x)
51 }
52}
53
54// uuid_v7_session:
55fn test_rand_uuid_v7_session() {
56 mut u := rand.new_uuid_v7_session()
57 uuid1 := u.next()
58 uuid2 := u.next()
59 uuid3 := u.next()
60 assert uuid1 != uuid2
61 assert uuid1 != uuid3
62 assert uuid2 != uuid3
63 assert uuid1.len == 36
64 assert uuid2.len == 36
65 assert uuid3.len == 36
66 mut prev_counter := `3`
67 for i in 0 .. 1000 {
68 x := u.next()
69 // check the version field is always 7:
70 assert x[14] == `7`
71 // and variant field is always 0b10:
72 assert x[19] in [`8`, `9`, `a`, `b`]
73
74 // verify counter increase
75 assert x[17] == prev_counter
76 if prev_counter == `9` {
77 prev_counter = `a`
78 } else if prev_counter == `f` {
79 prev_counter = `0`
80 } else {
81 prev_counter++
82 }
83 }
84}
85
86// ulids:
87fn test_ulids_are_unique() {
88 ulid1 := rand.ulid()
89 ulid2 := rand.ulid()
90 ulid3 := rand.ulid()
91 assert ulid1.len == 26
92 assert ulid2.len == 26
93 assert ulid3.len == 26
94 assert ulid1 != ulid2
95 assert ulid1 != ulid3
96 assert ulid2 != ulid3
97}
98
99fn test_ulids_max_start_character_is_ok() {
100 ulid1 := rand.ulid()
101 // the largest valid ULID encoded in Base32 is 7ZZZZZZZZZZZZZZZZZZZZZZZZZ
102 assert (int(ulid1[0]) - 48) <= 7
103}
104
105fn test_ulids_generated_in_the_same_millisecond_have_the_same_prefix() {
106 t := u64(time.utc().unix_milli())
107 mut ulid1 := ''
108 mut ulid2 := ''
109 mut ulid3 := ''
110 ulid1 = rand.ulid_at_millisecond(t)
111 ulid2 = rand.ulid_at_millisecond(t)
112 ulid3 = rand.ulid_at_millisecond(t)
113 ulid1_prefix := ulid1[0..10]
114 ulid2_prefix := ulid2[0..10]
115 ulid3_prefix := ulid3[0..10]
116 assert ulid1_prefix == ulid2_prefix
117 assert ulid1_prefix == ulid3_prefix
118}
119
120fn test_ulids_should_be_lexicographically_ordered_when_not_in_same_millisecond() {
121 ulid1 := rand.ulid()
122 time.sleep(1 * time.millisecond)
123 ulid2 := rand.ulid()
124 time.sleep(1 * time.millisecond)
125 ulid3 := rand.ulid()
126 mut all := [ulid3, ulid2, ulid1]
127 // eprintln('all before: ${all}')
128 all.sort()
129 // eprintln('all after: ${all}')
130 s1 := all[0]
131 s2 := all[1]
132 s3 := all[2]
133 assert s1 == ulid1
134 assert s2 == ulid2
135 assert s3 == ulid3
136}
137