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.
11 "golang.org/x/text/encoding"
12 "golang.org/x/text/encoding/internal"
13 "golang.org/x/text/encoding/internal/enctest"
14 "golang.org/x/text/transform"
17 func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
18 return "Decode", e.NewDecoder(), nil
20 func enc(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
21 return "Encode", e.NewEncoder(), internal.ErrASCIIReplacement
24 func TestNonRepertoire(t *testing.T) {
25 // Pick n large enough to cause an overflow in the destination buffer of
28 testCases := []struct {
29 init func(e encoding.Encoding) (string, transform.Transformer, error)
33 {dec, EUCKR, "\xfe\xfe", "\ufffd"},
34 // {dec, EUCKR, "א", "\ufffd"}, // TODO: why is this different?
36 {enc, EUCKR, "א", ""},
37 {enc, EUCKR, "aא", "a"},
38 {enc, EUCKR, "\uac00א", "\xb0\xa1"},
39 // TODO: should we also handle Jamo?
41 {dec, EUCKR, "\x80", "\ufffd"},
42 {dec, EUCKR, "\xff", "\ufffd"},
43 {dec, EUCKR, "\x81", "\ufffd"},
44 {dec, EUCKR, "\xb0\x40", "\ufffd@"},
45 {dec, EUCKR, "\xb0\xff", "\ufffd"},
46 {dec, EUCKR, "\xd0\x20", "\ufffd "},
47 {dec, EUCKR, "\xd0\xff", "\ufffd"},
49 {dec, EUCKR, strings.Repeat("\x81", n), strings.Repeat("걖", n/2)},
51 for _, tc := range testCases {
52 dir, tr, wantErr := tc.init(tc.e)
54 dst, _, err := transform.String(tr, tc.src)
56 t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, wantErr)
58 if got := string(dst); got != tc.want {
59 t.Errorf("%s %v(%q):\ngot %q\nwant %q", dir, tc.e, tc.src, got, tc.want)
64 func TestBasics(t *testing.T) {
65 // The encoded forms can be verified by the iconv program:
66 // $ echo 月日は百代 | iconv -f UTF-8 -t SHIFT-JIS | xxd
67 testCases := []struct {
74 // "A\uac02\uac35\uac56\ud401B\ud408\ud620\ud624C\u4f3d\u8a70D" is a
75 // nonsense string that contains ASCII, Hangul and CJK ideographs.
77 // "세계야, 안녕" translates as "Hello, world".
79 encoded: "A\x81\x41\x81\x61\x81\x81\xc6\xfeB\xc7\xa1\xc7\xfe\xc8\xa1C\xca\xa1\xfd\xfeD",
80 utf8: "A\uac02\uac35\uac56\ud401B\ud408\ud620\ud624C\u4f3d\u8a70D",
83 encoded: "\xbc\xbc\xb0\xe8\xbe\xdf\x2c\x20\xbe\xc8\xb3\xe7",
87 for _, tc := range testCases {
88 enctest.TestEncoding(t, tc.e, tc.encoded, tc.utf8, "", "")
92 func TestFiles(t *testing.T) { enctest.TestFile(t, EUCKR) }
94 func BenchmarkEncoding(b *testing.B) { enctest.Benchmark(b, EUCKR) }