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 main() { |
8 var list = []; | 8 var list = []; |
9 list.setRange(0, 0, const []); | 9 list.setRange(0, 0, const []); |
10 list.setRange(0, 0, []); | 10 list.setRange(0, 0, []); |
11 list.setRange(0, 0, const [], 1); | 11 list.setRange(0, 0, const [], 1); |
12 list.setRange(0, 0, [], 1); | 12 list.setRange(0, 0, [], 1); |
13 Expect.equals(0, list.length); | 13 Expect.equals(0, list.length); |
14 expectIOORE(() { list.setRange(0, 1, []); }); | 14 expectIOORE(() { list.setRange(0, 1, []); }); |
15 expectIOORE(() { list.setRange(0, 1, [], 1); }); | 15 expectIOORE(() { list.setRange(0, 1, [], 1); }); |
16 expectIOORE(() { list.setRange(0, 1, [1], 0); }); | 16 expectIOORE(() { list.setRange(0, 1, [1], 0); }); |
17 | 17 |
18 list.add(1); | 18 list.add(1); |
19 list.setRange(0, 0, [], 0); | 19 list.setRange(0, 0, [], 0); |
20 Expect.equals(1, list.length); | 20 Expect.equals(1, list.length); |
21 Expect.equals(1, list[0]); | 21 Expect.equals(1, list[0]); |
22 list.setRange(0, 0, const [], 0); | 22 list.setRange(0, 0, const [], 0); |
23 Expect.equals(1, list.length); | 23 Expect.equals(1, list.length); |
24 Expect.equals(1, list[0]); | 24 Expect.equals(1, list[0]); |
25 | 25 |
26 expectIOORE(() { list.setRange(0, 2, [1, 2]); }); | 26 expectIOORE(() { list.setRange(0, 2, [1, 2]); }); |
27 Expect.equals(1, list.length); | 27 Expect.equals(1, list.length); |
28 Expect.equals(1, list[0]); | 28 Expect.equals(1, list[0]); |
29 | 29 |
30 expectIOORE(() { list.setRange(0, 1, [1, 2], 2); }); | 30 expectSE(() { list.setRange(0, 1, [1, 2], 2); }); |
31 Expect.equals(1, list.length); | 31 Expect.equals(1, list.length); |
32 Expect.equals(1, list[0]); | 32 Expect.equals(1, list[0]); |
33 | 33 |
34 list.setRange(0, 1, [2], 0); | 34 list.setRange(0, 1, [2], 0); |
35 Expect.equals(1, list.length); | 35 Expect.equals(1, list.length); |
36 Expect.equals(2, list[0]); | 36 Expect.equals(2, list[0]); |
37 | 37 |
38 list.setRange(0, 1, const [3], 0); | 38 list.setRange(0, 1, const [3], 0); |
39 Expect.equals(1, list.length); | 39 Expect.equals(1, list.length); |
40 Expect.equals(3, list[0]); | 40 Expect.equals(3, list[0]); |
41 | 41 |
42 list.addAll([4, 5, 6]); | 42 list.addAll([4, 5, 6]); |
43 Expect.equals(4, list.length); | 43 Expect.equals(4, list.length); |
44 list.setRange(0, 4, [1, 2, 3, 4]); | 44 list.setRange(0, 4, [1, 2, 3, 4]); |
45 Expect.listEquals([1, 2, 3, 4], list); | 45 Expect.listEquals([1, 2, 3, 4], list); |
46 | 46 |
47 list.setRange(2, 2, [5, 6, 7, 8]); | 47 list.setRange(2, 4, [5, 6, 7, 8]); |
48 Expect.listEquals([1, 2, 5, 6], list); | 48 Expect.listEquals([1, 2, 5, 6], list); |
49 | 49 |
50 expectIOORE(() { list.setRange(4, 1, [5, 6, 7, 8]); }); | 50 expectIOORE(() { list.setRange(4, 5, [5, 6, 7, 8]); }); |
51 Expect.listEquals([1, 2, 5, 6], list); | 51 Expect.listEquals([1, 2, 5, 6], list); |
52 | 52 |
53 list.setRange(1, 2, [9, 10, 11, 12]); | 53 list.setRange(1, 3, [9, 10, 11, 12]); |
54 Expect.listEquals([1, 9, 10, 6], list); | 54 Expect.listEquals([1, 9, 10, 6], list); |
55 | 55 |
56 testNegativeIndices(); | 56 testNegativeIndices(); |
57 | 57 |
58 testNonExtendableList(); | 58 testNonExtendableList(); |
59 } | 59 } |
60 | 60 |
61 void expectIOORE(Function f) { | 61 void expectIOORE(Function f) { |
62 Expect.throws(f, (e) => e is RangeError); | 62 Expect.throws(f, (e) => e is RangeError); |
63 } | 63 } |
64 | 64 |
| 65 void expectSE(Function f) { |
| 66 Expect.throws(f, (e) => e is StateError); |
| 67 } |
| 68 |
| 69 void expectAE(Function f) { |
| 70 Expect.throws(f, (e) => e is ArgumentError); |
| 71 } |
| 72 |
65 void testNegativeIndices() { | 73 void testNegativeIndices() { |
66 var list = [1, 2]; | 74 var list = [1, 2]; |
67 expectIOORE(() { list.setRange(-1, 1, [1]); }); | 75 expectIOORE(() { list.setRange(-1, 1, [1]); }); |
68 expectIOORE(() { list.setRange(0, 1, [1], -1); }); | 76 expectAE(() { list.setRange(0, 1, [1], -1); }); |
69 | 77 |
70 // A negative length throws an ArgumentError. | 78 // A negative length throws an ArgumentError. |
71 Expect.throws(() { list.setRange(0, -1, [1]); }, | 79 expectIOORE(() { list.setRange(2, 1, [1]); }); |
72 (e) => e is ArgumentError); | |
73 | 80 |
74 Expect.throws(() { list.setRange(-1, -1, [1], -1); }, | 81 expectAE(() { list.setRange(-1, -2, [1], -1); }); |
75 (e) => e is ArgumentError); | |
76 Expect.listEquals([1, 2], list); | 82 Expect.listEquals([1, 2], list); |
77 | 83 |
78 // A zero length prevails, and does not throw an exception. | 84 expectIOORE(() { list.setRange(-1, -1, [1]); }); |
79 list.setRange(-1, 0, [1]); | |
80 Expect.listEquals([1, 2], list); | 85 Expect.listEquals([1, 2], list); |
81 | 86 |
| 87 // The skipCount is only used if the length is not 0. |
82 list.setRange(0, 0, [1], -1); | 88 list.setRange(0, 0, [1], -1); |
83 Expect.listEquals([1, 2], list); | 89 Expect.listEquals([1, 2], list); |
84 } | 90 } |
85 | 91 |
86 void testNonExtendableList() { | 92 void testNonExtendableList() { |
87 var list = new List<int>(6); | 93 var list = new List<int>(6); |
88 Expect.listEquals([null, null, null, null, null, null], list); | 94 Expect.listEquals([null, null, null, null, null, null], list); |
89 list.setRange(0, 3, [1, 2, 3, 4]); | 95 list.setRange(0, 3, [1, 2, 3, 4]); |
90 list.setRange(3, 3, [1, 2, 3, 4]); | 96 list.setRange(3, 6, [1, 2, 3, 4]); |
91 Expect.listEquals([1, 2, 3, 1, 2, 3], list); | 97 Expect.listEquals([1, 2, 3, 1, 2, 3], list); |
92 } | 98 } |
OLD | NEW |