| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 testIndexError(list, index, name) { | 56 testIndexError(list, index, name) { |
| 57 try { | 57 try { |
| 58 list[list.length]; | 58 list[list.length]; |
| 59 } catch (err, s) { | 59 } catch (err, s) { |
| 60 Expect.isTrue(err is RangeError, "$name[$index]"); | 60 Expect.isTrue(err is RangeError, "$name[$index]"); |
| 61 Expect.equals(list.length, err.invalidValue, "$name[$index] value"); | 61 Expect.equals(list.length, err.invalidValue, "$name[$index] value"); |
| 62 Expect.equals(list.length - 1, err.end, "$name[$index] end"); | 62 Expect.equals(list.length - 1, err.end, "$name[$index] end"); |
| 63 Expect.equals(0, err.start, "$name[$index] start"); | 63 Expect.equals(0, err.start, "$name[$index] start"); |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 |
| 66 testIndex(list, name) { | 67 testIndex(list, name) { |
| 67 testIndexError(list, list.length, name); // Just too big. | 68 testIndexError(list, list.length, name); // Just too big. |
| 68 testIndexError(list, -1, name); // Negative. | 69 testIndexError(list, -1, name); // Negative. |
| 69 testIndexError(list, 0x123456789, name); // > 2^32. | 70 testIndexError(list, 0x123456789, name); // > 2^32. |
| 70 testIndexError(list, -0x123456789, name); // < -2^32. | 71 testIndexError(list, -0x123456789, name); // < -2^32. |
| 71 } | 72 } |
| 72 | 73 |
| 73 // Slices. | 74 // Slices. |
| 74 testSliceError(list, start, end, name) { | 75 testSliceError(list, start, end, name) { |
| 75 name = "$name[$start:$end]"; | 76 name = "$name[$start:$end]"; |
| 76 var realError; | 77 var realError; |
| 77 try { | 78 try { |
| 78 RangeError.checkValidRange(start, end, list.length); | 79 RangeError.checkValidRange(start, end, list.length); |
| 79 } catch (e) { | 80 } catch (e) { |
| 80 realError = e; | 81 realError = e; |
| 81 } | 82 } |
| 82 var result; | 83 var result; |
| 83 try { | 84 try { |
| 84 result = list.sublist(start, end); | 85 result = list.sublist(start, end); |
| 85 } catch (actualError) { | 86 } catch (actualError) { |
| 86 Expect.isNotNull(realError, "$name should not fail"); | 87 Expect.isNotNull(realError, "$name should not fail"); |
| 87 Expect.isTrue(actualError is RangeError, "$name is-error: $actualError"); | 88 Expect.isTrue(actualError is RangeError, "$name is-error: $actualError"); |
| 88 Expect.equals(realError.name, actualError.name, "$name name"); | 89 Expect.equals(realError.name, actualError.name, "$name name"); |
| 89 Expect.equals(realError.invalidValue, actualError.invalidValue, | 90 Expect.equals(realError.invalidValue, actualError.invalidValue, |
| 90 "$name[0:l+1] value"); | 91 "$name[0:l+1] value"); |
| 91 Expect.equals(realError.start, actualError.start, "$name[0:l+1] start"); | 92 Expect.equals(realError.start, actualError.start, "$name[0:l+1] start"); |
| 92 Expect.equals(realError.end, actualError.end, "$name[0:l+1] end"); | 93 Expect.equals(realError.end, actualError.end, "$name[0:l+1] end"); |
| 93 return; | 94 return; |
| 94 } | 95 } |
| 95 // Didn't throw. | 96 // Didn't throw. |
| 96 Expect.isNull(realError, "$name should fail"); | 97 Expect.isNull(realError, "$name should fail"); |
| 97 Expect.equals(end - start, result.length, "$name result length"); | 98 Expect.equals(end - start, result.length, "$name result length"); |
| 98 } | 99 } |
| 99 | 100 |
| 100 testSlice(list, name) { | 101 testSlice(list, name) { |
| 101 testSliceError(list, 0, list.length, name); // Should not fail. | 102 testSliceError(list, 0, list.length, name); // Should not fail. |
| 102 testSliceError(list, 0, list.length + 1, name); | 103 testSliceError(list, 0, list.length + 1, name); |
| 103 testSliceError(list, 0, 0x123456789, name); | 104 testSliceError(list, 0, 0x123456789, name); |
| 104 testSliceError(list, -1, list.length, name); | 105 testSliceError(list, -1, list.length, name); |
| 105 testSliceError(list, -0x123456789, list.length, name); | 106 testSliceError(list, -0x123456789, list.length, name); |
| 106 testSliceError(list, list.length + 1, list.length + 1, name); | 107 testSliceError(list, list.length + 1, list.length + 1, name); |
| 107 testSliceError(list, -1, null, name); | 108 testSliceError(list, -1, null, name); |
| 108 if (list.length > 0) { | 109 if (list.length > 0) { |
| 109 testSliceError(list, list.length, list.length - 1, name); | 110 testSliceError(list, list.length, list.length - 1, name); |
| 110 } | 111 } |
| 111 } | 112 } |
| 112 | 113 |
| 113 testRangeErrors(list, name) { | 114 testRangeErrors(list, name) { |
| 114 testIndex(list, "$name#${list.length} index"); | 115 testIndex(list, "$name#${list.length} index"); |
| 115 testSlice(list, "$name#${list.length} slice"); | 116 testSlice(list, "$name#${list.length} slice"); |
| 116 } | 117 } |
| 118 |
| 117 // Empty lists. | 119 // Empty lists. |
| 118 testRangeErrors([], "list"); | 120 testRangeErrors([], "list"); |
| 119 testRangeErrors(new List(0), "fixed-list"); | 121 testRangeErrors(new List(0), "fixed-list"); |
| 120 testRangeErrors(const [], "const-list"); | 122 testRangeErrors(const [], "const-list"); |
| 121 testRangeErrors(new List.unmodifiable([]), "unmodifiable"); | 123 testRangeErrors(new List.unmodifiable([]), "unmodifiable"); |
| 122 testRangeErrors(new Uint8List(0), "typed-list"); | 124 testRangeErrors(new Uint8List(0), "typed-list"); |
| 123 testRangeErrors(new Uint8List.view(new Uint8List(0).buffer), "typed-list"); | 125 testRangeErrors(new Uint8List.view(new Uint8List(0).buffer), "typed-list"); |
| 124 testRangeErrors([1, 2, 3].sublist(1, 1), "sub-list"); | 126 testRangeErrors([1, 2, 3].sublist(1, 1), "sub-list"); |
| 125 // Non-empty lists. | 127 // Non-empty lists. |
| 126 testRangeErrors([1, 2, 3], "list"); | 128 testRangeErrors([1, 2, 3], "list"); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 Expect.listEquals([9, 9, 9, 9], list); | 195 Expect.listEquals([9, 9, 9, 9], list); |
| 194 | 196 |
| 195 // sort. | 197 // sort. |
| 196 list.setRange(0, 4, [3, 2, 1, 0]); | 198 list.setRange(0, 4, [3, 2, 1, 0]); |
| 197 list.sort(); | 199 list.sort(); |
| 198 Expect.listEquals([0, 1, 2, 3], list); | 200 Expect.listEquals([0, 1, 2, 3], list); |
| 199 list.setRange(0, 4, [1, 2, 3, 0]); | 201 list.setRange(0, 4, [1, 2, 3, 0]); |
| 200 list.sort(); | 202 list.sort(); |
| 201 Expect.listEquals([0, 1, 2, 3], list); | 203 Expect.listEquals([0, 1, 2, 3], list); |
| 202 list.setRange(0, 4, [1, 3, 0, 2]); | 204 list.setRange(0, 4, [1, 3, 0, 2]); |
| 203 list.sort((a, b) => b - a); // reverse compare. | 205 list.sort((a, b) => b - a); // reverse compare. |
| 204 Expect.listEquals([3, 2, 1, 0], list); | 206 Expect.listEquals([3, 2, 1, 0], list); |
| 205 list.setRange(0, 4, [1, 2, 3, 0]); | 207 list.setRange(0, 4, [1, 2, 3, 0]); |
| 206 list.sort((a, b) => b - a); | 208 list.sort((a, b) => b - a); |
| 207 Expect.listEquals([3, 2, 1, 0], list); | 209 Expect.listEquals([3, 2, 1, 0], list); |
| 208 | 210 |
| 209 // Some Iterable methods. | 211 // Some Iterable methods. |
| 210 | 212 |
| 211 list.setRange(0, 4, [0, 1, 2, 3]); | 213 list.setRange(0, 4, [0, 1, 2, 3]); |
| 212 // map. | 214 // map. |
| 213 testMap(val) {return val * 2 + 10; } | 215 testMap(val) { |
| 216 return val * 2 + 10; |
| 217 } |
| 218 |
| 214 List mapped = list.map(testMap).toList(); | 219 List mapped = list.map(testMap).toList(); |
| 215 Expect.equals(mapped.length, list.length); | 220 Expect.equals(mapped.length, list.length); |
| 216 for (var i = 0; i < list.length; i++) { | 221 for (var i = 0; i < list.length; i++) { |
| 217 Expect.equals(mapped[i], list[i] * 2 + 10); | 222 Expect.equals(mapped[i], list[i] * 2 + 10); |
| 218 } | 223 } |
| 219 | 224 |
| 220 matchAll(val) => true; | 225 matchAll(val) => true; |
| 221 matchSome(val) { return (val == 1 || val == 2); } | 226 matchSome(val) { |
| 222 matchSomeFirst(val) { return val == 0; } | 227 return (val == 1 || val == 2); |
| 223 matchSomeLast(val) { return val == 3; } | 228 } |
| 229 |
| 230 matchSomeFirst(val) { |
| 231 return val == 0; |
| 232 } |
| 233 |
| 234 matchSomeLast(val) { |
| 235 return val == 3; |
| 236 } |
| 237 |
| 224 matchNone(val) => false; | 238 matchNone(val) => false; |
| 225 | 239 |
| 226 // where. | 240 // where. |
| 227 Iterable filtered = list.where(matchSome); | 241 Iterable filtered = list.where(matchSome); |
| 228 Expect.equals(filtered.length, 2); | 242 Expect.equals(filtered.length, 2); |
| 229 | 243 |
| 230 // every | 244 // every |
| 231 Expect.isTrue(list.every(matchAll)); | 245 Expect.isTrue(list.every(matchAll)); |
| 232 Expect.isFalse(list.every(matchSome)); | 246 Expect.isFalse(list.every(matchSome)); |
| 233 Expect.isFalse(list.every(matchNone)); | 247 Expect.isFalse(list.every(matchNone)); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 318 |
| 305 void testFixedLengthList(List list) { | 319 void testFixedLengthList(List list) { |
| 306 testLengthInvariantOperations(list); | 320 testLengthInvariantOperations(list); |
| 307 testCannotChangeLength(list); | 321 testCannotChangeLength(list); |
| 308 } | 322 } |
| 309 | 323 |
| 310 void testCannotChangeLength(List list) { | 324 void testCannotChangeLength(List list) { |
| 311 isUnsupported(action()) { | 325 isUnsupported(action()) { |
| 312 Expect.throws(action, (e) => e is UnsupportedError); | 326 Expect.throws(action, (e) => e is UnsupportedError); |
| 313 } | 327 } |
| 328 |
| 314 isUnsupported(() => list.add(0)); | 329 isUnsupported(() => list.add(0)); |
| 315 isUnsupported(() => list.addAll([0])); | 330 isUnsupported(() => list.addAll([0])); |
| 316 isUnsupported(() => list.removeLast()); | 331 isUnsupported(() => list.removeLast()); |
| 317 isUnsupported(() => list.insert(0, 1)); | 332 isUnsupported(() => list.insert(0, 1)); |
| 318 isUnsupported(() => list.insertAll(0, [1])); | 333 isUnsupported(() => list.insertAll(0, [1])); |
| 319 isUnsupported(() => list.clear()); | 334 isUnsupported(() => list.clear()); |
| 320 isUnsupported(() => list.remove(1)); | 335 isUnsupported(() => list.remove(1)); |
| 321 isUnsupported(() => list.removeAt(1)); | 336 isUnsupported(() => list.removeAt(1)); |
| 322 isUnsupported(() => list.removeRange(0, 1)); | 337 isUnsupported(() => list.removeRange(0, 1)); |
| 323 isUnsupported(() => list.replaceRange(0, 1, [])); | 338 isUnsupported(() => list.replaceRange(0, 1, [])); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 void testConcurrentModification(action()) { | 479 void testConcurrentModification(action()) { |
| 465 testIterator(int when) { | 480 testIterator(int when) { |
| 466 list.length = 4; | 481 list.length = 4; |
| 467 list.setAll(0, [0, 1, 2, 3]); | 482 list.setAll(0, [0, 1, 2, 3]); |
| 468 Expect.throws(() { | 483 Expect.throws(() { |
| 469 for (var element in list) { | 484 for (var element in list) { |
| 470 if (element == when) action(); | 485 if (element == when) action(); |
| 471 } | 486 } |
| 472 }, (e) => e is ConcurrentModificationError); | 487 }, (e) => e is ConcurrentModificationError); |
| 473 } | 488 } |
| 489 |
| 474 testForEach(int when) { | 490 testForEach(int when) { |
| 475 list.length = 4; | 491 list.length = 4; |
| 476 list.setAll(0, [0, 1, 2, 3]); | 492 list.setAll(0, [0, 1, 2, 3]); |
| 477 Expect.throws(() { | 493 Expect.throws(() { |
| 478 list.forEach((var element) { | 494 list.forEach((var element) { |
| 479 if (element == when) action(); | 495 if (element == when) action(); |
| 480 }); | 496 }); |
| 481 }, (e) => e is ConcurrentModificationError); | 497 }, (e) => e is ConcurrentModificationError); |
| 482 } | 498 } |
| 499 |
| 483 // Test the change at different points of the iteration. | 500 // Test the change at different points of the iteration. |
| 484 testIterator(0); | 501 testIterator(0); |
| 485 testIterator(1); | 502 testIterator(1); |
| 486 testIterator(3); | 503 testIterator(3); |
| 487 testForEach(0); | 504 testForEach(0); |
| 488 testForEach(1); | 505 testForEach(1); |
| 489 testForEach(3); | 506 testForEach(3); |
| 490 } | 507 } |
| 491 | 508 |
| 492 testConcurrentModification(() => list.add(5)); | 509 testConcurrentModification(() => list.add(5)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 | 576 |
| 560 class Yes { | 577 class Yes { |
| 561 operator ==(var other) => true; | 578 operator ==(var other) => true; |
| 562 int get hashCode => 0; | 579 int get hashCode => 0; |
| 563 } | 580 } |
| 564 | 581 |
| 565 class MyList<E> extends ListBase<E> { | 582 class MyList<E> extends ListBase<E> { |
| 566 List<E> _source; | 583 List<E> _source; |
| 567 MyList(this._source); | 584 MyList(this._source); |
| 568 int get length => _source.length; | 585 int get length => _source.length; |
| 569 void set length(int length) { _source.length = length; } | 586 void set length(int length) { |
| 570 E operator[](int index) => _source[index]; | 587 _source.length = length; |
| 571 void operator[]=(int index, E value) { _source[index] = value; } | 588 } |
| 589 |
| 590 E operator [](int index) => _source[index]; |
| 591 void operator []=(int index, E value) { |
| 592 _source[index] = value; |
| 593 } |
| 572 } | 594 } |
| 573 | 595 |
| 574 class MyFixedList<E> extends ListBase<E> { | 596 class MyFixedList<E> extends ListBase<E> { |
| 575 List<E> _source; | 597 List<E> _source; |
| 576 MyFixedList(this._source); | 598 MyFixedList(this._source); |
| 577 int get length => _source.length; | 599 int get length => _source.length; |
| 578 void set length(int length) { throw new UnsupportedError("Fixed length!"); } | 600 void set length(int length) { |
| 579 E operator[](int index) => _source[index]; | 601 throw new UnsupportedError("Fixed length!"); |
| 580 void operator[]=(int index, E value) { _source[index] = value; } | 602 } |
| 603 |
| 604 E operator [](int index) => _source[index]; |
| 605 void operator []=(int index, E value) { |
| 606 _source[index] = value; |
| 607 } |
| 581 } | 608 } |
| 582 | 609 |
| 583 void testListConstructor() { | 610 void testListConstructor() { |
| 584 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. | 611 // Is fixed-length. |
| 612 Expect.throws(() { |
| 613 new List(0).add(4); |
| 614 }); |
| 585 Expect.throws(() { new List(-2); }); // Not negative. //# 01: ok | 615 Expect.throws(() { new List(-2); }); // Not negative. //# 01: ok |
| 586 Expect.throws(() { new List(null); }); // Not null. | 616 // Not null. |
| 617 Expect.throws(() { |
| 618 new List(null); |
| 619 }); |
| 587 Expect.listEquals([4], new List()..add(4)); | 620 Expect.listEquals([4], new List()..add(4)); |
| 588 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. | 621 // Is fixed-length. |
| 589 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. | 622 Expect.throws(() { |
| 590 Expect.throws(() { new List.filled(null, 42); }); // Not null. | 623 new List.filled(0, 42).add(4); |
| 624 }); |
| 625 // Not negative. |
| 626 Expect.throws(() { |
| 627 new List.filled(-2, 42); |
| 628 }); |
| 629 // Not null. |
| 630 Expect.throws(() { |
| 631 new List.filled(null, 42); |
| 632 }); |
| 591 } | 633 } |
| OLD | NEW |