OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 void main() { | 5 void main() { |
6 // Normal modifiable list. | 6 // Normal modifiable list. |
7 var l1 = [0, 1, 2, 3, 4]; | 7 var l1 = [0, 1, 2, 3, 4]; |
8 | 8 |
9 bool checkedMode = false; | 9 bool checkedMode = false; |
10 assert(checkedMode = true); | 10 assert(checkedMode = true); |
11 | 11 |
12 // Index must be integer and in range. | 12 // Index must be integer and in range. |
13 Expect.throws(() { l1.removeAt(-1); }, | 13 Expect.throws(() { l1.insertAt(-1, 5); }, |
14 (e) => e is RangeError, | 14 (e) => e is RangeError, |
15 "negative"); | 15 "negative"); |
16 Expect.throws(() { l1.removeAt(5); }, | 16 Expect.throws(() { l1.insertAt(6, 5); }, |
17 (e) => e is RangeError, | 17 (e) => e is RangeError, |
18 "too large"); | 18 "too large"); |
19 Expect.throws(() { l1.removeAt(null); }, | 19 Expect.throws(() { l1.insertAt(null, 5); }, |
20 (e) => e is ArgumentError, | 20 (e) => e is ArgumentError, |
21 "too large"); | 21 "too large"); |
22 Expect.throws(() { l1.removeAt("1"); }, | 22 Expect.throws(() { l1.insertAt("1", 5); }, |
23 (e) => (checkedMode ? e is TypeError | 23 (e) => (checkedMode ? e is TypeError |
24 : e is ArgumentError), | 24 : e is ArgumentError), |
25 "string"); | 25 "string"); |
26 Expect.throws(() { l1.removeAt(1.5); }, | 26 Expect.throws(() { l1.insertAt(1.5, 5); }, |
27 (e) => (checkedMode ? e is TypeError | 27 (e) => (checkedMode ? e is TypeError |
28 : e is ArgumentError), | 28 : e is ArgumentError), |
29 "double"); | 29 "double"); |
30 | 30 |
31 Expect.equals(2, l1.removeAt(2), "l1-remove2"); | 31 l1.insertAt(5, 5); |
32 Expect.equals(1, l1[1], "l1-1[1]"); | 32 Expect.equals(6, l1.length); |
| 33 Expect.equals(5, l1[5]); |
| 34 Expect.equals("[0, 1, 2, 3, 4, 5]", l1.toString()); |
33 | 35 |
34 Expect.equals(3, l1[2], "l1-1[2]"); | 36 l1.insertAt(0, -1); |
35 Expect.equals(4, l1[3], "l1-1[3]"); | 37 Expect.equals(7, l1.length); |
36 Expect.equals(4, l1.length, "length-1"); | 38 Expect.equals(-1, l1[0]); |
37 | 39 Expect.equals("[-1, 0, 1, 2, 3, 4, 5]", l1.toString()); |
38 Expect.equals(0, l1.removeAt(0), "l1-remove0"); | |
39 Expect.equals(1, l1[0], "l1-2[0]"); | |
40 Expect.equals(3, l1[1], "l1-2[1]"); | |
41 Expect.equals(4, l1[2], "l1-2[2]"); | |
42 Expect.equals(3, l1.length, "length-2"); | |
43 | 40 |
44 // Fixed size list. | 41 // Fixed size list. |
45 var l2 = new List(5); | 42 var l2 = new List(5); |
46 for (var i = 0; i < 5; i++) l2[i] = i; | 43 for (var i = 0; i < 5; i++) l2[i] = i; |
47 Expect.throws(() { l2.removeAt(2); }, | 44 Expect.throws(() { l2.insertAt(2, 5); }, |
48 (e) => e is UnsupportedError, | 45 (e) => e is UnsupportedError, |
49 "fixed-length"); | 46 "fixed-length"); |
50 | 47 |
51 // Unmodifiable list. | 48 // Unmodifiable list. |
52 var l3 = const [0, 1, 2, 3, 4]; | 49 var l3 = const [0, 1, 2, 3, 4]; |
53 Expect.throws(() { l3.removeAt(2); }, | 50 Expect.throws(() { l3.insertAt(2, 5); }, |
54 (e) => e is UnsupportedError, | 51 (e) => e is UnsupportedError, |
55 "unmodifiable"); | 52 "unmodifiable"); |
56 | 53 |
57 // Empty list is not special. | 54 // Empty list is not special. |
58 var l4 = []; | 55 var l4 = []; |
59 Expect.throws(() { l4.removeAt(0); }, | 56 l4.insertAt(0, 499); |
60 (e) => e is RangeError, | 57 Expect.equals(1, l4.length); |
61 "empty"); | 58 Expect.equals(499, l4[0]); |
62 } | 59 } |
OLD | NEW |