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