-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytes_test.go
54 lines (47 loc) · 1.25 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
package gp
import (
"bytes"
"testing"
)
func TestBytesCreation(t *testing.T) {
setupTest(t)
// Test BytesFromStr
b1 := BytesFromStr("hello")
if string(b1.Bytes()) != "hello" {
t.Errorf("BytesFromStr: expected 'hello', got '%s'", string(b1.Bytes()))
}
// Test MakeBytes
data := []byte("world")
b2 := MakeBytes(data)
if !bytes.Equal(b2.Bytes(), data) {
t.Errorf("MakeBytes: expected '%v', got '%v'", data, b2.Bytes())
}
}
func TestBytesDecode(t *testing.T) {
setupTest(t)
// Test UTF-8 decode
b := BytesFromStr("你好")
if !bytes.Equal(b.Bytes(), []byte("你好")) {
t.Errorf("BytesFromStr: expected '你好', got '%s'", string(b.Bytes()))
}
s := b.Decode("utf-8")
if s.String() != "你好" {
t.Errorf("Decode: expected '你好', got '%s'", s.String())
}
// Test ASCII decode
b2 := BytesFromStr("hello")
s2 := b2.Decode("ascii")
if s2.String() != "hello" {
t.Errorf("Decode: expected 'hello', got '%s'", s2.String())
}
}
func TestBytesConversion(t *testing.T) {
setupTest(t)
original := []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f} // "Hello" in hex
b := MakeBytes(original)
// Test conversion back to []byte
result := b.Bytes()
if !bytes.Equal(result, original) {
t.Errorf("Bytes conversion: expected %v, got %v", original, result)
}
}