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 Iterable iterable = list.getRange(start, end); |
Lasse Reichstein Nielsen
2013/04/11 10:56:15
Test that it throws if outside the range.
floitsch
2013/04/11 14:12:48
Done.
| |
9 Expect.listEquals([], const [].sublist(0, 0)); | 9 Expect.isFalse(iterable is List); |
10 if (start == end) { | |
11 Expect.isTrue(iterable.isEmpty); | |
12 return; | |
13 } | |
10 | 14 |
15 var iterator = iterable.iterator; | |
16 for (int i = start; i < end; i++) { | |
17 Expect.isTrue(iterator.moveNext()); | |
18 Expect.equals(iterator.current, list[i]); | |
19 } | |
20 Expect.isFalse(iterator.moveNext()); | |
11 | 21 |
12 Expect.listEquals([1, 2], [1, 2].sublist(0, 2)); | 22 if (isModifiable) { |
13 Expect.listEquals([1, 2], const [1, 2].sublist(0, 2)); | 23 for (int i = 0; i < list.length; i++) { |
24 list[i]++; | |
25 } | |
14 | 26 |
15 Expect.listEquals([1], [1, 2].sublist(0, 1)); | 27 iterator = iterable.iterator; |
16 Expect.listEquals([1], const [1, 2].sublist(0, 1)); | 28 for (int i = start; i < end; i++) { |
17 | 29 Expect.isTrue(iterator.moveNext()); |
18 Expect.listEquals([2], [1, 2].sublist(1, 2)); | 30 Expect.equals(iterator.current, list[i]); |
19 Expect.listEquals([2], const [1, 2].sublist(1, 2)); | 31 } |
20 | 32 } |
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 } | 33 } |
55 | 34 |
56 void expectAE(Function f) { | 35 main() { |
57 Expect.throws(f, (e) => e is ArgumentError); | 36 testGetRange([1, 2], 0, 1, true); |
37 testGetRange([], 0, 0, true); | |
38 testGetRange([1, 2, 3], 0, 0, true); | |
39 testGetRange([1, 2, 3], 1, 3, true); | |
40 testGetRange(const [1, 2], 0, 1, false); | |
41 testGetRange(const [], 0, 0, false); | |
42 testGetRange(const [1, 2, 3], 0, 0, false); | |
43 testGetRange(const [1, 2, 3], 1, 3, false); | |
Lasse Reichstein Nielsen
2013/04/11 10:56:15
Try testing with "abc".codeUnits and/or another no
floitsch
2013/04/11 14:12:48
Done.
| |
44 | |
45 expectRE(() => [1].getRange(-1, 1)); | |
46 expectRE(() => [3].getRange(0, -1)); | |
47 expectRE(() => [4].getRange(1, 0)); | |
48 | |
49 var list = [1, 2, 3, 4]; | |
50 var iterable = list.getRange(1, 3); | |
51 Expect.equals(2, iterable.first); | |
52 Expect.equals(3, iterable.last); | |
53 list.length = 1; | |
54 Expect.isTrue(iterable.isEmpty); | |
55 list.add(99); | |
56 Expect.equals(99, iterable.single); | |
57 list.add(499); | |
58 Expect.equals(499, iterable.last); | |
59 Expect.equals(2, iterable.length); | |
58 } | 60 } |
61 | |
62 void expectRE(Function f) { | |
63 Expect.throws(f, (e) => e is RangeError); | |
64 } | |
OLD | NEW |