Skip to content

Commit 9add252

Browse files
committed
feat: bytes kits
1 parent 66a85fd commit 9add252

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed

gox/bytesx/bytesx.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package bytesx
19+
20+
// SetAll set all bytes of the slice to value
21+
func SetAll(b []byte, v byte) {
22+
for i := 0; i < len(b); i++ {
23+
b[i] = v
24+
}
25+
}
26+
27+
// ZeroAll set all bytes of the slice to 0
28+
func ZeroAll(b []byte) {
29+
SetAll(b, 0)
30+
}
31+
32+
// FromUint16LE convert uint16 to []byte in little endian
33+
func FromUint16LE(v uint16) []byte {
34+
return []byte{
35+
byte(v),
36+
byte(v >> 8),
37+
}
38+
}
39+
40+
// ToUint16LE convert []byte of little endian to uint16
41+
func ToUint16LE(b []byte) uint16 {
42+
switch len(b) {
43+
case 0:
44+
return 0
45+
case 1:
46+
return uint16(b[0])
47+
default:
48+
return uint16(b[0]) | uint16(b[1])<<8
49+
}
50+
}
51+
52+
// FromUint16BE convert uint16 to []byte in big endian
53+
func FromUint16BE(v uint16) []byte {
54+
return []byte{
55+
byte(v >> 8),
56+
byte(v),
57+
}
58+
}
59+
60+
// ToUint16BE convert []byte of big endian to uint16
61+
func ToUint16BE(b []byte) uint16 {
62+
switch len(b) {
63+
case 0:
64+
return 0
65+
case 1:
66+
return uint16(b[0])
67+
default:
68+
return uint16(b[0])<<8 | uint16(b[1])
69+
}
70+
}

gox/bytesx/bytesx_test.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package bytesx
19+
20+
import (
21+
"reflect"
22+
"testing"
23+
)
24+
25+
func TestSetAll(t *testing.T) {
26+
type args struct {
27+
b []byte
28+
v byte
29+
want []byte
30+
}
31+
tests := []struct {
32+
name string
33+
args args
34+
}{
35+
{"nil", args{nil, 127, nil}},
36+
{"empty", args{[]byte{}, 127, []byte{}}},
37+
{"one", args{[]byte{0}, 127, []byte{127}}},
38+
{"multi", args{[]byte{0, 1, 2, 3, 4, 5, 6, 7}, 127, []byte{127, 127, 127, 127, 127, 127, 127, 127}}},
39+
}
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
SetAll(tt.args.b, tt.args.v)
43+
if !reflect.DeepEqual(tt.args.b, tt.args.want) {
44+
t.Errorf("SetAll() = %v, want %v", tt.args.b, tt.args.want)
45+
}
46+
})
47+
}
48+
}
49+
50+
func TestZeroAll(t *testing.T) {
51+
type args struct {
52+
b []byte
53+
want []byte
54+
}
55+
tests := []struct {
56+
name string
57+
args args
58+
}{
59+
{"nil", args{nil, nil}},
60+
{"empty", args{[]byte{}, []byte{}}},
61+
{"one", args{[]byte{0}, []byte{0}}},
62+
{"multi", args{[]byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{0, 0, 0, 0, 0, 0, 0, 0}}},
63+
}
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
ZeroAll(tt.args.b)
67+
if !reflect.DeepEqual(tt.args.b, tt.args.want) {
68+
t.Errorf("ZeroAll() = %v, want %v", tt.args.b, tt.args.want)
69+
}
70+
})
71+
}
72+
}
73+
74+
func TestFromUint16LE(t *testing.T) {
75+
type args struct {
76+
v uint16
77+
}
78+
tests := []struct {
79+
name string
80+
args args
81+
want []byte
82+
}{
83+
{"zero", args{0}, []byte{0, 0}},
84+
{"one", args{1}, []byte{1, 0}},
85+
{"random", args{0x1234}, []byte{0x34, 0x12}},
86+
{"max", args{65535}, []byte{255, 255}},
87+
}
88+
for _, tt := range tests {
89+
t.Run(tt.name, func(t *testing.T) {
90+
if got := FromUint16LE(tt.args.v); !reflect.DeepEqual(got, tt.want) {
91+
t.Errorf("FromUint16LE() = %v, want %v", got, tt.want)
92+
}
93+
})
94+
}
95+
}
96+
97+
func TestToUint16LE(t *testing.T) {
98+
type args struct {
99+
b []byte
100+
}
101+
tests := []struct {
102+
name string
103+
args args
104+
want uint16
105+
}{
106+
{"nil", args{nil}, 0},
107+
{"empty", args{[]byte{}}, 0},
108+
{"one", args{[]byte{1}}, 1},
109+
{"random", args{[]byte{0x34, 0x12}}, 0x1234},
110+
{"max", args{[]byte{255, 255}}, 65535},
111+
{"max+1", args{[]byte{255, 255, 1}}, 65535},
112+
}
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
if got := ToUint16LE(tt.args.b); got != tt.want {
116+
t.Errorf("ToUint16LE() = %v, want %v", got, tt.want)
117+
}
118+
})
119+
}
120+
}
121+
122+
func TestFromUint16BE(t *testing.T) {
123+
type args struct {
124+
v uint16
125+
}
126+
tests := []struct {
127+
name string
128+
args args
129+
want []byte
130+
}{
131+
{"zero", args{0}, []byte{0, 0}},
132+
{"one", args{1}, []byte{0, 1}},
133+
{"random", args{0x1234}, []byte{0x12, 0x34}},
134+
{"max", args{65535}, []byte{255, 255}},
135+
}
136+
for _, tt := range tests {
137+
t.Run(tt.name, func(t *testing.T) {
138+
if got := FromUint16BE(tt.args.v); !reflect.DeepEqual(got, tt.want) {
139+
t.Errorf("FromUint16BE() = %v, want %v", got, tt.want)
140+
}
141+
})
142+
}
143+
}
144+
145+
func TestToUint16BE(t *testing.T) {
146+
type args struct {
147+
b []byte
148+
}
149+
tests := []struct {
150+
name string
151+
args args
152+
want uint16
153+
}{
154+
{"nil", args{nil}, 0},
155+
{"empty", args{[]byte{}}, 0},
156+
{"one", args{[]byte{1}}, 1},
157+
{"random", args{[]byte{0x12, 0x34}}, 0x1234},
158+
{"max", args{[]byte{255, 255}}, 65535},
159+
{"max+1", args{[]byte{255, 255, 1}}, 65535},
160+
}
161+
for _, tt := range tests {
162+
t.Run(tt.name, func(t *testing.T) {
163+
if got := ToUint16BE(tt.args.b); got != tt.want {
164+
t.Errorf("ToUint16BE() = %v, want %v", got, tt.want)
165+
}
166+
})
167+
}
168+
}

0 commit comments

Comments
 (0)