-
Notifications
You must be signed in to change notification settings - Fork 14
/
minlength_test.go
151 lines (129 loc) · 4.24 KB
/
minlength_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
package sqids
import (
"reflect"
"testing"
)
func TestMinLengthSimple(t *testing.T) {
s, err := New(Options{
MinLength: uint8(len(defaultAlphabet)),
})
if err != nil {
t.Fatal(err)
}
numbers := []uint64{1, 2, 3}
id := "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM"
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if id != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, id, generatedID)
}
decodedNumbers := s.Decode(id)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
}
func TestMinLengthIncremental(t *testing.T) {
numbers := []uint64{1, 2, 3}
m := map[uint8]string{
6: "86Rf07",
7: "86Rf07x",
8: "86Rf07xd",
9: "86Rf07xd4",
10: "86Rf07xd4z",
11: "86Rf07xd4zB",
12: "86Rf07xd4zBm",
13: "86Rf07xd4zBmi",
}
m[uint8(len(defaultAlphabet))+0] = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM"
m[uint8(len(defaultAlphabet))+1] = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMy"
m[uint8(len(defaultAlphabet))+2] = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf"
m[uint8(len(defaultAlphabet))+3] = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf1"
for minLength, id := range m {
s, err := New(Options{
MinLength: minLength,
})
if err != nil {
t.Fatal(err)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if id != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, id, generatedID)
}
if len(generatedID) != int(minLength) {
t.Errorf("Encoding `%v` should produce `%v` length, but produced `%v` length instead", numbers, minLength, len(generatedID))
}
decodedNumbers := s.Decode(id)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
}
}
func TestMinLengthIncrementalNumbers(t *testing.T) {
s, err := New(Options{
MinLength: uint8(len(defaultAlphabet)),
})
if err != nil {
t.Fatal(err)
}
ids := map[string][]uint64{
"SvIzsqYMyQwI3GWgJAe17URxX8V924Co0DaTZLtFjHriEn5bPhcSkfmvOslpBu": {0, 0},
"n3qafPOLKdfHpuNw3M61r95svbeJGk7aAEgYn4WlSjXURmF8IDqZBy0CT2VxQc": {0, 1},
"tryFJbWcFMiYPg8sASm51uIV93GXTnvRzyfLleh06CpodJD42B7OraKtkQNxUZ": {0, 2},
"eg6ql0A3XmvPoCzMlB6DraNGcWSIy5VR8iYup2Qk4tjZFKe1hbwfgHdUTsnLqE": {0, 3},
"rSCFlp0rB2inEljaRdxKt7FkIbODSf8wYgTsZM1HL9JzN35cyoqueUvVWCm4hX": {0, 4},
"sR8xjC8WQkOwo74PnglH1YFdTI0eaf56RGVSitzbjuZ3shNUXBrqLxEJyAmKv2": {0, 5},
"uY2MYFqCLpgx5XQcjdtZK286AwWV7IBGEfuS9yTmbJvkzoUPeYRHr4iDs3naN0": {0, 6},
"74dID7X28VLQhBlnGmjZrec5wTA1fqpWtK4YkaoEIM9SRNiC3gUJH0OFvsPDdy": {0, 7},
"30WXpesPhgKiEI5RHTY7xbB1GnytJvXOl2p0AcUjdF6waZDo9Qk8VLzMuWrqCS": {0, 8},
"moxr3HqLAK0GsTND6jowfZz3SUx7cQ8aC54Pl1RbIvFXmEJuBMYVeW9yrdOtin": {0, 9},
}
for id, numbers := range ids {
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if id != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, id, generatedID)
}
decodedNumbers := s.Decode(id)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
}
}
func TestMinLengths(t *testing.T) {
for _, minLength := range []uint8{0, 1, 5, 10, uint8(len(defaultAlphabet))} {
for _, numbers := range [][]uint64{
{minUint64Value},
{0, 0, 0, 0, 0},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{100, 200, 300},
{1000, 2000, 3000},
{1000000},
{maxUint64Value},
} {
s, err := New(Options{
MinLength: minLength,
})
if err != nil {
t.Fatal(err)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if uint8(len(generatedID)) < minLength {
t.Errorf("Encoding `%v` with min length `%v` produced `%v`", numbers, minLength, generatedID)
}
decodedNumbers := s.Decode(generatedID)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", generatedID, numbers, decodedNumbers)
}
}
}
}