Debian Codegraph @

cpan/Test2-Suite/t/modules/Compare/Float.t

raw
1 use Test2::Bundle::Extended -target => 'Test2::Compare::Float';
2
3 my $num = $CLASS->new(input => '22.0', tolerance => .001);
4 my $neg_num = $CLASS->new(input => -22, tolerance => .001);
5 my $untrue = $CLASS->new(input => 0);
6 my $pre_num = $CLASS->new(input => '22.0', precision => 3);
7
8 isa_ok($num, $CLASS, 'Test2::Compare::Base');
9 isa_ok($untrue, $CLASS, 'Test2::Compare::Base');
10
11 subtest tolerance => sub {
12 is($num->tolerance, 0.001, "got expected tolerance for number");
13 is($untrue->tolerance, 1e-08, "got default tolerance for 0");
14 };
15
16 subtest name => sub {
17 is($num->name, '22.0 +/- ' . $num->tolerance, "got expected name for number");
18 is($untrue->name, '0 +/- ' . $untrue->tolerance, "got expected name for 0");
19 # Note: string length of mantissa varies by perl install, e.g. 1e-08 vs 1e-008
20
21 is($pre_num->name, '22.000', "got expected 3 digits of precision in name for 22.0, precision=5");
22 is($CLASS->new(input => '100.123456789012345', precision => 10)->name,
23 '100.1234567890',
24 'got expected precision in name at precision=10');
25 is($CLASS->new(input => '100.123456789012345', precision => 15)->name,
26 sprintf('%.*f', 15, '100.123456789012345'),
27 'got expected precision in name at precision=15'); # likely not 100.123456789012345!
28 is($CLASS->new(input => '100.123456789012345', precision => 20)->name,
29 sprintf('%.*f', 20, '100.123456789012345'),
30 'got expected precision in name at precision=20');
31 };
32
33 subtest operator => sub {
34 is($num->operator(), '', "no operator for number + nothing");
35 is($num->operator(undef), '', "no operator for number + undef");
36 is($num->operator(1), '==', "== operator for number + number");
37
38 is($untrue->operator(), '', "no operator for 0 + nothing");
39 is($untrue->operator(undef), '', "no operator for 0 + undef");
40 is($untrue->operator(1), '==', "== operator for 0 + number");
41
42 is($pre_num->operator(), '', "no operator for precision number + nothing");
43 is($pre_num->operator(undef), '', "no operator for precision number + undef");
44 is($pre_num->operator(1), 'eq', "eq operator for precision number + number");
45 };
46
47 subtest verify => sub {
48 ok(!$num->verify(exists => 0, got => undef), 'does not verify against DNE');
49 ok(!$num->verify(exists => 1, got => {}), 'ref will not verify');
50 ok(!$num->verify(exists => 1, got => undef), 'looking for a number, not undef');
51 ok(!$num->verify(exists => 1, got => 'x'), 'not looking for a string');
52 ok(!$num->verify(exists => 1, got => 1), 'wrong number');
53 ok($num->verify(exists => 1, got => 22), '22.0 == 22');
54 ok($num->verify(exists => 1, got => '22.0'), 'exact match with decimal');
55
56 ok(!$untrue->verify(exists => 0, got => undef), 'does not verify against DNE');
57 ok(!$untrue->verify(exists => 1, got => {}), 'ref will not verify');
58 ok(!$untrue->verify(exists => 1, got => undef), 'undef is not 0 for this test');
59 ok(!$untrue->verify(exists => 1, got => 'x'), 'x is not 0');
60 ok(!$untrue->verify(exists => 1, got => 1), '1 is not 0');
61 ok(!$untrue->verify(exists => 1, got => ''), '"" is not 0');
62 ok(!$untrue->verify(exists => 1, got => ' '), '" " is not 0');
63 ok($untrue->verify(exists => 1, got => 0), 'got 0');
64 ok($untrue->verify(exists => 1, got => '0.0'), '0.0 == 0');
65 ok($untrue->verify(exists => 1, got => '-0.0'), '-0.0 == 0');
66 };
67
68 subtest verify_float_tolerance => sub {
69 ok($num->verify(exists => 1, got => "22.0"), '22.0 == 22 +/- .001');
70 ok($num->verify(exists => 1, got => "22.0009"), '22.0009 == 22 +/- .001');
71 ok($num->verify(exists => 1, got => "21.9991"), '21.9991 == 22 +/- .001');
72 ok(!$num->verify(exists => 1, got => "22.0011"), '22.0009 != 22 +/- .001');
73 ok(!$num->verify(exists => 1, got => "21.9989"), '21.9989 != 22 +/- .001');
74 ok(!$num->verify(exists => 1, got => "23"), '23 != 22 +/- .001');
75
76 ok($num->verify(exists => 1, got => 22.0), '22.0 == 22 +/- .001');
77 ok($num->verify(exists => 1, got => 22.0009), '22.0009 == 22 +/- .001');
78 ok($num->verify(exists => 1, got => 21.9991), '21.9991 == 22 +/- .001');
79 ok(!$num->verify(exists => 1, got => 22.0011), '22.0009 != 22 +/- .001');
80 ok(!$num->verify(exists => 1, got => 21.9989), '21.9989 != 22 +/- .001');
81 ok(!$num->verify(exists => 1, got => 23), '23 != 22 +/- .001');
82
83 ok($neg_num->verify(exists => 1, got => -22.0), '-22.0 == -22 +/- .001');
84 ok($neg_num->verify(exists => 1, got => -22.0009), '-22.0009 == -22 +/- .001');
85 ok($neg_num->verify(exists => 1, got => -21.9991), '-21.9991 == -22 +/- .001');
86 ok(!$neg_num->verify(exists => 1, got => -22.0011), '-22.0009 != -22 +/- .001');
87 ok(!$neg_num->verify(exists => 1, got => -21.9989), '-21.9989 != -22 +/- .001');
88 ok(!$neg_num->verify(exists => 1, got => -23), '-23 != -22 +/- .001');
89 };
90 subtest verify_float_precision => sub {
91 ok($pre_num->verify(exists => 1, got => "22.0"), '22.0 == 22.000');
92 ok($pre_num->verify(exists => 1, got => "22.0001"), '22.0001 == 22.000');
93 ok($pre_num->verify(exists => 1, got => "21.9999"), '21.9999 == 22.000');
94 ok(!$pre_num->verify(exists => 1, got => "22.0011"), '22.0011 != 22.000');
95 ok(!$pre_num->verify(exists => 1, got => "21.9989"), '21.9989 != 22.000');
96 ok(!$pre_num->verify(exists => 1, got => "23"), '23 != 22.000');
97
98 ok($pre_num->verify(exists => 1, got => 22.0), '22.0 == 22.000');
99 ok($pre_num->verify(exists => 1, got => 22.00049), '22.00049 == 22.000');
100 ok(!$pre_num->verify(exists => 1, got => 22.00051), '22.00051 != 22.000');
101 ok($pre_num->verify(exists => 1, got => 21.99951), '21.99951 == 22.000');
102 ok(!$pre_num->verify(exists => 1, got => 22.0009), '22.0009 != 22.000');
103 ok(!$pre_num->verify(exists => 1, got => 21.9989), '21.9989 != 22.000');
104 ok(!$pre_num->verify(exists => 1, got => 23), '23 != 22.000');
105
106 ok($neg_num->verify(exists => 1, got => -22.0), '-22.0 == -22.000');
107 ok($neg_num->verify(exists => 1, got => -22.0009), '-22.0009 == -22.000');
108 ok($neg_num->verify(exists => 1, got => -21.9991), '-21.9991 == -22.000');
109 ok(!$neg_num->verify(exists => 1, got => -22.0011), '-22.0009 != -22.000');
110 ok(!$neg_num->verify(exists => 1, got => -21.9989), '-21.9989 != -22.000');
111 ok(!$neg_num->verify(exists => 1, got => -23), '-23 != -22.000');
112 };
113
114 subtest rounding_tolerance => sub {
115 my $round_08 = $CLASS->new(input => '60.48');
116 my $round_13 = $CLASS->new(input => '60.48', tolerance => 1e-13);
117 my $round_14 = $CLASS->new(input => '60.48', tolerance => 1e-14);
118
119 ok($round_08->verify(exists => 1, got => 60.48), ' 60.48 == ' . $round_08->name . " - inside tolerance");
120 ok($round_13->verify(exists => 1, got => 60.48), ' 60.48 == ' . $round_13->name . " - inside tolerance");
121 ok($round_14->verify(exists => 1, got => 60.48), ' 60.48 == ' . $round_14->name . " - inside tolerance");
122
123 ok($round_08->verify(exists => 1, got => 125 - 64.52), '125 - 64.52 == ' . $round_08->name . " - inside tolerance");
124 ok($round_13->verify(exists => 1, got => 125 - 64.52), '125 - 64.52 == ' . $round_13->name . " - inside tolerance");
125
126 todo 'broken on some platforms' => sub {
127 ok(!$round_14->verify(exists => 1, got => 125 - 64.52), '125 - 64.52 != ' . $round_14->name . " - outside tolerance");
128 };
129 };
130
131 subtest rounding_precision => sub {
132 my $round_08 = $CLASS->new(input => '60.48', precision => 8 );
133 my $round_13 = $CLASS->new(input => '60.48', precision => 13);
134 my $round_14 = $CLASS->new(input => '60.48', precision => 14);
135
136 ok($round_08->verify(exists => 1, got => 60.48), ' 60.48 == ' . $round_08->name . " - inside precision");
137 ok($round_13->verify(exists => 1, got => 60.48), ' 60.48 == ' . $round_13->name . " - inside precision");
138 ok($round_14->verify(exists => 1, got => 60.48), ' 60.48 == ' . $round_14->name . " - inside precision");
139
140 ok($round_08->verify(exists => 1, got => 125 - 64.52), '125 - 64.52 == ' . $round_08->name . " - inside precision");
141 ok($round_13->verify(exists => 1, got => 125 - 64.52), '125 - 64.52 == ' . $round_13->name . " - inside precision");
142
143 # unlike TOLERANCE, this should work on 32 and 64 bit platforms.
144 ok($round_14->verify(exists => 1, got => 125 - 64.52), '125 - 64.52 != ' . $round_14->name . " - outside precision");
145 };
146
147 like(
148 dies { $CLASS->new() },
149 qr/input must be defined for 'Float' check/,
150 "Cannot use undef as a number"
151 );
152
153 like(
154 dies { $CLASS->new(input => '') },
155 qr/input must be a number for 'Float' check/,
156 "Cannot use empty string as a number"
157 );
158
159 like(
160 dies { $CLASS->new(input => ' ') },
161 qr/input must be a number for 'Float' check/,
162 "Cannot use whitespace string as a number"
163 );
164
165 like(
166 dies { $CLASS->new(input => 1.234, precision => 5, tolerance => .001) },
167 qr/can't set both tolerance and precision/,
168 "Cannot use both precision and tolerance"
169 );
170 like(
171 dies { $CLASS->new(input => 1.234, precision => .05) },
172 qr/precision must be an integer/,
173 "precision can't be fractional"
174 );
175 like(
176 dies { $CLASS->new(input => 1.234, precision => -2) },
177 qr/precision must be an integer/,
178 "precision can't be negative"
179 );
180
181 done_testing;