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