| 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 // Dart test program testing closures. | 4 // Dart test program testing closures. | 
| 5 | 5 | 
| 6 class LocalFunctionTest { | 6 class LocalFunctionTest { | 
| 7   LocalFunctionTest() : field1 = 100, field2_ = 200  { } | 7   LocalFunctionTest() : field1 = 100, field2_ = 200  { } | 
| 8   static int f(int n) { | 8   static int f(int n) { | 
| 9     int a = 0; | 9     int a = 0; | 
| 10     g(int m) { | 10     g(int m) { | 
| 11       a = 3*n + m + 1;  // Capture parameter n and local a. | 11       a = 3*n + m + 1;  // Capture parameter n and local a. | 
| 12       return a; | 12       return a; | 
| 13     } | 13     } | 
| 14     var b = g(n); | 14     var b = g(n); | 
| 15     return a + b; | 15     return a + b; | 
| 16   } | 16   } | 
| 17   static int h(int n) { | 17   static int h(int n) { | 
| 18     k(int n) { | 18     k(int n) { | 
| 19       var a = new List.fixedLength(n); | 19       var a = new List(n); | 
| 20       var b = new List.fixedLength(n); | 20       var b = new List(n); | 
| 21       int i; | 21       int i; | 
| 22       for (i = 0; i < n; i++) { | 22       for (i = 0; i < n; i++) { | 
| 23         var j = i; | 23         var j = i; | 
| 24         a[i] = () => i;  // Captured i is always n. | 24         a[i] = () => i;  // Captured i is always n. | 
| 25         b[i] = () => j;  // Captured j varies from 0 to n-1. | 25         b[i] = () => j;  // Captured j varies from 0 to n-1. | 
| 26       } | 26       } | 
| 27       var a_sum = 0; | 27       var a_sum = 0; | 
| 28       var b_sum = 0; | 28       var b_sum = 0; | 
| 29       for (int i = 0; i < n; i++) { | 29       for (int i = 0; i < n; i++) { | 
| 30         a_sum += a[i](); | 30         a_sum += a[i](); | 
| 31         b_sum += b[i](); | 31         b_sum += b[i](); | 
| 32       } | 32       } | 
| 33       return a_sum + b_sum; | 33       return a_sum + b_sum; | 
| 34     } | 34     } | 
| 35     return k(n); | 35     return k(n); | 
| 36   } | 36   } | 
| 37   static int h2(int n) { | 37   static int h2(int n) { | 
| 38     k(int n) { | 38     k(int n) { | 
| 39       var a = new List.fixedLength(n); | 39       var a = new List(n); | 
| 40       var b = new List.fixedLength(n); | 40       var b = new List(n); | 
| 41       for (int i = 0; i < n; i++) { | 41       for (int i = 0; i < n; i++) { | 
| 42         var j = i; | 42         var j = i; | 
| 43         a[i] = () => i;  // Captured i varies from 0 to n-1. | 43         a[i] = () => i;  // Captured i varies from 0 to n-1. | 
| 44         b[i] = () => j;  // Captured j varies from 0 to n-1. | 44         b[i] = () => j;  // Captured j varies from 0 to n-1. | 
| 45       } | 45       } | 
| 46       var a_sum = 0; | 46       var a_sum = 0; | 
| 47       var b_sum = 0; | 47       var b_sum = 0; | 
| 48       for (int i = 0; i < n; i++) { | 48       for (int i = 0; i < n; i++) { | 
| 49         a_sum += a[i](); | 49         a_sum += a[i](); | 
| 50         b_sum += b[i](); | 50         b_sum += b[i](); | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 87       } else { | 87       } else { | 
| 88         return 1 + foo(n - 1);  // Local foo, not static foo. | 88         return 1 + foo(n - 1);  // Local foo, not static foo. | 
| 89       } | 89       } | 
| 90     }; | 90     }; | 
| 91     return foo(n);  // Local foo, not static foo. | 91     return foo(n);  // Local foo, not static foo. | 
| 92   } | 92   } | 
| 93   static void hep(Function f) { | 93   static void hep(Function f) { | 
| 94     f(); | 94     f(); | 
| 95   } | 95   } | 
| 96   static testNesting(int n) { | 96   static testNesting(int n) { | 
| 97     var a = new List.fixedLength(n*n); | 97     var a = new List(n*n); | 
| 98     f0() { | 98     f0() { | 
| 99       for (int i = 0; i < n; i++) { | 99       for (int i = 0; i < n; i++) { | 
| 100         int vi = i; | 100         int vi = i; | 
| 101         f1() { | 101         f1() { | 
| 102           for (int j = 0; j < n; j++) { | 102           for (int j = 0; j < n; j++) { | 
| 103             int vj = j; | 103             int vj = j; | 
| 104             a[i*n + j] = () => vi*n + vj; | 104             a[i*n + j] = () => vi*n + vj; | 
| 105           } | 105           } | 
| 106         } | 106         } | 
| 107         f1(); | 107         f1(); | 
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 183     Expect.equals(24*25/2, testNesting(5)); | 183     Expect.equals(24*25/2, testNesting(5)); | 
| 184     Expect.equals(true, testClosureCallStatement(7)); | 184     Expect.equals(true, testClosureCallStatement(7)); | 
| 185     Expect.equals(99, doThis(10, (n) => n * n - 1)); | 185     Expect.equals(99, doThis(10, (n) => n * n - 1)); | 
| 186     testExceptions(); | 186     testExceptions(); | 
| 187   } | 187   } | 
| 188 } | 188 } | 
| 189 | 189 | 
| 190 main() { | 190 main() { | 
| 191   LocalFunctionTest.testMain(); | 191   LocalFunctionTest.testMain(); | 
| 192 } | 192 } | 
| OLD | NEW | 
|---|