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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « tests/corelib/corelib.status ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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" show Queue;
6 import "dart:typed_data" show Uint8List, Float32List;
5 import "package:expect/expect.dart"; 7 import "package:expect/expect.dart";
6 8
7 dynamicCheck(input, {isInt, isString}) { 9 main() {
8 var copy = input.toList(); 10 // testIterable takes an iterable and a list expected to be equal to
9 Expect.isTrue(isInt == copy is List<int>); 11 // the iterable's toList result, including the type parameter of the list.
10 Expect.isTrue(isString == copy is List<String>); 12 testIterable([], []);
13 testIterable(<int>[], <int>[]);
14 testIterable(<String>[], <String>[]);
15 testIterable([1, 2, 3], [1, 2, 3]);
16 testIterable(<int>[1, 2, 3], <int>[1, 2, 3]);
17 testIterable(const [1, 2], [1, 2]);
18 testIterable(const <int>[1, 2], <int>[1, 2]);
19 testIterable({"x": 1, "y": 1}.keys, ["x", "y"]);
20 testIterable(<String, int>{"x": 1, "y": 1}.keys, <String>["x", "y"]);
21 testIterable({"x": 2, "y": 3}.values, [2, 3]);
22 testIterable(<String, int>{"x": 2, "y": 3}.values, <int>[2, 3]);
23 testIterable(new Iterable.generate(3), [0, 1, 2]);
24 testIterable(new Iterable<int>.generate(3), <int>[0, 1, 2]);
25 testIterable(new Iterable<String>.generate(3, (x)=>"$x"),
26 <String>["0", "1", "2"]);
27 testIterable(new Set.from([1, 2, 3]), [1, 2, 3]);
28 testIterable(new Set<int>.from([1, 2, 3]), <int>[1, 2, 3]);
29 testIterable(new Queue.from([1, 2, 3]), [1, 2, 3]);
30 testIterable(new Queue<int>.from(<int>[1, 2, 3]), <int>[1, 2, 3]);
31 testIterable(new Uint8List.fromList(<int>[1, 2, 3]), /// 01: ok
32 <int>[1, 2, 3]); /// 01: continued
33 testIterable(new Float32List.fromList([1.0, 2.0, 3.0]), /// 01: continued
34 <double>[1.0, 2.0, 3.0]); /// 01: continued
35 testIterable("abc".codeUnits, <int>[97, 98, 99]); /// 01: continued
36 testIterable("abc".runes, <int>[97, 98, 99]);
11 } 37 }
12 38
13 main() { 39 testIterable(Iterable iterable, List expected, [int depth = 0]) {
14 List<int> list1 = <int>[1, 2, 3]; 40 print(" " * depth + "${iterable.runtimeType} vs ${expected.runtimeType}");
15 List<int> list2 = const <int>[4, 5]; 41 test(iterable, expected);
16 List<String> list3 = <String>[]; 42 test(iterable, expected, growable: true);
17 Set<int> set1 = new Set<int>(); 43 test(iterable, expected, growable: false);
18 set1..add(11) 44 if (depth < 2) {
19 ..add(12) 45 depth++;
20 ..add(13); 46 testIterable(iterable.map((x) => x), new List.from(expected), depth);
21 Set<String> set2 = new Set<String>(); 47 testIterable(iterable.where((x) => true), expected, depth);
22 set2..add("foo") 48 testIterable(iterable.expand((x) => [x]), new List.from(expected), depth);
23 ..add("bar") 49 testIterable(iterable.map((x) => x), new List.from(expected), depth);
24 ..add("toto"); 50 testIterable(iterable.skipWhile((x) => false), expected, depth);
25 Set set3 = new Set(); 51 testIterable(iterable.takeWhile((x) => true), expected, depth);
52 testIterable(iterable.skip(0), expected, depth);
53 testIterable(iterable.take(expected.length * 2), expected, depth);
54 testIterable(iterable.toSet(), expected, depth);
55 }
56 }
26 57
27 var listCopy = list1.toList(); 58 test(Iterable iterable, List expected, { bool growable: true}) {
28 Expect.listEquals(list1, listCopy); 59 var list = iterable.toList(growable: growable);
29 Expect.isTrue(listCopy is List<int>); 60 Expect.listEquals(expected, list);
30 Expect.isFalse(listCopy is List<String>); 61 Expect.equals(expected is List<int>, list is List<int>, "int");
31 Expect.isFalse(identical(list1, listCopy)); 62 Expect.equals(expected is List<double>, list is List<double>, "double");
32 dynamicCheck(list1, isInt: true, isString: false); 63 Expect.equals(expected is List<String>, list is List<String>, "str");
33 64 if (growable) {
34 listCopy = list2.toList(); 65 int length = list.length;
35 Expect.listEquals(list2, listCopy); 66 list.add(null);
36 Expect.isTrue(listCopy is List<int>); 67 Expect.equals(length + 1, list.length);
37 Expect.isFalse(listCopy is List<String>); 68 } else {
38 Expect.isFalse(identical(list2, listCopy)); 69 Expect.throws(() { list.add(null); });
39 dynamicCheck(list2, isInt: true, isString: false); 70 }
40
41 listCopy = list3.toList();
42 Expect.listEquals(list3, listCopy);
43 Expect.isTrue(listCopy is List<String>);
44 Expect.isFalse(listCopy is List<int>);
45 Expect.isFalse(identical(list3, listCopy));
46 dynamicCheck(list3, isInt: false, isString: true);
47
48 listCopy = set1.toList();
49 Expect.equals(3, listCopy.length);
50 Expect.isTrue(listCopy.contains(11));
51 Expect.isTrue(listCopy.contains(12));
52 Expect.isTrue(listCopy.contains(13));
53 Expect.isTrue(listCopy is List<int>);
54 Expect.isFalse(listCopy is List<String>);
55 dynamicCheck(set1, isInt: true, isString: false);
56
57 listCopy = set2.toList();
58 Expect.equals(3, listCopy.length);
59 Expect.isTrue(listCopy.contains("foo"));
60 Expect.isTrue(listCopy.contains("bar"));
61 Expect.isTrue(listCopy.contains("toto"));
62 Expect.isTrue(listCopy is List<String>);
63 Expect.isFalse(listCopy is List<int>);
64 dynamicCheck(set2, isInt: false, isString: true);
65
66 listCopy = set3.toList();
67 Expect.isTrue(listCopy.isEmpty);
68 Expect.isTrue(listCopy is List<int>);
69 Expect.isTrue(listCopy is List<String>);
70 dynamicCheck(set3, isInt: true, isString: true);
71 } 71 }
OLDNEW
« 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