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.
10 "golang.org/x/text/encoding"
11 "golang.org/x/text/encoding/internal"
12 "golang.org/x/text/encoding/internal/enctest"
13 "golang.org/x/text/transform"
16 func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
17 return "Decode", e.NewDecoder(), nil
20 func encASCIISuperset(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
21 return "Encode", e.NewEncoder(), internal.ErrASCIIReplacement
24 func encEBCDIC(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
25 return "Encode", e.NewEncoder(), internal.RepertoireError(0x3f)
28 func TestNonRepertoire(t *testing.T) {
29 testCases := []struct {
30 init func(e encoding.Encoding) (string, transform.Transformer, error)
34 {dec, Windows1252, "\x81", "\ufffd"},
36 {encEBCDIC, CodePage037, "갂", ""},
38 {encEBCDIC, CodePage1047, "갂", ""},
39 {encEBCDIC, CodePage1047, "a¤갂", "\x81\x9F"},
41 {encEBCDIC, CodePage1140, "갂", ""},
42 {encEBCDIC, CodePage1140, "a€갂", "\x81\x9F"},
44 {encASCIISuperset, Windows1252, "갂", ""},
45 {encASCIISuperset, Windows1252, "a갂", "a"},
46 {encASCIISuperset, Windows1252, "\u00E9갂", "\xE9"},
48 for _, tc := range testCases {
49 dir, tr, wantErr := tc.init(tc.e)
51 dst, _, err := transform.String(tr, tc.src)
53 t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, wantErr)
55 if got := string(dst); got != tc.want {
56 t.Errorf("%s %v(%q):\ngot %q\nwant %q", dir, tc.e, tc.src, got, tc.want)
61 func TestBasics(t *testing.T) {
62 testCases := []struct {
68 encoded: "\xc8\x51\xba\x93\xcf",
72 encoded: "H\x82ll\x93 \x9d\xa7\xf4\x9c\xbe",
76 encoded: "H\xf3\xd3o \x98\xfd\x9f\xdd\xa1",
80 encoded: "\xc8\x54\x93\x93\x9f",
84 encoded: "\xc8\x9f\x93\x93\xcf",
88 encoded: "Hel\xe5\xf5",
92 encoded: "He\xbd\xd4",
96 encoded: "Hel\xb6\xf8",
100 encoded: "H\xd7\xc6o",
104 encoded: "Hel\xc2\xc9",
108 encoded: "H\xeel\xebo",
112 encoded: "Hel\xf5\xed",
120 encoded: "H\xea\xbfo",
124 encoded: "H\xe6l\xf9o",
128 encoded: "He\xfe\xd0o",
132 encoded: "H\xa4ll\xd8",
136 encoded: "H\xe6ll\xbd",
140 encoded: "He\x93\xad\x9c",
144 encoded: "He\x93\xad\x9c",
148 encoded: "He\xdf\xd7",
151 e: MacintoshCyrillic,
152 encoded: "He\xbe\x94",
156 encoded: "He\xb7\xf0",
160 encoded: "He\xe5\xe5o",
164 encoded: "H\xball\xfe",
168 encoded: "H\xe9ll\xf4 \xa5\xbA\xae\xa3\xd0",
172 encoded: "H\xe5ll\xd6",
196 encoded: "\x00\x40\x7f\x80\xab\xff",
197 utf8: "\u0000\u0040\u007f\uf780\uf7ab\uf7ff",
200 for _, tc := range testCases {
201 enctest.TestEncoding(t, tc.e, tc.encoded, tc.utf8, "", "")
205 var windows1255TestCases = []struct {
210 {'\x00', true, '\u0000'},
211 {'\x1a', true, '\u001a'},
212 {'\x61', true, '\u0061'},
213 {'\x7f', true, '\u007f'},
214 {'\x80', true, '\u20ac'},
215 {'\x95', true, '\u2022'},
216 {'\xa0', true, '\u00a0'},
217 {'\xc0', true, '\u05b0'},
218 {'\xfc', true, '\ufffd'},
219 {'\xfd', true, '\u200e'},
220 {'\xfe', true, '\u200f'},
221 {'\xff', true, '\ufffd'},
222 {encoding.ASCIISub, false, '\u0400'},
223 {encoding.ASCIISub, false, '\u2603'},
224 {encoding.ASCIISub, false, '\U0001f4a9'},
227 func TestDecodeByte(t *testing.T) {
228 for _, tc := range windows1255TestCases {
233 got := Windows1255.DecodeByte(tc.b)
236 t.Errorf("DecodeByte(%#02x): got %#08x, want %#08x", tc.b, got, want)
241 func TestEncodeRune(t *testing.T) {
242 for _, tc := range windows1255TestCases {
243 // There can be multiple tc.b values that map to tc.r = '\ufffd'.
244 if tc.r == '\ufffd' {
248 gotB, gotOK := Windows1255.EncodeRune(tc.r)
249 wantB, wantOK := tc.b, tc.ok
250 if gotB != wantB || gotOK != wantOK {
251 t.Errorf("EncodeRune(%#08x): got (%#02x, %t), want (%#02x, %t)", tc.r, gotB, gotOK, wantB, wantOK)
256 func TestFiles(t *testing.T) { enctest.TestFile(t, Windows1252) }
258 func BenchmarkEncoding(b *testing.B) { enctest.Benchmark(b, Windows1252) }