OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library utf.utf16_test; | 5 library utf.utf16_test; |
6 | 6 |
7 import 'package:expect/expect.dart'; | 7 import 'package:test/test.dart'; |
8 import 'package:utf/utf.dart'; | 8 import 'package:utf/utf.dart'; |
9 | 9 |
| 10 import 'expect.dart' as Expect; |
| 11 |
10 const String testKoreanCharSubset = """ | 12 const String testKoreanCharSubset = """ |
11 가각갂갃간갅갆갇갈갉갊갋갌갍갎갏감갑값갓갔강갖갗갘같갚갛 | 13 가각갂갃간갅갆갇갈갉갊갋갌갍갎갏감갑값갓갔강갖갗갘같갚갛 |
12 개객갞갟갠갡갢갣갤갥갦갧갨갩갪갫갬갭갮갯갰갱갲갳갴갵갶갷 | 14 개객갞갟갠갡갢갣갤갥갦갧갨갩갪갫갬갭갮갯갰갱갲갳갴갵갶갷 |
13 갸갹갺갻갼갽갾갿걀걁걂걃걄걅걆걇걈걉걊걋걌걍걎걏걐걑걒걓"""; | 15 갸갹갺갻갼갽갾갿걀걁걂걃걄걅걆걇걈걉걊걋걌걍걎걏걐걑걒걓"""; |
14 | 16 |
15 const String testHanWater = "水"; | 17 const String testHanWater = "水"; |
16 | 18 |
17 const List<int> testKoreanCharSubsetUtf16beBom = const<int>[ | 19 const List<int> testKoreanCharSubsetUtf16beBom = const <int>[ |
18 0xfe, 0xff, 0xac, 0x00, 0xac, 0x01, 0xac, 0x02, | 20 0xfe, 0xff, 0xac, 0x00, 0xac, 0x01, 0xac, 0x02, // 8 |
19 0xac, 0x03, 0xac, 0x04, 0xac, 0x05, 0xac, 0x06, | 21 0xac, 0x03, 0xac, 0x04, 0xac, 0x05, 0xac, 0x06, |
20 0xac, 0x07, 0xac, 0x08, 0xac, 0x09, 0xac, 0x0a, | 22 0xac, 0x07, 0xac, 0x08, 0xac, 0x09, 0xac, 0x0a, |
21 0xac, 0x0b, 0xac, 0x0c, 0xac, 0x0d, 0xac, 0x0e, | 23 0xac, 0x0b, 0xac, 0x0c, 0xac, 0x0d, 0xac, 0x0e, |
22 0xac, 0x0f, 0xac, 0x10, 0xac, 0x11, 0xac, 0x12, | 24 0xac, 0x0f, 0xac, 0x10, 0xac, 0x11, 0xac, 0x12, |
23 0xac, 0x13, 0xac, 0x14, 0xac, 0x15, 0xac, 0x16, | 25 0xac, 0x13, 0xac, 0x14, 0xac, 0x15, 0xac, 0x16, |
24 0xac, 0x17, 0xac, 0x18, 0xac, 0x19, 0xac, 0x1a, | 26 0xac, 0x17, 0xac, 0x18, 0xac, 0x19, 0xac, 0x1a, |
25 0xac, 0x1b, 0x00, 0x0a, 0xac, 0x1c, 0xac, 0x1d, | 27 0xac, 0x1b, 0x00, 0x0a, 0xac, 0x1c, 0xac, 0x1d, |
26 0xac, 0x1e, 0xac, 0x1f, 0xac, 0x20, 0xac, 0x21, | 28 0xac, 0x1e, 0xac, 0x1f, 0xac, 0x20, 0xac, 0x21, |
27 0xac, 0x22, 0xac, 0x23, 0xac, 0x24, 0xac, 0x25, | 29 0xac, 0x22, 0xac, 0x23, 0xac, 0x24, 0xac, 0x25, |
28 0xac, 0x26, 0xac, 0x27, 0xac, 0x28, 0xac, 0x29, | 30 0xac, 0x26, 0xac, 0x27, 0xac, 0x28, 0xac, 0x29, |
29 0xac, 0x2a, 0xac, 0x2b, 0xac, 0x2c, 0xac, 0x2d, | 31 0xac, 0x2a, 0xac, 0x2b, 0xac, 0x2c, 0xac, 0x2d, |
30 0xac, 0x2e, 0xac, 0x2f, 0xac, 0x30, 0xac, 0x31, | 32 0xac, 0x2e, 0xac, 0x2f, 0xac, 0x30, 0xac, 0x31, |
31 0xac, 0x32, 0xac, 0x33, 0xac, 0x34, 0xac, 0x35, | 33 0xac, 0x32, 0xac, 0x33, 0xac, 0x34, 0xac, 0x35, |
32 0xac, 0x36, 0xac, 0x37, 0x00, 0x0a, 0xac, 0x38, | 34 0xac, 0x36, 0xac, 0x37, 0x00, 0x0a, 0xac, 0x38, |
33 0xac, 0x39, 0xac, 0x3a, 0xac, 0x3b, 0xac, 0x3c, | 35 0xac, 0x39, 0xac, 0x3a, 0xac, 0x3b, 0xac, 0x3c, |
34 0xac, 0x3d, 0xac, 0x3e, 0xac, 0x3f, 0xac, 0x40, | 36 0xac, 0x3d, 0xac, 0x3e, 0xac, 0x3f, 0xac, 0x40, |
35 0xac, 0x41, 0xac, 0x42, 0xac, 0x43, 0xac, 0x44, | 37 0xac, 0x41, 0xac, 0x42, 0xac, 0x43, 0xac, 0x44, |
36 0xac, 0x45, 0xac, 0x46, 0xac, 0x47, 0xac, 0x48, | 38 0xac, 0x45, 0xac, 0x46, 0xac, 0x47, 0xac, 0x48, |
37 0xac, 0x49, 0xac, 0x4a, 0xac, 0x4b, 0xac, 0x4c, | 39 0xac, 0x49, 0xac, 0x4a, 0xac, 0x4b, 0xac, 0x4c, |
38 0xac, 0x4d, 0xac, 0x4e, 0xac, 0x4f, 0xac, 0x50, | 40 0xac, 0x4d, 0xac, 0x4e, 0xac, 0x4f, 0xac, 0x50, |
39 0xac, 0x51, 0xac, 0x52, 0xac, 0x53]; | 41 0xac, 0x51, 0xac, 0x52, 0xac, 0x53 |
| 42 ]; |
40 | 43 |
41 const List<int> testKoreanCharSubsetUtf16le = const<int> [ | 44 const List<int> testKoreanCharSubsetUtf16le = const <int>[ |
42 0x00, 0xac, 0x01, 0xac, 0x02, 0xac, 0x03, 0xac, | 45 0x00, 0xac, 0x01, 0xac, 0x02, 0xac, 0x03, 0xac, // 8 |
43 0x04, 0xac, 0x05, 0xac, 0x06, 0xac, 0x07, 0xac, | 46 0x04, 0xac, 0x05, 0xac, 0x06, 0xac, 0x07, 0xac, |
44 0x08, 0xac, 0x09, 0xac, 0x0a, 0xac, 0x0b, 0xac, | 47 0x08, 0xac, 0x09, 0xac, 0x0a, 0xac, 0x0b, 0xac, |
45 0x0c, 0xac, 0x0d, 0xac, 0x0e, 0xac, 0x0f, 0xac, | 48 0x0c, 0xac, 0x0d, 0xac, 0x0e, 0xac, 0x0f, 0xac, |
46 0x10, 0xac, 0x11, 0xac, 0x12, 0xac, 0x13, 0xac, | 49 0x10, 0xac, 0x11, 0xac, 0x12, 0xac, 0x13, 0xac, |
47 0x14, 0xac, 0x15, 0xac, 0x16, 0xac, 0x17, 0xac, | 50 0x14, 0xac, 0x15, 0xac, 0x16, 0xac, 0x17, 0xac, |
48 0x18, 0xac, 0x19, 0xac, 0x1a, 0xac, 0x1b, 0xac, | 51 0x18, 0xac, 0x19, 0xac, 0x1a, 0xac, 0x1b, 0xac, |
49 0x0a, 0x00, 0x1c, 0xac, 0x1d, 0xac, 0x1e, 0xac, | 52 0x0a, 0x00, 0x1c, 0xac, 0x1d, 0xac, 0x1e, 0xac, |
50 0x1f, 0xac, 0x20, 0xac, 0x21, 0xac, 0x22, 0xac, | 53 0x1f, 0xac, 0x20, 0xac, 0x21, 0xac, 0x22, 0xac, |
51 0x23, 0xac, 0x24, 0xac, 0x25, 0xac, 0x26, 0xac, | 54 0x23, 0xac, 0x24, 0xac, 0x25, 0xac, 0x26, 0xac, |
52 0x27, 0xac, 0x28, 0xac, 0x29, 0xac, 0x2a, 0xac, | 55 0x27, 0xac, 0x28, 0xac, 0x29, 0xac, 0x2a, 0xac, |
53 0x2b, 0xac, 0x2c, 0xac, 0x2d, 0xac, 0x2e, 0xac, | 56 0x2b, 0xac, 0x2c, 0xac, 0x2d, 0xac, 0x2e, 0xac, |
54 0x2f, 0xac, 0x30, 0xac, 0x31, 0xac, 0x32, 0xac, | 57 0x2f, 0xac, 0x30, 0xac, 0x31, 0xac, 0x32, 0xac, |
55 0x33, 0xac, 0x34, 0xac, 0x35, 0xac, 0x36, 0xac, | 58 0x33, 0xac, 0x34, 0xac, 0x35, 0xac, 0x36, 0xac, |
56 0x37, 0xac, 0x0a, 0x00, 0x38, 0xac, 0x39, 0xac, | 59 0x37, 0xac, 0x0a, 0x00, 0x38, 0xac, 0x39, 0xac, |
57 0x3a, 0xac, 0x3b, 0xac, 0x3c, 0xac, 0x3d, 0xac, | 60 0x3a, 0xac, 0x3b, 0xac, 0x3c, 0xac, 0x3d, 0xac, |
58 0x3e, 0xac, 0x3f, 0xac, 0x40, 0xac, 0x41, 0xac, | 61 0x3e, 0xac, 0x3f, 0xac, 0x40, 0xac, 0x41, 0xac, |
59 0x42, 0xac, 0x43, 0xac, 0x44, 0xac, 0x45, 0xac, | 62 0x42, 0xac, 0x43, 0xac, 0x44, 0xac, 0x45, 0xac, |
60 0x46, 0xac, 0x47, 0xac, 0x48, 0xac, 0x49, 0xac, | 63 0x46, 0xac, 0x47, 0xac, 0x48, 0xac, 0x49, 0xac, |
61 0x4a, 0xac, 0x4b, 0xac, 0x4c, 0xac, 0x4d, 0xac, | 64 0x4a, 0xac, 0x4b, 0xac, 0x4c, 0xac, 0x4d, 0xac, |
62 0x4e, 0xac, 0x4f, 0xac, 0x50, 0xac, 0x51, 0xac, | 65 0x4e, 0xac, 0x4f, 0xac, 0x50, 0xac, 0x51, 0xac, |
63 0x52, 0xac, 0x53, 0xac]; | 66 0x52, 0xac, 0x53, 0xac |
| 67 ]; |
64 | 68 |
65 void main() { | 69 void main() { |
66 testEncodeToUtf16(); | 70 test('encode to utf16', testEncodeToUtf16); |
67 testUtf16BytesToString(); | 71 test('utf16 bytes to string', testUtf16BytesToString); |
68 testIterableMethods(); | 72 test('iterable methods', testIterableMethods); |
69 } | 73 } |
70 | 74 |
71 void testEncodeToUtf16() { | 75 void testEncodeToUtf16() { |
72 Expect.listEquals([], encodeUtf16be("")); // TODO(dcarlson) should we skip bom
if empty? | 76 Expect.listEquals( |
| 77 [], encodeUtf16be("")); // TODO(dcarlson) should we skip bom if empty? |
73 Expect.listEquals(testKoreanCharSubsetUtf16beBom, | 78 Expect.listEquals(testKoreanCharSubsetUtf16beBom, |
74 encodeUtf16(testKoreanCharSubset), | 79 encodeUtf16(testKoreanCharSubset), "encode UTF-16(BE by default) Korean"); |
75 "encode UTF-16(BE by default) Korean"); | |
76 | 80 |
77 Expect.listEquals(testKoreanCharSubsetUtf16le, | 81 Expect.listEquals(testKoreanCharSubsetUtf16le, |
78 encodeUtf16le(testKoreanCharSubset), | 82 encodeUtf16le(testKoreanCharSubset), "encode UTF-16LE Korean"); |
79 "encode UTF-16LE Korean"); | |
80 } | 83 } |
81 | 84 |
82 void testUtf16BytesToString() { | 85 void testUtf16BytesToString() { |
83 Expect.stringEquals("", decodeUtf16([])); | 86 Expect.stringEquals("", decodeUtf16([])); |
84 Expect.stringEquals(testHanWater, decodeUtf16([0x6C, 0x34]), | 87 Expect.stringEquals( |
85 "Water variation 1"); | 88 testHanWater, decodeUtf16([0x6C, 0x34]), "Water variation 1"); |
86 Expect.stringEquals(testHanWater, decodeUtf16([0xFE, 0xFF, 0x6C, 0x34]), | 89 Expect.stringEquals( |
87 "Water variation 2"); | 90 testHanWater, decodeUtf16([0xFE, 0xFF, 0x6C, 0x34]), "Water variation 2"); |
88 Expect.stringEquals(testHanWater, decodeUtf16([0xFF, 0xFE, 0x34, 0x6C]), | 91 Expect.stringEquals( |
89 "Water variation 3"); | 92 testHanWater, decodeUtf16([0xFF, 0xFE, 0x34, 0x6C]), "Water variation 3"); |
90 | 93 |
91 Expect.stringEquals(testHanWater, decodeUtf16be([0x6C, 0x34]), | 94 Expect.stringEquals( |
92 "Water variation 4"); | 95 testHanWater, decodeUtf16be([0x6C, 0x34]), "Water variation 4"); |
93 Expect.stringEquals(testHanWater, | 96 Expect.stringEquals(testHanWater, decodeUtf16be([0xFE, 0xFF, 0x6C, 0x34]), |
94 decodeUtf16be([0xFE, 0xFF, 0x6C, 0x34]), | |
95 "Water variation 5"); | 97 "Water variation 5"); |
96 | 98 |
97 Expect.stringEquals(testHanWater, decodeUtf16le([0x34, 0x6C]), | 99 Expect.stringEquals( |
98 "Water variation 6"); | 100 testHanWater, decodeUtf16le([0x34, 0x6C]), "Water variation 6"); |
99 Expect.stringEquals(testHanWater, | 101 Expect.stringEquals(testHanWater, decodeUtf16le([0xFF, 0xFE, 0x34, 0x6C]), |
100 decodeUtf16le([0xFF, 0xFE, 0x34, 0x6C]), | |
101 "Water variation 7"); | 102 "Water variation 7"); |
102 | 103 |
103 Expect.stringEquals(testKoreanCharSubset, | 104 Expect.stringEquals(testKoreanCharSubset, |
104 decodeUtf16(testKoreanCharSubsetUtf16beBom), "UTF-16BE Korean"); | 105 decodeUtf16(testKoreanCharSubsetUtf16beBom), "UTF-16BE Korean"); |
105 } | 106 } |
106 | 107 |
107 void testIterableMethods() { | 108 void testIterableMethods() { |
108 // empty input | 109 // empty input |
109 Expect.isFalse(decodeUtf16AsIterable([]).iterator.moveNext()); | 110 Expect.isFalse(decodeUtf16AsIterable([]).iterator.moveNext()); |
110 | 111 |
111 IterableUtf16Decoder koreanDecoder = | 112 IterableUtf16Decoder koreanDecoder = |
112 decodeUtf16AsIterable(testKoreanCharSubsetUtf16beBom); | 113 decodeUtf16AsIterable(testKoreanCharSubsetUtf16beBom); |
113 // get the first character | 114 // get the first character |
114 Expect.equals(testKoreanCharSubset.codeUnits[0], koreanDecoder.first); | 115 Expect.equals(testKoreanCharSubset.codeUnits[0], koreanDecoder.first); |
115 // get the whole translation using the Iterable interface | 116 // get the whole translation using the Iterable interface |
116 Expect.stringEquals(testKoreanCharSubset, | 117 Expect.stringEquals(testKoreanCharSubset, |
117 new String.fromCharCodes(new List<int>.from(koreanDecoder))); | 118 new String.fromCharCodes(new List<int>.from(koreanDecoder))); |
118 | 119 |
119 // specify types | 120 // specify types |
120 Expect.equals(44032, (new List<int> | 121 Expect.equals( |
121 .from(decodeUtf16beAsIterable(testKoreanCharSubsetUtf16beBom)))[0]); | 122 44032, |
122 Expect.equals(44032, (new List<int> | 123 (new List<int>.from( |
123 .from(decodeUtf16leAsIterable(testKoreanCharSubsetUtf16le)))[0]); | 124 decodeUtf16beAsIterable(testKoreanCharSubsetUtf16beBom)))[0]); |
| 125 Expect.equals( |
| 126 44032, |
| 127 (new List<int>.from( |
| 128 decodeUtf16leAsIterable(testKoreanCharSubsetUtf16le)))[0]); |
124 bool stripBom = false; | 129 bool stripBom = false; |
125 Expect.equals(UNICODE_BOM, (new List<int> | 130 Expect.equals( |
126 .from(decodeUtf16beAsIterable(testKoreanCharSubsetUtf16beBom, | 131 UNICODE_BOM, |
127 0, null, stripBom)))[0]); | 132 (new List<int>.from(decodeUtf16beAsIterable( |
| 133 testKoreanCharSubsetUtf16beBom, 0, null, stripBom)))[0]); |
128 } | 134 } |
OLD | NEW |