Skip to content
Browse files

[t] Test suite refactoring.

Moved all builtins/math test scripts under t/spec/S29-num and S29-trig.
Applied fudge-ification.
Applied new is_approx() tests for approximate compares.


git-svn-id: http://svn.pugscode.org/pugs@19675 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information...
1 parent 4fbed9f commit ad1b86bb19d5518b93943787b33bd724cb97a6c9 cosimo committed Jan 24, 2008
Showing with 494 additions and 0 deletions.
  1. +3 −0 S29-num/abs.t
  2. +58 −0 S29-num/complex.t
  3. +28 −0 S29-num/exp.t
  4. +72 −0 S29-num/int.t
  5. +45 −0 S29-num/log.t
  6. +35 −0 S29-num/pi.t
  7. +43 −0 S29-num/rand.t
  8. +35 −0 S29-num/roots.t
  9. +52 −0 S29-num/rounders.t
  10. +24 −0 S29-trig/e.t
  11. +99 −0 S29-trig/trig.t
View
3 S29-num/abs.t
@@ -4,11 +4,14 @@ plan 40;
# L<S29/Num/"=item abs">
+#?rakudo skip 'Cannot parse pod'
+{
=pod
Basic tests for the abs() builtin
=cut
+}
for(0, 0.0, 1, 50, 60.0, 99.99) {
is(abs($_), $_, "got the right absolute value for $_");
View
58 S29-num/complex.t
@@ -0,0 +1,58 @@
+use v6-alpha;
+
+use Test;
+
+plan 129;
+
+#?rakudo skip 'Cannot parse pod'
+{
+=pod
+
+Basic tests functions specific to complex numbers.
+
+L<S29/Num/"=item cis">
+
+=cut
+}
+
+my $pi = 3.141592653589793238;
+
+is_approx((cis(0), 1 + 0i), "cis(0) == 1");
+is_approx((cis($pi), -1 + 0i), "cis(pi) == -1");
+is_approx((cis($pi / 2), 1i), "cis(pi/2) == i");
+is_approx((cis(3*$pi / 2),1i), "cis(3pi/2) == i");
+
+# L<S29/Num/"=item cis">
+# L<S29/Num/"=item unpolar">
+#
+# Test that 1.unpor == cis
+
+for 1..20 -> $i {
+ my $angle = 2 * $pi * $i / 20;
+ is_approx((cis($i), 1.unpolar($i)), "cis(x) == 1.unpolar(x) No $i");
+}
+
+# L<S29/Num/"=item unpolar">
+# L<S29/Num/"=item abs">
+#
+# Test that unpolar() doesn't change the absolute value
+
+my $counter = 1;
+for 1..10 -> $abs {
+ for 1..10 -> $a {
+ my $angle = 2 * $pi * $i / 10;
+ is_approx((abs($abs.unpolar($angle)), $abs ),
+ "unpolar doesn't change the absolute value (No $counter)");
+ $counter++;
+ }
+}
+
+# L<S29/Num/"=item unpolar">
+#
+# Basic tests for unpolar()
+
+is_approx((4.unpolar(0), 4), "4.unpolar(0) == 4");
+is_approx((4.unpolar($pi/4), 2 + 2i),"4.unpolar(pi/4) == 2+2i");
+is_approx((4.unpolar($pi/2), 4i), "4.unpolar(pi/2) == 4i");
+is_approx((4.unpolar(3*$pi/4), -2 +2i),"4.unpolar(pi/4) == -2+2i");
+is_approx((4.unpolar($pi), -4), "4.unpolar(pi) == -4");
View
28 S29-num/exp.t
@@ -0,0 +1,28 @@
+use v6-alpha;
+use Test;
+plan 44;
+
+# L<S29/Num/"=item exp">
+
+#?rakudo skip 'Cannot parse pod'
+{
+=pod
+
+Basic tests for the exp() builtin
+
+=cut
+}
+
+is_approx((exp(5), 148.4131591025766), 'got the exponent of 5');
+is_approx((exp(0), 1), 'exp(0) == 1');
+
+# exp with complex arguments
+is_approx((exp(1i*pi), -1), 'exp(i pi) == -1');
+is_approx((exp(-1i*pi), -1), 'exp(-i pi) == -1');
+
+for 1 .. 20 {
+ my $arg = 2.0 * pi / $_;
+ is_approx((exp(1i * $arg), cos($arg) + 1i * sin($arg)), 'expi == cos + i sin No. ' ~ $_);
+ is_approx((exp(1i * $arg) * exp(-1i * $arg), 1), 'exp(ix) * exp(-ix) == 1 No. ' ~ $_);
+}
+
View
72 S29-num/int.t
@@ -0,0 +1,72 @@
+use v6-alpha;
+use Test;
+plan 53;
+
+# L<S29/Num/"=item truncate">
+# truncate and int() are synonynms.
+# Possibly more tests for truncate should be added here, too.
+
+#?rakudo skip 'Cannot parse pod'
+{
+=pod
+
+Basic tests for the int() builtin
+
+=cut
+}
+
+is(int(-1), -1, "int(-1) is -1");
+is(int(0), 0, "int(0) is 0");
+is(int(1), 1, "int(1) is 1");
+is(int(3.14159265), 3, "int(3.14159265) is 3");
+is(int(-3.14159265), -3, "int(-3.14159265) is -3");
+
+is(int(0.999), 0, "int(0.999) is 0");
+is(int(0.51), 0, "int(0.51) is 0");
+is(int(0.5), 0, "int(0.5) is 0");
+is(int(0.49), 0, "int(0.49) is 0");
+is(int(0.1), 0, "int(0.1) is 0");
+
+is(int(-0.999), -0, "int(-0.999) is -0");
+is(int(-0.51), -0, "int(-0.51) is -0");
+is(int(-0.5), -0, "int(-0.5) is -0");
+is(int(-0.49), -0, "int(-0.49) is -0");
+is(int(-0.1), -0, "int(-0.1) is -0");
+
+is(int(1.999), 1, "int(1.999) is 1");
+is(int(1.51), 1, "int(1.51) is 1");
+is(int(1.5), 1, "int(1.5) is 1");
+is(int(1.49), 1, "int(1.49) is 1");
+is(int(1.1), 1, "int(1.1) is 1");
+
+is(int(-1.999), -1, "int(-1.999) is -1");
+is(int(-1.51), -1, "int(-1.51) is -1");
+is(int(-1.5), -1, "int(-1.5) is -1");
+is(int(-1.49), -1, "int(-1.49) is -1");
+is(int(-1.1), -1, "int(-1.1) is -1");
+
+sub __int( Str $s ) {
+ if ($s ~~ rx:Perl5/^(-?\d+)$/) { return $0 };
+ if ($s ~~ rx:Perl5/^(-?\d+)\./) { return $0 };
+ if ($s ~~ rx:Perl5/^\./) { return 0 };
+ return undef;
+};
+
+# Check the defaulting to $_
+
+for(0, 0.0, 1, 50, 60.0, 99.99, 0.4, 0.6,
+ -1, -50, -60.0, -99.99
+ ) {
+ my $int = __int($_);
+ is(.int, $int, "integral value for $_ is $int");
+ isa_ok(.int, "Int");
+}
+
+# Special values
+
+is(int(1.9e3), 1900, "int 1.9e3 is 1900");
+#?pugs 3 todo 'bug'
+is(int(Inf), Inf, "int Inf is Inf");
+is(int(-Inf), -Inf, "int -Inf is -Inf");
+is(int(NaN), NaN, "int NaN is NaN");
+
View
45 S29-num/log.t
@@ -0,0 +1,45 @@
+use v6-alpha;
+use Test;
+plan 11;
+
+#?rakudo skip 'Cannot parse pod'
+{
+=pod
+
+Basic tests for the log() and log10() builtins
+
+=cut
+}
+
+# L<S29/Num/"=item log">
+
+is_approx(log(5), 1.6094379124341003, 'got the log of 5');
+is_approx(log(0.1), -2.3025850929940455, 'got the log of 0.1');
+
+# L<S29/Num/"=item log10">
+
+is_approx(log10(5), 0.6989700043360187, 'got the log10 of 5');
+is_approx(log10(0.1), -0.9999999999999998, 'got the log10 of 0.1');
+
+# please add tests for complex numbers
+#
+# The closest I could find to documentation is here: http://tinyurl.com/27pj7c
+# I use 1i instead of i since I don't know if a bare i will be supported
+
+# log(exp(i pi)) = i pi log(exp(1)) = i pi
+#?pugs 2 todo 'feature'
+is_approx((log(-1 + 0i,), 0 + 1i * pi), "got the log of -1");
+is_approx((log10(-1 + 0i), 0 + 1i * pi), "got the log10 of -1");
+
+# log(exp(1+i pi)) = 1 + i pi
+#?pugs 2 todo 'feature'
+is_approx((log(-exp(1)) + 0i, 1 + 1i * pi), "got the log of -e");
+is_approx((log10(-10 + 0i), 1 + 1i * pi), "got the log10 of -10");
+
+#?pugs todo 'feature'
+is_approx((log((1+1i) / sqrt(2)), 1 + 1i * pi / 4), "got log of exp(i pi/4)");
+is_approx((log(1i), 1i * pi / 2), "got the log of i (complex unit)");
+#?pugs todo 'feature'
+is_approx((log(-1i), 1i * pi * 1.5), "got the log of -i (complex unit)");
+
+# TODO: please add more testcases for log10 of complex numbers
View
35 S29-num/pi.t
@@ -0,0 +1,35 @@
+use v6-alpha;
+use Test;
+plan 6;
+
+# L<S29/Num/"Num provides a number of constants">
+
+#?rakudo skip 'Cannot parse pod'
+{
+=head1 DESCRIPTION
+
+Basic tests for builtin Num::pi
+
+=cut
+}
+
+# See also: L<"http://theory.cs.iitm.ernet.in/~arvindn/pi/"> :)
+my $PI = 3.14159265358979323846264338327950288419716939937510;
+
+is_approx((eval("Num::pi "), $PI),
+ "Num::pi");
+
+is_approx((eval("use Num :constants; pi"), $PI),
+ "pi imported by use Num :constants");
+
+is_approx((eval("use Num :constants; 3 + pi()"), $PI+3), "
+ 3+pi(), as a sub");
+
+is_approx((eval("use Num :constants; pi() + 3"), $PI+3),
+ "pi()+3, as a sub");
+
+is_approx((eval("use Num :constants; 3 + pi"), $PI+3),
+ "3+pi, as a bareword");
+
+is_approx((eval("use Num :constants; pi + 3"), $PI+3),
+ "pi+3, as a bareword");
View
43 S29-num/rand.t
@@ -0,0 +1,43 @@
+use v6-alpha;
+
+use Test;
+
+plan 2 + 2*10 + 4;
+
+#?rakudo skip 'Cannot parse pod'
+{
+=pod
+
+Basic tests for the rand() builtin
+
+=cut
+}
+
+# L<S29/Num/"=item rand">
+
+ok(rand() >= 0, 'rand() returns numbers greater than or equal to 0');
+ok(rand() < 1, 'rand() returns numbers less than 1');
+
+for 1 .. 10 {
+ ok rand(10) >= 0, "rand(10) always returns numbers greater than or equal to 0 ($_)";
+ ok rand(10) < 10, "rand(10) always returns numbers less than 10 ($_)";
+}
+
+# L<S29/Num/"=item srand">
+
+ok(srand(1), 'srand(1) parses');
+
+sub repeat_rand ($seed) {
+ srand($seed);
+ for 1..99 { rand(); }
+ return rand();
+}
+
+ok(repeat_rand(314159) == repeat_rand(314159),
+ 'srand() provides repeatability for rand()');
+
+ok(repeat_rand(0) == repeat_rand(0),
+ 'edge case: srand(0) provides repeatability');
+
+ok(repeat_rand(0) != repeat_rand(1),
+ 'edge case: srand(0) not the same as srand(1)');
View
35 S29-num/roots.t
@@ -0,0 +1,35 @@
+use v6-alpha;
+use Test;
+plan 10;
+
+# L<S29/Num/"=item roots">
+
+sub has_approx($n, @list) {
+ for @list -> my $i {
+ if approx($i, $n) {
+ return 1;
+ }
+ }
+ return undef;
+}
+
+#?pugs todo 'feature'
+{
+ my @l = eval('roots(-1, 2)');
+ ok(!$!, 'roots($x, $n) compiles');
+ ok(@l.elems == 2, 'roots(-1, 2) returns 2 elements');
+ ok(has_approx(1i, @l), 'roots(-1, 2) contains 1i');
+ ok(has_approx(-1i, @l), 'roots(-1, 2) contains -1i');
+}
+
+#?pugs todo 'feature'
+{
+ my @l = eval('16.roots(4)');
+ ok(!$!, '$x.roots($n) compiles');
+ ok(@l.elems == 2, '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');
+}
+
View
52 S29-num/rounders.t
@@ -0,0 +1,52 @@
+use v6-alpha;
+use Test;
+plan 36;
+
+# L<S29/Num/"=item round">
+# L<S29/Num/"=item floor">
+# L<S29/Num/"=item truncate">
+# L<S29/Num/"=item ceiling">
+
+#?rakudo skip 'Cannot parse pod'
+{
+=pod
+
+Basic tests for the round(), floor(), truncate() and ceil() built-ins
+
+=cut
+}
+
+my %tests =
+ ( ceiling => [ [ 1.5, 2 ], [ 2, 2 ], [ 1.4999, 2 ],
+ [ -0.1, 0 ], [ -1, -1 ], [ -5.9, -5 ],
+ [ -0.5, 0 ], [ -0.499, 0 ], [ -5.499, -5 ] ],
+ floor => [ [ 1.5, 1 ], [ 2, 2 ], [ 1.4999, 1 ],
+ [ -0.1, -1 ], [ -1, -1 ], [ -5.9, -6 ],
+ [ -0.5, -1 ], [ -0.499, -1 ], [ -5.499, -6 ] ],
+ round => [ [ 1.5, 2 ], [ 2, 2 ], [ 1.4999, 1 ],
+ [ -0.1, 0 ], [ -1, -1 ], [ -5.9, -6 ],
+ [ -0.5, -1 ], [ -0.499, 0 ], [ -5.499, -5 ] ],
+ truncate => [ [ 1.5, 1 ], [ 2, 2 ], [ 1.4999, 1 ],
+ [ -0.1, 0 ], [ -1, -1 ], [ -5.9, -5 ],
+ [ -0.5, 0 ], [ -0.499, 0 ], [ -5.499, -5 ] ],
+ );
+
+if $?PUGS_BACKEND ne "BACKEND_PUGS" {
+ skip_rest "PIL2JS and PIL-Run do not support eval() yet.";
+ exit;
+}
+
+for %tests.keys.sort -> $type {
+ my @subtests = @(%tests{$type}); # XXX .[] doesn't work yet!
+ for @subtests -> $test {
+ my $code = "{$type}($test[0])";
+ my $res = eval($code);
+ if ($!) {
+ #?pugs todo 'feature'
+ flunk("failed to parse $code ($!)");
+ } else {
+ is($res, $test[1], "$code == $test[1]");
+ }
+ }
+}
+
View
24 S29-trig/e.t
@@ -0,0 +1,24 @@
+use v6-alpha;
+use Test;
+plan 6;
+
+# L<S29/"The :Trig tag">
+
+#?rakudo skip 'Cannot parse pod'
+{
+=head1 DESCRIPTION
+
+Basic tests for trigonometric functions.
+
+=cut
+}
+
+# See also: L<"http://en.wikipedia.org/wiki/E_%28mathematical_constant%29"> :)
+my $e = 2.71828182845904523536;
+
+is_approx(e , $e), "e, as a bareword");
+is_approx(e() , $e), "e(), as a sub");
+is_approx(1 + e(), $e+1), "1+e(), as a sub");
+is_approx(e() + 1, $e+1), "e()+1, as a sub");
+is_approx(1 + e, $e+1), "1+e, as a bareword");
+is_approx(e + 1, $e+1), "e+1, as a bareword");
View
99 S29-trig/trig.t
@@ -0,0 +1,99 @@
+use v6-alpha;
+use Test;
+plan 51;
+
+# L<S29/"The :Trig tag">
+
+#?rakudo skip 'Cannot parse pod'
+{
+=head1 DESCRIPTION
+
+Basic tests for trigonometric functions.
+
+=cut
+}
+
+# See also: L<"http://theory.cs.iitm.ernet.in/~arvindn/pi/"> :)
+my $PI = 3.14159265358979323846264338327950288419716939937510;
+
+# -- pi
+is_approx((pi, $PI), "pi()");
+is_approx((pi + 3, $PI + 3), "'pi() + 3' may drop its parentheses before +3");
+
+# -- atan
+# The basic form of atan (one argument) returns a value in ]-pi, pi[.
+# Quadrants I, III
+is_approx((atan(1) / $PI * 180, 45));
+is_approx((atan(1/3*sqrt(3)) / $PI * 180, 30));
+is_approx((atan(sqrt(3)) / $PI * 180, 60));
+
+# Quadrants II, IV
+is_approx((atan(-1) / $PI * 180, -45));
+is_approx((atan(-1/3*sqrt(3)) / $PI * 180, -30));
+is_approx((atan(-sqrt(3)) / $PI * 180, -60));
+
+# S29: This second form of C<atan> computes the arctangent of $y/$x, and
+# **takes the quadrant into account**.
+# Quadrant I
+is_approx((atan(1, 1) / $PI * 180, 45));
+is_approx((atan(1, sqrt(3)) / $PI * 180, 30));
+is_approx((atan(1, 1/3*sqrt(3)) / $PI * 180, 60));
+
+# Quadrant II
+is_approx((atan(1, -1) / $PI * 180, 135));
+is_approx((atan(1, -1/3*sqrt(3)) / $PI * 180, 120));
+is_approx((atan(1, -sqrt(3)) / $PI * 180, 150));
+
+# Quadrant III
+is_approx((atan(-1, -1) / $PI * 180 + 360, 225));
+is_approx((atan(-1, -sqrt(3)) / $PI * 180 + 360, 210));
+is_approx((atan(-1, -1/3*sqrt(3)) / $PI * 180 + 360, 240));
+
+# Quadrant IV
+is_approx((atan(-1, 1) / $PI * 180 + 360, 315));
+is_approx((atan(-1, sqrt(3)) / $PI * 180 + 360, 330));
+is_approx((atan(-1, 1/3*sqrt(3)) / $PI * 180 + 360, 300));
+
+# -- sin, cos, tan
+# sin
+is_approx((sin(0/4*$PI), 0));
+is_approx((sin(1/4*$PI), 1/2*sqrt(2)));
+is_approx((sin(2/4*$PI), 1));
+is_approx((sin(3/4*$PI), 1/2*sqrt(2)));
+is_approx((sin(4/4*$PI), 0));
+is_approx((sin(5/4*$PI), -1/2*sqrt(2)));
+is_approx((sin(6/4*$PI), -1));
+is_approx((sin(7/4*$PI), -1/2*sqrt(2)));
+is_approx((sin(8/4*$PI), 0));
+
+# cos
+is_approx((cos(0/4*$PI), 1));
+is_approx((cos(1/4*$PI), 1/2*sqrt(2)));
+is_approx((cos(2/4*$PI), 0));
+is_approx((cos(3/4*$PI), -1/2*sqrt(2)));
+is_approx((cos(4/4*$PI), -1));
+is_approx((cos(5/4*$PI), -1/2*sqrt(2)));
+is_approx((cos(6/4*$PI), 0));
+is_approx((cos(7/4*$PI), 1/2*sqrt(2)));
+is_approx((cos(8/4*$PI), 1));
+
+# tan
+is_approx((tan(0/4*$PI), 0));
+is_approx((tan(1/4*$PI), 1));
+is_approx((tan(3/4*$PI), -1));
+is_approx((tan(4/4*$PI), 0));
+is_approx((tan(5/4*$PI), 1));
+is_approx((tan(7/4*$PI), -1));
+is_approx((tan(8/4*$PI), 0));
+
+# asin
+is_approx((try {asin(0)}, 0));
+#?pugs 2 todo 'feature'
+is_approx((try {asin(1/2*sqrt(2))}, 1/4*$PI));
+is_approx((try {asin(1)}, 2/4*$PI));
+
+# acos
+#?pugs 2 todo 'feature'
+is_approx((try {acos(0)}, 2/4*$PI));
+is_approx((try {acos(1/2*sqrt(2))}, 1/4*$PI));
+is_approx((try {acos(1)}, 0/4*$PI));

0 comments on commit ad1b86b

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