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