Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 123 lines (116 sloc) 3.733 kB
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
ad1b86b [t] Test suite refactoring.
cosimo authored
2 use Test;
025f65e Tests for cube roots() of negative numbers
leto authored
3 plan 44;
ad1b86b [t] Test suite refactoring.
cosimo authored
4
5 # L<S29/Num/"=item roots">
6
3488142 [spec]
moritz authored
7 sub approx($a, $b){
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
8 my ($x,$y);
9 my $eps = 1e-3;
10 # coerce both to Complex
11 $x = $a + 0i;
12 $y = $b + 0i;
13 my $re = abs($x.re - $y.re);
14 my $im = abs($x.im - $y.im);
15 # both real and imag part must be with $eps of expected
16 return ( $re < $eps && $im < $eps );
3488142 [spec]
moritz authored
17 }
18
19 sub has_approx($n, @list) {
20 for @list -> $i {
21 if approx($i, $n) {
22 return 1;
1f5bd6f [spec] fixed roots.t, pmichaud++
moritz authored
23 }
ad1b86b [t] Test suite refactoring.
cosimo authored
24 }
3488142 [spec]
moritz authored
25 return undef;
ad1b86b [t] Test suite refactoring.
cosimo authored
26 }
27
28 {
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
29 my @l = roots(0, 1);
30 ok(@l.elems == 1, 'roots(0, 1) returns 1 element');
31 ok(has_approx(0, @l), 'roots(0, 1) contains 0');
32 }
33 {
34 my @l = roots(0, 0);
35 ok(@l.elems == 1, 'roots(0, 0) returns 1 element');
36 ok(@l[0] ~~ 'NaN', 'roots(0,0) returns NaN');
37 }
38 {
39 my @l = roots(0, -1);
40 ok(@l.elems == 1, 'roots(0, -1) returns 1 element');
41 ok(@l[0] ~~ 'NaN', 'roots(0,-1) returns NaN');
42 }
43 {
44 my @l = roots(100, -1);
45 ok(@l.elems == 1, 'roots(100, -1) returns 1 element');
46 ok(@l[0] ~~ 'NaN', 'roots(100,-1) returns NaN');
47 }
48 {
49 my @m = roots(1, 0);
50 ok(@m.elems == 1, 'roots(1, 0) returns 1 element');
51 ok(@m[0] ~~ 'NaN', 'roots(1,0) returns NaN');
52 }
53 {
54 my @l = roots(4, 2);
55 ok(@l.elems == 2, 'roots(4, 2) returns 2 elements');
56 ok(has_approx(2, @l), 'roots(4, 2) contains 2');
57 ok(has_approx(-2, @l), 'roots(4, 2) contains -2');
58 }
59 {
5fa47b5 S29-num/roots.t: update error message, avoid needless eval.
pmichaud authored
60 my @l = roots(-1, 2);
ad1b86b [t] Test suite refactoring.
cosimo authored
61 ok(@l.elems == 2, 'roots(-1, 2) returns 2 elements');
62 ok(has_approx(1i, @l), 'roots(-1, 2) contains 1i');
63 ok(has_approx(-1i, @l), 'roots(-1, 2) contains -1i');
64 }
65
66 #?pugs todo 'feature'
67 {
5fa47b5 S29-num/roots.t: update error message, avoid needless eval.
pmichaud authored
68 my @l = 16.roots(4);
036c2c9 Adding many roots tests and improve approx() to deal with various typ…
leto authored
69 ok(@l.elems == 4, '16.roots(4) returns 4 elements');
70 ok(has_approx(2, @l), '16.roots(4) contains 2');
71 ok(has_approx(-2, @l), '16.roots(4) contains -2');
72 ok(has_approx(2i, @l), '16.roots(4) contains 2i');
73 ok(has_approx(-2i, @l), '16.roots(4) contains -2i');
74 }
75 {
76 my @l = (-1).roots(2);
77 ok(@l.elems == 2, '(-1).roots(2) returns 2 elements');
78 ok(has_approx(1i, @l), '(-1).roots(2) contains i');
79 ok(has_approx(-1i, @l), '(-1).roots(2) contains -i');
80 }
81 {
82 my @l = 0e0.roots(2);
83 ok(@l.elems == 2, '0e0.roots(2) returns 2 elements');
84 ok(has_approx(0, @l), '0e0.roots(2) contains 0');
85 }
86 {
87 my @l = roots('NaN', 1);
88 ok(@l.elems == 1, 'roots(NaN, 1) returns 1 element');
89 ok(@l[0] ~~ 'NaN', 'roots(NaN,1) returns NaN');
90 }
91 {
92 my @l = roots('Inf', 1);
93 ok(@l.elems == 1, 'roots(Inf, 1) returns 1 element');
94 ok(@l[0] ~~ 'Inf', 'roots(Inf,1) returns Inf');
ad1b86b [t] Test suite refactoring.
cosimo authored
95 }
9ed6d8d Tests for roots() of a Complex
leto authored
96 {
97 my @l = roots(1i,2);
98 ok(@l.elems == 2, 'roots(1i,2) returns 2 elements');
99 ok(has_approx(exp(5i*pi/4), @l), 'exp(5i*pi/4) is a square root of i');
100 ok(has_approx(exp(1i*pi/4), @l), 'exp(1i*pi/4) is a square root of i');
101 }
102 {
103 my @l = roots(1+1i,2);
104 ok(@l.elems == 2, 'roots(1+1i,2) returns 2 elements');
105 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');
106 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');
107 }
20911d4 Tests for cube roots()
leto authored
108 {
109 my @l = 8.roots(3);
110 ok(@l.elems == 3, '8.roots(3) returns 3 elements');
111 ok(has_approx(2,@l), '2 is a cube root of 8');
112 ok(has_approx(exp(1/3*(log(8) + 2i*pi)),@l), 'exp(1/3*(log(8) + 2i*pi)) is a cube root of 8');
113 ok(has_approx(exp(1/3*(log(8) + 4i*pi)),@l), 'exp(1/3*(log(8) + 4i*pi)) is a cube root of 8');
114 }
025f65e Tests for cube roots() of negative numbers
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');
119 ok(has_approx(exp(1/3*(log(8) + 3i*pi)),@l), 'exp(1/3*(log(8) + 3i*pi)) is a cube root of 8');
120 ok(has_approx(exp(1/3*(log(8) + 5i*pi)),@l), 'exp(1/3*(log(8) + 5i*pi)) is a cube root of 8');
121 }
9ed6d8d Tests for roots() of a Complex
leto authored
122
Something went wrong with that request. Please try again.