OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // Dart test program for testing function type parameters. | |
5 | |
6 | |
7 class Param2Test { | |
8 | |
9 static forEach(List<int> a, int f(k)) { | |
10 for (int i = 0; i < a.length; i++) { | |
11 a[i] = f(a[i]); | |
12 } | |
13 } | |
14 | |
15 static int apply(f(int k), int arg) { | |
16 var res = f(arg); | |
17 return res; | |
18 } | |
19 | |
20 static exists(List<int> a, f(e)) { | |
21 for (int i = 0; i < a.length; i++) { | |
22 if (f(a[i])) return true; | |
23 } | |
24 return false; | |
25 } | |
26 | |
27 static testMain() { | |
28 int square(int x) { | |
29 return x * x; | |
30 } | |
31 Expect.equals(4, apply(square, 2)); | |
32 Expect.equals(100, apply(square, 10)); | |
33 | |
34 var v = [1, 2, 3, 4, 5, 6]; | |
35 forEach(v, square); | |
36 Expect.equals(1, v[0]); | |
37 Expect.equals(4, v[1]); | |
38 Expect.equals(9, v[2]); | |
39 Expect.equals(16, v[3]); | |
40 Expect.equals(25, v[4]); | |
41 Expect.equals(36, v[5]); | |
42 | |
43 isOdd(element) { | |
44 return element % 2 == 1; | |
45 } | |
46 | |
47 Expect.equals(true, exists([3, 5, 7, 11, 13], isOdd)); | |
48 Expect.equals(false, exists([2, 4, 10], isOdd)); | |
49 Expect.equals(false, exists([], isOdd)); | |
50 | |
51 v = [4, 5, 7]; | |
52 Expect.equals(true, exists(v, (e) => e % 2 == 1)); | |
53 Expect.equals(false, exists(v, f(e) => e == 6)); | |
54 | |
55 var isZero = (e) => e == 0; | |
56 Expect.equals(false, exists(v, isZero)); | |
57 } | |
58 } | |
59 | |
60 | |
61 main() { | |
62 Param2Test.testMain(); | |
63 } | |
OLD | NEW |