-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyid_test.go
72 lines (65 loc) · 1.71 KB
/
tinyid_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
package tinyid
import (
"strings"
"testing"
)
func TestGenerateTinyID(t *testing.T) {
// Test with default settings
id, err := generateTinyId(DefaultAlphabet, DefaultSize)
if err != nil {
t.Errorf("Error generating tiny ID: %v", err)
}
if len(id) != DefaultSize {
t.Errorf("Expected ID length to be %v, got %v", DefaultSize, len(id))
}
for _, char := range id {
if !strings.Contains(DefaultAlphabet, string(char)) {
t.Errorf("ID contains invalid character: %v", char)
}
}
// Test with custom settings
customAlphabet := "abc123"
customSize := 8
id, err = generateTinyId(customAlphabet, customSize)
if err != nil {
t.Errorf("Error generating tiny ID with custom settings: %v", err)
}
if len(id) != customSize {
t.Errorf("Expected ID length to be %v, got %v", customSize, len(id))
}
for _, char := range id {
if !strings.Contains(customAlphabet, string(char)) {
t.Errorf("ID contains invalid character: %v", char)
}
}
// Test with invalid size
_, err = generateTinyId(DefaultAlphabet, 0)
if err == nil {
t.Error("Expected error for invalid size, got nil")
}
}
func TestNewTinyID(t *testing.T) {
// Test with default settings
id, err := NewTinyID()
if err != nil {
t.Errorf("Error generating new tiny ID: %v", err)
}
if len(id) != DefaultSize {
t.Errorf("Expected ID length to be %v, got %v", DefaultSize, len(id))
}
for _, char := range id {
if !strings.Contains(DefaultAlphabet, string(char)) {
t.Errorf("ID contains invalid character: %v", char)
}
}
}
func BenchmarkNewTinyID(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = NewTinyID()
}
}
func BenchmarkGenerateTinyID(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = generateTinyId(DefaultAlphabet, DefaultSize)
}
}