-
Notifications
You must be signed in to change notification settings - Fork 0
/
base58_test.go
67 lines (58 loc) · 1.79 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
// Copyright (c) 2022 Teal.Finance contributors
// This file is part of Teal.Finance/BaseXX licensed under the MIT License.
// SPDX-License-Identifier: MIT
package base58
import (
"reflect"
"testing"
)
var cases = []struct {
name string
bin []byte
}{
{"nil", nil},
{"empty", []byte{}},
{"zero", []byte{0}},
{"one", []byte{1}},
{"two", []byte{2}},
{"ten", []byte{10}},
{"2zeros", []byte{0, 0}},
{"2ones", []byte{1, 1}},
{"64zeros", []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{"65zeros", []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{"ascii", []byte("c'est une longue chanson")},
{"utf8", []byte("Garçon, un café très fort !")},
}
func TestEncode(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
str := StdEncoding.EncodeToString(c.bin)
ni := len(c.bin)
if ni > 70 {
ni = 70 // print max the first 70 bytes
}
na := len(str)
if na > 70 {
na = 70 // print max the first 70 characters
}
t.Logf("bin len=%d [:%d]=%v", len(c.bin), ni, c.bin[:ni])
t.Logf("str len=%d [:%d]=%q", len(str), na, str[:na])
got, err := StdEncoding.DecodeString(str)
if err != nil {
t.Errorf("Decode() error = %v", err)
return
}
ng := len(got)
if ng > 70 {
ng = 70 // print max the first 70 bytes
}
t.Logf("got len=%d [:%d]=%v", len(got), ng, got[:ng])
if (len(got) == 0) && (len(c.bin) == 0) {
return
}
if !reflect.DeepEqual(got, c.bin) {
t.Errorf("Decode() = %v, want %v", got, c.bin)
}
})
}
}