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