-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortening_test.go
155 lines (131 loc) · 2.99 KB
/
shortening_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
152
153
154
155
package shortening
import (
"math"
"math/rand"
"testing"
"time"
)
type testCoder struct {
enc func(n uint64) []byte
dec func(b []byte) (uint64, error)
}
func (c *testCoder) Encode(n uint64) []byte { return c.enc(n) }
func (c *testCoder) Decode(b []byte) (uint64, error) { return c.dec(b) }
func propEncCounting(t *testing.T, charset string, enc encoder) {
base := len(charset)
var n uint64
for i := -1; i < base; i++ {
for j := 0; j < base; j++ {
id := enc.Encode(n)
if id[len(id)-1] != charset[j] {
t.Fatalf("Expected rightmost char to be %q in %q", charset[j], id)
}
if i != -1 && id[0] != charset[i] {
t.Fatalf("Expected leftmost char to be %q in %q", charset[i], id)
}
n++
}
}
}
func propRollover(t *testing.T, charset string, table []uint64, enc encoder) {
firstChar := charset[0]
lastChar := charset[len(charset)-1]
for i := 0; i < len(table); i++ {
a := enc.Encode(table[i] - 1)
b := enc.Encode(table[i])
if len(a)+1 != len(b) {
t.Fatal("error")
}
for _, c := range a {
if c != lastChar {
t.Fatalf("expected all chars to be %q in %q", lastChar, a)
}
}
for _, c := range b {
if c != firstChar {
t.Fatalf("expected all chars to be %q in %q", firstChar, b)
}
}
}
}
func propDecCounting(t *testing.T, charset string, dec decoder) {
base := len(charset)
var n uint64
for i := -1; i < base; i++ {
for j := 0; j < base; j++ {
id := []byte{charset[j]}
if i != -1 {
id = []byte{charset[i], charset[j]}
}
nn, err := dec.Decode(id)
if err != nil {
t.Logf("%q", id)
t.Fatal(err)
}
if nn != n {
t.Errorf("got: %d - expected: %d", nn, n)
}
n++
}
}
}
func propParity(t *testing.T, c coder) {
// test first 10k
for i := uint64(0); i < 100000; i++ {
n, err := c.Decode(c.Encode(i))
if err != nil {
t.Logf("test ID: %d", i)
t.Fatal(err)
}
if n != i {
t.Errorf("got: %d - expected: %d", n, i)
}
}
// test last 10k
for i := uint64(math.MaxUint64 - 100000); i < math.MaxUint64; i++ {
n, err := c.Decode(c.Encode(i))
if err != nil {
t.Logf("test ID: %d", i)
t.Fatal(err)
}
if n != i {
t.Errorf("got: %d - expected: %d", n, i)
}
}
// test random 10k
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 100000; i++ {
v := rnd.Uint64()
n, err := c.Decode(c.Encode(v))
if err != nil {
t.Logf("test ID: %d", v)
t.Fatal(err)
}
if n != v {
t.Errorf("got: %d - expected: %d", n, v)
}
}
}
const benchSet = 1 << 13 // 8192
func benchEncode(b *testing.B, enc encoder) {
ids := make([]uint64, benchSet)
for i := range ids {
ids[i] = rand.Uint64()
}
b.Run("encode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = enc.Encode(ids[i&(benchSet-1)])
}
})
}
func benchDecode(b *testing.B, c coder) {
urls := make([][]byte, benchSet)
for i := range urls {
urls[i] = c.Encode(rand.Uint64())
}
b.Run("decode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = c.Decode(urls[i&(benchSet-1)])
}
})
}