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