-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbase58_test.go
86 lines (77 loc) · 1.54 KB
/
base58_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
package base58
import (
"fmt"
"math/big"
"testing"
)
type testpair struct {
decoded int64
encoded string
}
var pairs = []testpair{
{10002343, "Tgmc"},
{1000, "if"},
{0, ""},
}
func ExampleEncodeBig() {
buf := EncodeBig(nil, big.NewInt(123456))
fmt.Printf("%s\n", buf)
// Output:
// CGy
}
func ExampleDecodeToBig() {
n, err := DecodeToBig([]byte("CGy"))
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("%d\n", n)
// Output:
// 123456
}
func TestEncode(t *testing.T) {
for _, p := range pairs {
var buf []byte = ([]byte)("noise")
buf = EncodeBig(buf, big.NewInt(p.decoded))
if string(buf) != "noise"+p.encoded {
t.Errorf("unexpected result: %q != %q", string(buf), p.encoded)
}
}
}
func TestDecode(t *testing.T) {
for _, data := range pairs {
var buf []byte = []byte(data.encoded)
n, err := DecodeToBig(buf)
if err != nil {
t.Errorf("decoding %q failed: %v", data.encoded, err)
}
if n.Int64() != data.decoded {
t.Errorf("unexpected result: %v != %v", n, data.decoded)
}
}
}
func TestDecodeCorrupt(t *testing.T) {
type corrupt struct {
input string
offset int
}
examples := []corrupt{
{"!!!!", 0},
{"x===", 1},
{"x0", 1},
{"xl", 1},
{"xI", 1},
{"xO", 1},
}
for _, e := range examples {
_, err := DecodeToBig([]byte(e.input))
switch err := err.(type) {
case CorruptInputError:
if int(err) != e.offset {
t.Errorf("Corruption in %q at offset %v, want %v", e.input, int(err), e.offset)
}
default:
t.Error("Decoder failed to detect corruption in", e)
}
}
}