Index: pkg/kernel/testcases/closures/loop2.dart |
diff --git a/pkg/kernel/testcases/closures/loop2.dart b/pkg/kernel/testcases/closures/loop2.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2069f03edff891f470fd199b8a65aa5556996fec |
--- /dev/null |
+++ b/pkg/kernel/testcases/closures/loop2.dart |
@@ -0,0 +1,30 @@ |
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+// |
+// Check that a variable declared and captured inside a loop is given a separate |
+// context for each iteration of the loop, so changes to the variable in |
+// subsequent iterations are not visible to closures capturing it in prior |
+// iterations. |
+ |
+void doit(int x) { |
+ final int max = 10; |
+ final double expectedSum = ((max - 1) * max) / 2; |
+ |
+ int counter = 0; |
+ var calls = []; |
+ while (counter < max) { |
+ int pos = counter; |
+ calls.add(() => pos + x); |
+ counter++; |
+ } |
+ |
+ double sum = 0.0; |
+ for (var c in calls) sum += c(); |
+ if (sum != expectedSum) |
+ throw new Exception("Unexpected sum = $sum != $expectedSum"); |
+} |
+ |
+void main() { |
+ doit(0); |
+} |