Permalink
Browse files

Fix bug in recursive script, add testing harness.

  • Loading branch information...
1 parent 12732f2 commit 63e0ebb67d1da115299b8c4b913e29fb15c5ab65 @colomon committed Dec 17, 2012
Showing with 26 additions and 2 deletions.
  1. +2 −2 bin/collatz-recursive.pl
  2. +24 −0 bin/testing-harness.pl
View
@@ -1,8 +1,8 @@
sub collatz-length(Int $n) {
given $n {
when 1 { 1 }
- when * %% 2 { collatz-length($_ / 2) }
- default { collatz-length(3 * $_ + 1) }
+ when * %% 2 { 1 + collatz-length($_ div 2) }
+ default { 1 + collatz-length(3 * $_ + 1) }
}
}
View
@@ -0,0 +1,24 @@
+my $perl6 = @*ARGS.shift;
+my @numbers = 1..100, 10000..10100;
+
+sub collatz-length(Int $n) {
+ given $n {
+ when 1 { 1 }
+ when * %% 2 { 1 + collatz-length($_ div 2) }
+ default { 1 + collatz-length(3 * $_ + 1) }
+ }
+}
+
+for @*ARGS -> $script {
+ my $results = qqx/$perl6 $script { @numbers }/;
+ my @mistakes;
+ for $results.lines.map({ $_.comb(/\d+/) }) -> $n, $length {
+ push @mistakes, $n if collatz-length($n.Int) != $length;
+ }
+ if @mistakes {
+ say "Mistakes in $script: { @mistakes }";
+ } else {
+ say "$script correct";
+ }
+}
+

0 comments on commit 63e0ebb

Please sign in to comment.