Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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