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