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
|
package main
import "testing"
func TestEncodeKnownValues(t *testing.T) {
tests := []struct {
id int64
want string
}{
{0, "AAAA"},
{1, "AAAQ"},
{256, "AEAA"},
{65535, "777Q"},
}
for _, tt := range tests {
got, err := encodeID(tt.id)
if err != nil {
t.Fatalf("encodeID(%d): %v", tt.id, err)
}
if got != tt.want {
t.Errorf("encodeID(%d) = %q, want %q", tt.id, got, tt.want)
}
}
}
func TestDecodeKnownValues(t *testing.T) {
tests := []struct {
s string
want int64
}{
{"AAAA", 0},
{"AAAQ", 1},
{"AEAA", 256},
{"777Q", 65535},
}
for _, tt := range tests {
got, err := decodeID(tt.s)
if err != nil {
t.Fatalf("decodeID(%q): %v", tt.s, err)
}
if got != tt.want {
t.Errorf("decodeID(%q) = %d, want %d", tt.s, got, tt.want)
}
}
}
func TestDecodeCaseInsensitive(t *testing.T) {
got, err := decodeID("aaaq")
if err != nil {
t.Fatalf("decodeID(aaaq): %v", err)
}
if got != 1 {
t.Errorf("decodeID(aaaq) = %d, want 1", got)
}
}
func TestRoundTrip(t *testing.T) {
for _, id := range []int64{0, 1, 100, 256, 1000, 65535, 100000, 1000000} {
s, err := encodeID(id)
if err != nil {
t.Fatalf("encodeID(%d): %v", id, err)
}
got, err := decodeID(s)
if err != nil {
t.Fatalf("decodeID(%q): %v", s, err)
}
if got != id {
t.Errorf("roundtrip(%d): got %d", id, got)
}
}
}
func TestEncodeOutOfRange(t *testing.T) {
if _, err := encodeID(-1); err == nil {
t.Error("encodeID(-1) should fail")
}
}
func TestDecodeInvalid(t *testing.T) {
invalids := []string{"", "A", "!!!!", "AAAAAA"}
for _, s := range invalids {
if _, err := decodeID(s); err == nil {
t.Errorf("decodeID(%q) should fail", s)
}
}
}
|