This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseconv_test.go
231 lines (194 loc) · 5.19 KB
/
baseconv_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
// Copyright 2019 Enrico Foltran. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package baseconv_test
import (
"fmt"
"testing"
"github.com/enricofoltran/baseconv"
)
func Example() {
encoded := baseconv.Base36.Encode(1234)
fmt.Println(encoded)
decoded, err := baseconv.Base36.Decode(encoded)
if err != nil {
panic(err)
}
fmt.Print(decoded)
// Output:
// ya
// 1234
}
func Example_base11() {
base11, err := baseconv.New("0123456789-", "$")
if err != nil {
panic(err)
}
encoded := base11.Encode(-1234)
fmt.Println(encoded)
decoded, err := base11.Decode(encoded)
if err != nil {
panic(err)
}
fmt.Print(decoded)
// Output:
// $-22
// -1234
}
func TestBaseConv(t *testing.T) {
nums := []int64{-10000000000, 10000000000}
for i := -100; i <= 100; i++ {
nums = append(nums, int64(i))
}
testcases := []struct {
name string
converter *baseconv.BaseConverter
nums []int64
}{
{"Base2", baseconv.Base2, nums},
{"Base16", baseconv.Base16, nums},
{"Base36", baseconv.Base36, nums},
{"Base56", baseconv.Base56, nums},
{"Base62", baseconv.Base62, nums},
{"Base64", baseconv.Base64, nums},
}
for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
for _, num := range tt.nums {
decoded, err := tt.converter.Decode(tt.converter.Encode(num))
if err != nil {
t.Errorf("Decode(Encode(%d)) err = %v", num, err)
}
if num != decoded {
t.Errorf("Decode(Encode(%d)); want=%d, got=%d", num, num, decoded)
}
}
})
}
}
func TestBase11(t *testing.T) {
base11, err := baseconv.New("0123456789-", "$")
if err != nil {
t.Fatalf("New(0123456789-, $) err = %v", err)
}
t.Run("Encode", func(t *testing.T) {
want := "-22"
if got := base11.Encode(1234); got != want {
t.Errorf("Encode(1234) = %s; want = %s", got, want)
}
want = "$-22"
if got := base11.Encode(-1234); got != want {
t.Errorf("Encode(1234) = %s; want = %s", got, want)
}
})
t.Run("Decode", func(t *testing.T) {
want := int64(1234)
if got, _ := base11.Decode("-22"); got != want {
t.Errorf("Decode(-22) = %d; want = %d", got, want)
}
want = int64(-1234)
if got, _ := base11.Decode("$-22"); got != want {
t.Errorf("Decode($-22) = %d; want = %d", got, want)
}
})
}
func TestBase20(t *testing.T) {
base20, err := baseconv.New("0123456789abcdefghij", "")
if err != nil {
t.Fatalf("New(0123456789abcdefghij, '') err = %v", err)
}
t.Run("Encode", func(t *testing.T) {
want := "31e"
if got := base20.Encode(1234); got != want {
t.Errorf("Encode(1234) = %s; want = %s", got, want)
}
want = "-31e"
if got := base20.Encode(-1234); got != want {
t.Errorf("Encode(1234) = %s; want = %s", got, want)
}
})
t.Run("Decode", func(t *testing.T) {
want := int64(1234)
if got, _ := base20.Decode("31e"); got != want {
t.Errorf("Decode(31e) = %d; want = %d", got, want)
}
want = int64(-1234)
if got, _ := base20.Decode("-31e"); got != want {
t.Errorf("Decode(-31e) = %d; want = %d", got, want)
}
})
}
func TestBase64(t *testing.T) {
t.Run("Encode", func(t *testing.T) {
want := "JI"
if got := baseconv.Base64.Encode(1234); got != want {
t.Errorf("Encode(1234) = %s; want = %s", got, want)
}
want = "$JI"
if got := baseconv.Base64.Encode(-1234); got != want {
t.Errorf("Encode(1234) = %s; want = %s", got, want)
}
})
t.Run("Decode", func(t *testing.T) {
want := int64(1234)
if got, _ := baseconv.Base64.Decode("JI"); got != want {
t.Errorf("Decode(JI) = %d; want = %d", got, want)
}
want = int64(-1234)
if got, _ := baseconv.Base64.Decode("$JI"); got != want {
t.Errorf("Decode($JI) = %d; want = %d", got, want)
}
})
}
func TestBase7(t *testing.T) {
base7, err := baseconv.New("cjdhel3", "g")
if err != nil {
t.Fatalf("New(cjdhel3, g) err = %v", err)
}
t.Run("Encode", func(t *testing.T) {
want := "hejd"
if got := base7.Encode(1234); got != want {
t.Errorf("Encode(1234) = %s; want = %s", got, want)
}
want = "ghejd"
if got := base7.Encode(-1234); got != want {
t.Errorf("Encode(1234) = %s; want = %s", got, want)
}
})
t.Run("Decode", func(t *testing.T) {
want := int64(1234)
if got, _ := base7.Decode("hejd"); got != want {
t.Errorf("Decode(hejd) = %d; want = %d", got, want)
}
want = int64(-1234)
if got, _ := base7.Decode("ghejd"); got != want {
t.Errorf("Decode(ghejd) = %d; want = %d", got, want)
}
})
}
func TestErrAlphabetEmptyString(t *testing.T) {
_, err := baseconv.New("", "")
if err != baseconv.ErrAlphabetEmptyString {
t.Fatalf("New('', '') err = %v; want = ErrAlphabetEmptyString", err)
}
}
func TestErrSignCharInBase(t *testing.T) {
_, err := baseconv.New("abc", "a")
if err != baseconv.ErrSignCharInBase {
t.Fatalf("New('abc', 'a') err = %v; want = ErrSignCharInBase", err)
}
_, err = baseconv.New("abc", "d")
if err != nil {
t.Fatalf("New('abc', 'd') err = %v; want = nil", err)
}
}
func TestString(t *testing.T) {
base7, err := baseconv.New("cjdhel3", "g")
if err != nil {
t.Fatalf("New(cjdhel3, g) err = %v", err)
}
want := "<BaseConverter: base7 (cjdhel3)>"
if got := fmt.Sprint(base7); got != want {
t.Fatalf("fmt.Sprint(base7) = %s; want = %s", got, want)
}
}