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 main() { |
| 6 test(String s, List<int> expectedRunes) { |
| 7 Runes runes = s.runes; |
| 8 Expect.identical(s, runes.string); |
| 9 |
| 10 // for-in |
| 11 var res = []; |
| 12 for (int rune in runes) { |
| 13 res.add(rune); |
| 14 } |
| 15 Expect.listEquals(expectedRunes, res); |
| 16 |
| 17 // manual iteration, backwards. |
| 18 res = []; |
| 19 for (var it = runes.iterator..reset(s.length); it.movePrevious();) { |
| 20 res.add(it.current); |
| 21 } |
| 22 Expect.listEquals(expectedRunes.reversed.toList(), res); |
| 23 |
| 24 // Setting rawIndex. |
| 25 RuneIterator it = runes.iterator; |
| 26 it.rawIndex = 1; |
| 27 Expect.equals(expectedRunes[1], it.current); |
| 28 |
| 29 it = runes.iterator; |
| 30 it.moveNext(); |
| 31 Expect.equals(0, it.rawIndex); |
| 32 it.moveNext(); |
| 33 Expect.equals(1, it.rawIndex); |
| 34 it.moveNext(); |
| 35 Expect.isTrue(1 < it.rawIndex); |
| 36 it.rawIndex = 1; |
| 37 Expect.equals(1, it.rawIndex); |
| 38 Expect.equals(expectedRunes[1], it.current); |
| 39 |
| 40 // Reset, moveNext. |
| 41 it.reset(1); |
| 42 Expect.equals(1, it.rawIndex); |
| 43 Expect.equals(null, it.current); |
| 44 it.moveNext(); |
| 45 Expect.equals(expectedRunes[1], it.current); |
| 46 |
| 47 // Reset, movePrevious. |
| 48 it.reset(1); |
| 49 Expect.equals(1, it.rawIndex); |
| 50 Expect.equals(null, it.current); |
| 51 it.movePrevious(); |
| 52 Expect.equals(0, it.rawIndex); |
| 53 Expect.equals(expectedRunes[0], it.current); |
| 54 |
| 55 // .map |
| 56 Expect.listEquals(expectedRunes.map((x) => x.toRadixString(16)).toList(), |
| 57 runes.map((x) => x.toRadixString(16)).toList()); |
| 58 } |
| 59 |
| 60 // First character must be single-code-unit for test. |
| 61 test("abc", [0x61, 0x62, 0x63]); |
| 62 test("\x00\u0000\u{000000}", [0, 0, 0]); |
| 63 test("\u{ffff}\u{10000}\u{10ffff}", [0xffff, 0x10000, 0x10ffff]); |
| 64 String string = new String.fromCharCodes( |
| 65 [0xdc00, 0xd800, 61, 0xd800, 0xdc00, 62, 0xdc00, 0xd800]); |
| 66 test(string, [0xdc00, 0xd800, 61, 0x10000, 62, 0xdc00, 0xd800]); |
| 67 |
| 68 // Setting position in the middle of a surrogate pair is not allowed. |
| 69 var r = new Runes("\u{10000}"); |
| 70 var it = r.iterator; |
| 71 it.moveNext(); |
| 72 Expect.equals(0x10000, it.current); |
| 73 |
| 74 // Setting rawIndex inside surrogate pair. |
| 75 Expect.throws(() { it.rawIndex = 1; }, (e) => e is Error); |
| 76 Expect.throws(() { it.reset(1); }, (e) => e is Error); |
| 77 } |
OLD | NEW |