OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 | 6 |
7 main() { | 7 main() { |
8 test(String s) { | 8 test(String s) { |
9 List<int> units = s.codeUnits; | 9 List<int> units = s.codeUnits; |
10 List<int> expectedUnits = <int>[]; | 10 List<int> expectedUnits = <int>[]; |
11 for (int i = 0; i < s.length; i++) { | 11 for (int i = 0; i < s.length; i++) { |
12 expectedUnits.add(s.codeUnitAt(i)); | 12 expectedUnits.add(s.codeUnitAt(i)); |
13 } | 13 } |
14 | 14 |
15 Expect.equals(s.length, units.length); | 15 Expect.equals(s.length, units.length); |
16 for (int i = 0; i < s.length; i++) { | 16 for (int i = 0; i < s.length; i++) { |
17 Expect.equals(s.codeUnitAt(i), units.elementAt(i)); | 17 Expect.equals(s.codeUnitAt(i), units.elementAt(i)); |
18 } | 18 } |
19 | 19 |
20 // for-in | 20 // for-in |
21 var res = []; | 21 var res = []; |
22 for (int unit in units) { | 22 for (int unit in units) { |
23 res.add(unit); | 23 res.add(unit); |
24 } | 24 } |
25 Expect.listEquals(expectedUnits, res); | 25 Expect.listEquals(expectedUnits, res); |
26 | 26 |
27 // .map | 27 // .map |
28 Expect.listEquals(expectedUnits.map((x) => x.toRadixString(16)).toList(), | 28 Expect.listEquals(expectedUnits.map((x) => x.toRadixString(16)).toList(), |
29 units.map((x) => x.toRadixString(16)).toList()); | 29 units.map((x) => x.toRadixString(16)).toList()); |
30 | 30 |
31 if (s == "") { | 31 if (s == "") { |
32 Expect.throws(() => units.first, (e) => e is StateError); | 32 Expect.throws(() => units.first, (e) => e is StateError); |
33 Expect.throws(() => units.last, (e) => e is StateError); | 33 Expect.throws(() => units.last, (e) => e is StateError); |
34 Expect.throws(() => units[0], (e) => e is RangeError); | 34 Expect.throws(() => units[0], (e) => e is RangeError); |
35 Expect.throws(() => units[0] = 499, (e) => e is UnsupportedError); | 35 Expect.throws(() => units[0] = 499, (e) => e is UnsupportedError); |
36 Expect.listEquals([], units.sublist(0, 0)); | 36 Expect.listEquals([], units.sublist(0, 0)); |
37 Expect.equals(-1, units.indexOf(42)); | 37 Expect.equals(-1, units.indexOf(42)); |
38 Expect.equals(-1, units.lastIndexOf(499)); | 38 Expect.equals(-1, units.lastIndexOf(499)); |
39 } else { | 39 } else { |
40 Expect.equals(s.codeUnitAt(0), units.first); | 40 Expect.equals(s.codeUnitAt(0), units.first); |
41 Expect.equals(s.codeUnitAt(s.length - 1), units.last); | 41 Expect.equals(s.codeUnitAt(s.length - 1), units.last); |
42 Expect.equals(s.codeUnitAt(0), units[0]); | 42 Expect.equals(s.codeUnitAt(0), units[0]); |
43 Expect.throws(() { units[0] = 499; }, (e) => e is UnsupportedError); | 43 Expect.throws(() { |
| 44 units[0] = 499; |
| 45 }, (e) => e is UnsupportedError); |
44 List<int> sub = units.sublist(1); | 46 List<int> sub = units.sublist(1); |
45 Expect.listEquals(s.substring(1, s.length).codeUnits, sub); | 47 Expect.listEquals(s.substring(1, s.length).codeUnits, sub); |
46 Expect.equals(-1, units.indexOf(-1)); | 48 Expect.equals(-1, units.indexOf(-1)); |
47 Expect.equals(0, units.indexOf(units[0])); | 49 Expect.equals(0, units.indexOf(units[0])); |
48 Expect.equals(-1, units.lastIndexOf(-1)); | 50 Expect.equals(-1, units.lastIndexOf(-1)); |
49 Expect.equals(units.length - 1, units.lastIndexOf(units[units.length - 1])
); | 51 Expect.equals( |
| 52 units.length - 1, units.lastIndexOf(units[units.length - 1])); |
50 } | 53 } |
51 | 54 |
52 Iterable reversed = units.reversed; | 55 Iterable reversed = units.reversed; |
53 int i = units.length - 1; | 56 int i = units.length - 1; |
54 for (int codeUnit in reversed) { | 57 for (int codeUnit in reversed) { |
55 Expect.equals(units[i--], codeUnit); | 58 Expect.equals(units[i--], codeUnit); |
56 } | 59 } |
57 } | 60 } |
58 | 61 |
59 test(""); | 62 test(""); |
60 test("abc"); | 63 test("abc"); |
61 test("\x00\u0000\u{000000}"); | 64 test("\x00\u0000\u{000000}"); |
62 test("\u{ffff}\u{10000}\u{10ffff}"); | 65 test("\u{ffff}\u{10000}\u{10ffff}"); |
63 String string = new String.fromCharCodes( | 66 String string = new String.fromCharCodes( |
64 [0xdc00, 0xd800, 61, 0xd9ab, 0xd9ab, 0xddef, 0xddef, 62, 0xdc00, 0xd800]); | 67 [0xdc00, 0xd800, 61, 0xd9ab, 0xd9ab, 0xddef, 0xddef, 62, 0xdc00, 0xd800]); |
65 test(string); | 68 test(string); |
66 string = "\x00\x7f\xff\u0100\ufeff\uffef\uffff" | 69 string = "\x00\x7f\xff\u0100\ufeff\uffef\uffff" |
67 "\u{10000}\u{12345}\u{1d800}\u{1dc00}\u{1ffef}\u{1ffff}"; | 70 "\u{10000}\u{12345}\u{1d800}\u{1dc00}\u{1ffef}\u{1ffff}"; |
68 test(string); | 71 test(string); |
69 | 72 |
70 // Reading each unit of a surrogate pair works. | 73 // Reading each unit of a surrogate pair works. |
71 var r = "\u{10000}".codeUnits; | 74 var r = "\u{10000}".codeUnits; |
72 var it = r.iterator; | 75 var it = r.iterator; |
73 Expect.isTrue(it.moveNext()); | 76 Expect.isTrue(it.moveNext()); |
74 Expect.equals(0xD800, it.current); | 77 Expect.equals(0xD800, it.current); |
75 Expect.isTrue(it.moveNext()); | 78 Expect.isTrue(it.moveNext()); |
76 Expect.equals(0xDC00, it.current); | 79 Expect.equals(0xDC00, it.current); |
77 } | 80 } |
OLD | NEW |