OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / text / encoding / japanese / all_test.go
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package japanese
6
7 import (
8         "fmt"
9         "strings"
10         "testing"
11
12         "golang.org/x/text/encoding"
13         "golang.org/x/text/encoding/internal"
14         "golang.org/x/text/encoding/internal/enctest"
15         "golang.org/x/text/transform"
16 )
17
18 func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
19         return "Decode", e.NewDecoder(), nil
20 }
21 func enc(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
22         return "Encode", e.NewEncoder(), internal.ErrASCIIReplacement
23 }
24
25 func TestNonRepertoire(t *testing.T) {
26         // Pick n to cause the destination buffer in transform.String to overflow.
27         const n = 100
28         long := strings.Repeat(".", n)
29         testCases := []struct {
30                 init      func(e encoding.Encoding) (string, transform.Transformer, error)
31                 e         encoding.Encoding
32                 src, want string
33         }{
34                 {enc, EUCJP, "갂", ""},
35                 {enc, EUCJP, "a갂", "a"},
36                 {enc, EUCJP, "丌갂", "\x8f\xb0\xa4"},
37
38                 {enc, ISO2022JP, "갂", ""},
39                 {enc, ISO2022JP, "a갂", "a"},
40                 {enc, ISO2022JP, "朗갂", "\x1b$BzF\x1b(B"}, // switch back to ASCII mode at end
41
42                 {enc, ShiftJIS, "갂", ""},
43                 {enc, ShiftJIS, "a갂", "a"},
44                 {enc, ShiftJIS, "\u2190갂", "\x81\xa9"},
45
46                 // Continue correctly after errors
47                 {dec, EUCJP, "\x8e\xa0", "\ufffd\ufffd"},
48                 {dec, EUCJP, "\x8e\xe0", "\ufffd"},
49                 {dec, EUCJP, "\x8e\xff", "\ufffd\ufffd"},
50                 {dec, EUCJP, "\x8ea", "\ufffda"},
51                 {dec, EUCJP, "\x8f\xa0", "\ufffd\ufffd"},
52                 {dec, EUCJP, "\x8f\xa1\xa0", "\ufffd\ufffd"},
53                 {dec, EUCJP, "\x8f\xa1a", "\ufffda"},
54                 {dec, EUCJP, "\x8f\xa1a", "\ufffda"},
55                 {dec, EUCJP, "\x8f\xa1a", "\ufffda"},
56                 {dec, EUCJP, "\x8f\xa2\xa2", "\ufffd"},
57                 {dec, EUCJP, "\xfe", "\ufffd"},
58                 {dec, EUCJP, "\xfe\xfc", "\ufffd"},
59                 {dec, EUCJP, "\xfe\xff", "\ufffd\ufffd"},
60                 // Correct handling of end of source
61                 {dec, EUCJP, strings.Repeat("\x8e", n), strings.Repeat("\ufffd", n)},
62                 {dec, EUCJP, strings.Repeat("\x8f", n), strings.Repeat("\ufffd", n)},
63                 {dec, EUCJP, strings.Repeat("\x8f\xa0", n), strings.Repeat("\ufffd", 2*n)},
64                 {dec, EUCJP, "a" + strings.Repeat("\x8f\xa1", n), "a" + strings.Repeat("\ufffd", n)},
65                 {dec, EUCJP, "a" + strings.Repeat("\x8f\xa1\xff", n), "a" + strings.Repeat("\ufffd", 2*n)},
66
67                 // Continue correctly after errors
68                 {dec, ShiftJIS, "\x80", "\u0080"}, // It's what the spec says.
69                 {dec, ShiftJIS, "\x81", "\ufffd"},
70                 {dec, ShiftJIS, "\x81\x7f", "\ufffd\u007f"},
71                 {dec, ShiftJIS, "\xe0", "\ufffd"},
72                 {dec, ShiftJIS, "\xe0\x39", "\ufffd\u0039"},
73                 {dec, ShiftJIS, "\xe0\x9f", "燹"},
74                 {dec, ShiftJIS, "\xe0\xfd", "\ufffd"},
75                 {dec, ShiftJIS, "\xef\xfc", "\ufffd"},
76                 {dec, ShiftJIS, "\xfc\xfc", "\ufffd"},
77                 {dec, ShiftJIS, "\xfc\xfd", "\ufffd"},
78                 {dec, ShiftJIS, "\xfdaa", "\ufffdaa"},
79
80                 {dec, ShiftJIS, strings.Repeat("\x81\x81", n), strings.Repeat("=", n)},
81                 {dec, ShiftJIS, strings.Repeat("\xe0\xfd", n), strings.Repeat("\ufffd", n)},
82                 {dec, ShiftJIS, "a" + strings.Repeat("\xe0\xfd", n), "a" + strings.Repeat("\ufffd", n)},
83
84                 {dec, ISO2022JP, "\x1b$", "\ufffd$"},
85                 {dec, ISO2022JP, "\x1b(", "\ufffd("},
86                 {dec, ISO2022JP, "\x1b@", "\ufffd@"},
87                 {dec, ISO2022JP, "\x1bZ", "\ufffdZ"},
88                 // incomplete escapes
89                 {dec, ISO2022JP, "\x1b$", "\ufffd$"},
90                 {dec, ISO2022JP, "\x1b$J.", "\ufffd$J."},             // illegal
91                 {dec, ISO2022JP, "\x1b$B.", "\ufffd"},                // JIS208
92                 {dec, ISO2022JP, "\x1b$(", "\ufffd$("},               // JIS212
93                 {dec, ISO2022JP, "\x1b$(..", "\ufffd$(.."},           // JIS212
94                 {dec, ISO2022JP, "\x1b$(" + long, "\ufffd$(" + long}, // JIS212
95                 {dec, ISO2022JP, "\x1b$(D.", "\ufffd"},               // JIS212
96                 {dec, ISO2022JP, "\x1b$(D..", "\ufffd"},              // JIS212
97                 {dec, ISO2022JP, "\x1b$(D...", "\ufffd\ufffd"},       // JIS212
98                 {dec, ISO2022JP, "\x1b(B.", "."},                     // ascii
99                 {dec, ISO2022JP, "\x1b(B..", ".."},                   // ascii
100                 {dec, ISO2022JP, "\x1b(J.", "."},                     // roman
101                 {dec, ISO2022JP, "\x1b(J..", ".."},                   // roman
102                 {dec, ISO2022JP, "\x1b(I\x20", "\ufffd"},             // katakana
103                 {dec, ISO2022JP, "\x1b(I\x20\x20", "\ufffd\ufffd"},   // katakana
104                 // recover to same state
105                 {dec, ISO2022JP, "\x1b(B\x1b.", "\ufffd."},
106                 {dec, ISO2022JP, "\x1b(I\x1b.", "\ufffdョ"},
107                 {dec, ISO2022JP, "\x1b(I\x1b$.", "\ufffd、ョ"},
108                 {dec, ISO2022JP, "\x1b(I\x1b(.", "\ufffdィョ"},
109                 {dec, ISO2022JP, "\x1b$B\x7e\x7e", "\ufffd"},
110                 {dec, ISO2022JP, "\x1b$@\x0a.", "\x0a."},
111                 {dec, ISO2022JP, "\x1b$B\x0a.", "\x0a."},
112                 {dec, ISO2022JP, "\x1b$(D\x0a.", "\x0a."},
113                 {dec, ISO2022JP, "\x1b$(D\x7e\x7e", "\ufffd"},
114                 {dec, ISO2022JP, "\x80", "\ufffd"},
115
116                 // TODO: according to https://encoding.spec.whatwg.org/#iso-2022-jp,
117                 // these should all be correct.
118                 // {dec, ISO2022JP, "\x1b(B\x0E", "\ufffd"},
119                 // {dec, ISO2022JP, "\x1b(B\x0F", "\ufffd"},
120                 {dec, ISO2022JP, "\x1b(B\x5C", "\u005C"},
121                 {dec, ISO2022JP, "\x1b(B\x7E", "\u007E"},
122                 // {dec, ISO2022JP, "\x1b(J\x0E", "\ufffd"},
123                 // {dec, ISO2022JP, "\x1b(J\x0F", "\ufffd"},
124                 // {dec, ISO2022JP, "\x1b(J\x5C", "\u00A5"},
125                 // {dec, ISO2022JP, "\x1b(J\x7E", "\u203E"},
126         }
127         for _, tc := range testCases {
128                 dir, tr, wantErr := tc.init(tc.e)
129                 t.Run(fmt.Sprintf("%s/%v/%q", dir, tc.e, tc.src), func(t *testing.T) {
130                         dst := make([]byte, 100000)
131                         src := []byte(tc.src)
132                         for i := 0; i <= len(tc.src); i++ {
133                                 nDst, nSrc, err := tr.Transform(dst, src[:i], false)
134                                 if err != nil && err != transform.ErrShortSrc && err != wantErr {
135                                         t.Fatalf("error on first call to Transform: %v", err)
136                                 }
137                                 n, _, err := tr.Transform(dst[nDst:], src[nSrc:], true)
138                                 nDst += n
139                                 if err != wantErr {
140                                         t.Fatalf("(%q|%q): got %v; want %v", tc.src[:i], tc.src[i:], err, wantErr)
141                                 }
142                                 if got := string(dst[:nDst]); got != tc.want {
143                                         t.Errorf("(%q|%q):\ngot  %q\nwant %q", tc.src[:i], tc.src[i:], got, tc.want)
144                                 }
145                         }
146                 })
147         }
148 }
149
150 func TestCorrect(t *testing.T) {
151         testCases := []struct {
152                 init      func(e encoding.Encoding) (string, transform.Transformer, error)
153                 e         encoding.Encoding
154                 src, want string
155         }{
156                 {dec, ShiftJIS, "\x9f\xfc", "滌"},
157                 {dec, ShiftJIS, "\xfb\xfc", "髙"},
158                 {dec, ShiftJIS, "\xfa\xb1", "﨑"},
159                 {enc, ShiftJIS, "滌", "\x9f\xfc"},
160                 {enc, ShiftJIS, "﨑", "\xed\x95"},
161         }
162         for _, tc := range testCases {
163                 dir, tr, _ := tc.init(tc.e)
164
165                 dst, _, err := transform.String(tr, tc.src)
166                 if err != nil {
167                         t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, nil)
168                 }
169                 if got := string(dst); got != tc.want {
170                         t.Errorf("%s %v(%q):\ngot  %q\nwant %q", dir, tc.e, tc.src, got, tc.want)
171                 }
172         }
173 }
174
175 func TestBasics(t *testing.T) {
176         // The encoded forms can be verified by the iconv program:
177         // $ echo 月日は百代 | iconv -f UTF-8 -t SHIFT-JIS | xxd
178         testCases := []struct {
179                 e         encoding.Encoding
180                 encPrefix string
181                 encSuffix string
182                 encoded   string
183                 utf8      string
184         }{{
185                 // "A。カ゚ 0208: etc 0212: etc" is a nonsense string that contains ASCII, half-width
186                 // kana, JIS X 0208 (including two near the kink in the Shift JIS second byte
187                 // encoding) and JIS X 0212 encodable codepoints.
188                 //
189                 // "月日は百代の過客にして、行かふ年も又旅人也。" is from the 17th century poem
190                 // "Oku no Hosomichi" and contains both hiragana and kanji.
191                 e: EUCJP,
192                 encoded: "A\x8e\xa1\x8e\xb6\x8e\xdf " +
193                         "0208: \xa1\xa1\xa1\xa2\xa1\xdf\xa1\xe0\xa1\xfd\xa1\xfe\xa2\xa1\xa2\xa2\xf4\xa6 " +
194                         "0212: \x8f\xa2\xaf\x8f\xed\xe3",
195                 utf8: "A。カ゚ " +
196                         "0208: \u3000\u3001\u00d7\u00f7\u25ce\u25c7\u25c6\u25a1\u7199 " +
197                         "0212: \u02d8\u9fa5",
198         }, {
199                 e: EUCJP,
200                 encoded: "\xb7\xee\xc6\xfc\xa4\xcf\xc9\xb4\xc2\xe5\xa4\xce\xb2\xe1\xb5\xd2" +
201                         "\xa4\xcb\xa4\xb7\xa4\xc6\xa1\xa2\xb9\xd4\xa4\xab\xa4\xd5\xc7\xaf" +
202                         "\xa4\xe2\xcb\xf4\xce\xb9\xbf\xcd\xcc\xe9\xa1\xa3",
203                 utf8: "月日は百代の過客にして、行かふ年も又旅人也。",
204         }, {
205                 e:         ISO2022JP,
206                 encSuffix: "\x1b\x28\x42",
207                 encoded: "\x1b\x28\x49\x21\x36\x5f\x1b\x28\x42 " +
208                         "0208: \x1b\x24\x42\x21\x21\x21\x22\x21\x5f\x21\x60\x21\x7d\x21\x7e\x22\x21\x22\x22\x74\x26",
209                 utf8: "。カ゚ " +
210                         "0208: \u3000\u3001\u00d7\u00f7\u25ce\u25c7\u25c6\u25a1\u7199",
211         }, {
212                 e:         ISO2022JP,
213                 encPrefix: "\x1b\x24\x42",
214                 encSuffix: "\x1b\x28\x42",
215                 encoded: "\x37\x6e\x46\x7c\x24\x4f\x49\x34\x42\x65\x24\x4e\x32\x61\x35\x52" +
216                         "\x24\x4b\x24\x37\x24\x46\x21\x22\x39\x54\x24\x2b\x24\x55\x47\x2f" +
217                         "\x24\x62\x4b\x74\x4e\x39\x3f\x4d\x4c\x69\x21\x23",
218                 utf8: "月日は百代の過客にして、行かふ年も又旅人也。",
219         }, {
220                 e: ShiftJIS,
221                 encoded: "A\xa1\xb6\xdf " +
222                         "0208: \x81\x40\x81\x41\x81\x7e\x81\x80\x81\x9d\x81\x9e\x81\x9f\x81\xa0\xea\xa4",
223                 utf8: "A。カ゚ " +
224                         "0208: \u3000\u3001\u00d7\u00f7\u25ce\u25c7\u25c6\u25a1\u7199",
225         }, {
226                 e: ShiftJIS,
227                 encoded: "\x8c\x8e\x93\xfa\x82\xcd\x95\x53\x91\xe3\x82\xcc\x89\xdf\x8b\x71" +
228                         "\x82\xc9\x82\xb5\x82\xc4\x81\x41\x8d\x73\x82\xa9\x82\xd3\x94\x4e" +
229                         "\x82\xe0\x96\x94\x97\xb7\x90\x6c\x96\xe7\x81\x42",
230                 utf8: "月日は百代の過客にして、行かふ年も又旅人也。",
231         }}
232
233         for _, tc := range testCases {
234                 enctest.TestEncoding(t, tc.e, tc.encoded, tc.utf8, tc.encPrefix, tc.encSuffix)
235         }
236 }
237
238 func TestFiles(t *testing.T) {
239         enctest.TestFile(t, EUCJP)
240         enctest.TestFile(t, ISO2022JP)
241         enctest.TestFile(t, ShiftJIS)
242 }
243
244 func BenchmarkEncoding(b *testing.B) {
245         enctest.Benchmark(b, EUCJP)
246         enctest.Benchmark(b, ISO2022JP)
247         enctest.Benchmark(b, ShiftJIS)
248 }