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]; | |
sra1
2015/09/09 17:16:05
Could also test that an index outside the 2^32 ran
Lasse Reichstein Nielsen
2015/09/10 10:08:54
Good point.
| |
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: $err"); | |
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: $err"); | |
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: $err"); | |
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"); | |
sra1
2015/09/09 17:16:05
Also new List.unmodifiable([])
Lasse Reichstein Nielsen
2015/09/10 10:08:54
Good idea. Done.
| |
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 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 testArgumentError(() => list.removeRange(-1, 5)); | 527 testArgumentError(() => list.removeRange(-1, 5)); |
469 testArgumentError(() => list.removeRange(4, 2)); | 528 testArgumentError(() => list.removeRange(4, 2)); |
470 testArgumentError(() => list.replaceRange(-1, 2, [9])); | 529 testArgumentError(() => list.replaceRange(-1, 2, [9])); |
471 testArgumentError(() => list.replaceRange(2, 5, [9])); | 530 testArgumentError(() => list.replaceRange(2, 5, [9])); |
472 testArgumentError(() => list.replaceRange(-1, 5, [9])); | 531 testArgumentError(() => list.replaceRange(-1, 5, [9])); |
473 testArgumentError(() => list.replaceRange(4, 2, [9])); | 532 testArgumentError(() => list.replaceRange(4, 2, [9])); |
474 } | 533 } |
475 | 534 |
476 class Yes { | 535 class Yes { |
477 operator ==(var other) => true; | 536 operator ==(var other) => true; |
537 int get hashCode => 0; | |
478 } | 538 } |
479 | 539 |
480 class MyList<E> extends ListBase<E> { | 540 class MyList<E> extends ListBase<E> { |
481 List<E> _source; | 541 List<E> _source; |
482 MyList(this._source); | 542 MyList(this._source); |
483 int get length => _source.length; | 543 int get length => _source.length; |
484 void set length(int length) { _source.length = length; } | 544 void set length(int length) { _source.length = length; } |
485 E operator[](int index) => _source[index]; | 545 E operator[](int index) => _source[index]; |
486 void operator[]=(int index, E value) { _source[index] = value; } | 546 void operator[]=(int index, E value) { _source[index] = value; } |
487 } | 547 } |
488 | 548 |
489 class MyFixedList<E> extends ListBase<E> { | 549 class MyFixedList<E> extends ListBase<E> { |
490 List<E> _source; | 550 List<E> _source; |
491 MyFixedList(this._source); | 551 MyFixedList(this._source); |
492 int get length => _source.length; | 552 int get length => _source.length; |
493 void set length(int length) { throw new UnsupportedError("Fixed length!"); } | 553 void set length(int length) { throw new UnsupportedError("Fixed length!"); } |
494 E operator[](int index) => _source[index]; | 554 E operator[](int index) => _source[index]; |
495 void operator[]=(int index, E value) { _source[index] = value; } | 555 void operator[]=(int index, E value) { _source[index] = value; } |
496 } | 556 } |
497 | 557 |
498 void testListConstructor() { | 558 void testListConstructor() { |
499 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. | 559 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. |
500 Expect.throws(() { new List(-2); }); // Not negative. /// 01: ok | 560 Expect.throws(() { new List(-2); }); // Not negative. /// 01: ok |
501 Expect.throws(() { new List(null); }); // Not null. | 561 Expect.throws(() { new List(null); }); // Not null. |
502 Expect.listEquals([4], new List()..add(4)); | 562 Expect.listEquals([4], new List()..add(4)); |
503 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. | 563 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. |
504 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. | 564 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. |
505 Expect.throws(() { new List.filled(null, 42); }); // Not null. | 565 Expect.throws(() { new List.filled(null, 42); }); // Not null. |
506 } | 566 } |
OLD | NEW |