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