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