Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 132 lines (122 sloc) 3.934 kB
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
276bd65 [t/spec] fudge "use Test" directive for Rakudo
moritz authored
2 #?rakudo emit #
db10870 [t/spec] patching difficulties spotted by STD
lwall authored
3 use Test <plan ok>;
276bd65 [t/spec] fudge "use Test" directive for Rakudo
moritz authored
4 #?rakudo emit use Test;
025f65e Tests for cube roots() of negative numbers
leto authored
5 plan 44;
ad1b86b [t] Test suite refactoring.
cosimo authored
6
f9947f8 [t/spec/S32-num] fix all smartlinks in this dir
moritz authored
7 # L<S32::Numeric/Num/"=item roots">
ad1b86b [t] Test suite refactoring.
cosimo authored
8
3488142 [spec]
moritz authored
9 sub approx($a, $b){
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
10 my ($x,$y);
11 my $eps = 1e-3;
12 # coerce both to Complex
13 $x = $a + 0i;
14 $y = $b + 0i;
15 my $re = abs($x.re - $y.re);
16 my $im = abs($x.im - $y.im);
17 # both real and imag part must be with $eps of expected
18 return ( $re < $eps && $im < $eps );
3488142 [spec]
moritz authored
19 }
20
21 sub has_approx($n, @list) {
22 for @list -> $i {
23 if approx($i, $n) {
24 return 1;
1f5bd6f [spec] fixed roots.t, pmichaud++
moritz authored
25 }
ad1b86b [t] Test suite refactoring.
cosimo authored
26 }
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
27 return Mu;
ad1b86b [t] Test suite refactoring.
cosimo authored
28 }
29
30 {
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
31 my @l = roots(0, 1);
32 ok(@l.elems == 1, 'roots(0, 1) returns 1 element');
33 ok(has_approx(0, @l), 'roots(0, 1) contains 0');
34 }
35 {
36 my @l = roots(0, 0);
37 ok(@l.elems == 1, 'roots(0, 0) returns 1 element');
38 ok(@l[0] ~~ 'NaN', 'roots(0,0) returns NaN');
39 }
40 {
41 my @l = roots(0, -1);
42 ok(@l.elems == 1, 'roots(0, -1) returns 1 element');
43 ok(@l[0] ~~ 'NaN', 'roots(0,-1) returns NaN');
44 }
45 {
46 my @l = roots(100, -1);
47 ok(@l.elems == 1, 'roots(100, -1) returns 1 element');
48 ok(@l[0] ~~ 'NaN', 'roots(100,-1) returns NaN');
49 }
50 {
51 my @m = roots(1, 0);
52 ok(@m.elems == 1, 'roots(1, 0) returns 1 element');
53 ok(@m[0] ~~ 'NaN', 'roots(1,0) returns NaN');
54 }
55 {
56 my @l = roots(4, 2);
57 ok(@l.elems == 2, 'roots(4, 2) returns 2 elements');
58 ok(has_approx(2, @l), 'roots(4, 2) contains 2');
59 ok(has_approx(-2, @l), 'roots(4, 2) contains -2');
60 }
61 {
5fa47b5 S29-num/roots.t: update error message, avoid needless eval.
pmichaud authored
62 my @l = roots(-1, 2);
ad1b86b [t] Test suite refactoring.
cosimo authored
63 ok(@l.elems == 2, 'roots(-1, 2) returns 2 elements');
64 ok(has_approx(1i, @l), 'roots(-1, 2) contains 1i');
65 ok(has_approx(-1i, @l), 'roots(-1, 2) contains -1i');
66 }
67
68 #?pugs todo 'feature'
69 {
5fa47b5 S29-num/roots.t: update error message, avoid needless eval.
pmichaud authored
70 my @l = 16.roots(4);
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
71 ok(@l.elems == 4, '16.roots(4) returns 4 elements');
72 ok(has_approx(2, @l), '16.roots(4) contains 2');
73 ok(has_approx(-2, @l), '16.roots(4) contains -2');
74 ok(has_approx(2i, @l), '16.roots(4) contains 2i');
75 ok(has_approx(-2i, @l), '16.roots(4) contains -2i');
76 }
77 {
78 my @l = (-1).roots(2);
79 ok(@l.elems == 2, '(-1).roots(2) returns 2 elements');
80 ok(has_approx(1i, @l), '(-1).roots(2) contains i');
81 ok(has_approx(-1i, @l), '(-1).roots(2) contains -i');
82 }
83 {
84 my @l = 0e0.roots(2);
85 ok(@l.elems == 2, '0e0.roots(2) returns 2 elements');
86 ok(has_approx(0, @l), '0e0.roots(2) contains 0');
87 }
88 {
595e7dd [t/spec] $thing ~~ "NaN" is not the same as $thing ~~ NaN
moritz authored
89 my @l = roots(NaN, 1);
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
90 ok(@l.elems == 1, 'roots(NaN, 1) returns 1 element');
e35338c [t/spec]i Fudge two questionable tests that fail hard.
colomon authored
91 #?rakudo skip "Should this be == or ~~?"
595e7dd [t/spec] $thing ~~ "NaN" is not the same as $thing ~~ NaN
moritz authored
92 ok(@l[0] ~~ NaN, 'roots(NaN,1) returns NaN');
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
93 }
94 {
595e7dd [t/spec] $thing ~~ "NaN" is not the same as $thing ~~ NaN
moritz authored
95 my @l = roots(Inf, 1);
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
96 ok(@l.elems == 1, 'roots(Inf, 1) returns 1 element');
e35338c [t/spec]i Fudge two questionable tests that fail hard.
colomon authored
97 #?rakudo skip "Should this be == or ~~?"
595e7dd [t/spec] $thing ~~ "NaN" is not the same as $thing ~~ NaN
moritz authored
98 ok(@l[0] ~~ Inf, 'roots(Inf,1) returns Inf');
ad1b86b [t] Test suite refactoring.
cosimo authored
99 }
1d1fb57 [t/spec] Replace references to pi the constant with a local variable.
colomon authored
100
101 my $pi = 312689/99532;
102
9ed6d8d Tests for roots() of a Complex
leto authored
103 {
104 my @l = roots(1i,2);
105 ok(@l.elems == 2, 'roots(1i,2) returns 2 elements');
1d1fb57 [t/spec] Replace references to pi the constant with a local variable.
colomon authored
106 ok(has_approx(exp(5i*$pi/4), @l), 'exp(5i*$pi/4) is a square root of i');
107 ok(has_approx(exp(1i*$pi/4), @l), 'exp(1i*$pi/4) is a square root of i');
9ed6d8d Tests for roots() of a Complex
leto authored
108 }
109 {
110 my @l = roots(1+1i,2);
111 ok(@l.elems == 2, 'roots(1+1i,2) returns 2 elements');
1d1fb57 [t/spec] Replace references to pi the constant with a local variable.
colomon authored
112 ok(has_approx(exp(log(2)/4 + 1i*$pi/8), @l),'exp(log(2)/4 + 1i*$pi/8) is a square root of 1+1i');
113 ok(has_approx(exp(log(2)/4 + 9i*$pi/8), @l),'exp(log(2)/4 + 9i*$pi/8) is a square root of 1+1i');
9ed6d8d Tests for roots() of a Complex
leto authored
114 }
20911d4 Tests for cube roots()
leto authored
115 {
116 my @l = 8.roots(3);
117 ok(@l.elems == 3, '8.roots(3) returns 3 elements');
118 ok(has_approx(2,@l), '2 is a cube root of 8');
1d1fb57 [t/spec] Replace references to pi the constant with a local variable.
colomon authored
119 ok(has_approx(exp(1/3*(log(8) + 2i*$pi)),@l), 'exp(1/3*(log(8) + 2i*$pi)) is a cube root of 8');
120 ok(has_approx(exp(1/3*(log(8) + 4i*$pi)),@l), 'exp(1/3*(log(8) + 4i*$pi)) is a cube root of 8');
20911d4 Tests for cube roots()
leto authored
121 }
025f65e Tests for cube roots() of negative numbers
leto authored
122 {
123 my @l = (-8).roots(3);
124 ok(@l.elems == 3, '(-8).roots(3) returns 3 elements');
9ed0e53 Fix typo in roots() test descriptions
leto authored
125 ok(has_approx(-2,@l), '2 is a cube root of -8');
1d1fb57 [t/spec] Replace references to pi the constant with a local variable.
colomon authored
126 ok(has_approx(exp(1/3*(log(8) + 3i*$pi)),@l), 'exp(1/3*(log(8) + 3i*$pi)) is a cube root of -8');
127 ok(has_approx(exp(1/3*(log(8) + 5i*$pi)),@l), 'exp(1/3*(log(8) + 5i*$pi)) is a cube root of -8');
025f65e Tests for cube roots() of negative numbers
leto authored
128 }
9ed6d8d Tests for roots() of a Complex
leto authored
129
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
130
131 # vim: ft=perl6
Something went wrong with that request. Please try again.