| OLD | NEW |
| 1 // Copyright (c) 2013, 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 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.insert(-1, 5); }, | 27 Expect.throws(() { l1.insert(-1, 5); }, |
| 16 (e) => e is RangeError, | 28 (e) => e is RangeError, |
| 17 "negative"); | 29 "negative"); |
| 18 Expect.throws(() { l1.insert(6, 5); }, | 30 Expect.throws(() { l1.insert(6, 5); }, |
| 19 (e) => e is RangeError, | 31 (e) => e is RangeError, |
| 20 "too large"); | 32 "too large"); |
| 21 Expect.throws(() { l1.insert(null, 5); }); | 33 Expect.throws(() { l1.insert(null, 5); }); |
| 22 Expect.throws(() { l1.insert("1", 5); }); | 34 Expect.throws(() { l1.insert("1", 5); }); |
| 23 Expect.throws(() { l1.insert(1.5, 5); }); | 35 Expect.throws(() { l1.insert(1.5, 5); }); |
| 24 | 36 |
| 25 l1.insert(5, 5); | 37 l1.insert(5, 5); |
| 26 Expect.equals(6, l1.length); | 38 Expect.equals(6, l1.length); |
| 27 Expect.equals(5, l1[5]); | 39 Expect.equals(5, l1[5]); |
| 28 Expect.equals("[0, 1, 2, 3, 4, 5]", l1.toString()); | 40 Expect.equals("[0, 1, 2, 3, 4, 5]", l1.toString()); |
| 29 | 41 |
| 30 l1.insert(0, -1); | 42 l1.insert(0, -1); |
| 31 Expect.equals(7, l1.length); | 43 Expect.equals(7, l1.length); |
| 32 Expect.equals(-1, l1[0]); | 44 Expect.equals(-1, l1[0]); |
| 33 Expect.equals("[-1, 0, 1, 2, 3, 4, 5]", l1.toString()); | 45 Expect.equals("[-1, 0, 1, 2, 3, 4, 5]", l1.toString()); |
| 46 } |
| 47 |
| 48 void main() { |
| 49 // Normal modifiable list. |
| 50 testModifiableList([0, 1, 2, 3, 4]); |
| 51 testModifiableList(new MyList([0, 1, 2, 3, 4])); |
| 34 | 52 |
| 35 // Fixed size list. | 53 // Fixed size list. |
| 36 var l2 = new List(5); | 54 var l2 = new List(5); |
| 37 for (var i = 0; i < 5; i++) l2[i] = i; | 55 for (var i = 0; i < 5; i++) l2[i] = i; |
| 38 Expect.throws(() { l2.insert(2, 5); }, | 56 Expect.throws(() { l2.insert(2, 5); }, |
| 39 (e) => e is UnsupportedError, | 57 (e) => e is UnsupportedError, |
| 40 "fixed-length"); | 58 "fixed-length"); |
| 41 | 59 |
| 42 // Unmodifiable list. | 60 // Unmodifiable list. |
| 43 var l3 = const [0, 1, 2, 3, 4]; | 61 var l3 = const [0, 1, 2, 3, 4]; |
| 44 Expect.throws(() { l3.insert(2, 5); }, | 62 Expect.throws(() { l3.insert(2, 5); }, |
| 45 (e) => e is UnsupportedError, | 63 (e) => e is UnsupportedError, |
| 46 "unmodifiable"); | 64 "unmodifiable"); |
| 47 | 65 |
| 48 // Empty list is not special. | 66 // Empty list is not special. |
| 49 var l4 = []; | 67 var l4 = []; |
| 50 l4.insert(0, 499); | 68 l4.insert(0, 499); |
| 51 Expect.equals(1, l4.length); | 69 Expect.equals(1, l4.length); |
| 52 Expect.equals(499, l4[0]); | 70 Expect.equals(499, l4[0]); |
| 53 } | 71 } |
| OLD | NEW |