-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbytes_test.go
199 lines (171 loc) · 4.12 KB
/
bytes_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
// Copyright (c) 2020 Xelaj Software
//
// This file is a part of go-dry package.
// See https://github.com/xelaj/go-dry/blob/master/LICENSE for details
package dry
import (
"bytes"
"io"
"reflect"
"strings"
"sync"
"testing"
)
type MyString struct {
str string
}
func (t MyString) String() string {
return t.str
}
type MyError struct {
str string
}
func (t MyError) Error() string {
return t.str
}
func Test_BytesReader(t *testing.T) {
expected := []byte("hello")
testBytesReaderFn := func(input any) {
result := make([]byte, 5)
returnedIoReader := BytesReader(input)
n, _ := returnedIoReader.Read(result)
if n != 5 {
t.FailNow()
}
for i := range result {
if result[i] != expected[i] {
t.FailNow()
}
}
n, err := returnedIoReader.Read(result)
if n != 0 || err != io.EOF {
t.FailNow()
}
}
testBytesReaderFn(strings.NewReader("hello"))
bytesInput := []byte("hello")
testBytesReaderFn(bytesInput)
testBytesReaderFn("hello")
myStr := MyString{"hello"}
testBytesReaderFn(myStr)
myErr := MyError{"hello"}
testBytesReaderFn(myErr)
}
func assertStringsEqual(t *testing.T, str1, str2 string) {
if str1 != str2 {
t.FailNow()
}
}
func Test_BytesMD5(t *testing.T) {
assertStringsEqual(
t, "5d41402abc4b2a76b9719d911017c592",
BytesMD5("hello"))
}
func Test_BytesEncodeBase64(t *testing.T) {
assertStringsEqual(
t, "aGVsbG8=",
BytesEncodeBase64("hello"))
}
func Test_BytesDecodeBase64(t *testing.T) {
assertStringsEqual(
t, "hello",
BytesDecodeBase64("aGVsbG8="))
}
func Test_BytesEncodeHex(t *testing.T) {
assertStringsEqual(
t, "68656c6c6f",
BytesEncodeHex("hello"))
}
func Test_BytesDecodeHex(t *testing.T) {
assertStringsEqual(
t, "hello",
BytesDecodeHex("68656C6C6F"))
}
//nolint:staticcheck // strongly protected by WaitGroup
func testCompressDecompress(t *testing.T,
compressFunc func([]byte) []byte,
decompressFunc func([]byte) []byte) {
testFn := func(wg *sync.WaitGroup, testData []byte) {
defer wg.Done()
compressedData := compressFunc(testData)
uncompressedData := decompressFunc(compressedData)
if !bytes.Equal(testData, uncompressedData) {
t.FailNow()
}
}
wg := new(sync.WaitGroup)
wg.Add(3)
go testFn(wg, []byte("hello123"))
go testFn(wg, []byte("gopher456"))
go testFn(wg, []byte("dry789"))
wg.Wait()
}
func Test_BytesDeflateInflate(t *testing.T) {
testCompressDecompress(t, BytesDeflate, BytesInflate)
}
func Test_BytesGzipUnGzip(t *testing.T) {
testCompressDecompress(t, BytesGzip, BytesUnGzip)
}
func bytesHeadTailTestHelper(
t *testing.T,
testMethod func([]byte, int) ([]string, []byte),
lines []byte, n int,
expectedLines []string, expectedRest []byte) {
resultLines, resultRest := testMethod(lines, n)
if !reflect.DeepEqual(resultLines, expectedLines) {
t.FailNow()
}
if !bytes.Equal(resultRest, expectedRest) {
t.FailNow()
}
}
func Test_BytesHead(t *testing.T) {
bytesHeadTailTestHelper(
t, BytesHead,
[]byte("line1\nline2\r\nline3\nline4\nline5"), 3,
[]string{"line1", "line2", "line3"}, []byte("line4\nline5"))
bytesHeadTailTestHelper(
t, BytesHead,
[]byte("line1\nline2\r\nline3\nline4\nline5"), 6,
[]string{"line1", "line2", "line3", "line4", "line5"}, []byte(""))
}
func Test_BytesTail(t *testing.T) {
bytesHeadTailTestHelper(
t, BytesTail,
[]byte("line1\nline2\nline3\nline4\r\nline5"), 2,
[]string{"line5", "line4"}, []byte("line1\nline2\nline3"))
bytesHeadTailTestHelper(
t, BytesTail,
[]byte("line1\nline2\r\nline3\nline4\nline5"), 6,
[]string{"line5", "line4", "line3", "line2", "line1"}, []byte(""))
}
func Test_BytesMap(t *testing.T) {
upper := func(b byte) byte {
return b - ('a' - 'A')
}
result := BytesMap(upper, []byte("hello"))
correct := []byte("HELLO")
if len(result) != len(correct) {
t.Fail()
}
for i := range result {
if result[i] != correct[i] {
t.Fail()
}
}
}
func Test_BytesFilter(t *testing.T) {
azFunc := func(b byte) bool {
return b >= 'A' && b <= 'Z'
}
result := BytesFilter(azFunc, []byte{1, 2, 3, 'A', 'f', 'R', 123})
correct := []byte{'A', 'R'}
if len(result) != len(correct) {
t.Fail()
}
for i := range result {
if result[i] != correct[i] {
t.Fail()
}
}
}