OLD | NEW |
(Empty) | |
| 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 |
| 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 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); |
| 10 |
| 11 void main() { |
| 12 for (var test in UNICODE_TESTS) { |
| 13 List<int> bytes = test[0]; |
| 14 String expected = test[1]; |
| 15 Expect.stringEquals(expected, decode(bytes)); |
| 16 } |
| 17 |
| 18 testDecodeSlice(); |
| 19 } |
| 20 |
| 21 void testDecodeSlice() { |
| 22 var decoder = UTF8.decoder; // Doesn't allow malformed. |
| 23 var ascii = [0x41, 0x42, 0x43, 0x44, 0x45]; |
| 24 Expect.equals("ABCDE", decoder.convert(ascii)); |
| 25 Expect.equals("ABCDE", decoder.convert(ascii, 0)); |
| 26 Expect.equals("ABCDE", decoder.convert(ascii, 0, ascii.length)); |
| 27 Expect.equals("CDE", decoder.convert(ascii, 2)); |
| 28 Expect.equals("BCD", decoder.convert(ascii, 1, 4)); |
| 29 Expect.equals("ABCD", decoder.convert(ascii, 0, 4)); |
| 30 |
| 31 Expect.throws(() => decoder.convert(ascii, -1)); // start < 0. |
| 32 Expect.throws(() => decoder.convert(ascii, 6)); // start > length |
| 33 Expect.throws(() => decoder.convert(ascii, 0, -1)); // end < 0 |
| 34 Expect.throws(() => decoder.convert(ascii, 0, 6)); // end > length |
| 35 Expect.throws(() => decoder.convert(ascii, 3, 2)); // end < start |
| 36 |
| 37 var utf8 = [0xc2, 0x81, 0xc2, 0x82, 0xe1, 0x81, 0x81]; |
| 38 Expect.equals("\u0081\u0082\u1041", decoder.convert(utf8)); |
| 39 Expect.equals("\u0082\u1041", decoder.convert(utf8, 2)); |
| 40 Expect.equals("\u0081\u0082", decoder.convert(utf8, 0, 4)); |
| 41 Expect.equals("\u0082", decoder.convert(utf8, 2, 4)); |
| 42 Expect.throws(() => decoder.convert(utf8, 1)); |
| 43 Expect.throws(() => decoder.convert(utf8, 0, 1)); |
| 44 Expect.throws(() => decoder.convert(utf8, 2, 5)); |
| 45 } |
OLD | NEW |