-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjutf_test.go
More file actions
90 lines (81 loc) · 2.05 KB
/
jutf_test.go
File metadata and controls
90 lines (81 loc) · 2.05 KB
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
// Copyright 2019-2020 Anders Bergh <anders1@gmail.com>
// MIT license (see LICENSE).
package jutf
import (
"reflect"
"testing"
)
func TestEncode(t *testing.T) {
tests := []struct {
name string
str string
want []byte
}{
// Special Java outputs
{"NUL", "\x00", []byte{0xc0, 0x80}},
{"four byte", "\U0001f4a9", []byte{0xed, 0xa0, 0xbd, 0xed, 0xb2, 0xa9}},
// Regular UTF-8 tests, can simply cast to []byte
{"one byte", "ASCII", []byte("ASCII")},
{"two byte", "åäö", []byte("åäö")},
{"three byte", "日本語", []byte("日本語")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Encode(tt.str); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Encode() = %q, want %q", got, tt.want)
}
})
}
}
func TestDecode(t *testing.T) {
tests := []struct {
name string
data []byte
want string
}{
{"NULL", []byte{0xc0, 0x80}, "\x00"},
{"cut off", []byte{0xc0}, ""},
{"surrogate pair", []byte{0xed, 0xa0, 0xbd, 0xed, 0xb2, 0xa9}, "\U0001f4a9"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := Decode(tt.data); got != tt.want {
t.Errorf("Decode() = %q, want %q; %e", got, tt.want, err)
}
})
}
}
func TestEncodeSame(t *testing.T) {
// all of these should be the same in utf-8 and java modified utf-8.
for i := 1; i <= 0xffff; i++ {
input := string(rune(i))
encoded := string(Encode(input))
if encoded != input {
t.Errorf("Encode(U+%x) = %q", i, encoded)
break
}
}
}
func TestEncodeDecodeAll(t *testing.T) {
for i := 0; i <= 0x10ffff; i++ {
input := string(rune(i))
encoded := Encode(input)
decoded, err := Decode(encoded)
if err != nil {
t.Errorf("Decode() (U+%x) returned error: %s", i, err)
} else if decoded != input {
t.Errorf("Decode() = %q, expected %s/U+%x", decoded, input, i)
}
}
}
func BenchmarkEncode(b *testing.B) {
for n := 0; n < b.N; n++ {
Encode("Hello\x00Wörld!!! \U0001f4a9")
}
}
func BenchmarkDecode(b *testing.B) {
tmp := Encode("Hello\x00Wörld!!! \U0001f4a9")
for n := 0; n < b.N; n++ {
_, _ = Decode(tmp)
}
}