-
Notifications
You must be signed in to change notification settings - Fork 14
/
sqids_test.go
47 lines (41 loc) · 1.18 KB
/
sqids_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
package sqids
import "testing"
func TestCalculateOffset(t *testing.T) {
for _, tt := range []struct {
alphabet string
numbers []uint64
want int
}{
{"", []uint64{}, -1},
{"", []uint64{0}, -1},
{"abcde", []uint64{0}, 3},
{"fghij", []uint64{0}, 3},
{"abcde", []uint64{1}, 4},
{"abcde", []uint64{2}, 0},
{defaultAlphabet, []uint64{24}, 60},
{defaultAlphabet, []uint64{25}, 61},
{defaultAlphabet, []uint64{26}, 4},
{defaultAlphabet, []uint64{27}, 5},
{defaultAlphabet, []uint64{1, 2, 3}, 55},
{defaultAlphabet, []uint64{4, 5, 6}, 2},
} {
if _, err := New(Options{Alphabet: tt.alphabet}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got := calculateOffset(tt.alphabet, tt.numbers, 0); got != tt.want {
t.Fatalf("calculateOffset(%q, %#v) = %d, want %d", tt.alphabet, tt.numbers, got, tt.want)
}
}
}
func TestDecode(t *testing.T) {
s, err := New()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := len(s.Decode("Re")), 0; got != want {
t.Fatalf(`len(s.Decode("Re")) = %d, want %d`, got, want)
}
if got, want := len(s.Decode("U9")), 1; got != want {
t.Fatalf(`len(s.Decode("U9")) = %d, want %d`, got, want)
}
}