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 testGetRange(list, start, end, bool isModifiable) { |
8 Expect.listEquals([], [].sublist(0, 0)); | 8 expectRE(() { list.getRange(-1, 0); }); |
9 Expect.listEquals([], const [].sublist(0, 0)); | 9 expectRE(() { list.getRange(0, -1); }); |
| 10 expectRE(() { list.getRange(1, 0); }); |
| 11 expectRE(() { list.getRange(0, list.length + 1); }); |
| 12 expectRE(() { list.getRange(list.length + 1, list.length + 1); }); |
| 13 Iterable iterable = list.getRange(start, end); |
| 14 Expect.isFalse(iterable is List); |
| 15 if (start == end) { |
| 16 Expect.isTrue(iterable.isEmpty); |
| 17 return; |
| 18 } |
10 | 19 |
| 20 var iterator = iterable.iterator; |
| 21 for (int i = start; i < end; i++) { |
| 22 Expect.isTrue(iterator.moveNext()); |
| 23 Expect.equals(iterator.current, list[i]); |
| 24 } |
| 25 Expect.isFalse(iterator.moveNext()); |
11 | 26 |
12 Expect.listEquals([1, 2], [1, 2].sublist(0, 2)); | 27 if (isModifiable) { |
13 Expect.listEquals([1, 2], const [1, 2].sublist(0, 2)); | 28 for (int i = 0; i < list.length; i++) { |
| 29 list[i]++; |
| 30 } |
14 | 31 |
15 Expect.listEquals([1], [1, 2].sublist(0, 1)); | 32 iterator = iterable.iterator; |
16 Expect.listEquals([1], const [1, 2].sublist(0, 1)); | 33 for (int i = start; i < end; i++) { |
17 | 34 Expect.isTrue(iterator.moveNext()); |
18 Expect.listEquals([2], [1, 2].sublist(1, 2)); | 35 Expect.equals(iterator.current, list[i]); |
19 Expect.listEquals([2], const [1, 2].sublist(1, 2)); | 36 } |
20 | 37 } |
21 Expect.listEquals([], [1, 2].sublist(0, 0)); | |
22 Expect.listEquals([], const [1, 2].sublist(0, 0)); | |
23 | |
24 Expect.listEquals([2, 3], [1, 2, 3, 4].sublist(1, 3)); | |
25 Expect.listEquals([2, 3], const [1, 2, 3, 4].sublist(1, 3)); | |
26 | |
27 Expect.listEquals([2, 3], [1, 2, 3, 4].sublist(1, 3)); | |
28 Expect.listEquals([2, 3], const [1, 2, 3, 4].sublist(1, 3)); | |
29 | |
30 expectAE(() => [].sublist(-1, null)); | |
31 expectAE(() => const [].sublist(-1, null)); | |
32 expectAE(() => [].sublist(-1, 0)); | |
33 expectAE(() => const [].sublist(-1, 0)); | |
34 expectAE(() => [].sublist(-1, -1)); | |
35 expectAE(() => const [].sublist(-1, -1)); | |
36 expectAE(() => [].sublist(-1, 1)); | |
37 expectAE(() => const [].sublist(-1, 1)); | |
38 expectAE(() => [].sublist(0, -1)); | |
39 expectAE(() => const [].sublist(0, -1)); | |
40 expectAE(() => [].sublist(0, 1)); | |
41 expectAE(() => const [].sublist(0, 1)); | |
42 expectAE(() => [].sublist(1, null)); | |
43 expectAE(() => const [].sublist(1, null)); | |
44 expectAE(() => [].sublist(1, 0)); | |
45 expectAE(() => const [].sublist(1, 0)); | |
46 expectAE(() => [].sublist(1, -1)); | |
47 expectAE(() => const [].sublist(1, -1)); | |
48 expectAE(() => [].sublist(1, 1)); | |
49 expectAE(() => const [].sublist(1, 1)); | |
50 | |
51 expectAE(() => [1].sublist(0, 2)); | |
52 expectAE(() => [1].sublist(1, 2)); | |
53 expectAE(() => [1].sublist(1, 0)); | |
54 } | 38 } |
55 | 39 |
56 void expectAE(Function f) { | 40 main() { |
57 Expect.throws(f, (e) => e is ArgumentError); | 41 testGetRange([1, 2], 0, 1, true); |
| 42 testGetRange([], 0, 0, true); |
| 43 testGetRange([1, 2, 3], 0, 0, true); |
| 44 testGetRange([1, 2, 3], 1, 3, true); |
| 45 testGetRange(const [1, 2], 0, 1, false); |
| 46 testGetRange(const [], 0, 0, false); |
| 47 testGetRange(const [1, 2, 3], 0, 0, false); |
| 48 testGetRange(const [1, 2, 3], 1, 3, false); |
| 49 testGetRange("abcd".codeUnits, 0, 1, false); |
| 50 testGetRange("abcd".codeUnits, 0, 0, false); |
| 51 testGetRange("abcd".codeUnits, 1, 3, false); |
| 52 |
| 53 expectRE(() => [1].getRange(-1, 1)); |
| 54 expectRE(() => [3].getRange(0, -1)); |
| 55 expectRE(() => [4].getRange(1, 0)); |
| 56 |
| 57 var list = [1, 2, 3, 4]; |
| 58 var iterable = list.getRange(1, 3); |
| 59 Expect.equals(2, iterable.first); |
| 60 Expect.equals(3, iterable.last); |
| 61 list.length = 1; |
| 62 Expect.isTrue(iterable.isEmpty); |
| 63 list.add(99); |
| 64 Expect.equals(99, iterable.single); |
| 65 list.add(499); |
| 66 Expect.equals(499, iterable.last); |
| 67 Expect.equals(2, iterable.length); |
58 } | 68 } |
| 69 |
| 70 void expectRE(Function f) { |
| 71 Expect.throws(f, (e) => e is RangeError); |
| 72 } |
OLD | NEW |