-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase53a_test.go
405 lines (338 loc) · 13.6 KB
/
base53a_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
package util_test
import (
"fmt"
"strings"
"testing"
"github.com/1f604/util"
)
func expect(t *testing.T, b53m *util.Base53IDManager, input_str string, input_csum byte, remap bool, expected_result interface{}) {
t.Helper()
id, err := b53m.NewBase53ID(input_str, input_csum, remap)
if err != nil {
util.Assert_error_equals(t, err, expected_result.(string), 2)
} else {
util.Assert_result_equals_interface(t, id.String(), err, expected_result.(string), 2)
}
}
func remap_helper(t *testing.T, b53m *util.Base53IDManager, input_str string, input_rune byte, output_str string) {
t.Helper()
result, err := b53m.NewBase53ID(input_str, input_rune, true)
if err != nil {
panic(err)
}
util.Assert_result_equals_interface(t, result.GetCombinedString(), err, output_str, 2)
}
func Test_Remapping(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
// remapping flag = true, test that it remaps
remap_helper(t, b53m, "8uO", '2', "8u02")
remap_helper(t, b53m, "OfJy", 'O', "0fJy0") // this test checks that the remapping works for the checksum character too
remap_helper(t, b53m, "O9vi", '9', "0gvig") // as does this one, which checks remapping for both O and 9
remap_helper(t, b53m, "OfH9", 'U', "0fHgU")
remap_helper(t, b53m, "O9tY", '9', "0gtYg")
remap_helper(t, b53m, "O9r9", 'B', "0grgB")
// remapping flag = false, test it errors
_, err := b53m.NewBase53ID("8uO", '2', false)
util.Assert_error_equals(t, err, "Base53: Input string contains illegal character", 1)
_, err = b53m.NewBase53ID("8u", 'O', false)
util.Assert_error_equals(t, err, "Base53: Input string contains illegal character", 1)
_, err = b53m.NewBase53ID("898", '2', false)
util.Assert_error_equals(t, err, "Base53: Input string contains illegal character", 1)
}
func Test_Base53_Validation_Fails_Illegal_Chars(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
illegal_chars := []byte{'1', '9', 'I', 'O', 'l', 'W', 'w', 'd', 'm'}
for _, ic := range illegal_chars {
// one character test
expect(t, b53m, string(ic), '4', false, "Base53: Input string contains illegal character")
expect(t, b53m, "4", ic, false, "Base53: Input string contains illegal character")
expect(t, b53m, "af4876", ic, false, "Base53: Input string contains illegal character")
// two character test
expect(t, b53m, string(ic)+"a", '4', false, "Base53: Input string contains illegal character")
expect(t, b53m, "a"+string(ic), '4', false, "Base53: Input string contains illegal character")
// three character test
expect(t, b53m, string(ic)+"ab", '4', false, "Base53: Input string contains illegal character")
expect(t, b53m, "ab"+string(ic), '4', false, "Base53: Input string contains illegal character")
expect(t, b53m, "a"+string(ic)+"b", '4', false, "Base53: Input string contains illegal character")
}
}
func Test_Conversions(t *testing.T) {
t.Parallel()
buf := []byte{50, 52, 53, 0, 0, 0, 0, 0}
uint1 := util.Convert_str_to_uint64(string(buf))
str := util.Convert_uint64_to_str(uint1, 3)
uint2 := util.Convert_str_to_uint64(str)
if uint1 != uint2 || string(buf[0:3]) != str {
fmt.Println(uint1, uint2)
fmt.Println(string(buf[0:3]), str)
panic("Unexpected result")
}
}
func Test_length_check(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
expect(t, b53m, "", '4', true, "Base53: Input string without checksum is too short (1 char min)")
expect(t, b53m, "", '4', false, "Base53: Input string without checksum is too short (1 char min)")
expect(t, b53m, strings.Repeat("a", 51), '4', true, "Base53: Input string without checksum is too long (51 chars max)")
expect(t, b53m, strings.Repeat("a", 52), '4', true, "Base53: Input string without checksum is too long (51 chars max)")
}
func Test_fails_illegal_pairs(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
illegal_pairs := []string{"vv", "nn", "VV", "rn"}
for _, ip := range illegal_pairs {
// 0 char test
expect(t, b53m, string(ip[0]), ip[1], true, "Base53: Input string contains illegal pair")
expect(t, b53m, string(ip[0]), ip[1], false, "Base53: Input string contains illegal pair")
expect(t, b53m, ip, '4', true, "Base53: Input string contains illegal pair")
expect(t, b53m, ip, '4', false, "Base53: Input string contains illegal pair")
// 1 char test
expect(t, b53m, "a"+string(ip[0]), ip[1], true, "Base53: Input string contains illegal pair")
expect(t, b53m, ip+"a", '4', true, "Base53: Input string contains illegal pair")
expect(t, b53m, "a"+ip, '4', true, "Base53: Input string contains illegal pair")
// 2 char test
expect(t, b53m, "ab"+string(ip[0]), ip[1], true, "Base53: Input string contains illegal pair")
expect(t, b53m, ip+"ab", '4', true, "Base53: Input string contains illegal pair")
expect(t, b53m, "ab"+ip, '4', true, "Base53: Input string contains illegal pair")
expect(t, b53m, "a"+ip+"b", '4', true, "Base53: Input string contains illegal pair")
}
}
func Test_checksum_validation(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
// Test checksum passes
expect(t, b53m, "0", '0', true, "Base53ID: 00 (str:0, csum:0)")
expect(t, b53m, "0gsk", 'i', true, "Base53ID: 0gski (str:0gsk, csum:i)")
expect(t, b53m, "Ogsk", 'i', true, "Base53ID: 0gski (str:0gsk, csum:i)")
expect(t, b53m, "Ogsk", 'i', false, "Base53: Input string contains illegal character")
// Test checksum fails
expect(t, b53m, "0", '2', true, "Base53: Input checksum does not match string")
expect(t, b53m, "0gsk", 'j', true, "Base53: Input checksum does not match string")
expect(t, b53m, "0g5k", 'i', true, "Base53: Input checksum does not match string")
expect(t, b53m, "0gSk", 'i', true, "Base53: Input checksum does not match string")
expect(t, b53m, "0gsK", 'i', true, "Base53: Input checksum does not match string")
expect(t, b53m, "0sgk", 'i', true, "Base53: Input checksum does not match string")
expect(t, b53m, "g0sk", 'i', true, "Base53: Input checksum does not match string")
expect(t, b53m, "0gsi", 'k', true, "Base53: Input checksum does not match string")
}
func Test_Generate_Random_generates_all_legal_chars(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
legal_alphabet := []rune{'0', '2', '3', '4', '5', '6', '7', '8', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z', 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z'}
for n := 2; n < 5; n++ {
seen := make(map[rune]bool)
for j := 0; j < 2000; j++ {
rndstr, err := b53m.B53_generate_random_Base53ID(n)
if err != nil {
panic(err)
}
for _, c := range rndstr.GetCombinedString() {
seen[c] = true
}
}
for _, c := range legal_alphabet {
_, ok := seen[c]
if !ok {
panic("Letter not generated: " + string(c))
}
}
}
}
func Test_Generate_Random_generates_all_legal_strings(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
// test it generates all legal 2 character strings
n := 3
results := make(map[string]bool)
if len(results) != 0 {
panic("Not expected length")
}
for i := 0; i < 50000; i++ { // should have less than 0.001% chance of failing
rs, err := b53m.B53_generate_random_Base53ID(n)
if err != nil {
panic(err)
}
results[rs.GetCombinedString()] = true
}
expected := 53*53 - 4*2
if len(results) != expected { // only 'VV' 'vv' 'rn' and 'nn' are disallowed, but these pairs can occur in two places
t.Fatalf("Unexpected number of generated strings, expected %d got %d", expected, len(results))
}
}
func Test_Generate_Random_generates_different_strings(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
results := make(map[string]bool)
if len(results) != 0 {
panic("Not expected length")
}
for i := 0; i < 1000; i++ {
rs, err := b53m.B53_generate_random_Base53ID(8)
if err != nil {
panic(err)
}
results[rs.GetCombinedString()] = true
}
if len(results) != 1000 { // should never get repeats.
panic("Unexpected number of generated strings")
}
}
func Test_Generate_Next_generates_right_number_of_strings(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
count := map[int]int{
1: 53,
2: 53*53 - 4*2, // this is the number of valid strings after removing those with illegal pairs
3: 148457 - 208, // I don't know how I calculated this number.
}
for i := 1; i < 4; i++ {
var id util.Base53ID
id, err := b53m.NewBase53ID(strings.Repeat("0", i), '0', false)
util.Check_err(err)
results := map[string]bool{
id.GetCombinedString(): true,
}
num_strings := count[i] // subtract the number of illegal pairs
for x := 1; x < num_strings; x++ {
id, err = b53m.B53_generate_next_Base53ID(id)
util.Check_err(err)
sid := id.GetCombinedString()
util.Assert_result_equals_interface(t, len(sid), err, i+1, 1)
results[sid] = true
}
util.Assert_result_equals_interface(t, len(results), err, num_strings, 1)
}
}
func Test_Generate_all_generates_right_number_of_strings(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
count := map[int]int{
2: 53,
3: 53*53 - 4*2, // this is the number of valid strings after removing those with illegal pairs
4: 148457 - 208, // I don't know how I calculated this number.
}
for i := 2; i < 5; i++ {
ids, err := b53m.B53_generate_all_Base53IDs(i)
util.Check_err(err)
size := len(ids)
expected_num_strings := count[i] // subtract the number of illegal pairs
util.Assert_result_equals_interface(t, size, err, expected_num_strings, 1)
results := map[string]bool{}
for _, id := range ids {
results[id.GetCombinedString()] = true
util.Assert_result_equals_interface(t, len(id.GetCombinedString()), err, i, 1)
}
util.Assert_result_equals_interface(t, len(results), err, expected_num_strings, 1)
}
}
func Test_Generate_all_uint64_optimized_generates_strings_with_trailing_zeroes(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
_, err := b53m.B53_generate_all_Base53IDs_int64_optimized(1, nil)
util.Assert_error_equals(t, err, "Error: minimum id length is 2", 1)
for i := 2; i < 5; i++ {
results, err := b53m.B53_generate_all_Base53IDs_int64_optimized(i, nil)
util.Assert_no_error(t, err, 1)
for _, num := range results {
arr := b53m.Convert_uint64_to_byte_array(num)
for j := i; j < 8; j++ {
util.Assert_result_equals_interface(t, arr[j], err, byte(0), 1)
}
}
}
}
func Test_Generate_all_uint64_optimized_generates_exact_same_strings(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
for n := 2; n < 5; n++ {
expected_ids, err := b53m.B53_generate_all_Base53IDs(n)
util.Check_err(err)
actual_ids, err := b53m.B53_generate_all_Base53IDs_int64_optimized(n, nil)
util.Check_err(err)
// check lengths are equal
util.Assert_result_equals_interface(t, len(actual_ids), err, len(expected_ids), 1)
// check each string is equal
for i := 0; i < len(actual_ids); i++ {
expected_str := expected_ids[i].GetCombinedString()
actual_str := util.Convert_uint64_to_str(actual_ids[i], n)
util.Assert_result_equals_interface(t, actual_str, err, expected_str, 1)
}
}
}
func test_optimized_helper(t *testing.T, b53m *util.Base53IDManager, n int, the_map map[string]interface{}, expected_values int) {
t.Helper()
should_be_added_fn := func(str string) bool {
_, ok := the_map[str]
return !ok
}
returned_expected_ids, err := b53m.B53_generate_all_Base53IDs(n)
expected_ids_without_blacklisted := []string{}
util.Check_err(err)
for _, id := range returned_expected_ids {
id_str := id.GetCombinedString()
_, ok := the_map[id_str]
if !ok {
expected_ids_without_blacklisted = append(expected_ids_without_blacklisted, id_str)
}
}
actual_ids, err := b53m.B53_generate_all_Base53IDs_int64_optimized(n, should_be_added_fn)
util.Check_err(err)
// fmt.Println(expected_ids_without_blacklisted)
// check lengths are equal
util.Assert_result_equals_interface(t, len(actual_ids), err, len(expected_ids_without_blacklisted), 1)
util.Assert_result_equals_interface(t, len(actual_ids), err, expected_values, 1)
// check each string is equal
for i := 0; i < len(actual_ids); i++ {
expected_str := expected_ids_without_blacklisted[i]
actual_str := util.Convert_uint64_to_str(actual_ids[i], n)
util.Assert_result_equals_interface(t, actual_str, err, expected_str, 1)
}
}
func Test_Generate_all_uint64_optimized_with_non_nil_map_n_equals_2(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
// no elements
the_map := make(map[string]interface{})
test_optimized_helper(t, b53m, 2, the_map, 53)
the_map["00"] = 123
// one element
test_optimized_helper(t, b53m, 2, the_map, 52)
// multiple
the_map["Vz"] = 123
the_map["z3"] = 123
test_optimized_helper(t, b53m, 2, the_map, 50)
// all elements
all_2_character_ids, err := b53m.B53_generate_all_Base53IDs(2)
util.Check_err(err)
for _, id := range all_2_character_ids {
id_str := id.GetCombinedString()
the_map[id_str] = 234
}
test_optimized_helper(t, b53m, 2, the_map, 0)
}
func Test_Generate_all_uint64_optimized_with_non_nil_map_n_equals_3(t *testing.T) {
t.Parallel()
b53m := util.NewBase53IDManager()
total_elems := 53*53 - 4*2
// no elements
the_map := make(map[string]interface{})
test_optimized_helper(t, b53m, 3, the_map, total_elems)
the_map["000"] = 123
// one element
test_optimized_helper(t, b53m, 3, the_map, total_elems-1)
// multiple
the_map["iMU"] = 123
the_map["pZg"] = 123
the_map["zz6"] = 123
test_optimized_helper(t, b53m, 3, the_map, total_elems-4)
// all elements
all_2_character_ids, err := b53m.B53_generate_all_Base53IDs(3)
util.Check_err(err)
for _, id := range all_2_character_ids {
id_str := id.GetCombinedString()
the_map[id_str] = 234
}
test_optimized_helper(t, b53m, 3, the_map, 0)
}