-
Notifications
You must be signed in to change notification settings - Fork 1
/
base62_test.go
50 lines (44 loc) · 1.24 KB
/
base62_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
package base62
import (
"math/rand"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestBase62(t *testing.T) {
wg := sync.WaitGroup{}
for i := 0; i < 1024; i++ {
d := []byte(random(i))
wg.Add(1)
go func(i int) {
assert.Equal(t, d, StdEncoding.DecodeString(StdEncoding.EncodeToString(d)))
assert.Equal(t, d, FlipEncoding.DecodeString(FlipEncoding.EncodeToString(d)))
assert.Equal(t, d, ShiftEncoding.DecodeString(ShiftEncoding.EncodeToString(d)))
assert.Equal(t, d, FlipShiftEncoding.DecodeString(FlipShiftEncoding.EncodeToString(d)))
wg.Done()
}(i)
}
wg.Wait()
}
func BenchmarkBase62(b *testing.B) {
for i := 0; i < b.N; i++ {
d := []byte(random(i))
StdEncoding.DecodeString(StdEncoding.EncodeToString(d))
FlipEncoding.DecodeString(FlipEncoding.EncodeToString(d))
ShiftEncoding.DecodeString(ShiftEncoding.EncodeToString(d))
FlipShiftEncoding.DecodeString(FlipShiftEncoding.EncodeToString(d))
}
}
func random(l int) string {
rand.Seed(time.Now().UnixNano())
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ" +
"abcdefghijklmnopqrstuvwxyzåäö" +
"0123456789")
var b strings.Builder
for i := 0; i < l; i++ {
b.WriteRune(chars[rand.Intn(len(chars))])
}
return b.String()
}