Chromium Code Reviews| 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(n); | 19 var a = new List(n); |
| 20 var b = new List(n); | 20 var b = new List(n); |
| 21 for (int i = 0; i < n; i++) { | 21 int i; |
| 22 for (i = 0; i < n; i++) { | |
| 22 var j = i; | 23 var j = i; |
| 23 a[i] = () => i; // Captured i is always n. | 24 a[i] = () => i; // Captured i is always n. |
| 24 b[i] = () => j; // Captured j varies from 0 to n-1. | 25 b[i] = () => j; // Captured j varies from 0 to n-1. |
| 25 } | 26 } |
| 26 var a_sum = 0; | 27 var a_sum = 0; |
| 27 var b_sum = 0; | 28 var b_sum = 0; |
| 28 for (int i = 0; i < n; i++) { | 29 for (int i = 0; i < n; i++) { |
| 29 a_sum += a[i](); | 30 a_sum += a[i](); |
| 30 b_sum += b[i](); | 31 b_sum += b[i](); |
| 31 } | 32 } |
| 32 return a_sum + b_sum; | 33 return a_sum + b_sum; |
| 33 } | 34 } |
| 34 return k(n); | 35 return k(n); |
| 35 } | 36 } |
| 37 static int h2(int n) { | |
| 38 k(int n) { | |
| 39 var a = new List(n); | |
| 40 var b = new List(n); | |
| 41 for (int i = 0; i < n; i++) { | |
| 42 var j = i; | |
| 43 a[i] = () => i; // Captured i varies from 0 to n-1. | |
| 44 b[i] = () => j; // Captured j varies from 0 to n-1. | |
| 45 } | |
| 46 var a_sum = 0; | |
| 47 var b_sum = 0; | |
| 48 for (int i = 0; i < n; i++) { | |
| 49 a_sum += a[i](); | |
| 50 b_sum += b[i](); | |
| 51 } | |
| 52 return a_sum + b_sum; | |
| 53 } | |
| 54 return k(n); | |
| 55 } | |
| 36 int field1; | 56 int field1; |
| 37 int field2_; | 57 int field2_; |
| 38 int get field2() { return field2_; } | 58 int get field2() { return field2_; } |
| 39 void set field2(int value) { field2_ = value; } | 59 void set field2(int value) { field2_ = value; } |
| 40 | 60 |
| 41 int method(int n) { | 61 int method(int n) { |
| 42 incField1() { field1++; } | 62 incField1() { field1++; } |
| 43 incField2() { field2++; } | 63 incField2() { field2++; } |
| 44 for (int i = 0; i < n; i++) { | 64 for (int i = 0; i < n; i++) { |
| 45 incField1(); | 65 incField1(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 | 135 |
| 116 static testExceptions() { | 136 static testExceptions() { |
| 117 var f = (int n) => n + 1; | 137 var f = (int n) => n + 1; |
| 118 Expect.equals(2, f(1)); | 138 Expect.equals(2, f(1)); |
| 119 Expect.equals(true, f is Function); | 139 Expect.equals(true, f is Function); |
| 120 Expect.equals(true, f is Object); | 140 Expect.equals(true, f is Object); |
| 121 Expect.equals("Closure", f.toString()); | 141 Expect.equals("Closure", f.toString()); |
| 122 bool exception_caught = false; | 142 bool exception_caught = false; |
| 123 try { | 143 try { |
| 124 f(1, 2); | 144 f(1, 2); |
| 125 } catch (ClosureArgumentMismatchException e) { | 145 } catch (ClosureArgumentMismatchException e) { |
|
ahe
2012/03/29 06:59:47
This exception is not specified in the language sp
| |
| 126 exception_caught = true; | 146 exception_caught = true; |
| 127 } | 147 } |
| 128 Expect.equals(true, exception_caught); | 148 Expect.equals(true, exception_caught); |
| 129 exception_caught = false; | 149 exception_caught = false; |
| 130 try { | 150 try { |
| 131 f(); | 151 f(); |
| 132 } catch (ClosureArgumentMismatchException e) { | 152 } catch (ClosureArgumentMismatchException e) { |
|
ahe
2012/03/29 06:59:47
Ditto.
| |
| 133 exception_caught = true; | 153 exception_caught = true; |
| 134 } | 154 } |
| 135 Expect.equals(true, exception_caught); | 155 Expect.equals(true, exception_caught); |
| 136 exception_caught = false; | 156 exception_caught = false; |
| 137 try { | 157 try { |
| 138 f.xyz(0); | 158 f.xyz(0); |
| 139 } catch (NoSuchMethodException e) { | 159 } catch (NoSuchMethodException e) { |
| 140 exception_caught = true; | 160 exception_caught = true; |
| 141 } | 161 } |
| 142 Expect.equals(true, exception_caught); | 162 Expect.equals(true, exception_caught); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 157 Expect.equals(3, g(2)); | 177 Expect.equals(3, g(2)); |
| 158 } | 178 } |
| 159 | 179 |
| 160 static int doThis(int n, int f(int n)) { | 180 static int doThis(int n, int f(int n)) { |
| 161 return f(n); | 181 return f(n); |
| 162 } | 182 } |
| 163 | 183 |
| 164 static testMain() { | 184 static testMain() { |
| 165 Expect.equals(2*(3*2 + 2 + 1), f(2)); | 185 Expect.equals(2*(3*2 + 2 + 1), f(2)); |
| 166 Expect.equals(10*10 + 10*9/2, h(10)); | 186 Expect.equals(10*10 + 10*9/2, h(10)); |
| 187 Expect.equals(90, h2(10)); | |
| 167 Expect.equals(320, new LocalFunctionTest().method(10)); | 188 Expect.equals(320, new LocalFunctionTest().method(10)); |
| 168 Expect.equals(145, new LocalFunctionTest().testExecute(10)); | 189 Expect.equals(145, new LocalFunctionTest().testExecute(10)); |
| 169 Expect.equals(5, testSelfReference1(5)); | 190 Expect.equals(5, testSelfReference1(5)); |
| 170 Expect.equals(5, testSelfReference3(5)); | 191 Expect.equals(5, testSelfReference3(5)); |
| 171 Expect.equals(24*25/2, testNesting(5)); | 192 Expect.equals(24*25/2, testNesting(5)); |
| 172 Expect.equals(true, testClosureCallStatement(7)); | 193 Expect.equals(true, testClosureCallStatement(7)); |
| 173 Expect.equals(99, doThis(10, int _(n) => n * n - 1)); | 194 Expect.equals(99, doThis(10, int _(n) => n * n - 1)); |
| 174 Expect.equals(99, doThis(10, int f(n) => n * n - 1)); | 195 Expect.equals(99, doThis(10, int f(n) => n * n - 1)); |
| 175 Expect.equals(99, doThis(10, (n) => n * n - 1)); | 196 Expect.equals(99, doThis(10, (n) => n * n - 1)); |
| 176 Expect.equals(99, doThis(10, f(n) => n * n - 1)); | 197 Expect.equals(99, doThis(10, f(n) => n * n - 1)); |
| 177 testExceptions(); | 198 testExceptions(); |
| 178 } | 199 } |
| 179 } | 200 } |
| 180 | 201 |
| 181 main() { | 202 main() { |
| 182 LocalFunctionTest.testMain(); | 203 LocalFunctionTest.testMain(); |
| 183 } | 204 } |
| OLD | NEW |