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 import "package:expect/expect.dart"; | |
7 | |
8 testIntClamp() { | |
9 Expect.equals(2, 2.clamp(1, 3)); | |
10 Expect.equals(1, 0.clamp(1, 3)); | |
11 Expect.equals(3, 4.clamp(1, 3)); | |
12 Expect.equals(-2, (-2).clamp(-3, -1)); | |
13 Expect.equals(-1, 0.clamp(-3, -1)); | |
14 Expect.equals(-3, (-4).clamp(-3, -1)); | |
15 Expect.equals(0, 1.clamp(0, 0)); | |
16 Expect.equals(0, (-1).clamp(0, 0)); | |
17 Expect.equals(0, 0.clamp(0, 0)); | |
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 } | |
24 | |
25 testDoubleClamp() { | |
26 Expect.equals(2.0, 2.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)); | |
29 Expect.equals(-2.0, (-2.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)); | |
32 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)); | |
35 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 } | |
41 | |
42 testDoubleClampInt() { | |
43 Expect.equals(2.0, 2.0.clamp(1, 3)); | |
44 Expect.equals(1, 0.0.clamp(1, 3)); | |
45 Expect.isTrue(0.0.clamp(1, 3) is int); | |
46 Expect.equals(3, 4.0.clamp(1, 3)); | |
47 Expect.isTrue(4.0.clamp(1, 3) is int); | |
48 Expect.equals(-2.0, (-2.0).clamp(-3, -1)); | |
49 Expect.equals(-1, 0.0.clamp(-3, -1)); | |
50 Expect.isTrue(0.0.clamp(-3, -1) is int); | |
51 Expect.equals(-3, (-4.0).clamp(-3, -1)); | |
52 Expect.isTrue((-4.0).clamp(-3, -1) 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, (-1.0).clamp(0, 0)); | |
56 Expect.isTrue((-1.0).clamp(0, 0) is int); | |
57 Expect.equals(0.0, 0.0.clamp(0, 0)); | |
58 Expect.isTrue(0.0.clamp(0, 0) is double); | |
59 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 } | |
65 | |
66 testDoubleClampExtremes() { | |
67 Expect.equals(2.0, 2.0.clamp(-double.INFINITY, double.INFINITY)); | |
68 Expect.equals(2.0, 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); | |
71 Expect.throws( | |
72 () => 0.0.clamp(double.NAN, double.INFINITY), (e) => e is ArgumentError); | |
73 } | |
74 | |
75 main() { | |
76 testIntClamp(); | |
77 testDoubleClamp(); | |
78 testDoubleClampInt(); | |
79 testDoubleClampExtremes(); | |
80 } | |
OLD | NEW |