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.
5 package traditionalchinese
13 "golang.org/x/text/encoding"
14 "golang.org/x/text/encoding/internal"
15 "golang.org/x/text/encoding/internal/enctest"
16 "golang.org/x/text/transform"
19 func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
20 return "Decode", e.NewDecoder(), nil
22 func enc(e encoding.Encoding) (dir string, t transform.Transformer, err error) {
23 return "Encode", e.NewEncoder(), internal.ErrASCIIReplacement
26 func TestNonRepertoire(t *testing.T) {
27 testCases := []struct {
28 init func(e encoding.Encoding) (string, transform.Transformer, error)
32 {dec, Big5, "\x80", "\ufffd"},
33 {dec, Big5, "\x81", "\ufffd"},
34 {dec, Big5, "\x81\x30", "\ufffd\x30"},
35 {dec, Big5, "\x81\x40", "\ufffd"},
36 {dec, Big5, "\x81\xa0", "\ufffd"},
37 {dec, Big5, "\xff", "\ufffd"},
40 {enc, Big5, "a갂", "a"},
41 {enc, Big5, "\u43f0갂", "\x87@"},
43 for _, tc := range testCases {
44 dir, tr, wantErr := tc.init(tc.e)
45 t.Run(fmt.Sprintf("%s/%v/%q", dir, tc.e, tc.src), func(t *testing.T) {
46 dst := make([]byte, 100)
48 for i := 0; i <= len(tc.src); i++ {
49 nDst, nSrc, err := tr.Transform(dst, src[:i], false)
50 if err != nil && err != transform.ErrShortSrc && err != wantErr {
51 t.Fatalf("error on first call to Transform: %v", err)
53 n, _, err := tr.Transform(dst[nDst:], src[nSrc:], true)
56 t.Fatalf("(%q|%q): got %v; want %v", tc.src[:i], tc.src[i:], err, wantErr)
58 if got := string(dst[:nDst]); got != tc.want {
59 t.Errorf("(%q|%q):\ngot %q\nwant %q", tc.src[:i], tc.src[i:], got, tc.want)
66 func TestBasics(t *testing.T) {
67 // The encoded forms can be verified by the iconv program:
68 // $ echo 月日は百代 | iconv -f UTF-8 -t SHIFT-JIS | xxd
69 testCases := []struct {
77 encoded: "A\x87\x40\x87\x41\x87\x45\xa1\x40\xfe\xfd\xfe\xfeZ\xa3\xe1",
78 utf8: "A\u43f0\u4c32\U00027267\u3000\U0002910d\u79d4Z€",
81 encoded: "\xaa\xe1\xb6\xa1\xa4\x40\xb3\xfd\xb0\x73\xa1\x41\xbf\x57\xb0\x75" +
82 "\xb5\x4c\xac\xdb\xbf\xcb\xa1\x43",
86 for _, tc := range testCases {
87 enctest.TestEncoding(t, tc.e, tc.encoded, tc.utf8, "", "")
91 func TestFiles(t *testing.T) { enctest.TestFile(t, Big5) }
93 func BenchmarkEncoding(b *testing.B) { enctest.Benchmark(b, Big5) }
95 // TestBig5CircumflexAndMacron tests the special cases listed in
96 // http://encoding.spec.whatwg.org/#big5
97 // Note that these special cases aren't preserved by round-tripping through
98 // decoding and encoding (since
99 // http://encoding.spec.whatwg.org/index-big5.txt does not have an entry for
100 // U+0304 or U+030C), so we can't test this in TestBasics.
101 func TestBig5CircumflexAndMacron(t *testing.T) {
102 src := "\x88\x5f\x88\x60\x88\x61\x88\x62\x88\x63\x88\x64\x88\x65\x88\x66 " +
103 "\x88\xa2\x88\xa3\x88\xa4\x88\xa5\x88\xa6"
104 want := "ÓǑÒ\u00ca\u0304Ế\u00ca\u030cỀÊ " +
105 "ü\u00ea\u0304ế\u00ea\u030cề"
106 dst, err := ioutil.ReadAll(transform.NewReader(
107 strings.NewReader(src), Big5.NewDecoder()))
111 if got := string(dst); got != want {
112 t.Fatalf("\ngot %q\nwant %q", got, want)