-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid_test.go
60 lines (46 loc) · 1.12 KB
/
uuid_test.go
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
// Copyright 2017 <CompanyName>, Inc. All Rights Reserved.
package hashgen
import (
"strings"
"testing"
"unicode"
)
// Run 'go test -bench=".*"' to get an idea of how many UUID are generated per second.
func BenchmarkUuidGen(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = getUuid()
}
}
func TestUuidGen(t *testing.T) {
sizehint := 1000
var counters map[string]int = make(map[string]int, sizehint)
for i := 0; i < sizehint; i++ {
uuid := getUuid()
counters[uuid]++
}
for k, v := range counters {
if v > 1 {
t.Errorf("Duplicate value %q", k)
}
}
}
// The generated UUID is used as a key, it must not have trailing space.
func TestUuidLength(t *testing.T) {
// Result must be a string
var uuid string = getUuid()
// Result must have no spaces
len1 := len(uuid)
var uuidPrime = strings.TrimSpace(uuid)
if len1 != len(uuidPrime) {
t.Error("Result UUID has spaces")
}
if len(uuid) != 36 {
t.Errorf("Expected length 36, got %d", len(uuid))
}
// Resulting bytes must all be printable
for _, c := range uuid {
if ok := unicode.IsPrint(rune(c)); !ok {
t.Error("Unprintable character")
}
}
}