-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_test.go
More file actions
62 lines (57 loc) · 1.47 KB
/
base_test.go
File metadata and controls
62 lines (57 loc) · 1.47 KB
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
package basex
import (
"log"
"math"
"testing"
"time"
)
var bases = []struct {
baseRune []rune
bitSize int
base int
}{
{b2, 1, 2},
{b8, 3, 8},
{b10, 4, 10},
{b16, 4, 16},
{b32, 5, 32},
{b36, 6, 36},
{b62, 6, 62},
{b64, 6, 64},
{human32, 5, 32},
{urlSafe, 7, 70},
{extended, 7, 80},
}
func TestNewBase(t *testing.T) {
for _, test := range bases {
b := NewBase(test.baseRune)
if b.base != test.base {
t.Errorf("Expected base %d, got %d for base %s", test.base, b.base, string(test.baseRune))
}
if b.bitSize != test.bitSize {
t.Errorf("Expected bit size %d, got %d for base %s", test.bitSize, b.bitSize, string(test.baseRune))
}
if len(b.alpha) != len(test.baseRune) {
t.Errorf("Expected alpha length %d, got %d for base %s", len(test.baseRune), len(b.alpha), string(test.baseRune))
}
}
}
func TestEncodeInt(t *testing.T) {
inputs := []uint64{0, 1, 2, 10, 16, 32, 36, 62, 63, 64, 255, 256, 100, 1000, 10000, 1000000, uint64(time.Now().Unix()), math.MaxInt8, math.MaxInt16, math.MaxInt32, math.MaxInt64}
for _, test := range bases {
b := NewBase(test.baseRune)
for _, input := range inputs {
enc := b.EncodeInt(input)
dec, err := b.DecodeInt(enc)
log.Println(enc, dec)
if err != nil {
t.Errorf("Error decoding value %s for base %s: %v", enc, string(test.baseRune), err)
continue
}
if dec != input {
t.Errorf("Expected encoded value %d, got %d for base %s", input, dec, string(test.baseRune))
continue
}
}
}
}