-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
124 lines (108 loc) · 2.85 KB
/
utils_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
package opm
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var alphabet = "aaaaaaaaaaaaaaaaaaaaabcdefghijklmnopqrstuvwxyz"
// shortReader provides broken implementation of io.Reader for test
type shortReader struct{}
func (sr shortReader) Read(p []byte) (int, error) {
return len(p) % 2, io.ErrUnexpectedEOF
}
func TestGenerateRandBytes(t *testing.T) {
original := rand.Reader
rand.Reader = shortReader{}
defer func() {
rand.Reader = original
}()
b, err := GenerateRandBytes(32)
assert.NotNil(t, err, fmt.Sprintf("generateRandomBytes did not report a short read: only read %d bytes", len(b)))
}
func TestXOR(t *testing.T) {
strs := []struct {
a []byte
b []byte
expected []byte
}{
{[]byte("goodbye"), []byte("hello"), []byte{15, 10, 3, 8, 13}},
{[]byte("onepuch"), []byte("onepiece"), []byte{0, 0, 0, 0, 28, 6, 11}},
{nil, []byte("testing"), nil},
}
for _, v := range strs {
if res := Xor(v.a, v.b); res != nil {
if !bytes.Equal(res, v.expected) {
t.Fatalf("XorBytes failed to return the expected result: got %v want %v", res, v.expected)
}
}
}
}
func TestStrConcat(t *testing.T) {
ss := []string{"a", "b", "c", "d", "e"}
expected := strings.Join(ss, "")
res := StrConcat(ss...)
assert.Equal(t, res, expected, fmt.Sprintf("StrConcat failed to concat %v: got %v want %v", strings.Join(ss, ","), res, expected))
}
func BenchmarkStrcon(b *testing.B) {
ss := []string{"a", "b", "c", "d", "e"}
for i := 0; i < b.N; i++ {
s := StrConcat(ss...)
_ = s
}
}
func TestContains(t *testing.T) {
ss := []string{"a", "b", "c", "d", "e"}
assert := assert.New(t)
assert.Equal(Contains(ss, "a"), true, fmt.Sprintf("StrConcat failed to contains 'a' in %v", ss))
assert.NotEqual(Contains(ss, "f"), true, fmt.Sprintf("StrConcat failed to contains 'f' in %v", ss))
}
func BenchmarkContains(b *testing.B) {
ss := strings.Split(alphabet, "")
s1 := "z"
for i := 0; i < b.N; i++ {
_ = Contains(ss, s1)
}
}
func BenchmarkInArrayString(b *testing.B) {
ss := strings.Split(alphabet, "")
s1 := "z"
for i := 0; i < b.N; i++ {
_ = InArrayString(ss, s1)
}
}
func TestNumFormat(t *testing.T) {
inTest := []struct {
a interface{}
expected string
}{
{int(5), "5"},
{int(-5), "-5"},
{int8(5), "5"},
{int8(-5), "-5"},
{int16(5), "5"},
{int16(-5), "-5"},
{int32(555555555), "555555555"},
{int32(-555555555), "-555555555"},
{int64(5), "5"},
{uint(5), "5"},
{uint8(5), "5"},
{uint16(5), "5"},
{uint32(5), "5"},
{uint64(5), "5"},
{float32(5.03), "5.03"},
{float32(-5.03), "-5.03"},
{float64(5.5555), "5.5555"},
{float64(-5.5555), "-5.5555"},
{"5", "5"},
{nil, ""},
}
for _, v := range inTest {
if res := NumFormat(v.a); res != v.expected {
t.Fatalf("NumFormat failed to convert number %v to string: got %v want %v", v.a, res, v.expected)
}
}
}