-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_test.go
165 lines (148 loc) · 3.35 KB
/
m_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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "testing"
var invalidSequenceTests = []string{
"\xed\xa0\x80\x80", // surrogate min
"\xed\xbf\xbf\x80", // surrogate max
// xx
"\x91\x80\x80\x80",
// s1
"\xC2\x7F\x80\x80",
"\xC2\xC0\x80\x80",
"\xDF\x7F\x80\x80",
"\xDF\xC0\x80\x80",
// s2
"\xE0\x9F\xBF\x80",
"\xE0\xA0\x7F\x80",
"\xE0\xBF\xC0\x80",
"\xE0\xC0\x80\x80",
// s3
"\xE1\x7F\xBF\x80",
"\xE1\x80\x7F\x80",
"\xE1\xBF\xC0\x80",
"\xE1\xC0\x80\x80",
// s4
"\xED\x7F\xBF\x80",
"\xED\x80\x7F\x80",
"\xED\x9F\xC0\x80",
"\xED\xA0\x80\x80",
// s5
"\xF0\x8F\xBF\xBF",
"\xF0\x90\x7F\xBF",
"\xF0\x90\x80\x7F",
"\xF0\xBF\xBF\xC0",
"\xF0\xBF\xC0\x80",
"\xF0\xC0\x80\x80",
// s6
"\xF1\x7F\xBF\xBF",
"\xF1\x80\x7F\xBF",
"\xF1\x80\x80\x7F",
"\xF1\xBF\xBF\xC0",
"\xF1\xBF\xC0\x80",
"\xF1\xC0\x80\x80",
// s7
"\xF4\x7F\xBF\xBF",
"\xF4\x80\x7F\xBF",
"\xF4\x80\x80\x7F",
"\xF4\x8F\xBF\xC0",
"\xF4\x8F\xC0\x80",
"\xF4\x90\x80\x80",
}
func TestInvalidUTF8(t *testing.T) {
for i, s := range invalidSequenceTests {
_, _, err := decodeRune([]byte(s))
if err == nil {
t.Errorf(
"should get invalid utf-8 sequence but didn't, index: %d | str: %#04x",
i,
s,
)
}
}
}
type Utf8Map struct {
r rune
str string
}
var utf8map = []Utf8Map{
{0x0000, "\x00"},
{0x0001, "\x01"},
{0x007e, "\x7e"},
{0x007f, "\x7f"},
{0x0080, "\xc2\x80"},
{0x0081, "\xc2\x81"},
{0x00bf, "\xc2\xbf"},
{0x00c0, "\xc3\x80"},
{0x00c1, "\xc3\x81"},
{0x00c8, "\xc3\x88"},
{0x00d0, "\xc3\x90"},
{0x00e0, "\xc3\xa0"},
{0x00f0, "\xc3\xb0"},
{0x00f8, "\xc3\xb8"},
{0x00ff, "\xc3\xbf"},
{0x0100, "\xc4\x80"},
{0x07ff, "\xdf\xbf"},
{0x0400, "\xd0\x80"},
{0x0800, "\xe0\xa0\x80"},
{0x0801, "\xe0\xa0\x81"},
{0x1000, "\xe1\x80\x80"},
{0xd000, "\xed\x80\x80"},
{0xd7ff, "\xed\x9f\xbf"}, // last code point before surrogate half.
{0xe000, "\xee\x80\x80"}, // first code point after surrogate half.
{0xfffe, "\xef\xbf\xbe"},
{0xffff, "\xef\xbf\xbf"},
{0x10000, "\xf0\x90\x80\x80"},
{0x10001, "\xf0\x90\x80\x81"},
{0x40000, "\xf1\x80\x80\x80"},
{0x10fffe, "\xf4\x8f\xbf\xbe"},
{0x10ffff, "\xf4\x8f\xbf\xbf"},
{0xFFFD, "\xef\xbf\xbd"},
}
var testStrings = []string{
"",
"abcd",
"☺☻☹",
"日a本b語ç日ð本Ê語þ日¥本¼語i日©",
"日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©日a本b語ç日ð本Ê語þ日¥本¼語i日©",
}
// Check that DecodeRune and DecodeLastRune correspond to
// the equivalent range loop.
func TestSequencing(t *testing.T) {
for _, ts := range testStrings {
for _, m := range utf8map {
for _, s := range []string{ts + m.str, m.str + ts, ts + m.str + ts} {
testSequence(t, s)
}
}
}
}
func testSequence(t *testing.T, s string) {
type info struct {
index int
r rune
}
index := make([]info, len(s))
b := []byte(s)
si := 0
j := 0
for i, r := range s {
if si != i {
t.Errorf("Sequence(%q) mismatched index %d, want %d", s, si, i)
return
}
index[j] = info{i, r}
j++
r1, size1, err := decodeRune(b[i:])
if err != nil {
t.Errorf("DecodeRune err should be nil, but got: %s", err)
return
}
if r != r1 {
t.Errorf("DecodeRune(%q) = %#04x, want %#04x", s[i:], r1, r)
return
}
si += size1
}
}