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 void main() { |
| 6 // Normal modifiable list. |
| 7 var l1 = [0, 1, 2, 3, 4]; |
| 8 |
| 9 bool checkedMode = false; |
| 10 assert(checkedMode = true); |
| 11 |
| 12 // Index must be integer and in range. |
| 13 Expect.throws(() { l1.insert(-1, 5); }, |
| 14 (e) => e is RangeError, |
| 15 "negative"); |
| 16 Expect.throws(() { l1.insert(6, 5); }, |
| 17 (e) => e is RangeError, |
| 18 "too large"); |
| 19 Expect.throws(() { l1.insert(null, 5); }); |
| 20 Expect.throws(() { l1.insert("1", 5); }); |
| 21 Expect.throws(() { l1.insert(1.5, 5); }); |
| 22 |
| 23 l1.insert(5, 5); |
| 24 Expect.equals(6, l1.length); |
| 25 Expect.equals(5, l1[5]); |
| 26 Expect.equals("[0, 1, 2, 3, 4, 5]", l1.toString()); |
| 27 |
| 28 l1.insert(0, -1); |
| 29 Expect.equals(7, l1.length); |
| 30 Expect.equals(-1, l1[0]); |
| 31 Expect.equals("[-1, 0, 1, 2, 3, 4, 5]", l1.toString()); |
| 32 |
| 33 // Fixed size list. |
| 34 var l2 = new List(5); |
| 35 for (var i = 0; i < 5; i++) l2[i] = i; |
| 36 Expect.throws(() { l2.insert(2, 5); }, |
| 37 (e) => e is UnsupportedError, |
| 38 "fixed-length"); |
| 39 |
| 40 // Unmodifiable list. |
| 41 var l3 = const [0, 1, 2, 3, 4]; |
| 42 Expect.throws(() { l3.insert(2, 5); }, |
| 43 (e) => e is UnsupportedError, |
| 44 "unmodifiable"); |
| 45 |
| 46 // Empty list is not special. |
| 47 var l4 = []; |
| 48 l4.insert(0, 499); |
| 49 Expect.equals(1, l4.length); |
| 50 Expect.equals(499, l4[0]); |
| 51 } |
OLD | NEW |