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 fs = []; | 8 var fs = []; |
9 for (var x = 1; x <= 3; x++) { | 9 for (var x = 1; x <= 3; x++) { |
10 fs.add(() { return x; }); | 10 fs.add(() { |
| 11 return x; |
| 12 }); |
11 } | 13 } |
12 Expect.equals(3, fs.length); | 14 Expect.equals(3, fs.length); |
13 Expect.equals(1, fs[0]()); | 15 Expect.equals(1, fs[0]()); |
14 Expect.equals(2, fs[1]()); | 16 Expect.equals(2, fs[1]()); |
15 Expect.equals(3, fs[2]()); | 17 Expect.equals(3, fs[2]()); |
16 } | 18 } |
17 | 19 |
18 closure1() { | 20 closure1() { |
19 var fs = []; | 21 var fs = []; |
20 for (var x = 0; x < 6; x++) { | 22 for (var x = 0; x < 6; x++) { |
21 fs.add(() { return x; }); | 23 fs.add(() { |
| 24 return x; |
| 25 }); |
22 x++; | 26 x++; |
23 } | 27 } |
24 Expect.equals(3, fs.length); | 28 Expect.equals(3, fs.length); |
25 Expect.equals(1, fs[0]()); | 29 Expect.equals(1, fs[0]()); |
26 Expect.equals(3, fs[1]()); | 30 Expect.equals(3, fs[1]()); |
27 Expect.equals(5, fs[2]()); | 31 Expect.equals(5, fs[2]()); |
28 } | 32 } |
29 | 33 |
30 closure2() { | 34 closure2() { |
31 var input = [1, 2, 3]; | 35 var input = [1, 2, 3]; |
32 var fs = []; | 36 var fs = []; |
33 for (var i = 0; i < input.length; i++) { | 37 for (var i = 0; i < input.length; i++) { |
34 fs.add(() { return input[i]; }); | 38 fs.add(() { |
| 39 return input[i]; |
| 40 }); |
35 } | 41 } |
36 Expect.equals(3, fs.length); | 42 Expect.equals(3, fs.length); |
37 Expect.equals(1, fs[0]()); | 43 Expect.equals(1, fs[0]()); |
38 Expect.equals(2, fs[1]()); | 44 Expect.equals(2, fs[1]()); |
39 Expect.equals(3, fs[2]()); | 45 Expect.equals(3, fs[2]()); |
40 } | 46 } |
41 | 47 |
42 closure3() { | 48 closure3() { |
43 var fs = []; | 49 var fs = []; |
44 for (var i = 0; | 50 for (var i = 0; |
45 i < 3; | 51 i < 3; |
46 (() { | 52 (() { |
47 fs.add(() => i); | 53 fs.add(() => i); |
48 i++; | 54 i++; |
49 })()) { | 55 })()) { |
50 i++; | 56 i++; |
51 } | 57 } |
52 Expect.equals(2, fs.length); | 58 Expect.equals(2, fs.length); |
53 Expect.equals(3, fs[0]()); | 59 Expect.equals(3, fs[0]()); |
54 Expect.equals(4, fs[1]()); | 60 Expect.equals(4, fs[1]()); |
55 } | 61 } |
56 | 62 |
57 closure4() { | 63 closure4() { |
58 var g; | 64 var g; |
59 for (var i = 0; | 65 for (var i = 0; |
60 (() { | 66 (() { |
61 g = () => i; | 67 g = () => i; |
62 return false; | 68 return false; |
63 })(); | 69 })(); |
64 i++){ | 70 i++) { |
65 Expect.equals(false, true); | 71 Expect.equals(false, true); |
66 } | 72 } |
67 Expect.equals(0, g()); | 73 Expect.equals(0, g()); |
68 } | 74 } |
69 | 75 |
70 main() { | 76 main() { |
71 closure0(); | 77 closure0(); |
72 closure1(); | 78 closure1(); |
73 closure2(); | 79 closure2(); |
74 closure3(); | 80 closure3(); |
75 closure4(); | 81 closure4(); |
76 } | 82 } |
OLD | NEW |