64d7bc5acf
Review URL: https://chromiumcodereview.appspot.com//10248007 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@7101 260f80e4-7a28-3924-810f-c04153c831b5
64 lines
1.5 KiB
Dart
64 lines
1.5 KiB
Dart
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
// Dart test program for testing function type parameters.
|
|
|
|
|
|
class Param2Test {
|
|
|
|
static forEach(List<int> a, int f(k)) {
|
|
for (int i = 0; i < a.length; i++) {
|
|
a[i] = f(a[i]);
|
|
}
|
|
}
|
|
|
|
static int apply(f(int k), int arg) {
|
|
var res = f(arg);
|
|
return res;
|
|
}
|
|
|
|
static exists(List<int> a, f(e)) {
|
|
for (int i = 0; i < a.length; i++) {
|
|
if (f(a[i])) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static testMain() {
|
|
int square(int x) {
|
|
return x * x;
|
|
}
|
|
Expect.equals(4, apply(square, 2));
|
|
Expect.equals(100, apply(square, 10));
|
|
|
|
var v = [1, 2, 3, 4, 5, 6];
|
|
forEach(v, square);
|
|
Expect.equals(1, v[0]);
|
|
Expect.equals(4, v[1]);
|
|
Expect.equals(9, v[2]);
|
|
Expect.equals(16, v[3]);
|
|
Expect.equals(25, v[4]);
|
|
Expect.equals(36, v[5]);
|
|
|
|
isOdd(element) {
|
|
return element % 2 == 1;
|
|
}
|
|
|
|
Expect.equals(true, exists([3, 5, 7, 11, 13], isOdd));
|
|
Expect.equals(false, exists([2, 4, 10], isOdd));
|
|
Expect.equals(false, exists([], isOdd));
|
|
|
|
v = [4, 5, 7];
|
|
Expect.equals(true, exists(v, (e) => e % 2 == 1));
|
|
Expect.equals(false, exists(v, f(e) => e == 6));
|
|
|
|
var isZero = (e) => e == 0;
|
|
Expect.equals(false, exists(v, isZero));
|
|
}
|
|
}
|
|
|
|
|
|
main() {
|
|
Param2Test.testMain();
|
|
}
|