forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvbase_test.go
79 lines (66 loc) · 1.74 KB
/
convbase_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
package strutil_test
import (
"testing"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestBaseConv(t *testing.T) {
tests := []struct {
give string
from int
to int
want string
}{
// 10 -> 16
{"123", 10, 16, "7b"},
{"7b", 16, 10, "123"},
// 10 -> 32
{"123", 10, 32, "3r"},
{"3r", 32, 10, "123"},
// 10 -> 62
{"123", 10, 62, "1Z"},
{"1Z", 62, 10, "123"},
// 10 -> 64
{"123", 10, 64, "1X"},
{"1X", 64, 10, "123"},
}
for _, tt := range tests {
t.Run(tt.give, func(t *testing.T) {
got := strutil.BaseConv(tt.give, tt.from, tt.to)
if got != tt.want {
t.Errorf("BaseConv() got = %v, want %v; give=%s", got, tt.want, tt.give)
}
})
}
}
func TestBase10Conv(t *testing.T) {
// fmt.Println(time.Now().Format("20060102150405"))
date := "20230829194900" // seconds
t.Run("date sec", func(t *testing.T) {
base16 := strutil.Base10Conv(date, 16)
assert.Eq(t, "12665b633e94", base16)
base32 := strutil.Base10Conv(date, 32)
assert.Eq(t, "icpdm6fkk", base32)
base62 := strutil.Base10Conv(date, 62)
assert.Eq(t, "5KaR3zRW", base62)
base64 := strutil.Base10Conv(date, 64)
assert.Eq(t, "4CproPWk", base64)
})
// fmt.Println(time.Now().Format("20060102150405.000"))
msDate := "20230829195105843"
t.Run("date ms", func(t *testing.T) {
base16 := strutil.Base10Conv(msDate, 16)
assert.Eq(t, "47dfd4fbaf9633", base16)
base32 := strutil.Base10Conv(msDate, 32)
assert.Eq(t, "huvqjtqv5hj", base32)
base62 := strutil.Base10Conv(msDate, 62)
assert.Eq(t, "1uEL5LJptx", base62)
base64 := strutil.Base10Conv(msDate, 64)
assert.Eq(t, "17TZjXHVoP", base64)
})
t.Run("panic", func(t *testing.T) {
assert.Panics(t, func() {
strutil.Base10Conv("invalid", 16)
})
})
}