| OLD | NEW |
| (Empty) |
| 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 | |
| 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.removeAt(-1); | |
| 31 }, (e) => e is RangeError, "negative"); | |
| 32 Expect.throws(() { | |
| 33 l1.removeAt(5); | |
| 34 }, (e) => e is RangeError, "too large"); | |
| 35 Expect.throws(() { | |
| 36 l1.removeAt(null); | |
| 37 }, (e) => e is ArgumentError, "too large"); | |
| 38 Expect.throws(() { | |
| 39 l1.removeAt("1"); | |
| 40 }, (e) => (checkedMode ? e is TypeError : e is ArgumentError), "string"); | |
| 41 Expect.throws(() { | |
| 42 l1.removeAt(1.5); | |
| 43 }, (e) => (checkedMode ? e is TypeError : e is ArgumentError), "double"); | |
| 44 | |
| 45 Expect.equals(2, l1.removeAt(2), "l1-remove2"); | |
| 46 Expect.equals(1, l1[1], "l1-1[1]"); | |
| 47 | |
| 48 Expect.equals(3, l1[2], "l1-1[2]"); | |
| 49 Expect.equals(4, l1[3], "l1-1[3]"); | |
| 50 Expect.equals(4, l1.length, "length-1"); | |
| 51 | |
| 52 Expect.equals(0, l1.removeAt(0), "l1-remove0"); | |
| 53 Expect.equals(1, l1[0], "l1-2[0]"); | |
| 54 Expect.equals(3, l1[1], "l1-2[1]"); | |
| 55 Expect.equals(4, l1[2], "l1-2[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])); | |
| 63 | |
| 64 // Fixed size list. | |
| 65 var l2 = new List(5); | |
| 66 for (var i = 0; i < 5; i++) l2[i] = i; | |
| 67 Expect.throws(() { | |
| 68 l2.removeAt(2); | |
| 69 }, (e) => e is UnsupportedError, "fixed-length"); | |
| 70 | |
| 71 // Unmodifiable list. | |
| 72 var l3 = const [0, 1, 2, 3, 4]; | |
| 73 Expect.throws(() { | |
| 74 l3.removeAt(2); | |
| 75 }, (e) => e is UnsupportedError, "unmodifiable"); | |
| 76 | |
| 77 // Empty list is not special. | |
| 78 var l4 = []; | |
| 79 Expect.throws(() { | |
| 80 l4.removeAt(0); | |
| 81 }, (e) => e is RangeError, "empty"); | |
| 82 } | |
| OLD | NEW |