-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcuid_test.go
76 lines (64 loc) · 1.2 KB
/
cuid_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cuid
import (
"reflect"
"testing"
)
func Test_CUIDType(t *testing.T) {
c := New()
if reflect.TypeOf(c).Name() != "string" {
t.Error("Incorrect CUID type")
}
}
func Test_CUIDFormat(t *testing.T) {
c := New()
if err := IsCuid(c); err != nil {
t.Error("Incorrect format")
}
}
func Test_CUIDCollisions(t *testing.T) {
ids := map[string]bool{}
for i := 0; i < 600000; i++ {
id := New()
if ids[id] == true {
t.Errorf("Collision detected, at iteration %d", i)
}
ids[id] = true
}
}
func Test_CUIDSlugFormat(t *testing.T) {
c := Slug()
if err := IsSlug(c); err != nil {
t.Errorf("Slug incorrect format. Len: %d", len(c))
}
}
func Test_CUIDSlugCollisions(t *testing.T) {
ids := map[string]bool{}
for i := 0; i < 60000; i++ {
id := Slug()
if ids[id] == true {
t.Errorf("Collision detected, at iteration %d", i)
}
ids[id] = true
}
}
func newCUID(chn chan error) {
New()
chn <- nil
}
func Test_DataRaces(t *testing.T) {
chn := make(chan error)
go newCUID(chn)
go newCUID(chn)
<-chn
<-chn
}
func Benchmark_CUIDGeneration(b *testing.B) {
for i := 0; i < b.N; i++ {
New()
}
}
func Benchmark_CUIDSlugGeneration(b *testing.B) {
for i := 0; i < b.N; i++ {
Slug()
}
}