dart-sdk/tests/corelib_2/num_clamp_test.dart
Lasse R.H. Nielsen 0b58c4bd10 Change some constant declarations to lowerCase.
Retain the old values.

Reapply of https://dart-review.googlesource.com/c/sdk/+/20680 with fixes
for VM method fingerprints.

Change-Id: Ie14e7ccc3194d5561983348e6b6752728913ff4d
Reviewed-on: https://dart-review.googlesource.com/20664
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
2017-11-14 12:59:14 +00:00

68 lines
2.3 KiB
Dart

// Copyright (c) 2012, 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.
// Test num.clamp.
import "package:expect/expect.dart";
testIntClamp() {
Expect.equals(2, 2.clamp(1, 3));
Expect.equals(1, 0.clamp(1, 3));
Expect.equals(3, 4.clamp(1, 3));
Expect.equals(-2, (-2).clamp(-3, -1));
Expect.equals(-1, 0.clamp(-3, -1));
Expect.equals(-3, (-4).clamp(-3, -1));
Expect.equals(0, 1.clamp(0, 0));
Expect.equals(0, (-1).clamp(0, 0));
Expect.equals(0, 0.clamp(0, 0));
Expect.throwsArgumentError(() => 0.clamp(0, -1));
}
testDoubleClamp() {
Expect.equals(2.0, 2.clamp(1.0, 3.0));
Expect.equals(1.0, 0.clamp(1.0, 3.0));
Expect.equals(3.0, 4.clamp(1.0, 3.0));
Expect.equals(-2.0, (-2.0).clamp(-3.0, -1.0));
Expect.equals(-1.0, 0.0.clamp(-3.0, -1.0));
Expect.equals(-3.0, (-4.0).clamp(-3.0, -1.0));
Expect.equals(0.0, 1.0.clamp(0.0, 0.0));
Expect.equals(0.0, (-1.0).clamp(0.0, 0.0));
Expect.equals(0.0, 0.0.clamp(0.0, 0.0));
Expect.throwsArgumentError(() => 0.0.clamp(0.0, -1.0));
}
testDoubleClampInt() {
Expect.equals(2.0, 2.0.clamp(1, 3));
Expect.equals(1, 0.0.clamp(1, 3));
Expect.isTrue(0.0.clamp(1, 3) is int);
Expect.equals(3, 4.0.clamp(1, 3));
Expect.isTrue(4.0.clamp(1, 3) is int);
Expect.equals(-2.0, (-2.0).clamp(-3, -1));
Expect.equals(-1, 0.0.clamp(-3, -1));
Expect.isTrue(0.0.clamp(-3, -1) is int);
Expect.equals(-3, (-4.0).clamp(-3, -1));
Expect.isTrue((-4.0).clamp(-3, -1) is int);
Expect.equals(0, 1.0.clamp(0, 0));
Expect.isTrue(1.0.clamp(0, 0) is int);
Expect.equals(0, (-1.0).clamp(0, 0));
Expect.isTrue((-1.0).clamp(0, 0) is int);
Expect.equals(0.0, 0.0.clamp(0, 0));
Expect.isTrue(0.0.clamp(0, 0) is double);
Expect.throwsArgumentError(() => 0.0.clamp(0, -1));
}
testDoubleClampExtremes() {
Expect.equals(2.0, 2.0.clamp(-double.infinity, double.infinity));
Expect.equals(2.0, 2.0.clamp(-double.infinity, double.nan));
Expect.equals(double.infinity, 2.0.clamp(double.infinity, double.nan));
Expect.isTrue(2.0.clamp(double.nan, double.nan).isNaN);
Expect.throwsArgumentError(() => 0.0.clamp(double.nan, double.infinity));
}
main() {
testIntClamp();
testDoubleClamp();
testDoubleClampInt();
testDoubleClampExtremes();
}