dart-sdk/tests/language/param2_test.dart
Jacob Richman 2dcd56ef43 Format all tests.
There are far too many files here to review everyone carefully.
Spot checking most of the diffs look good as test code is generally written
with less care than application code so lots of ugly formatting get through.
If people notice files where the automated formatting bothers them feel free
to comment indicating file names and I'll move spaces within comments to make
the formatting cleaner and use comments to force block formatting as I have
done for other case where formatting looked bad.

BUG=
R=efortuna@google.com

Review-Url: https://codereview.chromium.org/2771453003 .
2017-04-17 14:53:02 -07:00

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.
import "package:expect/expect.dart";
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, (e) => e == 6));
var isZero = (e) => e == 0;
Expect.equals(false, exists(v, isZero));
}
}
main() {
Param2Test.testMain();
}