OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 closure0() { | |
6 var input = [1, 2, 3]; | |
7 var fs = []; | |
8 for (var x in input) { | |
9 fs.add(fun() { return x; }); | |
10 } | |
11 Expect.equals(3, fs.length); | |
12 Expect.equals(1, fs[0]()); | |
13 Expect.equals(2, fs[1]()); | |
14 Expect.equals(3, fs[2]()); | |
15 } | |
16 | |
17 closure1() { | |
18 var input = [1, 2, 3]; | |
19 var fs = []; | |
20 for (var x in input) { | |
21 fs.add(fun() { return x; }); | |
22 x++; | |
23 } | |
24 Expect.equals(3, fs.length); | |
25 Expect.equals(2, fs[0]()); | |
26 Expect.equals(3, fs[1]()); | |
27 Expect.equals(4, fs[2]()); | |
28 } | |
29 | |
30 closure2() { | |
31 var input = [1, 2, 3]; | |
32 var fs = []; | |
33 for (var i = 0; i < input.length; i++) { | |
34 var j = i; | |
35 fs.add(fun() { return input[j]; }); | |
36 } | |
37 Expect.equals(3, fs.length); | |
38 Expect.equals(1, fs[0]()); | |
39 Expect.equals(2, fs[1]()); | |
40 Expect.equals(3, fs[2]()); | |
41 } | |
42 | |
43 closure3() { | |
44 var input = [1, 2, 3]; | |
45 var fs = []; | |
46 for (var i = 0; i < input.length; i++) { | |
47 var x = input[i]; | |
48 fs.add(fun() { return x; }); | |
49 x++; | |
50 } | |
51 Expect.equals(3, fs.length); | |
52 Expect.equals(2, fs[0]()); | |
53 Expect.equals(3, fs[1]()); | |
54 Expect.equals(4, fs[2]()); | |
55 } | |
56 | |
57 closure4() { | |
58 var input = [1, 2, 3]; | |
59 var fs = []; | |
60 var x; | |
61 for (var i = 0; i < input.length; i++) { | |
62 x = input[i]; | |
63 fs.add(fun() { return x; }); | |
64 x++; | |
65 } | |
66 Expect.equals(3, fs.length); | |
67 Expect.equals(4, fs[0]()); | |
68 Expect.equals(4, fs[1]()); | |
69 Expect.equals(4, fs[2]()); | |
70 } | |
71 | |
72 closure5() { | |
73 var input = [1, 2, 3]; | |
74 var fs = []; | |
75 var i = 0; | |
76 do { | |
77 var x = input[i]; | |
78 fs.add(fun() { return x; }); | |
79 } while (++i < input.length); | |
80 Expect.equals(3, fs.length); | |
81 Expect.equals(1, fs[0]()); | |
82 Expect.equals(2, fs[1]()); | |
83 Expect.equals(3, fs[2]()); | |
84 } | |
85 | |
86 main() { | |
87 closure0(); | |
88 closure1(); | |
89 closure2(); | |
90 closure3(); | |
91 closure4(); | |
92 closure5(); | |
93 } | |
OLD | NEW |