-
Notifications
You must be signed in to change notification settings - Fork 0
/
thespine_test.go
408 lines (374 loc) · 8.44 KB
/
thespine_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package thespine
import (
"fmt"
"log"
"strings"
"testing"
"unicode/utf8"
)
func ExampleDecode() {
str := "nespithe"
o, err := Decode(str)
if err != nil {
log.Fatal(err)
}
fmt.Println(o)
// Output: thespine
}
func ExampleDecodeText() {
str := "nespithe erecshyrinol"
o, err := DecodeText(str)
if err != nil {
log.Fatal(err)
}
fmt.Println(o)
// Output: thespine nolyricshere
}
func ExampleEncode() {
str := "nolyricshere"
o, err := EncodeText(str)
if err != nil {
log.Fatal(err)
}
fmt.Println(o)
// Output: erecshyrinol
}
func ExampleEncodeText() {
str := "nolyricshere thespine"
o, err := EncodeText(str)
if err != nil {
log.Fatal(err)
}
fmt.Println(o)
// Output: erecshyrinol nespithe
}
func Test_Decode(t *testing.T) {
t.Parallel()
tests := []struct {
str string
name string
want string
wantErr bool
}{
{
str: "",
name: "empty string",
want: "",
wantErr: false,
},
{
str: "erecshyrinol",
name: "the song",
want: "nolyricshere",
wantErr: false,
},
{
str: "nespithe",
name: "the album",
want: "thespine",
wantErr: false,
},
{
str: "seteernkub",
name: "the tech",
want: "kubernetes",
wantErr: false,
},
{
str: "ᚬᚩᚡᚣ",
name: "the runes",
want: "ᚩᚡᚣᚬ",
wantErr: false,
},
{
str: "\xf2",
name: "the invalid string",
want: "",
wantErr: true,
},
{
str: "\xf0\x9f\x9a\x80ketroc",
name: "the cringe",
want: "rocket\xf0\x9f\x9a\x80",
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got, err := Decode(test.str)
if test.wantErr && err == nil {
t.Fatalf("decode err wanted but got none")
}
if got != test.want {
t.Fatalf("decode got: '%s'\nwant: '%s'\n", got, test.want)
}
})
}
}
func Test_Encode(t *testing.T) {
t.Parallel()
tests := []struct {
str string
name string
want string
wantErr bool
}{
{
str: "",
name: "empty string",
want: "",
wantErr: false,
},
{
str: "nolyricshere",
name: "the song",
want: "erecshyrinol",
wantErr: false,
},
{
str: "\xf2",
name: "the invalid string",
want: "",
wantErr: true,
},
{
str: "rocket\xf0\x9f\x9a\x80",
name: "the cringe",
want: "\xf0\x9f\x9a\x80ketroc",
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
enc, err := Encode(test.str)
if test.wantErr && err == nil {
t.Fatalf("encode err wanted but got none")
}
if enc != test.want {
t.Fatalf("encode got: '%s'\nwant: '%s'\n", enc, test.want)
}
})
}
}
func FuzzEncode(f *testing.F) {
tcs := []string{"rocket\xf0\x9f\x9a\x80", "abba acdc", "Hello, 世界!"}
for _, tc := range tcs {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, og string) {
ec, _ := Encode(og)
if ec != "" {
dc, _ := Decode(ec)
ogr := []rune(og)
dcr := []rune(dc)
if og != dc {
t.Errorf("Before: '%q', after: '%q'", og, dc)
}
if len(ogr) != len(dcr) {
t.Errorf("Length before: %d, after: %d", len(og), len(dc))
}
}
})
}
func Test_EncodeText(t *testing.T) {
t.Parallel()
tests := []struct {
str string
name string
want string
wantErr bool
}{
{
str: "meh noob",
name: "the noob",
want: "meh bnoo",
wantErr: false,
},
{
str: "The Putrefying Road In The 19th Extremity (...Somewhere Inside The Bowels Of The Endlessness...)",
name: "the noob2",
want: "The gyinrefPut dRoa In The h19t ityremExt ehermew.So(.. ideIns The elsBow Of The ..)ss.snelesEnd",
wantErr: false,
},
{
str: "\xf2",
name: "the invalid string",
want: "",
wantErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
o, _ := EncodeText(test.str)
if o != test.want {
t.Fatalf("encode got: '%s'\nwant: '%s'\n", o, test.want)
}
})
}
}
func Test_DecodeText(t *testing.T) {
t.Parallel()
tests := []struct {
str string
name string
want string
wantErr bool
}{
{
str: "The gyinrefPut dRoa In The h19t ityremExt ehermew.So(.. ideIns The elsBow Of The ..)ss.snelesEnd",
name: "the puzzle",
want: "The Putrefying Road In The 19th Extremity (...Somewhere Inside The Bowels Of The Endlessness...)",
wantErr: false,
},
{
str: "\xf2",
name: "the invalid string",
want: "",
wantErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
o, _ := DecodeText(test.str)
if o != test.want {
t.Fatalf("encode got: '%s'\nwant: '%s'\n", o, test.want)
}
})
}
}
func FuzzEncodeDecodeComprehensive(f *testing.F) {
// Add seed corpus
seeds := []string{
"", // Empty string
"a", // Single char
"ab", // Two chars
"abc", // Three chars (group size)
"abcd", // More than group size
"Hello, 世界!", // Mixed ASCII and Unicode
"🌍🌎🌏", // Only emojis
" ", // Multiple spaces
"a\nb\tc", // Special whitespace
"a b c", // Multiple consecutive spaces
strings.Repeat("a", 1000), // Long string
"ᚠᛇᚻ᛫ᛒᛦᚦ", // Runes
"\u200B\u200C\u200D", // Zero-width characters
"a\u0300\u0301b\u0302c", // Combining diacritical marks
}
for _, seed := range seeds {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, input string) {
// Skip invalid UTF-8
if !utf8.ValidString(input) {
return
}
// Test 1: Encode->Decode roundtrip
encoded, err := Encode(input)
if err != nil {
// Some inputs might legitimately fail to encode
return
}
decoded, err := Decode(encoded)
if err != nil {
t.Errorf("Failed to decode encoded string: %v", err)
return
}
if decoded != input {
t.Errorf("Roundtrip failed: input=%q, got=%q", input, decoded)
}
// Test 2: Check encoded string properties
inputRunes := []rune(input)
encodedRunes := []rune(encoded)
if len(inputRunes) != len(encodedRunes) {
t.Errorf("Length mismatch: input=%d, encoded=%d", len(inputRunes), len(encodedRunes))
}
// Test 3: Multiple encode/decode cycles
current := input
for i := range 3 {
encoded, err := Encode(current)
if err != nil {
t.Errorf("Failed at cycle %d: %v", i, err)
return
}
decoded, err := Decode(encoded)
if err != nil {
t.Errorf("Failed at cycle %d: %v", i, err)
return
}
if decoded != current {
t.Errorf("Cycle %d failed: expected=%q, got=%q", i, current, decoded)
}
current = decoded
}
})
}
func FuzzEncodeDecodeText(f *testing.F) {
seeds := []string{
"",
"hello world",
" spaced words ",
"one two three four",
"Hello,\nWorld!",
"Tab\there",
"Mixed 世界 Unicode",
"🌍 Earth 🌎 Globe 🌏",
strings.Repeat("word ", 100),
}
for _, seed := range seeds {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, input string) {
// Skip invalid UTF-8
if !utf8.ValidString(input) {
return
}
// Test 1: EncodeText->DecodeText roundtrip
encoded, err := EncodeText(input)
if err != nil {
return
}
decoded, err := DecodeText(encoded)
if err != nil {
t.Errorf("Failed to decode encoded text: %v", err)
return
}
// Normalize spaces for comparison since that's part of the spec
normalizeSpaces := func(s string) string {
return strings.Join(strings.Fields(s), " ")
}
normalizedInput := normalizeSpaces(input)
normalizedDecoded := normalizeSpaces(decoded)
if normalizedDecoded != normalizedInput {
t.Errorf("Roundtrip failed:\ninput=%q\ngot=%q", normalizedInput, normalizedDecoded)
}
// Test 2: Check word boundaries are preserved
inputWords := strings.Fields(input)
encodedWords := strings.Fields(encoded)
if len(inputWords) != len(encodedWords) {
t.Errorf("Word count mismatch: input=%d, encoded=%d", len(inputWords), len(encodedWords))
}
})
}
// Add benchmark tests.
func BenchmarkEncode(b *testing.B) {
inputs := []struct {
name string
str string
}{
{"small", "hello"},
{"medium", strings.Repeat("hello", 100)},
{"large", strings.Repeat("hello", 1000)},
{"unicode", "Hello, 世界! 🌍"},
}
for _, input := range inputs {
b.Run(input.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Encode(input.str)
}
})
}
}