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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 // Tests for closures sharing mutable bindings. | 7 // Tests for closures sharing mutable bindings. |
8 | 8 |
9 var f; | 9 var f; |
10 var g; | 10 var g; |
11 | 11 |
12 setupPlain() { | 12 setupPlain() { |
13 int j = 1000; | 13 int j = 1000; |
14 // Two closures sharing variable 'j'; j initially is 1000. | 14 // Two closures sharing variable 'j'; j initially is 1000. |
15 f = (int x) { var q = j; j = x; return q;}; | 15 f = (int x) { |
16 g = (int x) { var q = j; j = x; return q;}; | 16 var q = j; |
| 17 j = x; |
| 18 return q; |
| 19 }; |
| 20 g = (int x) { |
| 21 var q = j; |
| 22 j = x; |
| 23 return q; |
| 24 }; |
17 } | 25 } |
18 | 26 |
19 setupLoop() { | 27 setupLoop() { |
20 for (int i = 0; i < 2; i++) { | 28 for (int i = 0; i < 2; i++) { |
21 int j = i * 1000; // The last stored closure has j initially 1000. | 29 int j = i * 1000; // The last stored closure has j initially 1000. |
22 // Two closures sharing variable 'j'. | 30 // Two closures sharing variable 'j'. |
23 f = (int x) { var q = j; j = x; return q;}; | 31 f = (int x) { |
24 g = (int x) { var q = j; j = x; return q;}; | 32 var q = j; |
| 33 j = x; |
| 34 return q; |
| 35 }; |
| 36 g = (int x) { |
| 37 var q = j; |
| 38 j = x; |
| 39 return q; |
| 40 }; |
25 } | 41 } |
26 } | 42 } |
27 | 43 |
28 setupNestedLoop() { | 44 setupNestedLoop() { |
29 for (int outer = 0; outer < 2; outer++) { | 45 for (int outer = 0; outer < 2; outer++) { |
30 int j = outer * 1000; | 46 int j = outer * 1000; |
31 for (int i = 0; i < 2; i++) { | 47 for (int i = 0; i < 2; i++) { |
32 // Two closures sharing variable 'j' in a loop at different nesting. | 48 // Two closures sharing variable 'j' in a loop at different nesting. |
33 f = (int x) { var q = j; j = x; return q;}; | 49 f = (int x) { |
34 g = (int x) { var q = j; j = x; return q;}; | 50 var q = j; |
| 51 j = x; |
| 52 return q; |
| 53 }; |
| 54 g = (int x) { |
| 55 var q = j; |
| 56 j = x; |
| 57 return q; |
| 58 }; |
35 } | 59 } |
36 } | 60 } |
37 } | 61 } |
38 | 62 |
39 test(setup) { | 63 test(setup) { |
40 setup(); | 64 setup(); |
41 Expect.equals(1000, f(100)); | 65 Expect.equals(1000, f(100)); |
42 Expect.equals(100, f(200)); | 66 Expect.equals(100, f(200)); |
43 Expect.equals(200, f(300)); | 67 Expect.equals(200, f(300)); |
44 Expect.equals(300, g(400)); | 68 Expect.equals(300, g(400)); |
45 Expect.equals(400, g(500)); | 69 Expect.equals(400, g(500)); |
46 } | 70 } |
47 | 71 |
48 | |
49 main() { | 72 main() { |
50 test(setupPlain); | 73 test(setupPlain); |
51 test(setupLoop); | 74 test(setupLoop); |
52 test(setupNestedLoop); | 75 test(setupNestedLoop); |
53 } | 76 } |
OLD | NEW |