Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Unified Diff: tests/corelib/iterable_to_list_test.dart

Issue 1999793002: Make Iterable.toList more efficient if the length is known. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Avoid iterator for empty list Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/corelib/corelib.status ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/corelib/iterable_to_list_test.dart
diff --git a/tests/corelib/iterable_to_list_test.dart b/tests/corelib/iterable_to_list_test.dart
index 3ee443a0a7895a57d03ca1e4b8c23490cadbfd7b..2257d8fce3258a0d9b95610c9bf2da7e16b6bdcc 100644
--- a/tests/corelib/iterable_to_list_test.dart
+++ b/tests/corelib/iterable_to_list_test.dart
@@ -2,70 +2,70 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+import "dart:collection" show Queue;
+import "dart:typed_data" show Uint8List, Float32List;
import "package:expect/expect.dart";
-dynamicCheck(input, {isInt, isString}) {
- var copy = input.toList();
- Expect.isTrue(isInt == copy is List<int>);
- Expect.isTrue(isString == copy is List<String>);
-}
-
main() {
- List<int> list1 = <int>[1, 2, 3];
- List<int> list2 = const <int>[4, 5];
- List<String> list3 = <String>[];
- Set<int> set1 = new Set<int>();
- set1..add(11)
- ..add(12)
- ..add(13);
- Set<String> set2 = new Set<String>();
- set2..add("foo")
- ..add("bar")
- ..add("toto");
- Set set3 = new Set();
-
- var listCopy = list1.toList();
- Expect.listEquals(list1, listCopy);
- Expect.isTrue(listCopy is List<int>);
- Expect.isFalse(listCopy is List<String>);
- Expect.isFalse(identical(list1, listCopy));
- dynamicCheck(list1, isInt: true, isString: false);
-
- listCopy = list2.toList();
- Expect.listEquals(list2, listCopy);
- Expect.isTrue(listCopy is List<int>);
- Expect.isFalse(listCopy is List<String>);
- Expect.isFalse(identical(list2, listCopy));
- dynamicCheck(list2, isInt: true, isString: false);
-
- listCopy = list3.toList();
- Expect.listEquals(list3, listCopy);
- Expect.isTrue(listCopy is List<String>);
- Expect.isFalse(listCopy is List<int>);
- Expect.isFalse(identical(list3, listCopy));
- dynamicCheck(list3, isInt: false, isString: true);
-
- listCopy = set1.toList();
- Expect.equals(3, listCopy.length);
- Expect.isTrue(listCopy.contains(11));
- Expect.isTrue(listCopy.contains(12));
- Expect.isTrue(listCopy.contains(13));
- Expect.isTrue(listCopy is List<int>);
- Expect.isFalse(listCopy is List<String>);
- dynamicCheck(set1, isInt: true, isString: false);
+ // testIterable takes an iterable and a list expected to be equal to
+ // the iterable's toList result, including the type parameter of the list.
+ testIterable([], []);
+ testIterable(<int>[], <int>[]);
+ testIterable(<String>[], <String>[]);
+ testIterable([1, 2, 3], [1, 2, 3]);
+ testIterable(<int>[1, 2, 3], <int>[1, 2, 3]);
+ testIterable(const [1, 2], [1, 2]);
+ testIterable(const <int>[1, 2], <int>[1, 2]);
+ testIterable({"x": 1, "y": 1}.keys, ["x", "y"]);
+ testIterable(<String, int>{"x": 1, "y": 1}.keys, <String>["x", "y"]);
+ testIterable({"x": 2, "y": 3}.values, [2, 3]);
+ testIterable(<String, int>{"x": 2, "y": 3}.values, <int>[2, 3]);
+ testIterable(new Iterable.generate(3), [0, 1, 2]);
+ testIterable(new Iterable<int>.generate(3), <int>[0, 1, 2]);
+ testIterable(new Iterable<String>.generate(3, (x)=>"$x"),
+ <String>["0", "1", "2"]);
+ testIterable(new Set.from([1, 2, 3]), [1, 2, 3]);
+ testIterable(new Set<int>.from([1, 2, 3]), <int>[1, 2, 3]);
+ testIterable(new Queue.from([1, 2, 3]), [1, 2, 3]);
+ testIterable(new Queue<int>.from(<int>[1, 2, 3]), <int>[1, 2, 3]);
+ testIterable(new Uint8List.fromList(<int>[1, 2, 3]), /// 01: ok
+ <int>[1, 2, 3]); /// 01: continued
+ testIterable(new Float32List.fromList([1.0, 2.0, 3.0]), /// 01: continued
+ <double>[1.0, 2.0, 3.0]); /// 01: continued
+ testIterable("abc".codeUnits, <int>[97, 98, 99]); /// 01: continued
+ testIterable("abc".runes, <int>[97, 98, 99]);
+}
- listCopy = set2.toList();
- Expect.equals(3, listCopy.length);
- Expect.isTrue(listCopy.contains("foo"));
- Expect.isTrue(listCopy.contains("bar"));
- Expect.isTrue(listCopy.contains("toto"));
- Expect.isTrue(listCopy is List<String>);
- Expect.isFalse(listCopy is List<int>);
- dynamicCheck(set2, isInt: false, isString: true);
+testIterable(Iterable iterable, List expected, [int depth = 0]) {
+ print(" " * depth + "${iterable.runtimeType} vs ${expected.runtimeType}");
+ test(iterable, expected);
+ test(iterable, expected, growable: true);
+ test(iterable, expected, growable: false);
+ if (depth < 2) {
+ depth++;
+ testIterable(iterable.map((x) => x), new List.from(expected), depth);
+ testIterable(iterable.where((x) => true), expected, depth);
+ testIterable(iterable.expand((x) => [x]), new List.from(expected), depth);
+ testIterable(iterable.map((x) => x), new List.from(expected), depth);
+ testIterable(iterable.skipWhile((x) => false), expected, depth);
+ testIterable(iterable.takeWhile((x) => true), expected, depth);
+ testIterable(iterable.skip(0), expected, depth);
+ testIterable(iterable.take(expected.length * 2), expected, depth);
+ testIterable(iterable.toSet(), expected, depth);
+ }
+}
- listCopy = set3.toList();
- Expect.isTrue(listCopy.isEmpty);
- Expect.isTrue(listCopy is List<int>);
- Expect.isTrue(listCopy is List<String>);
- dynamicCheck(set3, isInt: true, isString: true);
+test(Iterable iterable, List expected, { bool growable: true}) {
+ var list = iterable.toList(growable: growable);
+ Expect.listEquals(expected, list);
+ Expect.equals(expected is List<int>, list is List<int>, "int");
+ Expect.equals(expected is List<double>, list is List<double>, "double");
+ Expect.equals(expected is List<String>, list is List<String>, "str");
+ if (growable) {
+ int length = list.length;
+ list.add(null);
+ Expect.equals(length + 1, list.length);
+ } else {
+ Expect.throws(() { list.add(null); });
+ }
}
« no previous file with comments | « tests/corelib/corelib.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698