1N/A#!./perl
1N/A
1N/A#
1N/A# test recursive functions.
1N/A#
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = qw(. ../lib);
1N/A require "test.pl";
1N/A plan(tests => 28);
1N/A}
1N/A
1N/Ause strict;
1N/A
1N/Asub gcd {
1N/A return gcd($_[0] - $_[1], $_[1]) if ($_[0] > $_[1]);
1N/A return gcd($_[0], $_[1] - $_[0]) if ($_[0] < $_[1]);
1N/A $_[0];
1N/A}
1N/A
1N/Asub factorial {
1N/A $_[0] < 2 ? 1 : $_[0] * factorial($_[0] - 1);
1N/A}
1N/A
1N/Asub fibonacci {
1N/A $_[0] < 2 ? 1 : fibonacci($_[0] - 2) + fibonacci($_[0] - 1);
1N/A}
1N/A
1N/A# Highly recursive, highly aggressive.
1N/A# Kids, don't try this at home.
1N/A#
1N/A# For example ackermann(4,1) will take quite a long time.
1N/A# It will simply eat away your memory. Trust me.
1N/A
1N/Asub ackermann {
1N/A return $_[1] + 1 if ($_[0] == 0);
1N/A return ackermann($_[0] - 1, 1) if ($_[1] == 0);
1N/A ackermann($_[0] - 1, ackermann($_[0], $_[1] - 1));
1N/A}
1N/A
1N/A# Highly recursive, highly boring.
1N/A
1N/Asub takeuchi {
1N/A $_[1] < $_[0] ?
1N/A takeuchi(takeuchi($_[0] - 1, $_[1], $_[2]),
1N/A takeuchi($_[1] - 1, $_[2], $_[0]),
1N/A takeuchi($_[2] - 1, $_[0], $_[1]))
1N/A : $_[2];
1N/A}
1N/A
1N/Ais(gcd(1147, 1271), 31, "gcd(1147, 1271) == 31");
1N/A
1N/Ais(gcd(1908, 2016), 36, "gcd(1908, 2016) == 36");
1N/A
1N/Ais(factorial(10), 3628800, "factorial(10) == 3628800");
1N/A
1N/Ais(factorial(factorial(3)), 720, "factorial(factorial(3)) == 720");
1N/A
1N/Ais(fibonacci(10), 89, "fibonacci(10) == 89");
1N/A
1N/Ais(fibonacci(fibonacci(7)), 17711, "fibonacci(fibonacci(7)) == 17711");
1N/A
1N/Amy @ack = qw(1 2 3 4 2 3 4 5 3 5 7 9 5 13 29 61);
1N/A
1N/Afor my $x (0..3) {
1N/A for my $y (0..3) {
1N/A my $a = ackermann($x, $y);
1N/A is($a, shift(@ack), "ackermann($x, $y) == $a");
1N/A }
1N/A}
1N/A
1N/Amy ($x, $y, $z) = (18, 12, 6);
1N/A
1N/Ais(takeuchi($x, $y, $z), $z + 1, "takeuchi($x, $y, $z) == $z + 1");
1N/A
1N/A{
1N/A sub get_first1 {
1N/A get_list1(@_)->[0];
1N/A }
1N/A
1N/A sub get_list1 {
1N/A return [curr_test] unless $_[0];
1N/A my $u = get_first1(0);
1N/A [$u];
1N/A }
1N/A my $x = get_first1(1);
1N/A ok($x, "premature FREETMPS (change 5699)");
1N/A}
1N/A
1N/A{
1N/A sub get_first2 {
1N/A return get_list2(@_)->[0];
1N/A }
1N/A
1N/A sub get_list2 {
1N/A return [curr_test] unless $_[0];
1N/A my $u = get_first2(0);
1N/A return [$u];
1N/A }
1N/A my $x = get_first2(1);
1N/A ok($x, "premature FREETMPS (change 5699)");
1N/A}
1N/A
1N/A{
1N/A local $^W = 0; # We do not need recursion depth warning.
1N/A
1N/A sub sillysum {
1N/A return $_[0] + ($_[0] > 0 ? sillysum($_[0] - 1) : 0);
1N/A }
1N/A
1N/A is(sillysum(1000), 1000*1001/2, "recursive sum of 1..1000");
1N/A}
1N/A
1N/A# check ok for recursion depth > 65536
1N/A{
1N/A my $r;
1N/A eval {
1N/A $r = runperl(
1N/A nolib => 1,
1N/A stderr => 1,
1N/A prog => q{$d=0; $e=1; sub c { ++$d; if ($d > 66000) { $e=0 } else { c(); c() unless $d % 32768 } --$d } c(); exit $e});
1N/A };
1N/A SKIP: {
1N/A skip("Out of memory -- increase your data/heap?", 2)
1N/A if $r =~ /Out of memory/i;
1N/A is($r, '', "64K deep recursion - no output expected");
1N/A
1N/A if ($^O eq 'MacOS') {
1N/A ok(1, "$^O: \$? is unreliable");
1N/A } else {
1N/A is($?, 0, "64K deep recursion - no coredump expected");
1N/A }
1N/A
1N/A }
1N/A}
1N/A