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 // Test foreach (aka. for-in) functionality. | 8 // Test foreach (aka. for-in) functionality. |
9 | 9 |
10 testIterator(List expect, Iterable input) { | 10 testIterator(List expect, Iterable input) { |
(...skipping 19 matching lines...) Expand all Loading... |
30 final List<T> values; | 30 final List<T> values; |
31 MyIterable(List<T> values) : this.values = values; | 31 MyIterable(List<T> values) : this.values = values; |
32 Iterator get iterator { | 32 Iterator get iterator { |
33 return new MyListIterator(values); | 33 return new MyListIterator(values); |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 class MyListIterator<T> implements Iterator<T> { | 37 class MyListIterator<T> implements Iterator<T> { |
38 final List<T> values; | 38 final List<T> values; |
39 int index; | 39 int index; |
40 MyListIterator(List<T> values) : this.values = values, index = -1; | 40 MyListIterator(List<T> values) |
| 41 : this.values = values, |
| 42 index = -1; |
41 | 43 |
42 bool moveNext() => ++index < values.length; | 44 bool moveNext() => ++index < values.length; |
43 T get current => (0 <= index && index < values.length) ? values[index] : null; | 45 T get current => (0 <= index && index < values.length) ? values[index] : null; |
44 } | 46 } |
45 | 47 |
46 void main() { | 48 void main() { |
47 testIterator([], []); | 49 testIterator([], []); |
48 testIterator([], new MyIterable([])); | 50 testIterator([], new MyIterable([])); |
49 testIterator([1], [1]); | 51 testIterator([1], [1]); |
50 testIterator([1], new MyIterable([1])); | 52 testIterator([1], new MyIterable([1])); |
51 testIterator([1,2,3], [1,2,3]); | 53 testIterator([1, 2, 3], [1, 2, 3]); |
52 testIterator([1,2,3], new MyIterable([1,2,3])); | 54 testIterator([1, 2, 3], new MyIterable([1, 2, 3])); |
53 testIterator(["a","b","c"], ["a","b","c"]); | 55 testIterator(["a", "b", "c"], ["a", "b", "c"]); |
54 testIterator(["a","b","c"], new MyIterable(["a","b","c"])); | 56 testIterator(["a", "b", "c"], new MyIterable(["a", "b", "c"])); |
55 | 57 |
56 // Several nested for-in's. | 58 // Several nested for-in's. |
57 for (var x in [[["a"]]]) { | 59 for (var x in [ |
| 60 [ |
| 61 ["a"] |
| 62 ] |
| 63 ]) { |
58 for (var y in x) { | 64 for (var y in x) { |
59 for (var z in y) { | 65 for (var z in y) { |
60 Expect.equals("a", z); | 66 Expect.equals("a", z); |
61 } | 67 } |
62 } | 68 } |
63 } | 69 } |
64 | 70 |
65 // Simultaneous iteration of the same iterable. | 71 // Simultaneous iteration of the same iterable. |
66 for (var iterable in [[1,2,3], new MyIterable([1,2,3])]) { | 72 for (var iterable in [ |
| 73 [1, 2, 3], |
| 74 new MyIterable([1, 2, 3]) |
| 75 ]) { |
67 int result = 0; | 76 int result = 0; |
68 for (var x in iterable) { | 77 for (var x in iterable) { |
69 for (var y in iterable) { | 78 for (var y in iterable) { |
70 result += x * y; | 79 result += x * y; |
71 } | 80 } |
72 } | 81 } |
73 Expect.equals(36, result); | 82 Expect.equals(36, result); |
74 } | 83 } |
75 | 84 |
76 // Using the same variable (showing that the expression is evaluated | 85 // Using the same variable (showing that the expression is evaluated |
77 // in the outer scope). | 86 // in the outer scope). |
78 int result = 0; | 87 int result = 0; |
79 var x = [1,2,3]; | 88 var x = [1, 2, 3]; |
80 for (var x in x) { | 89 for (var x in x) { |
81 result += x; | 90 result += x; |
82 } | 91 } |
83 Expect.equals(6, result); | 92 Expect.equals(6, result); |
84 } | 93 } |
OLD | NEW |