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