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 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 typedef T F<T>(T t); | 8 typedef T F<T>(T t); |
9 | 9 |
10 class Parameterized<T> { | 10 class Parameterized<T> { |
11 Parameterized() { } | 11 Parameterized() {} |
12 T mul3(F f, T t) { return 3*f(t); } | 12 T mul3(F f, T t) { |
| 13 return 3 * f(t); |
| 14 } |
| 15 |
13 T test(T t) { | 16 T test(T t) { |
14 return mul3((T t) { return 3*t; }, t); | 17 return mul3((T t) { |
| 18 return 3 * t; |
| 19 }, t); |
15 } | 20 } |
16 } | 21 } |
17 | 22 |
18 class LocalFunction2Test { | 23 class LocalFunction2Test { |
19 static int f(int n) { | 24 static int f(int n) { |
20 int a = 0; | 25 int a = 0; |
21 var g = (int n) { | 26 var g = (int n) { |
22 a += n; | 27 a += n; |
23 return a; | 28 return a; |
24 }; | 29 }; |
25 var h = (int n) { | 30 var h = (int n) { |
26 a += 10*n; | 31 a += 10 * n; |
27 return a; | 32 return a; |
28 }; | 33 }; |
29 return g(n) + h(n); | 34 return g(n) + h(n); |
30 } | 35 } |
31 | 36 |
32 static testMain() { | 37 static testMain() { |
33 Expect.equals(3 + 33, f(3)); | 38 Expect.equals(3 + 33, f(3)); |
34 Expect.equals(9.0, new Parameterized<double>().test(1.0)); | 39 Expect.equals(9.0, new Parameterized<double>().test(1.0)); |
35 } | 40 } |
36 } | 41 } |
37 | 42 |
38 main() { | 43 main() { |
39 LocalFunction2Test.testMain(); | 44 LocalFunction2Test.testMain(); |
40 } | 45 } |
OLD | NEW |