Browse files

[t] added named arg tests to trig and num functions

git-svn-id: http://svn.pugscode.org/pugs@25134 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information...
1 parent 8f45c7a commit 576906b48b584b3be9e71388c1df3c730958ee12 frew committed Jan 31, 2009
Showing with 363 additions and 16 deletions.
  1. +23 −1 S29-num/complex.t
  2. +11 −2 S29-num/exp.t
  3. +13 −4 S29-num/int.t
  4. +14 −2 S29-num/log.t
  5. +19 −1 S29-num/rand.t
  6. +19 −2 S29-num/rounders.t
  7. +12 −2 S29-num/sign.t
  8. +8 −1 S29-num/sqrt.t
  9. +244 −1 S29-trig/trig.t
View
24 S29-num/complex.t
@@ -2,7 +2,7 @@ use v6;
use Test;
-plan 129;
+plan 138;
=begin pod
@@ -21,6 +21,14 @@ my $pi = 3.141592653589793238;
is_approx(cis(3*$pi / 2),-1i, "cis(3pi/2) == -i");
}
+#?rakudo skip 'named args'
+{
+ is_approx(cis(:angle(0)), 1 + 0i, "cis(:angle(0)) == 1");
+ is_approx(cis(:angle($pi)), -1 + 0i, "cis(:angle(pi)) == -1");
+ is_approx(cis(:angle($pi / 2)), 1i, "cis(:angle(pi/2)) == i");
+ is_approx(cis(:angle(3*$pi / 2)),-1i, "cis(:angle(3pi/2)) == -i");
+}
+
# L<S29/Num/"=item cis">
# L<S29/Num/"=item unpolar">
#
@@ -50,6 +58,20 @@ my $pi = 3.141592653589793238;
}
}
+#?rakudo skip 'named args'
+{
+ # L<S29/Num/"=item unpolar">
+ #
+ # Basic tests for unpolar()
+ my $s = 2 * sqrt(2);
+
+ is_approx(unpolar(:mag(4), 0), 4, "unpolar(:mag(4), 0) == 4");
+ is_approx(unpolar(:mag(4), $pi/4), $s + ($s)i ,"unpolar(:mag(4), pi/4) == 2+2i");
+ is_approx(unpolar(:mag(4), $pi/2), 4i, "unpolar(:mag(4), pi/2) == 4i");
+ is_approx(unpolar(:mag(4), 3*$pi/4), -$s + ($s)i,"unpolar(:mag(4), pi/4) == -2+2i");
+ is_approx(unpolar(:mag(4), $pi), -4, "unpolar(:mag(4), pi) == -4");
+}
+
{
# L<S29/Num/"=item unpolar">
#
View
13 S29-num/exp.t
@@ -1,15 +1,24 @@
use v6;
use Test;
-plan 44;
+plan 48;
# L<S29/Num/"=item exp">
-=begin pod
+=begin pod
Basic tests for the exp() builtin
=end pod
+#?rakudo skip 'named args'
+{
+ is_approx(exp(:exponent(5)), 148.4131591025766, 'got the exponent of 5');
+ is_approx(exp(:exponent(0)), 1, 'exp(:exponent(0)) == 1');
+
+ is_approx(exp(:exponent(1i*pi)), -1, 'exp(:exponent(i pi)) == -1');
+ is_approx(exp(:exponent(-1i*pi)), -1, 'exp(:exponent(-i pi)) == -1');
+}
+
is_approx(exp(5), 148.4131591025766, 'got the exponent of 5');
is_approx(exp(0), 1, 'exp(0) == 1');
View
17 S29-num/int.t
@@ -1,10 +1,10 @@
use v6;
use Test;
-plan 61;
+plan 66;
# L<S29/Num/"=item truncate">
-# truncate and int() are synonynms.
-# Possibly more tests for truncate should be added here, too.
+# truncate and int() are synonynms.
+# Possibly more tests for truncate should be added here, too.
=begin pod
@@ -21,6 +21,15 @@ 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");
+#?rakudo skip 'named args'
+{
+ is(int(:x(-1)), -1, "int(-1) is -1");
+ is(int(:x(0)), 0, "int(0) is 0");
+ is(int(:x(1)), 1, "int(1) is 1");
+ is(int(:x(3.14159265)), 3, "int(3.14159265) is 3");
+ is(int(:x(-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");
@@ -60,7 +69,7 @@ is(int('3e4d5'), 3e4, "int('3e4d5') is 3e4");
return $s;
};
- # Check the defaulting to $_
+ # 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($_);
View
16 S29-num/log.t
@@ -1,6 +1,6 @@
use v6;
use Test;
-plan 12;
+plan 16;
=begin pod
@@ -13,16 +13,28 @@ Basic tests for the log() and log10() builtins
is_approx(log(5), 1.6094379124341003, 'got the log of 5');
is_approx(log(0.1), -2.3025850929940455, 'got the log of 0.1');
+#?rakudo skip 'named args'
+{
+ is_approx(log(:x(5)), 1.6094379124341003, 'got the log of 5');
+ is_approx(log(:x(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');
+#?rakudo skip 'named args'
+{
+ is_approx(log10(:x(5)), 0.6989700043360187, 'got the log10 of 5');
+ is_approx(log10(:x(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");
View
20 S29-num/rand.t
@@ -2,7 +2,7 @@ use v6;
use Test;
-plan 6;
+plan 9;
=begin pod
@@ -35,3 +35,21 @@ lives_ok { srand(1) }, 'srand(1) lives and parses';
ok(repeat_rand(0) != repeat_rand(1),
'edge case: srand(0) not the same as srand(1)');
}
+
+#?rakudo skip 'named args'
+{
+ sub repeat_rand ($seed) {
+ srand(:x($seed));
+ for 1..99 { rand; }
+ return rand;
+ }
+
+ ok(repeat_rand(314159) == repeat_rand(314159),
+ 'srand(:x(...)) provides repeatability for rand');
+
+ ok(repeat_rand(0) == repeat_rand(0),
+ 'edge case: srand(:x(0)) provides repeatability');
+
+ ok(repeat_rand(0) != repeat_rand(1),
+ 'edge case: srand(:x(0)) not the same as srand(:x(1))');
+}
View
21 S29-num/rounders.t
@@ -1,6 +1,6 @@
use v6;
use Test;
-plan 40;
+plan 42;
# L<S29/Num/"=item round">
# L<S29/Num/"=item floor">
@@ -37,7 +37,6 @@ for %tests.keys.sort -> $type {
my @subtests = @(%tests{$type}); # XXX .[] doesn't work yet!
for @subtests -> $test {
my $code = "{$type}({$test[0]})";
-# say $code;
my $res = eval($code);
if ($!) {
@@ -49,6 +48,24 @@ for %tests.keys.sort -> $type {
}
}
+#?rakudo skip 'named args'
+{
+for %tests.keys.sort -> $type {
+ my @subtests = @(%tests{$type}); # XXX .[] doesn't work yet!
+ for @subtests -> $test {
+ my $code = "{$type}(:x({$test[0]}))";
+ my $res = eval($code);
+
+ if ($!) {
+ #?pugs todo 'feature'
+ flunk("failed to parse $code ($!)");
+ } else {
+ ok($res == $test[1], "$code == {$test[1]}");
+ }
+ }
+ }
+}
+
for %tests.keys.sort -> $t {
isa_ok eval("{$t}(1.1)"), Int, "rounder $t returns an int";
View
14 S29-num/sign.t
@@ -1,10 +1,10 @@
use v6;
use Test;
-plan 6;
+plan 11;
# L<S29/Num/"=item sign">
-=begin pod
+=begin pod
Basic tests for the sign() builtin
@@ -15,5 +15,15 @@ is(sign(-100), -1, 'got the right sign for -100');
is(sign(100), 1, 'got the right sign for 100');
is(sign(1.5), 1, 'got the right sign for 1.5');
is(sign(-1.5), -1, 'got the right sign for -1.5');
+
+#?rakudo skip 'named args'
+{
+ is(sign(:x(0)), 0, 'got the right sign for 0');
+ is(sign(:x(-100)), -1, 'got the right sign for -100');
+ is(sign(:x(100)), 1, 'got the right sign for 100');
+ is(sign(:x(1.5)), 1, 'got the right sign for 1.5');
+ is(sign(:x(-1.5)), -1, 'got the right sign for -1.5');
+}
+
#?rakudo todo 'feature'
dies_ok { sign(undef) }, 'sign on undefined value fails';
View
9 S29-num/sqrt.t
@@ -1,6 +1,6 @@
use v6;
use Test;
-plan 7;
+plan 10;
# L<S29/Num/"=item sqrt">
@@ -14,6 +14,13 @@ is_approx(sqrt(2), 1.4142135623730951, 'got the square root of 2');
is_approx(sqrt(5), 2.23606797749979, 'got the square root of 5');
is(sqrt(-1), NaN, 'sqrt(-1) is NaN');
+#?rakudo skip 'named args'
+{
+ is_approx(sqrt(:x(2)), 1.4142135623730951, 'got the square root of 2 with named args');
+ is_approx(sqrt(:x(5)), 2.23606797749979, 'got the square root of 5 with named args');
+ is(sqrt(:x(-1)), NaN, 'sqrt(:x(-1)) is NaN');
+}
+
# The spec specifies a branch cut in the complex plane of -pi <= theta <= pi
is_approx(sqrt(-1 +0i), 1i, 'got the square root of -1+0i');
View
245 S29-trig/trig.t
@@ -1,6 +1,6 @@
use v6;
use Test;
-plan 68;
+plan 200;
# L<S29/"The :Trig tag">
@@ -139,3 +139,246 @@ is_approx( cotanh(1), 1/tanh(1) );
# asech
is_approx( asech(1), 0 );
+
+#?rakudo skip 'named args'
+{
+
+# -- atan
+# The basic form of atan (one argument) returns a value in ]-pi, pi[.
+# Quadrants I, III
+is_approx(atan(:x(1)) / $PI * 180, 45);
+is_approx(atan(:x(1/3*sqrt(3))) / $PI * 180, 30);
+is_approx(atan(:x(sqrt(3))) / $PI * 180, 60);
+
+# Quadrants II, IV
+is_approx(atan(:x(-1)) / $PI * 180, -45);
+is_approx(atan(:x(-1/3*sqrt(3))) / $PI * 180, -30);
+is_approx(atan(:x(-sqrt(3))) / $PI * 180, -60);
+
+# S29: C<atan2> computes the arctangent of $y/$x, and
+# **takes the quadrant into account**. The second argument is
+# assumed to be 1 if it is not present.
+# Quadrant I
+is_approx(atan2(:x(1), 1) / $PI * 180, 45);
+is_approx(atan2(:x(1)) / $PI * 180, 45);
+is_approx(atan2(:x(1), sqrt(3)) / $PI * 180, 30);
+is_approx(atan2(:x(1), 1/3*sqrt(3)) / $PI * 180, 60);
+
+# Quadrant II
+is_approx(atan2(:x(1), -1) / $PI * 180, 135);
+is_approx(atan2(:x(1), -1/3*sqrt(3)) / $PI * 180, 120);
+is_approx(atan2(:x(1), -sqrt(3)) / $PI * 180, 150);
+
+# Quadrant III
+is_approx(atan2(x => -1, -1) / $PI * 180 + 360, 225);
+is_approx(atan2(x => -1, -sqrt(3)) / $PI * 180 + 360, 210);
+is_approx(atan2(x => -1, -1/3*sqrt(3)) / $PI * 180 + 360, 240);
+
+# Quadrant IV
+is_approx(atan2(x => -1, 1) / $PI * 180 + 360, 315);
+is_approx(atan2(x => -1) / $PI * 180 + 360, 315);
+is_approx(atan2(x => -1, sqrt(3)) / $PI * 180 + 360, 330);
+is_approx(atan2(x => -1, 1/3*sqrt(3)) / $PI * 180 + 360, 300);
+
+# -- sin, cos, tan
+# sin
+is_approx(sin(:x(0/4*$PI)), 0);
+is_approx(sin(:x(1/4*$PI)), 1/2*sqrt(2));
+is_approx(sin(:x(2/4*$PI)), 1);
+is_approx(sin(:x(3/4*$PI)), 1/2*sqrt(2));
+is_approx(sin(:x(4/4*$PI)), 0);
+is_approx(sin(:x(5/4*$PI)), -1/2*sqrt(2));
+is_approx(sin(:x(6/4*$PI)), -1);
+is_approx(sin(:x(7/4*$PI)), -1/2*sqrt(2));
+is_approx(sin(:x(8/4*$PI)), 0);
+
+# cos
+is_approx(cos(:x(0/4*$PI)), 1);
+is_approx(cos(:x(1/4*$PI)), 1/2*sqrt(2));
+is_approx(cos(:x(2/4*$PI)), 0);
+is_approx(cos(:x(3/4*$PI)), -1/2*sqrt(2));
+is_approx(cos(:x(4/4*$PI)), -1);
+is_approx(cos(:x(5/4*$PI)), -1/2*sqrt(2));
+is_approx(cos(:x(6/4*$PI)), 0);
+is_approx(cos(:x(7/4*$PI)), 1/2*sqrt(2));
+is_approx(cos(:x(8/4*$PI)), 1);
+
+# tan
+is_approx(tan(:x(0/4*$PI)), 0);
+is_approx(tan(:x(1/4*$PI)), 1);
+is_approx(tan(:x(3/4*$PI)), -1);
+is_approx(tan(:x(4/4*$PI)), 0);
+is_approx(tan(:x(5/4*$PI)), 1);
+is_approx(tan(:x(7/4*$PI)), -1);
+is_approx(tan(:x(8/4*$PI)), 0);
+
+# sec
+is_approx(sec(:x(0)), 1);
+is_approx(sec(:x($PI)), -1);
+
+
+# asin
+is_approx(asin(:x(0)), 0);
+#?pugs 2 todo 'feature'
+is_approx(asin(:x(1/2*sqrt(2))), 1/4*$PI);
+is_approx(asin(:x(1)), 2/4*$PI);
+
+# acos
+#?pugs 2 todo 'feature'
+is_approx(acos(:x(0)), 2/4*$PI);
+is_approx(acos(:x(1/2*sqrt(2))), 1/4*$PI);
+is_approx(acos(:x(1)), 0/4*$PI);
+
+
+# cosh
+is_approx( cosh(:x(0)), 1);
+is_approx( cosh(:x(1)), 0.5*(exp(1) + exp(-1)) );
+
+# sinh
+is_approx( sinh(:x(0)), 0);
+is_approx( sinh(:x(1)), 0.5*(exp(1) - exp(-1)) );
+
+# asinh
+is_approx( asinh(:x(0)), 0 );
+
+# acosh
+is_approx( acosh(:x(1)), 0 );
+
+# tanh
+is_approx( tanh(:x(0)), 0);
+is_approx( tanh(:x(1)), sinh(1)/cosh(1) );
+
+# atanh
+is_approx( atanh(:x(0)), 0 );
+
+# sech
+is_approx( sech(:x(0)), 1 );
+
+# cosech
+is_approx( cosech(:x(2)), 1/sinh(2) );
+
+# cotanh
+is_approx( cotanh(:x(1)), 1/tanh(1) );
+
+# asech
+is_approx( asech(:x(1)), 0 );
+# -- atan
+# The basic form of atan (one argument) returns a value in ]-pi, pi[.
+# Quadrants I, III
+is_approx(atan(:x(1)) / $PI * 180, 45);
+is_approx(atan(:x(1/3*sqrt(3))) / $PI * 180, 30);
+is_approx(atan(:x(sqrt(3))) / $PI * 180, 60);
+
+# Quadrants II, IV
+is_approx(atan(:x(-1)) / $PI * 180, -45);
+is_approx(atan(:x(-1/3*sqrt(3))) / $PI * 180, -30);
+is_approx(atan(:x(-sqrt(3))) / $PI * 180, -60);
+
+# S29: C<atan2> computes the arctangent of $y/$x, and
+# **takes the quadrant into account**. The second argument is
+# assumed to be 1 if it is not present.
+# Quadrant I
+is_approx(atan2(x => 1, 1) / $PI * 180, 45);
+is_approx(atan2(:x(1)) / $PI * 180, 45);
+is_approx(atan2(x => 1, sqrt(3)) / $PI * 180, 30);
+is_approx(atan2(x => 1, 1/3*sqrt(3)) / $PI * 180, 60);
+
+# Quadrant II
+is_approx(atan2(x => 1, -1) / $PI * 180, 135);
+is_approx(atan2(x => 1, -1/3*sqrt(3)) / $PI * 180, 120);
+is_approx(atan2(x => 1, -sqrt(3)) / $PI * 180, 150);
+
+# Quadrant III
+is_approx(atan2(x => -1, -1) / $PI * 180 + 360, 225);
+is_approx(atan2(x => -1, -sqrt(3)) / $PI * 180 + 360, 210);
+is_approx(atan2(x => -1, -1/3*sqrt(3)) / $PI * 180 + 360, 240);
+
+# Quadrant IV
+is_approx(atan2(x => -1, 1) / $PI * 180 + 360, 315);
+is_approx(atan2(x => -1) / $PI * 180 + 360, 315);
+is_approx(atan2(x => -1, sqrt(3)) / $PI * 180 + 360, 330);
+is_approx(atan2(x => -1, 1/3*sqrt(3)) / $PI * 180 + 360, 300);
+
+# -- sin, cos, tan
+# sin
+is_approx(sin(:x(0/4*$PI)), 0);
+is_approx(sin(:x(1/4*$PI)), 1/2*sqrt(2));
+is_approx(sin(:x(2/4*$PI)), 1);
+is_approx(sin(:x(3/4*$PI)), 1/2*sqrt(2));
+is_approx(sin(:x(4/4*$PI)), 0);
+is_approx(sin(:x(5/4*$PI)), -1/2*sqrt(2));
+is_approx(sin(:x(6/4*$PI)), -1);
+is_approx(sin(:x(7/4*$PI)), -1/2*sqrt(2));
+is_approx(sin(:x(8/4*$PI)), 0);
+
+# cos
+is_approx(cos(:x(0/4*$PI)), 1);
+is_approx(cos(:x(1/4*$PI)), 1/2*sqrt(2));
+is_approx(cos(:x(2/4*$PI)), 0);
+is_approx(cos(:x(3/4*$PI)), -1/2*sqrt(2));
+is_approx(cos(:x(4/4*$PI)), -1);
+is_approx(cos(:x(5/4*$PI)), -1/2*sqrt(2));
+is_approx(cos(:x(6/4*$PI)), 0);
+is_approx(cos(:x(7/4*$PI)), 1/2*sqrt(2));
+is_approx(cos(:x(8/4*$PI)), 1);
+
+# tan
+is_approx(tan(:x(0/4*$PI)), 0);
+is_approx(tan(:x(1/4*$PI)), 1);
+is_approx(tan(:x(3/4*$PI)), -1);
+is_approx(tan(:x(4/4*$PI)), 0);
+is_approx(tan(:x(5/4*$PI)), 1);
+is_approx(tan(:x(7/4*$PI)), -1);
+is_approx(tan(:x(8/4*$PI)), 0);
+
+# sec
+is_approx(sec(:x(0)), 1);
+is_approx(sec(:x($PI)), -1);
+
+
+# asin
+is_approx(asin(:x(0)), 0);
+#?pugs 2 todo 'feature'
+is_approx(asin(:x(1/2*sqrt(2))), 1/4*$PI);
+is_approx(asin(:x(1)), 2/4*$PI);
+
+# acos
+#?pugs 2 todo 'feature'
+is_approx(acos(:x(0)), 2/4*$PI);
+is_approx(acos(:x(1/2*sqrt(2))), 1/4*$PI);
+is_approx(acos(:x(1)), 0/4*$PI);
+
+
+# cosh
+is_approx( cosh(:x(0)), 1);
+is_approx( cosh(:x(1)), 0.5*(exp(1) + exp(-1)) );
+
+# sinh
+is_approx( sinh(:x(0)), 0);
+is_approx( sinh(:x(1)), 0.5*(exp(1) - exp(-1)) );
+
+# asinh
+is_approx( asinh(:x(0)), 0 );
+
+# acosh
+is_approx( acosh(:x(1)), 0 );
+
+# tanh
+is_approx( tanh(:x(0)), 0);
+is_approx( tanh(:x(1)), sinh(1)/cosh(1) );
+
+# atanh
+is_approx( atanh(:x(0)), 0 );
+
+# sech
+is_approx( sech(:x(0)), 1 );
+
+# cosech
+is_approx( cosech(:x(2)), 1/sinh(2) );
+
+# cotanh
+is_approx( cotanh(:x(1)), 1/tanh(1) );
+
+# asech
+is_approx( asech(:x(1)), 0 );
+}

0 comments on commit 576906b

Please sign in to comment.