OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 | |
7 // Tests for closures sharing mutable bindings. | |
8 | |
9 var f; | |
10 var g; | |
11 | |
12 setupPlain() { | |
13 int j = 1000; | |
14 // Two closures sharing variable 'j'; j initially is 1000. | |
15 f = (int x) { | |
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 }; | |
25 } | |
26 | |
27 setupLoop() { | |
28 for (int i = 0; i < 2; i++) { | |
29 int j = i * 1000; // The last stored closure has j initially 1000. | |
30 // Two closures sharing variable 'j'. | |
31 f = (int x) { | |
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 }; | |
41 } | |
42 } | |
43 | |
44 setupNestedLoop() { | |
45 for (int outer = 0; outer < 2; outer++) { | |
46 int j = outer * 1000; | |
47 for (int i = 0; i < 2; i++) { | |
48 // Two closures sharing variable 'j' in a loop at different nesting. | |
49 f = (int x) { | |
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 }; | |
59 } | |
60 } | |
61 } | |
62 | |
63 test(setup) { | |
64 setup(); | |
65 Expect.equals(1000, f(100)); | |
66 Expect.equals(100, f(200)); | |
67 Expect.equals(200, f(300)); | |
68 Expect.equals(300, g(400)); | |
69 Expect.equals(400, g(500)); | |
70 } | |
71 | |
72 main() { | |
73 test(setupPlain); | |
74 test(setupLoop); | |
75 test(setupNestedLoop); | |
76 } | |
OLD | NEW |