-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec_test.go
352 lines (309 loc) · 10.1 KB
/
codec_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package conust
import (
"fmt"
"math/rand"
"strconv"
"testing"
)
func TestCodec(t *testing.T) {
codecTests := []struct {
name string
input string
encoded string
decoded string
}{
{name: "empty", input: "", encoded: "", decoded: ""},
{name: "zero 1", input: "0", encoded: "5", decoded: "0"},
{name: "zero 2", input: "+000", encoded: "5", decoded: "0"},
{name: "zero 3", input: "-000", encoded: "5", decoded: "0"},
{name: "zero 4", input: "000.0000", encoded: "5", decoded: "0"},
{
name: "all digits",
input: "1234567890abcdefghij.klmnopqrstuvwxyz",
encoded: "7k1234567890abcdefghijklmnopqrstuvwxyz",
decoded: "1234567890abcdefghij.klmnopqrstuvwxyz",
},
{
name: "negative all digits",
input: "-1234567890abcdefghij.klmnopqrstuvwxyz",
encoded: "3fyxwvutsrqzponmlkjihgfedcba9876543210~",
decoded: "-1234567890abcdefghij.klmnopqrstuvwxyz",
},
{name: "holes in the middle", input: "005f002k00.0i0k0", encoded: "785f002k000i0k", decoded: "5f002k00.0i0k"},
{name: "one", input: "1", encoded: "711", decoded: "1"},
{name: "ugly one", input: "+00001", encoded: "711", decoded: "1"},
{name: "negative one", input: "-1", encoded: "3yy~", decoded: "-1"},
{name: "ugly negative one", input: "-000001", encoded: "3yy~", decoded: "-1"},
{name: "ugly positive int", input: "+00000123000", encoded: "76123", decoded: "123000"},
{name: "ugly negative int", input: "-00000123000", encoded: "3tyxw~", decoded: "-123000"},
{name: "fractional", input: "54321.12345", encoded: "755432112345", decoded: "54321.12345"},
{name: "negative fractional", input: "-54321.12345", encoded: "3uuvwxyyxwvu~", decoded: "-54321.12345"},
{
name: "ugly fractional",
input: "+00054321000.00012345000",
encoded: "785432100000012345",
decoded: "54321000.00012345",
},
{
name: "ugly negative fractional",
input: "-00054321000.00012345000",
encoded: "3ruvwxyzzzzzzyxwvu~",
decoded: "-54321000.00012345",
},
{name: "cowboy hat", input: "cowboy.hat", encoded: "76cowboyhat", decoded: "cowboy.hat"},
{name: "negative cowboy hat", input: "-cowboy.hat", encoded: "3tnb3ob1ip6~", decoded: "-cowboy.hat"},
{
name: "maximum int length",
input: "12345678901234567890123456789012345.1",
encoded: "7z1123456789012345678901234567890123451",
decoded: "12345678901234567890123456789012345.1",
},
{
name: "maximum negative int length",
input: "-12345678901234567890123456789012345.1",
encoded: "30yyxwvutsrqzyxwvutsrqzyxwvutsrqzyxwvuy~",
decoded: "-12345678901234567890123456789012345.1",
},
{
name: "maximum fracleading zero count",
input: "0.000000000000000000000000000000000004325430",
encoded: "60y432543",
decoded: "0.00000000000000000000000000000000000432543",
},
{
name: "example 1",
input: "12000000000000000000000000000000000000",
encoded: "7z412",
decoded: "12000000000000000000000000000000000000",
},
{name: "example 2", input: "1200", encoded: "7412", decoded: "1200"},
{name: "example 3", input: "12", encoded: "7212", decoded: "12"},
{name: "example 4", input: "1.2", encoded: "7112", decoded: "1.2"},
{name: "example 5", input: "0.12", encoded: "6z12", decoded: "0.12"},
{name: "example 6", input: "0.0012", encoded: "6x12", decoded: "0.0012"},
{
name: "example 6.2",
input: "0.0000000000000000000000000000000000012",
encoded: "60y12",
decoded: "0.0000000000000000000000000000000000012",
},
{
name: "example 6.3",
input: "-0.0000000000000000000000000000000000012",
encoded: "4z1yx~",
decoded: "-0.0000000000000000000000000000000000012",
},
{name: "example 7", input: "-0.0012", encoded: "42yx~", decoded: "-0.0012"},
{name: "example 8", input: "-0.12", encoded: "40yx~", decoded: "-0.12"},
{name: "example 9", input: "-1.2", encoded: "3yyx~", decoded: "-1.2"},
{name: "example 10", input: "-12", encoded: "3xyx~", decoded: "-12"},
{name: "example 11", input: "-1200", encoded: "3vyx~", decoded: "-1200"},
{
name: "example 12",
input: "-12000000000000000000000000000000000000",
encoded: "30vyx~",
decoded: "-12000000000000000000000000000000000000",
},
}
codec := new(Codec)
for _, i := range codecTests {
t.Run(i.name, func(t *testing.T) {
encoded, ok := codec.EncodeToken(i.input)
if !ok {
t.Fatalf("Encoding failed for: %v\n", i.input)
}
if i.encoded != encoded {
t.Fatalf("Encoding expected: %v, got %v\n", i.encoded, encoded)
}
decoded, ok := codec.DecodeToken(encoded)
if !ok {
t.Fatalf("Decoding failed for: %v <- %v\n", i.input, decoded)
}
if i.decoded != decoded {
t.Fatalf("Decoding expected: %v, got %v\n", i.decoded, decoded)
}
})
}
}
func TestCodec_EncodeToken_Failure(t *testing.T) {
codecTests := []struct {
name string
input string
}{
{name: "space 1", input: " 123"},
{name: "space 2", input: "123 "},
{name: "space 3", input: "1 23"},
{name: "multiple decimal points 1", input: "1.2.3"},
{name: "multiple decimal points 2", input: "1.23."},
{name: "unexpected character 1", input: "X123"},
{name: "unexpected character 2", input: "123X"},
{name: "unexpected character 3", input: "12X3"},
}
codec := new(Codec)
for _, i := range codecTests {
t.Run(i.name, func(t *testing.T) {
encoded, ok := codec.EncodeToken(i.input)
if ok || encoded != "" {
t.Fatalf("Encoding should have failed for: %v\n", i.input)
}
})
}
}
func TestCodec_DecodeToken_Failure(t *testing.T) {
codecTests := []struct {
name string
input string
}{
{name: "too short", input: "4b"},
{name: "magnitude error", input: "600"},
{name: "no negative terminator", input: "40zx"},
{name: "non digit char", input: "7z412X"},
{name: "bad prefix", input: "2z412"},
}
codec := new(Codec)
for _, i := range codecTests {
t.Run(i.name, func(t *testing.T) {
decoded, ok := codec.DecodeToken(i.input)
if ok || decoded != "" {
t.Fatalf("Decoding should have failed for: %v\n", i.input)
}
})
}
}
func TestSortedness(t *testing.T) {
step := 0.01
prev := LessThanAny
c := new(Codec)
for i := -111111.0; i <= 111111.0; i++ {
str := fmt.Sprintf("%3f", i*step)
encoded, ok := c.EncodeToken(str)
if !ok {
t.Fatal("Encoding failed for", i)
}
if prev >= encoded {
t.Fatal("at", i*step, " ", prev, "is not smaller than", encoded)
}
prev = encoded
}
}
func BenchmarkEncoding(b *testing.B) {
step := 0.001
c := new(Codec)
to := float64(b.N / 2)
from := -1 * to
for i := from; i <= to; i++ {
str := strconv.FormatFloat(i*step, 'f', -1, 64)
encoded, ok := c.EncodeToken(str)
if !ok {
b.Fatal("Encoding failed for", i)
}
_, ok = c.DecodeToken(encoded)
if !ok {
b.Fatal("Decoding failed for", encoded, "in", i)
}
}
}
func TestEncodeMixedText(t *testing.T) {
testCases := []struct {
name string
input string
ok bool
output string
}{
{name: "empty", input: "", ok: true, output: ""},
{name: "no numbers", input: "quick brown fox", ok: true, output: "quick brown fox"},
{name: "only numbers", input: "423", ok: true, output: "73423"},
{name: "only numbers end with space", input: "423 ", ok: true, output: "73423 "},
{name: "only numbers starts with space", input: " 423", ok: true, output: " 73423"},
{name: "sign is like text", input: "-423", ok: true, output: "- 73423"},
{name: "mixed 1.1", input: "300Z", ok: true, output: "733 Z"},
{name: "mixed 1.2", input: "300 Z", ok: true, output: "733 Z"},
{name: "mixed 2.1", input: "A300Z", ok: true, output: "A 733 Z"},
{name: "mixed 2.2", input: "A 300 Z", ok: true, output: "A 733 Z"},
{name: "mixed 3.1", input: "A300", ok: true, output: "A 733"},
{name: "mixed 3.2", input: "A 300", ok: true, output: "A 733"},
{
name: "mixed 4",
input: "If 2x + 3y = 8 and 4x + 12y = 28, what is x and y?",
ok: true,
output: "If 712 x + 713 y = 718 and 714 x + 7212 y = 7228 , what is x and y?",
},
{name: "mixed c1", input: "SomeCam300D", ok: true, output: "SomeCam 733 D"},
{name: "mixed c2", input: "SomeCam600D", ok: true, output: "SomeCam 736 D"},
{name: "mixed c3", input: "SomeCam1000D", ok: true, output: "SomeCam 741 D"},
{name: "mixed c4", input: "SomeCam1100D", ok: true, output: "SomeCam 7411 D"},
}
c := new(Codec)
for _, i := range testCases {
t.Run(i.name, func(t *testing.T) {
encoded, ok := c.EncodeMixedText(i.input)
if ok != i.ok {
t.Fatalf("ok expected %v got %v", i.ok, ok)
}
if encoded != i.output {
t.Fatalf("output expected %s got %s", i.output, encoded)
}
})
}
}
func BenchmarkEncodeMixedText(b *testing.B) {
var charPool = [...]byte{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
' ', ' ', ' ', ' ', ' ', ' ',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
}
var genText = make([]byte, 1024)
var textLen = len(genText)
rand.Seed(42)
c := new(Codec)
for i := 0; i < textLen; i++ {
genText[i] = charPool[rand.Intn(len(charPool))]
}
for i := 0; i < b.N; i++ {
start := rand.Intn(textLen)
end := rand.Intn(textLen-start) + start
fragment := string(genText[start:end])
_, ok := c.EncodeMixedText(fragment)
if !ok {
b.Fatal("Encoding failed for " + fragment)
}
}
}
func ExampleCodec_EncodeToken() {
c := new(Codec)
out, ok := c.EncodeToken("86400")
fmt.Printf("%q, %v\n", out, ok)
out, ok = c.EncodeToken("-3.14")
fmt.Printf("%q, %v\n", out, ok)
out, ok = c.EncodeToken("-base36.number")
fmt.Printf("%q, %v\n", out, ok)
// Output:
// "75864", true
// "3ywyv~", true
// "3top7lwtc5dol8~", true
}
func ExampleCodec_DecodeToken() {
c := new(Codec)
out, ok := c.DecodeToken("42yx~")
fmt.Printf("%q, %v\n", out, ok)
out, ok = c.DecodeToken("6w125")
fmt.Printf("%q, %v\n", out, ok)
// Output:
// "-0.0012", true
// "0.000125", true
}
func ExampleCodec_EncodeMixedText() {
c := new(Codec)
out, ok := c.EncodeMixedText("SomeCam 40d")
fmt.Printf("%q, %v\n", out, ok)
out, ok = c.EncodeMixedText("SomeCam350d")
fmt.Printf("%q, %v\n", out, ok)
out, ok = c.EncodeMixedText("SomeCam1100 d")
fmt.Printf("%q, %v\n", out, ok)
// Output:
// "SomeCam 724 d", true
// "SomeCam 7335 d", true
// "SomeCam 7411 d", true
}