[t/spec] Refudge / tweak / add number tests.
[pugs.git] / t / spec / S32-num / real-bridge.t
blob74192504b065f8b2d86c3e73ad21d798b1dc19e0
1 use v6;
2 use Test;
3 plan *;
5 =begin pod
7 Basic tests easily defining a Real type
9 =end pod
11 class Fixed2 does Real {
12     has Int $.one-hundredths;
14     multi method new(Int $a) {
15         self.bless(*, :one-hundredths($a * 100));
16     }
18     multi method new(Rat $a) {
19         self.bless(*, :one-hundredths(floor($a * 100)));
20     }
22     method Bridge() {
23         $.one-hundredths.Bridge / 100.Bridge;
24     }
27 my $zero = Fixed2.new(0);
28 my $one = Fixed2.new(1);
29 my $one-and-one-hundredth = Fixed2.new(1.01);
30 my $one-and-ninety-nine-hundredths = Fixed2.new(1.99);  
31 my $ten = Fixed2.new(10);
32 my $neg-pi = Fixed2.new(-3.14);
34 isa_ok $zero, Fixed2, "Fixed2 sanity test";
35 isa_ok $one, Fixed2, "Fixed2 sanity test";
36 isa_ok $one-and-one-hundredth, Fixed2, "Fixed2 sanity test";
37 isa_ok $neg-pi, Fixed2, "Fixed2 sanity test";
38 ok $zero ~~ Real, "Fixed2 sanity test";
39 ok $one ~~ Real, "Fixed2 sanity test";
40 ok $one-and-one-hundredth ~~ Real, "Fixed2 sanity test";
41 ok $neg-pi ~~ Real, "Fixed2 sanity test";
43 is $zero.succ, 1, "0.succ works";
44 is $neg-pi.succ, -2.14, "(-3.14).succ works";
45 is $zero.pred, -1, "0.pred works";
46 is $neg-pi.pred, -4.14, "(-3.14).pred works";
49     my $i = $neg-pi.Int;
50     isa_ok $i, Int, "-3.14.Int is an Int";
51     is $i, -3, "-3.14.Int is -3";
52     
53     $i = $one-and-ninety-nine-hundredths.Int;
54     isa_ok $i, Int, "1.99.Int is an Int";
55     is $i, 1, "1.99.Int is 1";
59     my $i = $neg-pi.Rat;
60     isa_ok $i, Rat, "-3.14.Rat is an Rat";
61     is_approx $i, -3.14, "-3.14.Rat is -3.14";
62     
63     $i = $one-and-ninety-nine-hundredths.Rat;
64     isa_ok $i, Rat, "1.99.Rat is an Rat";
65     is_approx $i, 1.99, "1.99.Rat is 1.99";
69     my $i = $neg-pi.Num;
70     isa_ok $i, Num, "-3.14.Num is an Num";
71     is_approx $i, -3.14, "-3.14.Num is -3.14";
72     
73     $i = $one-and-ninety-nine-hundredths.Num;
74     isa_ok $i, Num, "1.99.Num is an Num";
75     is_approx $i, 1.99, "1.99.Num is 1.99";
78 is_approx $zero.abs, 0, "0.abs works";
79 ok $zero.abs ~~ Real, "0.abs produces a Real";
80 is_approx $one.abs, 1, "1.abs works";
81 ok $one.abs ~~ Real, "1.abs produces a Real";
82 is_approx $one-and-one-hundredth.abs, 1.01, "1.01.abs works";
83 ok $one-and-one-hundredth.abs ~~ Real, "1.01.abs produces a Real";
84 is_approx $neg-pi.abs, 3.14, "-3.14.abs works";
85 ok $neg-pi.abs ~~ Real, "-3.14.abs produces a Real";
87 is_approx $zero.sign, 0, "0.sign works";
88 is_approx $one.sign, 1, "1.sign works";
89 is_approx $one-and-one-hundredth.sign, 1, "1.01.sign works";
90 is_approx $neg-pi.sign, -1, "-3.14.sign works";
92 is $zero <=> 0, 0, "0 == 0";
93 is $one <=> 0.Num, 1, "1 > 0";
94 is $one-and-one-hundredth <=> 1.1, -1, "1.01 < 1.1";
95 is $neg-pi <=> -3, -1, "-3.14 < -3";
96 is -1 <=> $zero, -1, "-1 < 0";
97 is 1.Rat <=> $one, 0, "1 == 1";
98 is 1.001 <=> $one-and-one-hundredth, -1, "1.001 < 1.01";
99 is $neg-pi <=> -3.14, 0, "-3.14 == -3.14";
101 nok $zero < 0, "not 0 < 0";
102 nok $one < 0.Num, "not 1 < 0";
103 ok $one-and-one-hundredth < 1.1, "1.01 < 1.1";
104 ok $neg-pi < -3, "-3.14 < -3";
105 ok -1 < $zero, "-1 < 0";
106 nok 1.Rat < $one, "not 1 < 1";
107 ok 1.001 < $one-and-one-hundredth, "1.001 < 1.01";
108 nok $neg-pi < -3.14, "not -3.14 < -3.14";
110 ok $zero <= 0, "0 <= 0";
111 nok $one <= 0.Num, "not 1 <= 0";
112 ok $one-and-one-hundredth <= 1.1, "1.01 <= 1.1";
113 ok $neg-pi <= -3, "-3.14 <= -3";
114 ok -1 <= $zero, "-1 <= 0";
115 ok 1.Rat <= $one, "1 <= 1";
116 ok 1.001 <= $one-and-one-hundredth, "1.001 <= 1.01";
117 ok $neg-pi <= -3.14, "-3.14 <= -3.14";
119 nok $zero > 0, "not 0 > 0";
120 ok $one > 0.Num, "1 > 0";
121 nok $one-and-one-hundredth > 1.1, "not 1.01 > 1.1";
122 nok $neg-pi > -3, "not -3.14 > -3";
123 nok -1 > $zero, "not -1 > 0";
124 nok 1.Rat > $one, "not 1 > 1";
125 nok 1.001 > $one-and-one-hundredth, "not 1.001 > 1.01";
126 nok $neg-pi > -3.14, "not -3.14 > -3.14";
128 ok $zero >= 0, "0 >= 0";
129 ok $one >= 0.Num, "1 >= 0";
130 nok $one-and-one-hundredth >= 1.1, "not 1.01 >= 1.1";
131 nok $neg-pi >= -3, "not -3.14 >= -3";
132 nok -1 >= $zero, "not -1 >= 0";
133 ok 1.Rat >= $one, "1 >= 1";
134 nok 1.001 >= $one-and-one-hundredth, "not 1.001 >= 1.01";
135 ok $neg-pi >= -3.14, "-3.14 >= -3.14";
137 ok $zero == 0, "0 == 0";
138 nok $one == 0.Num, "not 1 == 0";
139 nok $one-and-one-hundredth == 1.1, "not 1.01 == 1.1";
140 nok $neg-pi == -3, "not -3.14 == -3";
141 nok -1 == $zero, "not -1 == 0";
142 ok 1.Rat == $one, "1 == 1";
143 nok 1.001 == $one-and-one-hundredth, "not 1.001 == 1.01";
144 ok $neg-pi == -3.14, "-3.14 == -3.14";
146 nok $zero != 0, "not 0 != 0";
147 ok $one != 0.Num, "1 != 0";
148 ok $one-and-one-hundredth != 1.1, "1.01 != 1.1";
149 ok $neg-pi != -3, "-3.14 != -3";
150 ok -1 != $zero, "-1 != 0";
151 nok 1.Rat != $one, "not 1 != 1";
152 ok 1.001 != $one-and-one-hundredth, "1.001 != 1.01";
153 nok $neg-pi != -3.14, "not -3.14 != -3.14";
155 is $zero cmp 0, 0, "0 eq 0";
156 is $one cmp 0.Num, 1, "1 gt 0";
157 is $one-and-one-hundredth cmp 1.1, -1, "1.01 lt 1.1";
158 is $neg-pi cmp -3, -1, "-3.14 lt -3";
159 is -1 cmp $zero, -1, "-1 lt 0";
160 is 1.Rat cmp $one, 0, "1 eq 1";
161 is 1.001 cmp $one-and-one-hundredth, -1, "1.001 lt 1.01";
162 is $neg-pi cmp -3.14, 0, "-3.14 eq -3.14";
164 nok $zero before 0, "not 0 before 0";
165 nok $one before 0.Num, "not 1 before 0";
166 ok $one-and-one-hundredth before 1.1, "1.01 before 1.1";
167 ok $neg-pi before -3, "-3.14 before -3";
168 ok -1 before $zero, "-1 before 0";
169 nok 1.Rat before $one, "not 1 before 1";
170 ok 1.001 before $one-and-one-hundredth, "1.001 before 1.01";
171 nok $neg-pi before -3.14, "not -3.14 before -3.14";
173 nok $zero after 0, "not 0 after 0";
174 ok $one after 0.Num, "1 after 0";
175 nok $one-and-one-hundredth after 1.1, "not 1.01 after 1.1";
176 nok $neg-pi after -3, "not -3.14 after -3";
177 nok -1 after $zero, "not -1 after 0";
178 nok 1.Rat after $one, "not 1 after 1";
179 nok 1.001 after $one-and-one-hundredth, "not 1.001 after 1.01";
180 nok $neg-pi after -3.14, "not -3.14 after -3.14";
182 is_approx -$zero, 0, "-0 == 0";
183 is_approx -$one, -1, "-1 == -1";
184 is_approx -$one-and-one-hundredth, -1.01, "-1.01 == -1.01";
185 is_approx -$neg-pi, 3.14, "-(-3.14) == 3.14";
187 is $one - $one, 0, "1 - 1 == 0";
188 is $one - 1, 0, "1 - 1 == 0";
189 is $one-and-one-hundredth - $one-and-one-hundredth, 0, "1.01 - 1.01 == 0";
190 is $one-and-one-hundredth - 1.01, 0, "1.01 - 1.01 == 0";
191 is_approx 1.01 - $one, 0.01, "1.01 - 1 == 0.01";
192 is_approx $one-and-one-hundredth - 1.Num, 0.01, "1.01 - 1 == 0.01";
194 is_approx $one-and-one-hundredth.log, 1.01.log, "1.01.log is correct";
195 is_approx log($one-and-one-hundredth), 1.01.log, "log(1.01) is correct";
196 is_approx $one-and-one-hundredth.log($ten), 1.01.log10, "1.01.log(10) is correct";
197 is_approx log($one-and-one-hundredth, $ten), 1.01.log10, "log(1.01, 10) is correct";
198 is_approx $one-and-one-hundredth.log($ten * 1i), 1.01.log / log(10i), "1.01.log(10i) is correct";
199 is_approx ($one-and-one-hundredth * 1i).log($ten), log(1.01i) / log(10), "1.01i.log(10) is correct";
201 is_approx $one-and-one-hundredth.cis, 1.01.cis, "1.01.cis is correct";
202 is_approx cis($one-and-one-hundredth), 1.01.cis, "cis(1.01) is correct";
203 is_approx $one-and-one-hundredth.unpolar($neg-pi), 1.01.unpolar(-3.14), "1.01.unpolar(-3.14) is correct";
204 is_approx unpolar($one-and-one-hundredth, $neg-pi), 1.01.unpolar(-3.14), "1.01.unpolar(-3.14) is correct";
206 is $one-and-one-hundredth.floor, 1, "1.01.floor is correct";
207 is floor(1), 1, "1.floor is correct";
208 is $one-and-one-hundredth.ceiling, 2, "1.01.ceiling is correct";
209 is ceiling(1), 1, "1.ceiling is correct";
210 is $one-and-one-hundredth.truncate, 1, "1.01.truncate is correct";
211 is truncate($neg-pi), -3, "-3.14.truncate is correct";
212 is $one-and-one-hundredth.round(1/100), 1.01, "1.01.round(1/100) is correct";
213 is round($one-and-one-hundredth, 1/10), 1, "1.01.round(1/10) is correct";
214 is round($one-and-one-hundredth), 1, "1.01.round is correct";
216 is $one-and-one-hundredth ** $neg-pi, 1.01 ** -3.14, "1.01 ** -3.14 is correct";
217 is $neg-pi ** $one, -3.14 ** 1, "-3.14 ** 1 is correct";
219 is $one-and-one-hundredth.exp, 1.01.exp, "1.01.exp is correct";
220 is $neg-pi.exp, (-3.14).exp, "-3.14.exp is correct";
221 is $one-and-one-hundredth.exp(10.Rat), 1.01.exp(10), "1.01.exp(10) is correct";
222 is 2.exp($neg-pi), 2.exp(-3.14), "2.exp(-3.14) is correct";
223 is_approx $one-and-one-hundredth.exp(10i), 1.01.exp(10i), "1.01.exp(10i) is correct";
224 is_approx 2i.exp($neg-pi), 2i.exp(-3.14), "2i.exp(-3.14) is correct";
227     my @l = $neg-pi.roots(4);
228     ok(@l.elems == 4, '(-3.14).roots(4) returns 4 elements');
229     my $quartic = (-3.14.Complex) ** .25;
230     ok(@l.grep({ ($_ - $quartic).abs < 1e-5 }).Bool, '(-3.14) ** 1/4 is a quartic root of -3.14');
231     ok(@l.grep({ ($_ + $quartic).abs < 1e-5 }).Bool, '-(-3.14) ** 1/4 is a quartic root of -3.14');
232     ok(@l.grep({ ($_ - $quartic\i).abs < 1e-5 }).Bool, '(-3.14)i ** 1/4 is a quartic root of -3.14');
233     ok(@l.grep({ ($_ + $quartic\i).abs < 1e-5 }).Bool, '-(-3.14)i ** 1/4 is a quartic root of -3.14');
236 done_testing;
238 # vim: ft=perl6