Skip to content
Browse files

Adding many roots tests and improve approx() to deal with various typ…

…es correctly

git-svn-id: http://svn.pugscode.org/pugs@25965 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information...
1 parent 6aabd3a commit 036c2c92e6d22149a73058f6c934fba0dff15b2f leto committed
Showing with 67 additions and 7 deletions.
  1. +67 −7 S32-num/roots.t
View
74 S32-num/roots.t
@@ -1,11 +1,19 @@
use v6;
use Test;
-plan 8;
+plan 30;
# L<S29/Num/"=item roots">
sub approx($a, $b){
- ($a-$b).abs < 0.001;
+ my ($x,$y);
+ my $eps = 1e-3;
+ # coerce both to Complex
+ $x = $a + 0i;
+ $y = $b + 0i;
+ my $re = abs($x.re - $y.re);
+ my $im = abs($x.im - $y.im);
+ # both real and imag part must be with $eps of expected
+ return ( $re < $eps && $im < $eps );
}
sub has_approx($n, @list) {
@@ -18,6 +26,37 @@ sub has_approx($n, @list) {
}
{
+ my @l = roots(0, 1);
+ ok(@l.elems == 1, 'roots(0, 1) returns 1 element');
+ ok(has_approx(0, @l), 'roots(0, 1) contains 0');
+}
+{
+ my @l = roots(0, 0);
+ ok(@l.elems == 1, 'roots(0, 0) returns 1 element');
+ ok(@l[0] ~~ 'NaN', 'roots(0,0) returns NaN');
+}
+{
+ my @l = roots(0, -1);
+ ok(@l.elems == 1, 'roots(0, -1) returns 1 element');
+ ok(@l[0] ~~ 'NaN', 'roots(0,-1) returns NaN');
+}
+{
+ my @l = roots(100, -1);
+ ok(@l.elems == 1, 'roots(100, -1) returns 1 element');
+ ok(@l[0] ~~ 'NaN', 'roots(100,-1) returns NaN');
+}
+{
+ my @m = roots(1, 0);
+ ok(@m.elems == 1, 'roots(1, 0) returns 1 element');
+ ok(@m[0] ~~ 'NaN', 'roots(1,0) returns NaN');
+}
+{
+ my @l = roots(4, 2);
+ ok(@l.elems == 2, 'roots(4, 2) returns 2 elements');
+ ok(has_approx(2, @l), 'roots(4, 2) contains 2');
+ ok(has_approx(-2, @l), 'roots(4, 2) contains -2');
+}
+{
my @l = roots(-1, 2);
ok(@l.elems == 2, 'roots(-1, 2) returns 2 elements');
ok(has_approx(1i, @l), 'roots(-1, 2) contains 1i');
@@ -27,9 +66,30 @@ sub has_approx($n, @list) {
#?pugs todo 'feature'
{
my @l = 16.roots(4);
- ok(@l.elems == 4, 'roots(16, 4) returns 4 elements');
- ok(has_approx(2, @l), 'roots(16, 4) contains 2');
- ok(has_approx(2i, @l), 'roots(16, 4) contains 2i');
- ok(has_approx(-2, @l), 'roots(16, 4) contains -2');
- ok(has_approx(-2i, @l), 'roots(16, 4) contains -2i');
+ ok(@l.elems == 4, '16.roots(4) returns 4 elements');
+ ok(has_approx(2, @l), '16.roots(4) contains 2');
+ ok(has_approx(-2, @l), '16.roots(4) contains -2');
+ ok(has_approx(2i, @l), '16.roots(4) contains 2i');
+ ok(has_approx(-2i, @l), '16.roots(4) contains -2i');
+}
+{
+ my @l = (-1).roots(2);
+ ok(@l.elems == 2, '(-1).roots(2) returns 2 elements');
+ ok(has_approx(1i, @l), '(-1).roots(2) contains i');
+ ok(has_approx(-1i, @l), '(-1).roots(2) contains -i');
+}
+{
+ my @l = 0e0.roots(2);
+ ok(@l.elems == 2, '0e0.roots(2) returns 2 elements');
+ ok(has_approx(0, @l), '0e0.roots(2) contains 0');
+}
+{
+ my @l = roots('NaN', 1);
+ ok(@l.elems == 1, 'roots(NaN, 1) returns 1 element');
+ ok(@l[0] ~~ 'NaN', 'roots(NaN,1) returns NaN');
+}
+{
+ my @l = roots('Inf', 1);
+ ok(@l.elems == 1, 'roots(Inf, 1) returns 1 element');
+ ok(@l[0] ~~ 'Inf', 'roots(Inf,1) returns Inf');
}

0 comments on commit 036c2c9

Please sign in to comment.
Something went wrong with that request. Please try again.