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