Marcel van Lohuizen | 1848af7 | 2015-12-10 20:57:06 +0100 | [diff] [blame] | 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 korean |
| 6 | |
| 7 | import ( |
Marcel van Lohuizen | b1a461c | 2017-02-21 17:51:05 +0100 | [diff] [blame] | 8 | "strings" |
Marcel van Lohuizen | 1848af7 | 2015-12-10 20:57:06 +0100 | [diff] [blame] | 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/text/encoding" |
| 12 | "golang.org/x/text/encoding/internal" |
Marcel van Lohuizen | 767daa1 | 2017-02-21 15:40:53 +0100 | [diff] [blame] | 13 | "golang.org/x/text/encoding/internal/enctest" |
Marcel van Lohuizen | 1848af7 | 2015-12-10 20:57:06 +0100 | [diff] [blame] | 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) { |
Marcel van Lohuizen | b1a461c | 2017-02-21 17:51:05 +0100 | [diff] [blame] | 25 | // Pick n large enough to cause an overflow in the destination buffer of |
| 26 | // transform.String. |
| 27 | const n = 10000 |
Marcel van Lohuizen | 1848af7 | 2015-12-10 20:57:06 +0100 | [diff] [blame] | 28 | testCases := []struct { |
| 29 | init func(e encoding.Encoding) (string, transform.Transformer, error) |
| 30 | e encoding.Encoding |
| 31 | src, want string |
| 32 | }{ |
| 33 | {dec, EUCKR, "\xfe\xfe", "\ufffd"}, |
| 34 | // {dec, EUCKR, "א", "\ufffd"}, // TODO: why is this different? |
| 35 | |
| 36 | {enc, EUCKR, "א", ""}, |
| 37 | {enc, EUCKR, "aא", "a"}, |
| 38 | {enc, EUCKR, "\uac00א", "\xb0\xa1"}, |
| 39 | // TODO: should we also handle Jamo? |
Marcel van Lohuizen | b1a461c | 2017-02-21 17:51:05 +0100 | [diff] [blame] | 40 | |
| 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"}, |
| 48 | |
| 49 | {dec, EUCKR, strings.Repeat("\x81", n), strings.Repeat("걖", n/2)}, |
Marcel van Lohuizen | 1848af7 | 2015-12-10 20:57:06 +0100 | [diff] [blame] | 50 | } |
| 51 | for _, tc := range testCases { |
| 52 | dir, tr, wantErr := tc.init(tc.e) |
| 53 | |
| 54 | dst, _, err := transform.String(tr, tc.src) |
| 55 | if err != wantErr { |
| 56 | t.Errorf("%s %v(%q): got %v; want %v", dir, tc.e, tc.src, err, wantErr) |
| 57 | } |
| 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) |
| 60 | } |
| 61 | } |
| 62 | } |
Marcel van Lohuizen | 767daa1 | 2017-02-21 15:40:53 +0100 | [diff] [blame] | 63 | |
| 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 { |
| 68 | e encoding.Encoding |
| 69 | encoded string |
| 70 | utf8 string |
| 71 | }{{ |
| 72 | // Korean tests. |
| 73 | // |
| 74 | // "A\uac02\uac35\uac56\ud401B\ud408\ud620\ud624C\u4f3d\u8a70D" is a |
| 75 | // nonsense string that contains ASCII, Hangul and CJK ideographs. |
| 76 | // |
| 77 | // "세계야, 안녕" translates as "Hello, world". |
| 78 | e: EUCKR, |
| 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", |
| 81 | }, { |
| 82 | e: EUCKR, |
| 83 | encoded: "\xbc\xbc\xb0\xe8\xbe\xdf\x2c\x20\xbe\xc8\xb3\xe7", |
| 84 | utf8: "세계야, 안녕", |
| 85 | }} |
| 86 | |
| 87 | for _, tc := range testCases { |
| 88 | enctest.TestEncoding(t, tc.e, tc.encoded, tc.utf8, "", "") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestFiles(t *testing.T) { enctest.TestFile(t, EUCKR) } |
| 93 | |
| 94 | func BenchmarkEncoding(b *testing.B) { enctest.Benchmark(b, EUCKR) } |