-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathencoding_unicode_test.go
168 lines (145 loc) · 3.32 KB
/
encoding_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
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
package gostrutils
import (
"reflect"
"testing"
)
func TestEncodeUTF16BigEndian(t *testing.T) {
b := utf16ByteSliceBigEndian()
result := EncodeUTF16("TEST㑬", true, true)
if !reflect.DeepEqual(b, result) {
t.Errorf("Result and expected result are different:\n%+v\n%+v", b, result)
}
}
func TestEncodeUTF16LittleEndian(t *testing.T) {
b := utf16ByteSliceLittleEndian()
result := EncodeUTF16("TEST㑬", false, true)
if !reflect.DeepEqual(b, result) {
t.Errorf("Result and expected result are different:\n%+v\n%+v", b, result)
}
}
func TestDecodeUTF16(t *testing.T) {
b := utf16ByteSliceLittleEndian()
utf8, err := DecodeUTF16(b)
if err != nil {
t.Errorf("Error decoding: %s", err)
}
if utf8 != "TEST㑬" {
t.Errorf("Expected 'TEST㑬', got: '%s'", utf8)
}
}
func TestDecodeUTF16BigEndian(t *testing.T) {
b := []byte{0xff, 0xfe, 'A', 0x00}
str, err := DecodeUTF16(b)
if str != "A" {
t.Errorf("Invalid string result, expected 'A' string, got: '%s'", str)
}
if err != nil {
t.Errorf("Have error: %s", err)
}
}
func TestDecodeUTF16NoBOM(t *testing.T) {
b := []byte{0x00, 'A'}
str, err := DecodeUTF16(b)
if str != "A" {
t.Errorf("Invalid string result, expected 'A' string, got: '%s'", str)
}
if err != nil {
t.Errorf("Have error: %s", err)
}
}
func TestDecodeUTF16Invalid(t *testing.T) {
b := []byte{0xff}
str, err := DecodeUTF16(b)
if str != "" {
t.Errorf("Invalid string result, expected empty string, got: '%s'", str)
}
if err == nil {
t.Errorf("Expected error to return, got nil")
}
}
func TestDecodeUTF16Empty(t *testing.T) {
b := []byte{}
str, err := DecodeUTF16(b)
if str != "" {
t.Errorf("Invalid string result, expected empty string, got: '%s'", str)
}
if err == nil {
t.Errorf("Expected error to return, got nil")
}
}
func TestUTF16BomInvalid(t *testing.T) {
b := []byte{0xff}
result := UTF16Bom(b)
if result >= 0 {
t.Errorf("Expected '-1', got '%d'", result)
}
}
func TestUTF16BomBigEndian(t *testing.T) {
b := []byte{0xfe, 0xff}
result := UTF16Bom(b)
if result != 1 {
t.Errorf("Expected '1' got '%d'", result)
}
}
func TestUTF8BomLittleEndian(t *testing.T) {
b := []byte{0xff, 0xfe}
result := UTF16Bom(b)
if result != 2 {
t.Errorf("Expected '2' got '%d'", result)
}
}
func TestUTF8BomNoBOM(t *testing.T) {
b := []byte{'A', 0x00, 'B', 0x00}
result := UTF16Bom(b)
if result != 0 {
t.Errorf("Expected '0', got '%d'", result)
}
}
func utf16ByteSliceLittleEndian() []byte {
return []byte{
0xff, // BOM
0xfe, // BOM
'T',
0x00,
'E',
0x00,
'S',
0x00,
'T',
0x00,
0x6C,
0x34,
}
}
func utf16ByteSliceBigEndian() []byte {
return []byte{
0xfe, // BOM
0xff, // BOM
0x00,
'T',
0x00,
'E',
0x00,
'S',
0x00,
'T',
0x34,
0x6C,
}
}
func TestHexToUTF16RunesHelloWorld(t *testing.T) {
expected := "Hello World"
s := "\x00H\x00e\x00l\x00l\x00o\x00 \x00W\x00o\x00r\x00l\x00d"
result := HexToUTF16Runes(s, true)
if string(result) != expected {
t.Errorf("Expected for %s, got %#U (%s)", expected, result, string(result))
}
}
func TestHexToUTF16RunesHelloWorldHebrew(t *testing.T) {
expected := "שלום עולם"
s := "\x05\xe9\x05\xdc\x05\xd5\x05\xdd\x00 \x05\xe2\x05\xd5\x05\xdc\x05\xdd"
result := HexToUTF16Runes(s, true)
if string(result) != expected {
t.Errorf("Expected for %s, got %#U (%s)", expected, result, string(result))
}
}