8fd6d0aafd
Committed: https://code.google.com/p/dart/source/detail?r=19755 Reverted: http://code.google.com/p/dart/source/detail?r=19756 Review URL: https://codereview.chromium.org//12212016 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@20996 260f80e4-7a28-3924-810f-c04153c831b5
72 lines
1.8 KiB
Dart
72 lines
1.8 KiB
Dart
// Copyright (c) 2011, 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.
|
|
// Dart test program for testing for statement which captures loop variable.
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
var f;
|
|
|
|
main() {
|
|
// Capture the loop variable, ensure we capture the right value.
|
|
for (int i = 0; i < 10; i++) {
|
|
if (i == 7) {
|
|
f = () => "i = $i";
|
|
}
|
|
}
|
|
Expect.equals("i = 7", f());
|
|
|
|
// There is only one instance of k. The captured variable continues
|
|
// to change.
|
|
int k;
|
|
for (k = 0; k < 10; k++) {
|
|
if (k == 7) {
|
|
f = () => "k = $k";
|
|
}
|
|
}
|
|
Expect.equals("k = 10", f());
|
|
|
|
// l gets modified after it's captured. n++ is executed on the
|
|
// newly introduced instance of n (i.e. the instance of the loop
|
|
// iteration after the value is captured).
|
|
for (int n = 0; n < 10; n++) {
|
|
var l = n;
|
|
if (l == 7) {
|
|
f = () => "l = $l, n = $n";
|
|
}
|
|
l++;
|
|
}
|
|
Expect.equals("l = 8, n = 7", f());
|
|
|
|
// Loop variable is incremented in loop body instead of the increment.
|
|
// expression. Thus, the captured value is incremented by one before
|
|
// a new loop variable instance is created.
|
|
for (int i = 0; i < 10; /*nothing*/) {
|
|
if (i == 7) {
|
|
f = () => "i = $i";
|
|
}
|
|
i++;
|
|
}
|
|
Expect.equals("i = 8", f());
|
|
|
|
// Make sure continue still ensures the loop variable is captured correctly.
|
|
for (int i = 0; i < 10; i++) {
|
|
if (i == 7) {
|
|
f = () => "i = $i";
|
|
}
|
|
continue;
|
|
i++;
|
|
}
|
|
Expect.equals("i = 7", f());
|
|
|
|
// Nested loops with captured variables.
|
|
for (int k = 0; k < 5; k++) {
|
|
for (int i = 0; i < 10; i++) {
|
|
if (k == 3 && i == 7) {
|
|
f = () => "k = $k, i = $i";
|
|
}
|
|
}
|
|
}
|
|
Expect.equals("k = 3, i = 7", f());
|
|
}
|