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) { 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) { |
| 23 bool checkedMode = false; |
| 24 assert(checkedMode = true); |
| 25 |
| 26 // Index must be integer and in range. |
| 27 Expect.throws(() { l1.insert(-1, 5); }, |
| 28 (e) => e is RangeError, |
| 29 "negative"); |
| 30 Expect.throws(() { l1.insert(6, 5); }, |
| 31 (e) => e is RangeError, |
| 32 "too large"); |
| 33 Expect.throws(() { l1.insert(null, 5); }); |
| 34 Expect.throws(() { l1.insert("1", 5); }); |
| 35 Expect.throws(() { l1.insert(1.5, 5); }); |
| 36 |
| 37 l1.insert(5, 5); |
| 38 Expect.equals(6, l1.length); |
| 39 Expect.equals(5, l1[5]); |
| 40 Expect.equals("[0, 1, 2, 3, 4, 5]", l1.toString()); |
| 41 |
| 42 l1.insert(0, -1); |
| 43 Expect.equals(7, l1.length); |
| 44 Expect.equals(-1, l1[0]); |
| 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])); |
| 52 |
| 53 // Fixed size list. |
| 54 var l2 = new List(5); |
| 55 for (var i = 0; i < 5; i++) l2[i] = i; |
| 56 Expect.throws(() { l2.insert(2, 5); }, |
| 57 (e) => e is UnsupportedError, |
| 58 "fixed-length"); |
| 59 |
| 60 // Unmodifiable list. |
| 61 var l3 = const [0, 1, 2, 3, 4]; |
| 62 Expect.throws(() { l3.insert(2, 5); }, |
| 63 (e) => e is UnsupportedError, |
| 64 "unmodifiable"); |
| 65 |
| 66 // Empty list is not special. |
| 67 var l4 = []; |
| 68 l4.insert(0, 499); |
| 69 Expect.equals(1, l4.length); |
| 70 Expect.equals(499, l4[0]); |
| 71 } |
OLD | NEW |