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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'unicode_tests.dart'; | 7 import 'unicode_tests.dart'; |
8 | 8 |
9 List<int> encode(String str) => new Utf8Encoder().convert(str); | 9 List<int> encode(String str) => new Utf8Encoder().convert(str); |
10 List<int> encode2(String str) => UTF8.encode(str); | 10 List<int> encode2(String str) => UTF8.encode(str); |
11 | 11 |
12 void main() { | 12 void main() { |
13 for (var test in UNICODE_TESTS) { | 13 for (var test in UNICODE_TESTS) { |
14 List<int> bytes = test[0]; | 14 List<int> bytes = test[0]; |
15 String string = test[1]; | 15 String string = test[1]; |
16 Expect.listEquals(bytes, encode(string)); | 16 Expect.listEquals(bytes, encode(string)); |
17 Expect.listEquals(bytes, encode2(string)); | 17 Expect.listEquals(bytes, encode2(string)); |
18 } | 18 } |
19 | 19 |
20 testEncodeSlice(); | 20 testEncodeSlice(); |
21 } | 21 } |
22 | 22 |
23 void testEncodeSlice() { | 23 void testEncodeSlice() { |
24 var encoder = UTF8.encoder; | 24 var encoder = UTF8.encoder; |
25 String ascii = "ABCDE"; | 25 String ascii = "ABCDE"; |
26 Expect.listEquals([0x41, 0x42, 0x43, 0x44, 0x45], | 26 Expect.listEquals([0x41, 0x42, 0x43, 0x44, 0x45], encoder.convert(ascii)); |
27 encoder.convert(ascii)); | 27 Expect.listEquals([0x41, 0x42, 0x43, 0x44, 0x45], encoder.convert(ascii, 0)); |
28 Expect.listEquals([0x41, 0x42, 0x43, 0x44, 0x45], | 28 Expect |
29 encoder.convert(ascii, 0)); | 29 .listEquals([0x41, 0x42, 0x43, 0x44, 0x45], encoder.convert(ascii, 0, 5)); |
30 Expect.listEquals([0x41, 0x42, 0x43, 0x44, 0x45], | 30 Expect.listEquals([0x42, 0x43, 0x44, 0x45], encoder.convert(ascii, 1)); |
31 encoder.convert(ascii, 0, 5)); | 31 Expect.listEquals([0x41, 0x42, 0x43, 0x44], encoder.convert(ascii, 0, 4)); |
32 Expect.listEquals([0x42, 0x43, 0x44, 0x45], | 32 Expect.listEquals([0x42, 0x43, 0x44], encoder.convert(ascii, 1, 4)); |
33 encoder.convert(ascii, 1)); | |
34 Expect.listEquals([0x41, 0x42, 0x43, 0x44], | |
35 encoder.convert(ascii, 0, 4)); | |
36 Expect.listEquals([0x42, 0x43, 0x44], | |
37 encoder.convert(ascii, 1, 4)); | |
38 | 33 |
39 Expect.throws(() => encoder.convert(ascii, -1)); // start < 0. | 34 Expect.throws(() => encoder.convert(ascii, -1)); // start < 0. |
40 Expect.throws(() => encoder.convert(ascii, 6)); // start > length | 35 Expect.throws(() => encoder.convert(ascii, 6)); // start > length |
41 Expect.throws(() => encoder.convert(ascii, 0, -1)); // end < 0 | 36 Expect.throws(() => encoder.convert(ascii, 0, -1)); // end < 0 |
42 Expect.throws(() => encoder.convert(ascii, 0, 6)); // end > length | 37 Expect.throws(() => encoder.convert(ascii, 0, 6)); // end > length |
43 Expect.throws(() => encoder.convert(ascii, 3, 2)); // end < start | 38 Expect.throws(() => encoder.convert(ascii, 3, 2)); // end < start |
44 | 39 |
45 var unicode = "\u0081\u0082\u1041\u{10101}"; | 40 var unicode = "\u0081\u0082\u1041\u{10101}"; |
46 | 41 |
47 Expect.listEquals([0xc2, 0x81, 0xc2, 0x82, 0xe1, 0x81, 0x81, | 42 Expect.listEquals( |
48 0xf0, 0x90, 0x84, 0x81], | 43 [0xc2, 0x81, 0xc2, 0x82, 0xe1, 0x81, 0x81, 0xf0, 0x90, 0x84, 0x81], |
49 encoder.convert(unicode)); | 44 encoder.convert(unicode)); |
50 Expect.listEquals([0xc2, 0x81, 0xc2, 0x82, 0xe1, 0x81, 0x81, | 45 Expect.listEquals( |
51 0xf0, 0x90, 0x84, 0x81], | 46 [0xc2, 0x81, 0xc2, 0x82, 0xe1, 0x81, 0x81, 0xf0, 0x90, 0x84, 0x81], |
52 encoder.convert(unicode, 0, unicode.length)); | 47 encoder.convert(unicode, 0, unicode.length)); |
53 Expect.listEquals([0xc2, 0x82, 0xe1, 0x81, 0x81, | 48 Expect.listEquals([0xc2, 0x82, 0xe1, 0x81, 0x81, 0xf0, 0x90, 0x84, 0x81], |
54 0xf0, 0x90, 0x84, 0x81], | 49 encoder.convert(unicode, 1)); |
55 encoder.convert(unicode, 1)); | 50 Expect.listEquals( |
56 Expect.listEquals([0xc2, 0x82, 0xe1, 0x81, 0x81], | 51 [0xc2, 0x82, 0xe1, 0x81, 0x81], encoder.convert(unicode, 1, 3)); |
57 encoder.convert(unicode, 1, 3)); | |
58 // Split in the middle of a surrogate pair. | 52 // Split in the middle of a surrogate pair. |
59 Expect.listEquals([0xc2, 0x82, 0xe1, 0x81, 0x81, | 53 Expect.listEquals([0xc2, 0x82, 0xe1, 0x81, 0x81, 0xed, 0xa0, 0x80], |
60 0xed, 0xa0, 0x80], | 54 encoder.convert(unicode, 1, 4)); |
61 encoder.convert(unicode, 1, 4)); | |
62 } | 55 } |
OLD | NEW |