mirror of
https://github.com/vlang/v.git
synced 2025-09-10 07:47:20 -04:00
rand: check the separators for the generated UUIDs in random_identifiers_test.v
This commit is contained in:
parent
649041c8b4
commit
880f2169b7
@ -1,23 +1,32 @@
|
|||||||
import time
|
import time
|
||||||
import rand
|
import rand
|
||||||
|
|
||||||
|
fn validate_separators(uuid string) {
|
||||||
|
assert uuid[8] == `-`
|
||||||
|
assert uuid[13] == `-`
|
||||||
|
assert uuid[18] == `-`
|
||||||
|
assert uuid[23] == `-`
|
||||||
|
assert uuid.len == 36
|
||||||
|
}
|
||||||
|
|
||||||
// uuid_v4:
|
// uuid_v4:
|
||||||
fn test_rand_uuid_v4() {
|
fn test_rand_uuid_v4() {
|
||||||
uuid1 := rand.uuid_v4()
|
uuid1 := rand.uuid_v4()
|
||||||
uuid2 := rand.uuid_v4()
|
uuid2 := rand.uuid_v4()
|
||||||
uuid3 := rand.uuid_v4()
|
uuid3 := rand.uuid_v4()
|
||||||
|
validate_separators(uuid1)
|
||||||
|
validate_separators(uuid2)
|
||||||
|
validate_separators(uuid3)
|
||||||
assert uuid1 != uuid2
|
assert uuid1 != uuid2
|
||||||
assert uuid1 != uuid3
|
assert uuid1 != uuid3
|
||||||
assert uuid2 != uuid3
|
assert uuid2 != uuid3
|
||||||
assert uuid1.len == 36
|
|
||||||
assert uuid2.len == 36
|
|
||||||
assert uuid3.len == 36
|
|
||||||
for i in 0 .. 1000 {
|
for i in 0 .. 1000 {
|
||||||
x := rand.uuid_v4()
|
x := rand.uuid_v4()
|
||||||
// check the version field is always 4:
|
// check the version field is always 4:
|
||||||
assert x[14] == `4`
|
assert x[14] == `4`
|
||||||
// and the clock_seq_hi_and_reserved field is valid too:
|
// and the clock_seq_hi_and_reserved field is valid too:
|
||||||
assert x[19] in [`8`, `9`, `a`, `b`]
|
assert x[19] in [`8`, `9`, `a`, `b`]
|
||||||
|
validate_separators(x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,18 +35,19 @@ fn test_rand_uuid_v7() {
|
|||||||
uuid1 := rand.uuid_v7()
|
uuid1 := rand.uuid_v7()
|
||||||
uuid2 := rand.uuid_v7()
|
uuid2 := rand.uuid_v7()
|
||||||
uuid3 := rand.uuid_v7()
|
uuid3 := rand.uuid_v7()
|
||||||
|
validate_separators(uuid1)
|
||||||
|
validate_separators(uuid2)
|
||||||
|
validate_separators(uuid3)
|
||||||
assert uuid1 != uuid2
|
assert uuid1 != uuid2
|
||||||
assert uuid1 != uuid3
|
assert uuid1 != uuid3
|
||||||
assert uuid2 != uuid3
|
assert uuid2 != uuid3
|
||||||
assert uuid1.len == 36
|
|
||||||
assert uuid2.len == 36
|
|
||||||
assert uuid3.len == 36
|
|
||||||
for i in 0 .. 1000 {
|
for i in 0 .. 1000 {
|
||||||
x := rand.uuid_v7()
|
x := rand.uuid_v7()
|
||||||
// check the version field is always 7:
|
// check the version field is always 7:
|
||||||
assert x[14] == `7`
|
assert x[14] == `7`
|
||||||
// and variant field is always 0b10:
|
// and variant field is always 0b10:
|
||||||
assert x[19] in [`8`, `9`, `a`, `b`]
|
assert x[19] in [`8`, `9`, `a`, `b`]
|
||||||
|
validate_separators(x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user