-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanoid_test.go
179 lines (149 loc) · 3.32 KB
/
nanoid_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Tests & benchmarks
package nanoid_test
import (
"github.com/IndexStorm/nanoid-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestStandard(t *testing.T) {
t.Run("general", func(t *testing.T) {
f, err := nanoid.Standard(21)
assert.NoError(t, err, "should be no error")
id := f()
assert.Len(t, id, 21, "should return the same length as the ID specified length")
t.Log(id)
})
t.Run("negative ID length", func(t *testing.T) {
_, err := nanoid.Standard(-1)
assert.Error(t, err, "should error if passed ID length is negative")
})
t.Run("invalid length (256)", func(t *testing.T) {
_, err := nanoid.Standard(256)
assert.Error(t, err, "should error if length > 255")
})
t.Run("invalid length (1)", func(t *testing.T) {
_, err := nanoid.Standard(1)
assert.Error(t, err, "should error if length < 2")
})
}
func TestCustom(t *testing.T) {
t.Run("general", func(t *testing.T) {
f, err := nanoid.CustomASCII("abcdef", 21)
assert.NoError(t, err, "should be no error")
id := f()
assert.Len(t, id, 21, "should return the same length as the ID specified length")
t.Log(id)
})
}
func TestMustCustom(t *testing.T) {
t.Run("general", func(t *testing.T) {
f := nanoid.MustCustomASCII("abcdef", 21)
id := f()
assert.Len(t, id, 21, "should return the same length as the ID specified length")
t.Log(id)
})
}
func TestMustCustomPanic(t *testing.T) {
t.Run("general", func(t *testing.T) {
f := func() {
nanoid.MustCustomASCII("abcdef", 1)
}
assert.Panics(t, f, "MustCustomASCII should have paniced")
})
}
func TestFlatDistribution(t *testing.T) {
tries := 500_000
set := "0123456789" // 10.
length := len(set)
hits := make(map[rune]int)
f, err := nanoid.CustomASCII(set, length)
if err != nil {
panic(err)
}
for i := 0; i < tries; i++ {
id := f()
for _, r := range id {
hits[r]++
}
}
for _, count := range hits {
require.InEpsilon(t, length*tries/len(set), count, 0.01, "should have flat-distribution")
}
}
func TestCollisions(t *testing.T) {
tries := 500_000
used := make(map[string]bool)
f, err := nanoid.Standard(8)
if err != nil {
panic(err)
}
for i := 0; i < tries; i++ {
id := f()
require.False(t, used[id], "shouldn't be any colliding IDs")
used[id] = true
}
}
func Benchmark8NanoID(b *testing.B) {
f, err := nanoid.Standard(8)
if err != nil {
panic(err)
}
for n := 0; n < b.N; n++ {
f()
}
}
func Benchmark21NanoID(b *testing.B) {
f, err := nanoid.Standard(21)
if err != nil {
panic(err)
}
for n := 0; n < b.N; n++ {
f()
}
}
func Benchmark36NanoID(b *testing.B) {
f, err := nanoid.Standard(36)
if err != nil {
panic(err)
}
for n := 0; n < b.N; n++ {
f()
}
}
func Benchmark255NanoID(b *testing.B) {
f, err := nanoid.Standard(255)
if err != nil {
panic(err)
}
for n := 0; n < b.N; n++ {
f()
}
}
func BenchmarkCustomUnicodeNanoID(b *testing.B) {
f, err := nanoid.CustomASCII("°Ô‘š±?¿⾃ⶃⵏ⟎⸸ⵌ", 8)
if err != nil {
panic(err)
}
for n := 0; n < b.N; n++ {
f()
}
}
func BenchmarkCustomASCIINanoID(b *testing.B) {
f, err := nanoid.CustomASCII("0123456789", 8)
if err != nil {
panic(err)
}
for n := 0; n < b.N; n++ {
f()
}
}
func BenchmarkASCIINanoID(b *testing.B) {
f, err := nanoid.ASCII(21)
if err != nil {
panic(err)
}
for n := 0; n < b.N; n++ {
f()
}
}