OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 utf8_test; | |
6 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
7 import 'dart:convert'; | 6 import 'dart:convert'; |
8 | 7 |
9 // Google favorite: "Îñţérñåţîöñåļîžåţîờñ". | 8 // Google favorite: "Îñţérñåţîöñåļîžåţîờñ". |
10 const INTER_BYTES = const [0xc3, 0x8e, 0xc3, 0xb1, 0xc5, 0xa3, 0xc3, 0xa9, 0x72, | 9 const INTER_BYTES = const [0xc3, 0x8e, 0xc3, 0xb1, 0xc5, 0xa3, 0xc3, 0xa9, 0x72, |
11 0xc3, 0xb1, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xc3, | 10 0xc3, 0xb1, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xc3, |
12 0xb6, 0xc3, 0xb1, 0xc3, 0xa5, 0xc4, 0xbc, 0xc3, 0xae, | 11 0xb6, 0xc3, 0xb1, 0xc3, 0xa5, 0xc4, 0xbc, 0xc3, 0xae, |
13 0xc5, 0xbe, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xe1, | 12 0xc5, 0xbe, 0xc3, 0xa5, 0xc5, 0xa3, 0xc3, 0xae, 0xe1, |
14 0xbb, 0x9d, 0xc3, 0xb1]; | 13 0xbb, 0x9d, 0xc3, 0xb1]; |
15 const INTER_STRING = "Îñţérñåţîöñåļîžåţîờñ"; | 14 const INTER_STRING = "Îñţérñåţîöñåļîžåţîờñ"; |
(...skipping 15 matching lines...) Expand all Loading... |
31 0xe0, 0xa4, 0xbe, 0x20, 0xe0, 0xa4, 0x85, 0xe0, 0xa4, | 30 0xe0, 0xa4, 0xbe, 0x20, 0xe0, 0xa4, 0x85, 0xe0, 0xa4, |
32 0xa3, 0xe0, 0xa4, 0xbe, 0xe0, 0xa4, 0xae, 0xe0, 0xa4, | 31 0xa3, 0xe0, 0xa4, 0xbe, 0xe0, 0xa4, 0xae, 0xe0, 0xa4, |
33 0xbe, 0xe0, 0xa4, 0xb2, 0xe0, 0xa5, 0x88]; | 32 0xbe, 0xe0, 0xa4, 0xb2, 0xe0, 0xa5, 0x88]; |
34 const SIVA_STRING2 = "िसवा अणामालै"; | 33 const SIVA_STRING2 = "िसवा अणामालै"; |
35 | 34 |
36 // DESERET CAPITAL LETTER BEE, unicode 0x10412(0xD801+0xDC12) | 35 // DESERET CAPITAL LETTER BEE, unicode 0x10412(0xD801+0xDC12) |
37 // UTF-8: F0 90 90 92 | 36 // UTF-8: F0 90 90 92 |
38 const BEE_BYTES = const [0xf0, 0x90, 0x90, 0x92]; | 37 const BEE_BYTES = const [0xf0, 0x90, 0x90, 0x92]; |
39 const BEE_STRING = "𐐒"; | 38 const BEE_STRING = "𐐒"; |
40 | 39 |
41 const DIGIT_BYTES = const [ 0x35 ]; | |
42 const DIGIT_STRING = "5"; | |
43 | |
44 const ASCII_BYTES = const [ 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, | 40 const ASCII_BYTES = const [ 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, |
45 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, | 41 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, |
46 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, | 42 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, |
47 0x79, 0x7A ]; | 43 0x79, 0x7A ]; |
48 const ASCII_STRING = "abcdefghijklmnopqrstuvwxyz"; | 44 const ASCII_STRING = "abcdefghijklmnopqrstuvwxyz"; |
49 | 45 |
50 const TESTS = const [ | 46 const TESTS = const [ |
51 const [ const [], "" ], | |
52 const [ INTER_BYTES, INTER_STRING ], | 47 const [ INTER_BYTES, INTER_STRING ], |
53 const [ BLUEBERRY_BYTES, BLUEBERRY_STRING ], | 48 const [ BLUEBERRY_BYTES, BLUEBERRY_STRING ], |
54 const [ SIVA_BYTES1, SIVA_STRING1 ], | 49 const [ SIVA_BYTES1, SIVA_STRING1 ], |
55 const [ SIVA_BYTES2, SIVA_STRING2 ], | 50 const [ SIVA_BYTES2, SIVA_STRING2 ], |
56 const [ BEE_BYTES, BEE_STRING ], | 51 const [ BEE_BYTES, BEE_STRING ], |
57 const [ DIGIT_BYTES, DIGIT_STRING ], | |
58 const [ ASCII_BYTES, ASCII_STRING ]]; | 52 const [ ASCII_BYTES, ASCII_STRING ]]; |
59 | 53 |
60 List<int> encode(String str) => new Utf8Encoder().convert(str); | 54 List<int> encode(String str) { |
61 List<int> encode2(String str) => UTF8.encode(str); | 55 List<int> bytes; |
| 56 ChunkedConversionSink byteSink = |
| 57 new ByteConversionSink.withCallback((result) => bytes = result); |
| 58 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 59 stringConversionSink.add(str); |
| 60 stringConversionSink.close(); |
| 61 return bytes; |
| 62 } |
| 63 |
| 64 List<int> encode2(String str) { |
| 65 List<int> bytes; |
| 66 ChunkedConversionSink byteSink = |
| 67 new ByteConversionSink.withCallback((result) => bytes = result); |
| 68 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 69 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 70 stringSink.write(str); |
| 71 stringSink.close(); |
| 72 return bytes; |
| 73 } |
| 74 |
| 75 List<int> encode3(String str) { |
| 76 List<int> bytes; |
| 77 ChunkedConversionSink byteSink = |
| 78 new ByteConversionSink.withCallback((result) => bytes = result); |
| 79 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 80 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 81 str.codeUnits.forEach(stringSink.writeCharCode); |
| 82 stringSink.close(); |
| 83 return bytes; |
| 84 } |
| 85 |
| 86 List<int> encode4(String str) { |
| 87 List<int> bytes; |
| 88 ChunkedConversionSink byteSink = |
| 89 new ByteConversionSink.withCallback((result) => bytes = result); |
| 90 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 91 ClosableStringSink stringSink = stringConversionSink.asStringSink(); |
| 92 str.runes.forEach(stringSink.writeCharCode); |
| 93 stringSink.close(); |
| 94 return bytes; |
| 95 } |
| 96 |
| 97 List<int> encode5(String str) { |
| 98 List<int> bytes; |
| 99 ChunkedConversionSink byteSink = |
| 100 new ByteConversionSink.withCallback((result) => bytes = result); |
| 101 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 102 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
| 103 List<int> tmpBytes = UTF8.encode(str); |
| 104 inputByteSink.add(tmpBytes); |
| 105 inputByteSink.close(); |
| 106 return bytes; |
| 107 } |
| 108 |
| 109 List<int> encode6(String str) { |
| 110 List<int> bytes; |
| 111 ChunkedConversionSink byteSink = |
| 112 new ByteConversionSink.withCallback((result) => bytes = result); |
| 113 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 114 ByteConversionSink inputByteSink = stringConversionSink.asUtf8Sink(false); |
| 115 List<int> tmpBytes = UTF8.encode(str); |
| 116 tmpBytes.forEach((b) => inputByteSink.addSlice([0, b, 1], 1, 2, false)); |
| 117 inputByteSink.close(); |
| 118 return bytes; |
| 119 } |
| 120 |
| 121 List<int> encode7(String str) { |
| 122 List<int> bytes; |
| 123 ChunkedConversionSink byteSink = |
| 124 new ByteConversionSink.withCallback((result) => bytes = result); |
| 125 var stringConversionSink = new Utf8Encoder().startChunkedConversion(byteSink); |
| 126 stringConversionSink.addSlice("1" + str + "2", 1, str.length + 1, false); |
| 127 stringConversionSink.close(); |
| 128 return bytes; |
| 129 } |
| 130 |
62 | 131 |
63 main() { | 132 main() { |
64 Expect.equals(2, BEE_STRING.length); | 133 Expect.equals(2, BEE_STRING.length); |
65 var tests = TESTS.expand((test) { | 134 var tests = TESTS.expand((test) { |
66 var bytes = test[0]; | 135 var bytes = test[0]; |
67 var string = test[1]; | 136 var string = test[1]; |
68 var longBytes = []; | 137 var longBytes = []; |
69 var longString = ""; | 138 var longString = ""; |
70 for (int i = 0; i < 100; i++) { | 139 for (int i = 0; i < 100; i++) { |
71 longBytes.addAll(bytes); | 140 longBytes.addAll(bytes); |
72 longString += string; | 141 longString += string; |
73 } | 142 } |
74 return [test, [longBytes, longString]]; | 143 return [test, [longBytes, longString]]; |
75 }); | 144 }); |
76 for (var test in tests) { | 145 for (var test in tests) { |
77 List<int> bytes = test[0]; | 146 List<int> bytes = test[0]; |
78 String string = test[1]; | 147 String string = test[1]; |
79 Expect.listEquals(bytes, encode(string)); | 148 Expect.listEquals(bytes, encode(string)); |
80 Expect.listEquals(bytes, encode2(string)); | 149 Expect.listEquals(bytes, encode2(string)); |
| 150 Expect.listEquals(bytes, encode3(string)); |
| 151 Expect.listEquals(bytes, encode4(string)); |
| 152 Expect.listEquals(bytes, encode5(string)); |
| 153 Expect.listEquals(bytes, encode6(string)); |
| 154 Expect.listEquals(bytes, encode7(string)); |
81 } | 155 } |
82 } | 156 } |
OLD | NEW |