| 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(() { | |
| 44 units[0] = 499; | |
| 45 }, (e) => e is UnsupportedError); | |
| 46 List<int> sub = units.sublist(1); | |
| 47 Expect.listEquals(s.substring(1, s.length).codeUnits, sub); | |
| 48 Expect.equals(-1, units.indexOf(-1)); | |
| 49 Expect.equals(0, units.indexOf(units[0])); | |
| 50 Expect.equals(-1, units.lastIndexOf(-1)); | |
| 51 Expect.equals( | |
| 52 units.length - 1, units.lastIndexOf(units[units.length - 1])); | |
| 53 } | |
| 54 | |
| 55 Iterable reversed = units.reversed; | |
| 56 int i = units.length - 1; | |
| 57 for (int codeUnit in reversed) { | |
| 58 Expect.equals(units[i--], codeUnit); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 test(""); | |
| 63 test("abc"); | |
| 64 test("\x00\u0000\u{000000}"); | |
| 65 test("\u{ffff}\u{10000}\u{10ffff}"); | |
| 66 String string = new String.fromCharCodes( | |
| 67 [0xdc00, 0xd800, 61, 0xd9ab, 0xd9ab, 0xddef, 0xddef, 62, 0xdc00, 0xd800]); | |
| 68 test(string); | |
| 69 string = "\x00\x7f\xff\u0100\ufeff\uffef\uffff" | |
| 70 "\u{10000}\u{12345}\u{1d800}\u{1dc00}\u{1ffef}\u{1ffff}"; | |
| 71 test(string); | |
| 72 | |
| 73 // Reading each unit of a surrogate pair works. | |
| 74 var r = "\u{10000}".codeUnits; | |
| 75 var it = r.iterator; | |
| 76 Expect.isTrue(it.moveNext()); | |
| 77 Expect.equals(0xD800, it.current); | |
| 78 Expect.isTrue(it.moveNext()); | |
| 79 Expect.equals(0xDC00, it.current); | |
| 80 } | |
| OLD | NEW |