| 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 "dart:collection"; | 5 import "dart:collection"; |
| 6 import "dart:typed_data"; | 6 import "dart:typed_data"; |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 | 8 |
| 9 void main() { | 9 void main() { |
| 10 // Typed lists - fixed length and can only contain integers. | 10 // Typed lists - fixed length and can only contain integers. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 testGrowableList(new MyList([]).toList()); | 39 testGrowableList(new MyList([]).toList()); |
| 40 | 40 |
| 41 testTypedGrowableList(new Uint8List(0).toList()); | 41 testTypedGrowableList(new Uint8List(0).toList()); |
| 42 testTypedGrowableList(new Int8List(0).toList()); | 42 testTypedGrowableList(new Int8List(0).toList()); |
| 43 testTypedGrowableList(new Uint16List(0).toList()); | 43 testTypedGrowableList(new Uint16List(0).toList()); |
| 44 testTypedGrowableList(new Int16List(0).toList()); | 44 testTypedGrowableList(new Int16List(0).toList()); |
| 45 testTypedGrowableList(new Uint32List(0).toList()); | 45 testTypedGrowableList(new Uint32List(0).toList()); |
| 46 testTypedGrowableList(new Int32List(0).toList()); | 46 testTypedGrowableList(new Int32List(0).toList()); |
| 47 | 47 |
| 48 testListConstructor(); | 48 testListConstructor(); |
| 49 |
| 50 // Regression for issue http://dartbug.com/24295 |
| 51 testIndex(list, name) { |
| 52 try { |
| 53 list[list.length]; |
| 54 } catch (err, s) { |
| 55 Expect.isTrue(err is RangeError, name); |
| 56 Expect.equals(list.length, err.invalidValue, "$name value"); |
| 57 Expect.equals(list.length - 1, err.end, "$name end"); |
| 58 Expect.equals(0, err.start, "$name start"); |
| 59 } |
| 60 } |
| 61 // Slices. |
| 62 testSlice(list, name) { |
| 63 try { |
| 64 list.sublist(0, list.length + 1); |
| 65 } catch (err) { |
| 66 Expect.isTrue(err is RangeError, "$name[0:l+1] is-error"); |
| 67 Expect.equals("end", err.name, "$name[0:l+1] name"); |
| 68 Expect.equals(list.length + 1, err.invalidValue, "$name[0:l+1] value"); |
| 69 Expect.equals(0, err.start, "$name[0:l+1] start"); |
| 70 Expect.equals(list.length, err.end, "$name[0:l+1] end"); |
| 71 } |
| 72 try { |
| 73 list.sublist(list.length + 1, list.length + 1); |
| 74 } catch (err) { |
| 75 Expect.isTrue(err is RangeError, "$name[l+1:l+1] is-error"); |
| 76 Expect.equals("start", err.name, "$name[l+1:l+1] name"); |
| 77 Expect.equals(list.length + 1, err.invalidValue, "$name[l+1:l+1] value"); |
| 78 Expect.equals(0, err.start, "$name[l+1:l+1] start"); |
| 79 Expect.equals(list.length, err.end, "$name[l+1:l+1] end"); |
| 80 } |
| 81 if (list.length == 0) return; |
| 82 try { |
| 83 list.sublist(1, list.length + 1); |
| 84 } catch (err) { |
| 85 Expect.isTrue(err is RangeError, "$name[1:l+1] is-error"); |
| 86 Expect.equals("end", err.name, "$name[1:l+1] name"); |
| 87 Expect.equals(list.length + 1, err.invalidValue, "$name[1:l+1] value"); |
| 88 Expect.equals(1, err.start, "$name[1:l+1] start"); |
| 89 Expect.equals(list.length, err.end, "$name[1:l+1] end"); |
| 90 } |
| 91 } |
| 92 testRangeErrors(list, name) { |
| 93 testIndex(list, "$name#${list.length} index"); |
| 94 testSlice(list, "$name#${list.length} slice"); |
| 95 } |
| 96 // Empty lists. |
| 97 testRangeErrors([], "list"); |
| 98 testRangeErrors(new List(0), "fixed-list"); |
| 99 testRangeErrors(const [], "const-list"); |
| 100 testRangeErrors(new Uint8List(0), "typed-list"); |
| 101 testRangeErrors([1, 2, 3].sublist(1, 1), "sub-list"); |
| 102 // Non-empty lists. |
| 103 testRangeErrors([1, 2, 3], "list"); |
| 104 testRangeErrors(new List(3), "fixed-list"); |
| 105 testRangeErrors(const [1, 2, 3], "const-list"); |
| 106 testRangeErrors(new Uint8List(3), "typed-list"); |
| 107 testRangeErrors([1, 2, 3, 4, 5].sublist(1, 3), "sub-list"); |
| 49 } | 108 } |
| 50 | 109 |
| 51 void testLength(int length, List list) { | 110 void testLength(int length, List list) { |
| 52 Expect.equals(length, list.length); | 111 Expect.equals(length, list.length); |
| 53 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty); | 112 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty); |
| 54 (length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty); | 113 (length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty); |
| 55 } | 114 } |
| 56 | 115 |
| 57 void testTypedLengthInvariantOperations(List list) { | 116 void testTypedLengthInvariantOperations(List list) { |
| 58 // length | 117 // length |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 | 556 |
| 498 void testListConstructor() { | 557 void testListConstructor() { |
| 499 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. | 558 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. |
| 500 Expect.throws(() { new List(-2); }); // Not negative. /// 01: ok | 559 Expect.throws(() { new List(-2); }); // Not negative. /// 01: ok |
| 501 Expect.throws(() { new List(null); }); // Not null. | 560 Expect.throws(() { new List(null); }); // Not null. |
| 502 Expect.listEquals([4], new List()..add(4)); | 561 Expect.listEquals([4], new List()..add(4)); |
| 503 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. | 562 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. |
| 504 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. | 563 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. |
| 505 Expect.throws(() { new List.filled(null, 42); }); // Not null. | 564 Expect.throws(() { new List.filled(null, 42); }); // Not null. |
| 506 } | 565 } |
| OLD | NEW |