Skip to content

Commit b6d3a5d

Browse files
committed
test: add tests for string and byte slice conversions
- Add tests to verify correct conversion between strings and byte slices, including round-trip conversions and handling of empty values Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 5243c99 commit b6d3a5d

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

bytesconv/bytesconv_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package bytesconv
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
func TestStrToBytesAndBack(t *testing.T) {
9+
strings := []string{
10+
"",
11+
"hello",
12+
"世界",
13+
"1234567890",
14+
"with spaces and symbols!@#",
15+
}
16+
17+
for _, s := range strings {
18+
b := StrToBytes(s)
19+
if string(b) != s {
20+
t.Errorf("StrToBytes(%q) = %v, want %v", s, b, []byte(s))
21+
}
22+
// Round-trip
23+
s2 := BytesToStr(b)
24+
if s2 != s {
25+
t.Errorf("BytesToStr(StrToBytes(%q)) = %q, want %q", s, s2, s)
26+
}
27+
}
28+
}
29+
30+
func TestBytesToStrAndBack(t *testing.T) {
31+
byteSlices := [][]byte{
32+
[]byte(""),
33+
[]byte("hello"),
34+
[]byte("世界"),
35+
[]byte("1234567890"),
36+
[]byte("with spaces and symbols!@#"),
37+
}
38+
39+
for _, b := range byteSlices {
40+
s := BytesToStr(b)
41+
if s != string(b) {
42+
t.Errorf("BytesToStr(%v) = %q, want %q", b, s, string(b))
43+
}
44+
// Round-trip
45+
b2 := StrToBytes(s)
46+
if !bytes.Equal(b2, b) {
47+
t.Errorf("StrToBytes(BytesToStr(%v)) = %v, want %v", b, b2, b)
48+
}
49+
}
50+
}
51+
52+
func TestStrToBytes_Empty(t *testing.T) {
53+
b := StrToBytes("")
54+
if len(b) != 0 {
55+
t.Errorf("StrToBytes(\"\") = %v, want []", b)
56+
}
57+
}
58+
59+
func TestBytesToStr_Empty(t *testing.T) {
60+
s := BytesToStr([]byte{})
61+
if s != "" {
62+
t.Errorf("BytesToStr([]) = %q, want \"\"", s)
63+
}
64+
}

0 commit comments

Comments
 (0)