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 import 'dart:collection'; | 6 import 'dart:collection'; |
7 | 7 |
8 class MyList extends ListBase { | 8 class MyList extends ListBase { |
9 List list; | 9 List list; |
10 MyList(this.list); | 10 MyList(this.list); |
11 | 11 |
12 get length => list.length; | 12 get length => list.length; |
13 set length(val) { list.length = val; } | 13 set length(val) { |
| 14 list.length = val; |
| 15 } |
14 | 16 |
15 operator [](index) => list[index]; | 17 operator [](index) => list[index]; |
16 operator []=(index, val) => list[index] = val; | 18 operator []=(index, val) => list[index] = val; |
17 | 19 |
18 String toString() => "[" + join(", ") + "]"; | 20 String toString() => "[" + join(", ") + "]"; |
19 } | 21 } |
20 | 22 |
21 // l1 must be a modifiable list with 5 elements from 0 to 4. | 23 // l1 must be a modifiable list with 5 elements from 0 to 4. |
22 void testModifiableList(l1) { | 24 void testModifiableList(l1) { |
23 bool checkedMode = false; | 25 bool checkedMode = false; |
24 assert(checkedMode = true); | 26 assert(checkedMode = true); |
25 | 27 |
26 // Index must be integer and in range. | 28 // Index must be integer and in range. |
27 Expect.throws(() { l1.removeAt(-1); }, | 29 Expect.throws(() { |
28 (e) => e is RangeError, | 30 l1.removeAt(-1); |
29 "negative"); | 31 }, (e) => e is RangeError, "negative"); |
30 Expect.throws(() { l1.removeAt(5); }, | 32 Expect.throws(() { |
31 (e) => e is RangeError, | 33 l1.removeAt(5); |
32 "too large"); | 34 }, (e) => e is RangeError, "too large"); |
33 Expect.throws(() { l1.removeAt(null); }, | 35 Expect.throws(() { |
34 (e) => e is ArgumentError, | 36 l1.removeAt(null); |
35 "too large"); | 37 }, (e) => e is ArgumentError, "too large"); |
36 Expect.throws(() { l1.removeAt("1"); }, | 38 Expect.throws(() { |
37 (e) => (checkedMode ? e is TypeError | 39 l1.removeAt("1"); |
38 : e is ArgumentError), | 40 }, (e) => (checkedMode ? e is TypeError : e is ArgumentError), "string"); |
39 "string"); | 41 Expect.throws(() { |
40 Expect.throws(() { l1.removeAt(1.5); }, | 42 l1.removeAt(1.5); |
41 (e) => (checkedMode ? e is TypeError | 43 }, (e) => (checkedMode ? e is TypeError : e is ArgumentError), "double"); |
42 : e is ArgumentError), | |
43 "double"); | |
44 | 44 |
45 Expect.equals(2, l1.removeAt(2), "l1-remove2"); | 45 Expect.equals(2, l1.removeAt(2), "l1-remove2"); |
46 Expect.equals(1, l1[1], "l1-1[1]"); | 46 Expect.equals(1, l1[1], "l1-1[1]"); |
47 | 47 |
48 Expect.equals(3, l1[2], "l1-1[2]"); | 48 Expect.equals(3, l1[2], "l1-1[2]"); |
49 Expect.equals(4, l1[3], "l1-1[3]"); | 49 Expect.equals(4, l1[3], "l1-1[3]"); |
50 Expect.equals(4, l1.length, "length-1"); | 50 Expect.equals(4, l1.length, "length-1"); |
51 | 51 |
52 Expect.equals(0, l1.removeAt(0), "l1-remove0"); | 52 Expect.equals(0, l1.removeAt(0), "l1-remove0"); |
53 Expect.equals(1, l1[0], "l1-2[0]"); | 53 Expect.equals(1, l1[0], "l1-2[0]"); |
54 Expect.equals(3, l1[1], "l1-2[1]"); | 54 Expect.equals(3, l1[1], "l1-2[1]"); |
55 Expect.equals(4, l1[2], "l1-2[2]"); | 55 Expect.equals(4, l1[2], "l1-2[2]"); |
56 Expect.equals(3, l1.length, "length-2"); | 56 Expect.equals(3, l1.length, "length-2"); |
57 } | 57 } |
58 | 58 |
59 void main() { | 59 void main() { |
60 // Normal modifiable list. | 60 // Normal modifiable list. |
61 testModifiableList([0, 1, 2, 3, 4]); | 61 testModifiableList([0, 1, 2, 3, 4]); |
62 testModifiableList(new MyList([0, 1, 2, 3, 4])); | 62 testModifiableList(new MyList([0, 1, 2, 3, 4])); |
63 | 63 |
64 // Fixed size list. | 64 // Fixed size list. |
65 var l2 = new List(5); | 65 var l2 = new List(5); |
66 for (var i = 0; i < 5; i++) l2[i] = i; | 66 for (var i = 0; i < 5; i++) l2[i] = i; |
67 Expect.throws(() { l2.removeAt(2); }, | 67 Expect.throws(() { |
68 (e) => e is UnsupportedError, | 68 l2.removeAt(2); |
69 "fixed-length"); | 69 }, (e) => e is UnsupportedError, "fixed-length"); |
70 | 70 |
71 // Unmodifiable list. | 71 // Unmodifiable list. |
72 var l3 = const [0, 1, 2, 3, 4]; | 72 var l3 = const [0, 1, 2, 3, 4]; |
73 Expect.throws(() { l3.removeAt(2); }, | 73 Expect.throws(() { |
74 (e) => e is UnsupportedError, | 74 l3.removeAt(2); |
75 "unmodifiable"); | 75 }, (e) => e is UnsupportedError, "unmodifiable"); |
76 | 76 |
77 // Empty list is not special. | 77 // Empty list is not special. |
78 var l4 = []; | 78 var l4 = []; |
79 Expect.throws(() { l4.removeAt(0); }, | 79 Expect.throws(() { |
80 (e) => e is RangeError, | 80 l4.removeAt(0); |
81 "empty"); | 81 }, (e) => e is RangeError, "empty"); |
82 } | 82 } |
OLD | NEW |