-
Notifications
You must be signed in to change notification settings - Fork 1
/
unicode_test.go
101 lines (92 loc) · 2.08 KB
/
unicode_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
package gp
import (
"testing"
)
func TestMakeStr(t *testing.T) {
setupTest(t)
tests := []struct {
name string
input string
expected string
length int
byteCount int
}{
{
name: "empty string",
input: "",
expected: "",
length: 0,
byteCount: 0,
},
{
name: "ascii string",
input: "hello",
expected: "hello",
length: 5,
byteCount: 5, // ASCII character each takes 1 byte
},
{
name: "unicode string",
input: "你好世界",
expected: "你好世界",
length: 4,
byteCount: 12, // Chinese character each takes 3 bytes
},
{
name: "mixed string",
input: "hello世界",
expected: "hello世界",
length: 7,
byteCount: 11, // 5 ASCII characters (5 bytes) + 2 Chinese characters (6 bytes)
},
{
name: "special unicode",
input: "π∑€",
expected: "π∑€",
length: 3,
byteCount: 8, // π(2bytes) + ∑(3bytes) + €(3bytes) = 8bytes
},
}
for _, tt := range tests {
pyStr := MakeStr(tt.input)
// Test String() method
if got := pyStr.String(); got != tt.expected {
t.Errorf("MakeStr(%q).String() = %q, want %q", tt.input, got, tt.expected)
}
// Test Len() method
if got := pyStr.Len(); got != tt.length {
t.Errorf("MakeStr(%q).Len() = %d, want %d", tt.input, got, tt.length)
}
// Test ByteLen() method
if got := pyStr.ByteLen(); got != tt.byteCount {
t.Errorf("MakeStr(%q).ByteLen() = %d, want %d", tt.input, got, tt.byteCount)
}
}
}
func TestStrEncode(t *testing.T) {
setupTest(t)
tests := []struct {
name string
input string
encoding string
}{
{
name: "utf-8 encoding",
input: "hello世界",
encoding: "utf-8",
},
{
name: "ascii encoding",
input: "hello",
encoding: "ascii",
},
}
for _, tt := range tests {
pyStr := MakeStr(tt.input)
encoded := pyStr.Encode(tt.encoding)
decoded := encoded.Decode(tt.encoding)
if got := decoded.String(); got != tt.input {
t.Errorf("String encode/decode roundtrip failed: got %q, want %q", got, tt.input)
}
}
}