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