-
Notifications
You must be signed in to change notification settings - Fork 0
/
short64_test.go
66 lines (54 loc) · 1.58 KB
/
short64_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
package shortening
import (
"math"
"testing"
)
var testCoder64 = &testCoder{Encode, Decode}
func TestEncode64(t *testing.T) {
// verify counting for two place values
propEncCounting(t, CharSet64, testCoder64)
// checks expected values before and after a place value change.
propRollover(t, CharSet64, minTable[1:], testCoder64)
// manualy check max uint64 encode
maxEncoded := "O---------_"
if e := string(testCoder64.Encode(math.MaxUint64)); e != maxEncoded {
t.Errorf("got: %q - expected: %q", e, maxEncoded)
}
}
func TestDecode64(t *testing.T) {
propDecCounting(t, CharSet64, testCoder64)
// manualy check max uint64 encode
n, err := testCoder64.Decode([]byte("O---------_"))
if err != nil {
t.Error(err)
}
if max := uint64(math.MaxUint64); n != max {
t.Errorf("got: %q - expected: %q", n, max)
}
}
func TestDecode64Errors(t *testing.T) {
tests := []struct {
ID []byte
Err error
}{
{nil, InvalidDecodeLen},
{[]byte(""), InvalidDecodeLen},
{[]byte("-_987654321"), Overflow},
{[]byte("AAAAAAAAAAAA"), InvalidDecodeLen},
{[]byte("*"), InvalidCharacter},
{[]byte("\xFF"), InvalidCharacter},
}
for _, test := range tests {
_, err := testCoder64.Decode(test.ID)
if err == nil {
t.Logf("%q", test.ID)
t.Error("error expected. got nil")
} else if err != test.Err {
t.Logf("%q", test)
t.Fatalf("got: %q - expected: %q", err, test.Err)
}
}
}
func TestEncDec64Parity(t *testing.T) { propParity(t, testCoder64) }
func BenchmarkEncode64(b *testing.B) { benchEncode(b, testCoder64) }
func BenchmarkDecode64(b *testing.B) { benchDecode(b, testCoder64) }