golang.org/x/text@v0.14.0/encoding/simplifiedchinese/all_test.go (about) 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 simplifiedchinese 6 7 import ( 8 "strings" 9 "testing" 10 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" 15 ) 16 17 func dec(e encoding.Encoding) (dir string, t transform.Transformer, err error) { 18 return "Decode", e.NewDecoder(), nil 19 } 20 func enc(e encoding.Encoding) (dir string, t transform.Transformer, err error) { 21 return "Encode", e.NewEncoder(), internal.ErrASCIIReplacement 22 } 23 24 func TestNonRepertoire(t *testing.T) { 25 // Pick n large enough to overflow the destination buffer of transform.String. 26 const n = 10000 27 testCases := []struct { 28 init func(e encoding.Encoding) (string, transform.Transformer, error) 29 e encoding.Encoding 30 src, want string 31 }{ 32 {dec, GBK, "a\xfe\xfeb", "a\ufffdb"}, 33 {dec, HZGB2312, "~{z~", "\ufffd"}, 34 35 {enc, GBK, "갂", ""}, 36 {enc, GBK, "a갂", "a"}, 37 {enc, GBK, "\u4e02갂", "\x81@"}, 38 39 {enc, HZGB2312, "갂", ""}, 40 {enc, HZGB2312, "a갂", "a"}, 41 {enc, HZGB2312, "\u6cf5갂", "~{1C~}"}, 42 43 {dec, GBK, "\xa2\xe3", "€"}, 44 {dec, GB18030, "\x80", "€"}, 45 46 {dec, GB18030, "\x81", "\ufffd"}, 47 {dec, GB18030, "\x81\x20", "\ufffd "}, 48 {dec, GB18030, "\xfe\xfe", "\ufffd"}, 49 {dec, GB18030, "\xfe\xff", "\ufffd\ufffd"}, 50 {dec, GB18030, "\xfe\x30", "\ufffd0"}, 51 {dec, GB18030, "\xfe\x30\x30 ", "\ufffd00 "}, 52 {dec, GB18030, "\xfe\x30\xff ", "\ufffd0\ufffd "}, 53 {dec, GB18030, "\xfe\x30\x81\x21", "\ufffd0\ufffd!"}, 54 55 {dec, GB18030, strings.Repeat("\xfe\x30", n), strings.Repeat("\ufffd0", n)}, 56 57 {dec, HZGB2312, "~/", "\ufffd"}, 58 {dec, HZGB2312, "~", "\ufffd"}, 59 {dec, HZGB2312, "~~~", "~\ufffd"}, 60 {dec, HZGB2312, "~{a\x80", "\ufffd"}, 61 {dec, HZGB2312, "~{a\x80", "\ufffd"}, 62 {dec, HZGB2312, "~{" + strings.Repeat("z~", n), strings.Repeat("\ufffd", n)}, 63 {dec, HZGB2312, "~{" + strings.Repeat("\xfe\x30", n), strings.Repeat("\ufffd", n*2)}, 64 } 65 for _, tc := range testCases { 66 dir, tr, wantErr := tc.init(tc.e) 67 68 dst, _, err := transform.String(tr, tc.src) 69 if err != wantErr { 70 t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, wantErr) 71 } 72 if got := string(dst); got != tc.want { 73 t.Errorf("%s %v(%q):\ngot %q\nwant %q", dir, tc.e, tc.src, got, tc.want) 74 } 75 } 76 } 77 78 func TestBasics(t *testing.T) { 79 // The encoded forms can be verified by the iconv program: 80 // $ echo 月日は百代 | iconv -f UTF-8 -t SHIFT-JIS | xxd 81 testCases := []struct { 82 e encoding.Encoding 83 encPrefix string 84 encoded string 85 utf8 string 86 }{{ 87 // "\u0081\u00de\u00df\u00e0\u00e1\u00e2\u00e3\uffff\U00010000" is a 88 // nonsense string that contains GB18030 encodable codepoints of which 89 // only U+00E0 and U+00E1 are GBK encodable. 90 // 91 // "A\u3000\u554a\u4e02\u4e90\u72dc\u7349\u02ca\u2588Z€" is a nonsense 92 // string that contains ASCII and GBK encodable codepoints from Levels 93 // 1-5 as well as the Euro sign. 94 // 95 // "A\u43f0\u4c32\U00027267\u3000\U0002910d\u79d4Z€" is a nonsense string 96 // that contains ASCII and Big5 encodable codepoints from the Basic 97 // Multilingual Plane and the Supplementary Ideographic Plane as well as 98 // the Euro sign. 99 // 100 // "花间一壶酒,独酌无相亲。" (simplified) and 101 // "花間一壺酒,獨酌無相親。" (traditional) 102 // are from the 8th century poem "Yuè Xià Dú Zhuó". 103 e: GB18030, 104 encoded: "\x81\x30\x81\x31\x81\x30\x89\x37\x81\x30\x89\x38\xa8\xa4\xa8\xa2" + 105 "\x81\x30\x89\x39\x81\x30\x8a\x30\x84\x31\xa4\x39\x90\x30\x81\x30", 106 utf8: "\u0081\u00de\u00df\u00e0\u00e1\u00e2\u00e3\uffff\U00010000", 107 }, { 108 e: GB18030, 109 encoded: "\xbb\xa8\xbc\xe4\xd2\xbb\xba\xf8\xbe\xc6\xa3\xac\xb6\xc0\xd7\xc3" + 110 "\xce\xde\xcf\xe0\xc7\xd7\xa1\xa3", 111 utf8: "花间一壶酒,独酌无相亲。", 112 }, { 113 e: GBK, 114 encoded: "A\xa1\xa1\xb0\xa1\x81\x40\x81\x80\xaa\x40\xaa\x80\xa8\x40\xa8\x80Z\x80", 115 utf8: "A\u3000\u554a\u4e02\u4e90\u72dc\u7349\u02ca\u2588Z€", 116 }, { 117 e: GBK, 118 encoded: "\xbb\xa8\xbc\xe4\xd2\xbb\xba\xf8\xbe\xc6\xa3\xac\xb6\xc0\xd7\xc3" + 119 "\xce\xde\xcf\xe0\xc7\xd7\xa1\xa3", 120 utf8: "花间一壶酒,独酌无相亲。", 121 }, { 122 e: HZGB2312, 123 encoded: "A~{\x21\x21~~\x30\x21~}Z~~", 124 utf8: "A\u3000~\u554aZ~", 125 }, { 126 e: HZGB2312, 127 encPrefix: "~{", 128 encoded: ";(<dR;:x>F#,6@WCN^O`GW!#", 129 utf8: "花间一壶酒,独酌无相亲。", 130 }, { 131 e: GBK, 132 encoded: "\x80", 133 utf8: "€", 134 }, { 135 e: GB18030, 136 encoded: "\xa2\xe3", 137 utf8: "€", 138 }} 139 140 for _, tc := range testCases { 141 enctest.TestEncoding(t, tc.e, tc.encoded, tc.utf8, tc.encPrefix, "") 142 } 143 } 144 145 func TestFiles(t *testing.T) { 146 enctest.TestFile(t, GB18030) 147 enctest.TestFile(t, GBK) 148 enctest.TestFile(t, HZGB2312) 149 } 150 151 func BenchmarkEncoding(b *testing.B) { 152 enctest.Benchmark(b, GB18030) 153 enctest.Benchmark(b, GBK) 154 enctest.Benchmark(b, HZGB2312) 155 }