blob: d59fb8f06f4124749efd0f59ed9b29b641bee655 [file] [log] [blame]
asgerf@google.come50fd282015-01-13 11:57:15 +00001// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5import "package:expect/expect.dart";
6
7run(callback) => callback();
8
9
10initializer() {
11 var closure;
12 for (var i=0, fn = () => i; i < 3; i++) {
13 i += 1;
14 closure = fn;
15 }
16 Expect.equals(1, closure());
17}
18
19condition() {
20 var closures = [];
21 check(callback) {
22 closures.add(callback);
23 return callback();
24 }
25 var values = [];
26 for (var i=0; check(() => ++i) < 8; ++i) {
27 values.add(i);
28 }
29 Expect.listEquals([1, 3, 5, 7], values);
30 Expect.listEquals([2, 4, 6, 8, 10], closures.map(run).toList());
31}
32
33body() {
34 var closures = [];
35 for (var i=0, j=0; i<3; i++) {
36 j++;
37 closures.add(() => i);
38 closures.add(() => j);
39 }
40 Expect.listEquals([0, 1, 1, 2, 2, 3], closures.map(run).toList());
41}
42
43update() {
44 var closures = [];
45 check(callback) {
46 closures.add(callback);
47 return callback();
48 }
49 var values = [];
50 for (var i=0; i < 4; check(() => ++i)) {
51 values.add(i);
52 }
53 Expect.listEquals([0, 1, 2, 3], values);
54 Expect.listEquals([2, 3, 4, 5], closures.map(run).toList());
55}
56
57initializer_condition() {
58 var values = [];
59 for (var i=0, fn = () => i; run(() => ++i) < 3; ) {
60 values.add(i);
61 values.add(fn());
62 }
63 Expect.listEquals([1, 1, 2, 1], values);
64}
65
66initializer_update() {
67 var update_closures = [];
68 update(callback) {
69 update_closures.add(callback);
70 return callback();
71 }
72 var init_closure;
73 for (var i=0, fn = () => i; i < 4; update(() => ++i)) {
74 init_closure = fn;
75 if (i == 0) {
76 ++i; // Mutate copy of 'i' from first iteration.
77 }
78 }
79 Expect.equals(1, init_closure());
80 Expect.listEquals([3, 4, 5], update_closures.map(run).toList());
81 Expect.equals(1, init_closure());
82}
83
84initializer_body() {
85 var closures = [];
86 for (var i=0, fn = () => i; i < 3; i++) {
87 closures.add(() => i);
88 closures.add(fn);
89 fn = () => i;
90 }
91 Expect.listEquals([0, 0, 1, 0, 2, 1], closures.map(run).toList());
92}
93
94condition_update() {
95 var cond_closures = [];
96 check(callback) {
97 cond_closures.add(callback);
98 return callback();
99 }
100 var update_closures = [];
101 update(callback) {
102 update_closures.add(callback);
103 return callback();
104 }
105 var values = [];
106 for (var i=0; check(() => i) < 4; update(() => ++i)) {
107 values.add(i);
108 }
109 Expect.listEquals([0, 1, 2, 3], values);
110
111 Expect.listEquals([0, 1, 2, 3, 4], cond_closures.map(run).toList());
112 Expect.listEquals( [2, 3, 4, 5], update_closures.map(run).toList());
113 Expect.listEquals([0, 2, 3, 4, 5], cond_closures.map(run).toList());
114}
115
116condition_body() {
117 var cond_closures = [];
118 check(callback) {
119 cond_closures.add(callback);
120 return callback();
121 }
122 var body_closures = [];
123 do_body(callback) {
124 body_closures.add(callback);
125 return callback();
126 }
127 for (var i=0; check(() => i) < 4; ++i) {
128 do_body(() => i);
129 }
130 Expect.listEquals([0, 1, 2, 3, 4], cond_closures.map(run).toList());
131 Expect.listEquals([0, 1, 2, 3], body_closures.map(run).toList());
132}
133
134initializer_condition_update() {
135 var init;
136 var cond_closures = [];
137 check(callback) {
138 cond_closures.add(callback);
139 return callback();
140 }
141 var update_closures = [];
142 update(callback) {
143 update_closures.add(callback);
144 return callback();
145 }
146 var values = [];
147 for (var i=0, fn = () => i; check(() => ++i) < 8; update(() => ++i)) {
148 init = fn;
149 values.add(i);
150 }
151 Expect.listEquals([1, 3, 5, 7], values);
152 Expect.equals(1, init());
153
154 Expect.listEquals([2, 4, 6, 8, 10], cond_closures.map(run).toList());
155 Expect.listEquals( [5, 7, 9, 11], update_closures.map(run).toList());
156}
157
158initializer_condition_body() {
159 var init;
160 var cond_closures = [];
161 check(callback) {
162 cond_closures.add(callback);
163 return callback();
164 }
165 var body_closures = [];
166 do_body(callback) {
167 body_closures.add(callback);
168 return callback();
169 }
170 var values = [];
171 for (var i=0, fn = () => i; check(() => ++i) < 8; ) {
172 init = fn;
173 do_body(() => ++i);
174 values.add(i);
175 }
176 Expect.listEquals([2, 4, 6, 8], values);
177 Expect.equals(2, init());
178
179 Expect.listEquals([3, 5, 7, 9, 10], cond_closures.map(run).toList());
180 Expect.listEquals([4, 6, 8, 10], body_closures.map(run).toList());
181}
182
183initializer_update_body() {
184 var init;
185 var update_closures = [];
186 update(callback) {
187 update_closures.add(callback);
188 return callback();
189 }
190 var body_closures = [];
191 do_body(callback) {
192 body_closures.add(callback);
193 return callback();
194 }
195 var values = [];
196 for (var i=0, fn = () => i; i < 8; update(() => ++i)) {
197 init = fn;
198 do_body(() => ++i);
199 values.add(i);
200 }
201 Expect.listEquals([1, 3, 5, 7], values);
202 Expect.equals(1, init());
203
204 Expect.listEquals( [4, 6, 8, 9], update_closures.map(run).toList());
205 Expect.listEquals([2, 5, 7, 9], body_closures.map(run).toList());
206}
207
208condition_update_body() {
209 var cond_closures = [];
210 check(callback) {
211 cond_closures.add(callback);
212 return callback();
213 }
214 var update_closures = [];
215 update(callback) {
216 update_closures.add(callback);
217 return callback();
218 }
219 var body_closures = [];
220 do_body(callback) {
221 body_closures.add(callback);
222 return callback();
223 }
224 var values = [];
225 for (var i=0; check(() => i) < 8; update(() => ++i)) {
226 do_body(() => ++i);
227 values.add(i);
228 }
229 Expect.listEquals([1, 3, 5, 7], values);
230
231 Expect.listEquals([1, 3, 5, 7, 8], cond_closures.map(run).toList());
232 Expect.listEquals( [4, 6, 8, 9], update_closures.map(run).toList());
233 Expect.listEquals([2, 5, 7, 9], body_closures.map(run).toList());
234 Expect.listEquals([2, 5, 7, 9, 9], cond_closures.map(run).toList());
235}
236
237initializer_condition_update_body() {
238 var init;
239 var cond_closures = [];
240 check(callback) {
241 cond_closures.add(callback);
242 return callback();
243 }
244 var update_closures = [];
245 update(callback) {
246 update_closures.add(callback);
247 return callback();
248 }
249 var body_closures = [];
250 do_body(callback) {
251 body_closures.add(callback);
252 return callback();
253 }
254 var values = [];
255 for (var i=0, fn = () => i; check(() => i) < 8; update(() => ++i)) {
256 init = fn;
257 do_body(() => ++i);
258 values.add(i);
259 }
260 Expect.listEquals([1, 3, 5, 7], values);
261 Expect.equals(1, init());
262
263 Expect.listEquals([1, 3, 5, 7, 8], cond_closures.map(run).toList());
264 Expect.listEquals( [4, 6, 8, 9], update_closures.map(run).toList());
265 Expect.listEquals([2, 5, 7, 9], body_closures.map(run).toList());
266 Expect.listEquals([2, 5, 7, 9, 9], cond_closures.map(run).toList());
267}
268
269main() {
270 initializer();
271 condition();
272 update();
273 body();
274 initializer_condition();
275 initializer_update();
276 initializer_body();
277 condition_update();
278 condition_body();
279 initializer_condition_update();
280 initializer_condition_body();
281 initializer_update_body();
282 condition_update_body();
283 initializer_condition_update_body();
284}