OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 |
5 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
6 | 7 |
7 main() { | 8 main() { |
8 bool checkedMode = false; | 9 bool checkedMode = false; |
9 assert((checkedMode = true)); | 10 assert((checkedMode = true)); |
10 void test(expectedList, generatedIterable) { | 11 void test(expectedList, generatedIterable) { |
11 Expect.equals(expectedList.length, generatedIterable.length); | 12 Expect.equals(expectedList.length, generatedIterable.length); |
12 Expect.listEquals(expectedList, generatedIterable.toList()); | 13 Expect.listEquals(expectedList, generatedIterable.toList()); |
13 } | 14 } |
14 | |
15 test([], new Iterable.generate(0)); | 15 test([], new Iterable.generate(0)); |
16 test([0], new Iterable.generate(1)); | 16 test([0], new Iterable.generate(1)); |
17 test([0, 1, 2, 3, 4], new Iterable.generate(5)); | 17 test([0,1,2,3,4], new Iterable.generate(5)); |
18 test(["0", "1", "2", "3", "4"], new Iterable.generate(5, (x) => "$x")); | 18 test(["0","1","2","3","4"], new Iterable.generate(5, (x) => "$x")); |
19 test([2, 3, 4, 5, 6], new Iterable.generate(7).skip(2)); | 19 test([2,3,4,5,6], new Iterable.generate(7).skip(2)); |
20 test([0, 1, 2, 3, 4], new Iterable.generate(7).take(5)); | 20 test([0,1,2,3,4], new Iterable.generate(7).take(5)); |
21 test([], new Iterable.generate(5).skip(6)); | 21 test([], new Iterable.generate(5).skip(6)); |
22 test([], new Iterable.generate(5).take(0)); | 22 test([], new Iterable.generate(5).take(0)); |
23 test([], new Iterable.generate(5).take(3).skip(3)); | 23 test([], new Iterable.generate(5).take(3).skip(3)); |
24 test([], new Iterable.generate(5).skip(6).take(0)); | 24 test([], new Iterable.generate(5).skip(6).take(0)); |
25 | 25 |
26 // Test types. | 26 // Test types. |
27 | 27 |
28 Iterable<int> it = new Iterable<int>.generate(5); | 28 Iterable<int> it = new Iterable<int>.generate(5); |
29 Expect.isTrue(it is Iterable<int>); | 29 Expect.isTrue(it is Iterable<int>); |
30 Expect.isTrue(it.iterator is Iterator<int>); | 30 Expect.isTrue(it.iterator is Iterator<int>); |
31 Expect.isTrue(it is! Iterable<String>); | 31 Expect.isTrue(it is! Iterable<String>); |
32 Expect.isTrue(it.iterator is! Iterator<String>); | 32 Expect.isTrue(it.iterator is! Iterator<String>); |
33 test([0, 1, 2, 3, 4], it); | 33 test([0,1,2,3,4], it); |
34 | 34 |
35 Iterable<String> st = new Iterable<String>.generate(5, (x) => "$x"); | 35 Iterable<String> st = new Iterable<String>.generate(5, (x) => "$x"); |
36 Expect.isTrue(st is Iterable<String>); | 36 Expect.isTrue(st is Iterable<String>); |
37 Expect.isTrue(st.iterator is Iterator<String>); | 37 Expect.isTrue(st.iterator is Iterator<String>); |
38 Expect.isFalse(st is Iterable<int>); | 38 Expect.isFalse(st is Iterable<int>); |
39 Expect.isFalse(st.iterator is Iterator<int>); | 39 Expect.isFalse(st.iterator is Iterator<int>); |
40 test(["0", "1", "2", "3", "4"], st); | 40 test(["0","1","2","3","4"], st); |
41 | 41 |
42 if (checkedMode) { | 42 if (checkedMode) { |
43 Expect.throws(() => new Iterable<String>.generate(5)); | 43 Expect.throws(() => new Iterable<String>.generate(5)); |
44 } | 44 } |
45 | 45 |
46 // Omitted generator function means `(int x) => x`, and the type parameters | 46 // Omitted generator function means `(int x) => x`, and the type parameters |
47 // must then be compatible with `int`. | 47 // must then be compatible with `int`. |
48 // Check that we catch invalid type parameters. | 48 // Check that we catch invalid type parameters. |
49 | 49 |
50 // Valid types: | 50 // Valid types: |
(...skipping 11 matching lines...) Expand all Loading... |
62 if (checkedMode) { // //# 01:
ok | 62 if (checkedMode) { // //# 01:
ok |
63 Expect.throws(() => new Iterable<Null>.generate(5).elementAt(2)); //# 01: co
ntinued | 63 Expect.throws(() => new Iterable<Null>.generate(5).elementAt(2)); //# 01: co
ntinued |
64 } else { // //# 01:
continued | 64 } else { // //# 01:
continued |
65 Iterable<dynamic> iter5 = new Iterable<Null>.generate(5); // //# 01:
continued | 65 Iterable<dynamic> iter5 = new Iterable<Null>.generate(5); // //# 01:
continued |
66 Expect.equals(2, iter5.elementAt(2)); // //# 01:
continued | 66 Expect.equals(2, iter5.elementAt(2)); // //# 01:
continued |
67 } // //# 01:
continued | 67 } // //# 01:
continued |
68 Expect.throws(() => new Iterable<bool>.generate(5)); | 68 Expect.throws(() => new Iterable<bool>.generate(5)); |
69 | 69 |
70 // Regression: https://github.com/dart-lang/sdk/issues/26358 | 70 // Regression: https://github.com/dart-lang/sdk/issues/26358 |
71 var count = 0; | 71 var count = 0; |
72 var iter = new Iterable.generate(5, (v) { | 72 var iter = new Iterable.generate(5, (v) { count++; return v; }); |
73 count++; | |
74 return v; | |
75 }); | |
76 Expect.equals(0, count); | 73 Expect.equals(0, count); |
77 Expect.equals(2, iter.elementAt(2)); // Doesn't compute the earlier values. | 74 Expect.equals(2, iter.elementAt(2)); // Doesn't compute the earlier values. |
78 Expect.equals(1, count); | 75 Expect.equals(1, count); |
79 Expect.equals(2, iter.skip(2).first); // Doesn't compute the skipped values. | 76 Expect.equals(2, iter.skip(2).first); // Doesn't compute the skipped values. |
80 Expect.equals(2, count); | 77 Expect.equals(2, count); |
81 } | 78 } |
OLD | NEW |