OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:collection'; |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 testRemove(base) { |
| 9 int length = base.length; |
| 10 for (int i = 0; i < length; i++) { |
| 11 Expect.isFalse(base.isEmpty); |
| 12 base.remove(base.first); |
| 13 } |
| 14 Expect.isTrue(base.isEmpty); |
| 15 } |
| 16 |
| 17 testRemoveAll(base, Iterable removes) { |
| 18 Set retained = new Set(); |
| 19 for (var element in base) { |
| 20 if (!removes.contains(element)) { |
| 21 retained.add(element); |
| 22 } |
| 23 } |
| 24 String name = "$base.removeAll($removes) -> $retained"; |
| 25 base.removeAll(removes); |
| 26 for (var value in base) { |
| 27 Expect.isFalse(removes.contains(value), "$name: Found $value"); |
| 28 } |
| 29 for (var value in retained) { |
| 30 Expect.isTrue(base.contains(value), "$name: Found $value"); |
| 31 } |
| 32 } |
| 33 |
| 34 testRetainAll(base, Iterable retains) { |
| 35 Set retained = new Set(); |
| 36 for (var element in base) { |
| 37 if (retains.contains(element)) { |
| 38 retained.add(element); |
| 39 } |
| 40 } |
| 41 String name = "$base.retainAll($retains) -> $retained"; |
| 42 base.retainAll(retains); |
| 43 for (var value in base) { |
| 44 Expect.isTrue(retains.contains(value), "$name: Found $value"); |
| 45 } |
| 46 for (var value in retained) { |
| 47 Expect.isTrue(base.contains(value), "$name: Found $value"); |
| 48 } |
| 49 } |
| 50 |
| 51 testRemoveWhere(base, bool test(value)) { |
| 52 Set retained = new Set(); |
| 53 for (var element in base) { |
| 54 if (!test(element)) { |
| 55 retained.add(element); |
| 56 } |
| 57 } |
| 58 String name = "$base.removeWhere(...) -> $retained"; |
| 59 base.removeWhere(test); |
| 60 for (var value in base) { |
| 61 Expect.isFalse(test(value), "$name: Found $value"); |
| 62 } |
| 63 for (var value in retained) { |
| 64 Expect.isTrue(base.contains(value), "$name: Found $value"); |
| 65 } |
| 66 } |
| 67 |
| 68 testRetainWhere(base, bool test(value)) { |
| 69 Set retained = new Set(); |
| 70 for (var element in base) { |
| 71 if (test(element)) { |
| 72 retained.add(element); |
| 73 } |
| 74 } |
| 75 String name = "$base.retainWhere(...) -> $retained"; |
| 76 base.retainWhere(test); |
| 77 for (var value in base) { |
| 78 Expect.isTrue(test(value), "$name: Found $value"); |
| 79 } |
| 80 for (var value in retained) { |
| 81 Expect.isTrue(base.contains(value), "$name: Found $value"); |
| 82 } |
| 83 } |
| 84 |
| 85 void main() { |
| 86 var collections = [ |
| 87 [], [1], [2], [1, 2], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 88 [1, 3, 5, 7, 9], [2, 4, 6, 8, 10] |
| 89 ]; |
| 90 for (var base in collections) { |
| 91 for (var delta in collections) { |
| 92 testRemove(base.toList()); |
| 93 testRemove(base.toSet()); |
| 94 |
| 95 var deltaSet = delta.toSet(); |
| 96 testRemoveWhere(base.toList(), deltaSet.contains); |
| 97 testRetainWhere(base.toList(), |
| 98 (e) => !deltaSet.contains(e)); |
| 99 |
| 100 testRemoveAll(base.toSet(), delta); |
| 101 testRemoveAll(base.toSet(), deltaSet); |
| 102 testRetainAll(base.toSet(), delta); |
| 103 testRetainAll(base.toSet(), deltaSet); |
| 104 testRemoveWhere(base.toSet(), deltaSet.contains); |
| 105 testRetainWhere(base.toSet(), (e) => !deltaSet.contains(e)); |
| 106 |
| 107 // Test the ListBase class's List implementation. |
| 108 testRemoveWhere(new MyList(base.toList()), deltaSet.contains); |
| 109 testRetainWhere(new MyList(base.toList()), |
| 110 (e) => !deltaSet.contains(e)); |
| 111 |
| 112 } |
| 113 } |
| 114 } |
| 115 |
| 116 class MyList<E> extends ListBase<E> { |
| 117 List<E> _source; |
| 118 MyList(this._source); |
| 119 int get length => _source.length; |
| 120 void set length(int length) { _source.length = length; } |
| 121 E operator[](int index) => _source[index]; |
| 122 void operator[]=(int index, E value) { _source[index] = value; } |
| 123 } |
OLD | NEW |