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