OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, 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 // Test num.clamp. |
| 5 |
| 6 testIntClamp() { |
| 7 Expect.equals(2, 2.clamp(1, 3)); |
| 8 Expect.equals(1, 0.clamp(1, 3)); |
| 9 Expect.equals(3, 4.clamp(1, 3)); |
| 10 Expect.equals(-2, (-2).clamp(-3, -1)); |
| 11 Expect.equals(-1, 0.clamp(-3, -1)); |
| 12 Expect.equals(-3, (-4).clamp(-3, -1)); |
| 13 Expect.equals(0, 1.clamp(0, 0)); |
| 14 Expect.equals(0, (-1).clamp(0, 0)); |
| 15 Expect.equals(0, 0.clamp(0, 0)); |
| 16 Expect.throws(() => 0.clamp(0, -1), (e) => e is ArgumentError); |
| 17 Expect.throws(() => 0.clamp("str", -1), |
| 18 (e) => e is ArgumentError || e is TypeError); |
| 19 Expect.throws(() => 0.clamp(0, "2"), |
| 20 (e) => e is ArgumentError || e is TypeError); |
| 21 } |
| 22 |
| 23 testDoubleClamp() { |
| 24 Expect.equals(2.0, 2.clamp(1.0, 3.0)); |
| 25 Expect.equals(1.0, 0.clamp(1.0, 3.0)); |
| 26 Expect.equals(3.0, 4.clamp(1.0, 3.0)); |
| 27 Expect.equals(-2.0, (-2.0).clamp(-3.0, -1.0)); |
| 28 Expect.equals(-1.0, 0.0.clamp(-3.0, -1.0)); |
| 29 Expect.equals(-3.0, (-4.0).clamp(-3.0, -1.0)); |
| 30 Expect.equals(0.0, 1.0.clamp(0.0, 0.0)); |
| 31 Expect.equals(0.0, (-1.0).clamp(0.0, 0.0)); |
| 32 Expect.equals(0.0, 0.0.clamp(0.0, 0.0)); |
| 33 Expect.throws(() => 0.0.clamp(0.0, -1.0), (e) => e is ArgumentError); |
| 34 Expect.throws(() => 0.0.clamp("str", -1.0), |
| 35 (e) => e is ArgumentError || e is TypeError); |
| 36 Expect.throws(() => 0.0.clamp(0.0, "2"), |
| 37 (e) => e is ArgumentError || e is TypeError); |
| 38 } |
| 39 |
| 40 testDoubleClampInt() { |
| 41 Expect.equals(2.0, 2.0.clamp(1, 3)); |
| 42 Expect.equals(1, 0.0.clamp(1, 3)); |
| 43 Expect.isTrue(0.0.clamp(1, 3) is int); |
| 44 Expect.equals(3, 4.0.clamp(1, 3)); |
| 45 Expect.isTrue(4.0.clamp(1, 3) is int); |
| 46 Expect.equals(-2.0, (-2.0).clamp(-3, -1)); |
| 47 Expect.equals(-1, 0.0.clamp(-3, -1)); |
| 48 Expect.isTrue(0.0.clamp(-3, -1) is int); |
| 49 Expect.equals(-3, (-4.0).clamp(-3, -1)); |
| 50 Expect.isTrue((-4.0).clamp(-3, -1) is int); |
| 51 Expect.equals(0, 1.0.clamp(0, 0)); |
| 52 Expect.isTrue(1.0.clamp(0, 0) is int); |
| 53 Expect.equals(0, (-1.0).clamp(0, 0)); |
| 54 Expect.isTrue((-1.0).clamp(0, 0) is int); |
| 55 Expect.equals(0.0, 0.0.clamp(0, 0)); |
| 56 Expect.isTrue(0.0.clamp(0, 0) is double); |
| 57 Expect.throws(() => 0.0.clamp(0, -1), (e) => e is ArgumentError); |
| 58 Expect.throws(() => 0.0.clamp("str", -1), |
| 59 (e) => e is ArgumentError || e is TypeError); |
| 60 Expect.throws(() => 0.0.clamp(0, "2"), |
| 61 (e) => e is ArgumentError || e is TypeError); |
| 62 } |
| 63 |
| 64 testDoubleClampExtremes() { |
| 65 Expect.equals(2.0, 2.0.clamp(-double.INFINITY, double.INFINITY)); |
| 66 Expect.equals(2.0, 2.0.clamp(-double.INFINITY, double.NAN)); |
| 67 Expect.equals(double.INFINITY, 2.0.clamp(double.INFINITY, double.NAN)); |
| 68 Expect.isTrue(2.0.clamp(double.NAN, double.NAN).isNaN); |
| 69 Expect.throws(() => 0.0.clamp(double.NAN, double.INFINITY), |
| 70 (e) => e is ArgumentError); |
| 71 } |
| 72 |
| 73 main() { |
| 74 testIntClamp(); |
| 75 testDoubleClamp(); |
| 76 testDoubleClampInt(); |
| 77 testDoubleClampExtremes(); |
| 78 } |
OLD | NEW |