OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); | 9 String decode(List<int> bytes) => new Utf8Decoder().convert(bytes); |
10 | 10 |
11 void main() { | 11 void main() { |
12 for (var test in UNICODE_TESTS) { | 12 for (var test in UNICODE_TESTS) { |
13 List<int> bytes = test[0]; | 13 List<int> bytes = test[0]; |
14 String expected = test[1]; | 14 String expected = test[1]; |
15 Expect.stringEquals(expected, decode(bytes)); | 15 Expect.stringEquals(expected, decode(bytes)); |
16 } | 16 } |
17 | 17 |
18 testDecodeSlice(); | 18 testDecodeSlice(); |
19 testErrorOffset(); | 19 testErrorOffset(); |
20 } | 20 } |
21 | 21 |
22 void testDecodeSlice() { | 22 void testDecodeSlice() { |
23 var decoder = UTF8.decoder; // Doesn't allow malformed. | 23 var decoder = UTF8.decoder; // Doesn't allow malformed. |
24 var ascii = [0x41, 0x42, 0x43, 0x44, 0x45]; | 24 var ascii = [0x41, 0x42, 0x43, 0x44, 0x45]; |
25 Expect.equals("ABCDE", decoder.convert(ascii)); | 25 Expect.equals("ABCDE", decoder.convert(ascii)); |
26 Expect.equals("ABCDE", decoder.convert(ascii, 0)); | 26 Expect.equals("ABCDE", decoder.convert(ascii, 0)); |
27 Expect.equals("ABCDE", decoder.convert(ascii, 0, ascii.length)); | 27 Expect.equals("ABCDE", decoder.convert(ascii, 0, ascii.length)); |
28 Expect.equals("CDE", decoder.convert(ascii, 2)); | 28 Expect.equals("CDE", decoder.convert(ascii, 2)); |
29 Expect.equals("BCD", decoder.convert(ascii, 1, 4)); | 29 Expect.equals("BCD", decoder.convert(ascii, 1, 4)); |
30 Expect.equals("ABCD", decoder.convert(ascii, 0, 4)); | 30 Expect.equals("ABCD", decoder.convert(ascii, 0, 4)); |
31 | 31 |
32 Expect.throws(() => decoder.convert(ascii, -1)); // start < 0. | 32 Expect.throws(() => decoder.convert(ascii, -1)); // start < 0. |
33 Expect.throws(() => decoder.convert(ascii, 6)); // start > length | 33 Expect.throws(() => decoder.convert(ascii, 6)); // start > length |
34 Expect.throws(() => decoder.convert(ascii, 0, -1)); // end < 0 | 34 Expect.throws(() => decoder.convert(ascii, 0, -1)); // end < 0 |
35 Expect.throws(() => decoder.convert(ascii, 0, 6)); // end > length | 35 Expect.throws(() => decoder.convert(ascii, 0, 6)); // end > length |
36 Expect.throws(() => decoder.convert(ascii, 3, 2)); // end < start | 36 Expect.throws(() => decoder.convert(ascii, 3, 2)); // end < start |
37 | 37 |
38 var utf8 = [0xc2, 0x81, 0xc2, 0x82, 0xe1, 0x81, 0x81]; | 38 var utf8 = [0xc2, 0x81, 0xc2, 0x82, 0xe1, 0x81, 0x81]; |
39 Expect.equals("\u0081\u0082\u1041", decoder.convert(utf8)); | 39 Expect.equals("\u0081\u0082\u1041", decoder.convert(utf8)); |
40 Expect.equals("\u0082\u1041", decoder.convert(utf8, 2)); | 40 Expect.equals("\u0082\u1041", decoder.convert(utf8, 2)); |
41 Expect.equals("\u0081\u0082", decoder.convert(utf8, 0, 4)); | 41 Expect.equals("\u0081\u0082", decoder.convert(utf8, 0, 4)); |
42 Expect.equals("\u0082", decoder.convert(utf8, 2, 4)); | 42 Expect.equals("\u0082", decoder.convert(utf8, 2, 4)); |
43 Expect.throws(() => decoder.convert(utf8, 1)); | 43 Expect.throws(() => decoder.convert(utf8, 1)); |
44 Expect.throws(() => decoder.convert(utf8, 0, 1)); | 44 Expect.throws(() => decoder.convert(utf8, 0, 1)); |
45 Expect.throws(() => decoder.convert(utf8, 2, 5)); | 45 Expect.throws(() => decoder.convert(utf8, 2, 5)); |
46 } | 46 } |
47 | 47 |
48 void testErrorOffset() { | 48 void testErrorOffset() { |
49 // Test that failed convert calls have an offset in the exception. | 49 // Test that failed convert calls have an offset in the exception. |
50 testExn(input, offset) { | 50 testExn(input, offset) { |
51 Expect.throws(() { UTF8.decoder.convert(input); }, | 51 Expect.throws(() { |
52 (e) => e is FormatException && | 52 UTF8.decoder.convert(input); |
53 input == e.source && | 53 }, (e) => e is FormatException && input == e.source && offset == e.offset); |
54 offset == e.offset); | |
55 } | 54 } |
56 | 55 |
57 // Bad encoding, points to first bad byte. | 56 // Bad encoding, points to first bad byte. |
58 testExn([0x80, 0x00], 0); | 57 testExn([0x80, 0x00], 0); |
59 testExn([0xC0, 0x00], 1); | 58 testExn([0xC0, 0x00], 1); |
60 testExn([0xE0, 0x00], 1); | 59 testExn([0xE0, 0x00], 1); |
61 testExn([0xE0, 0x80, 0x00], 2); | 60 testExn([0xE0, 0x80, 0x00], 2); |
62 testExn([0xF0, 0x00], 1); | 61 testExn([0xF0, 0x00], 1); |
63 testExn([0xF0, 0x80, 0x00], 2); | 62 testExn([0xF0, 0x80, 0x00], 2); |
64 testExn([0xF0, 0x80, 0x80, 0x00], 3); | 63 testExn([0xF0, 0x80, 0x80, 0x00], 3); |
65 testExn([0xF8, 0x00], 0); | 64 testExn([0xF8, 0x00], 0); |
66 // Short encoding, points to end. | 65 // Short encoding, points to end. |
67 testExn([0xC0], 1); | 66 testExn([0xC0], 1); |
68 testExn([0xE0], 1); | 67 testExn([0xE0], 1); |
69 testExn([0xE0, 0x80], 2); | 68 testExn([0xE0, 0x80], 2); |
70 testExn([0xF0], 1); | 69 testExn([0xF0], 1); |
71 testExn([0xF0, 0x80], 2); | 70 testExn([0xF0, 0x80], 2); |
72 testExn([0xF0, 0x80, 0x80], 3); | 71 testExn([0xF0, 0x80, 0x80], 3); |
73 // Overlong encoding, points to start of encoding. | 72 // Overlong encoding, points to start of encoding. |
74 testExn([0xC0, 0x80], 0); | 73 testExn([0xC0, 0x80], 0); |
75 testExn([0xC1, 0xBF], 0); | 74 testExn([0xC1, 0xBF], 0); |
76 testExn([0xE0, 0x80, 0x80], 0); | 75 testExn([0xE0, 0x80, 0x80], 0); |
77 testExn([0xE0, 0x9F, 0xBF], 0); | 76 testExn([0xE0, 0x9F, 0xBF], 0); |
78 testExn([0xF0, 0x80, 0x80, 0x80], 0); | 77 testExn([0xF0, 0x80, 0x80, 0x80], 0); |
79 testExn([0xF0, 0x8F, 0xBF, 0xBF], 0); | 78 testExn([0xF0, 0x8F, 0xBF, 0xBF], 0); |
80 // Invalid character (value too large, over 0x10FFFF). | 79 // Invalid character (value too large, over 0x10FFFF). |
81 testExn([0xF4, 0x90, 0x80, 0x80], 0); | 80 testExn([0xF4, 0x90, 0x80, 0x80], 0); |
82 testExn([0xF7, 0xBF, 0xBF, 0xBF], 0); | 81 testExn([0xF7, 0xBF, 0xBF, 0xBF], 0); |
83 } | 82 } |
OLD | NEW |