# tsanta.p6. Pugs version of tsanta.pl. use v6; sub length (Str $s) returns Int { +split("", $s) } sub golf_score (Str $script) returns Int { my $fh = open($script) or die("open '$script' failed"); my $golf = 0; my $dollar_dot = 0; # $. not implemented yet for =$fh -> $line { ++$dollar_dot; $golf += length($line) - 1 unless $dollar_dot==1 && $line.index("#!") == 0; } $fh.close(); return $golf; } sub print_golf_score (*@scr) { my $tot = 0; for @scr -> $s { $tot += golf_score($s) } print "You shot a round of $tot strokes.\n"; } sub build_file (Str $fname, Str $data) { my $fh = open('>'~$fname) or die("open '$fname' failed"); print($fh, $data) or die("print '$fname' failed"); $fh.close(); } sub slurp_file (Str $fname) returns Str { my $fh = open($fname) or die("open '$fname' failed"); my $s; for =$fh -> $line { $s ~= $line } $fh.close(); return $s; } sub check_one (Str $scr, Str $label, Str $data, Str $exp) { my $intmp = 'in.tmp'; my $outtmp = 'out.tmp'; build_file($intmp, $data); my $cmd = "pugs $scr $intmp >$outtmp"; print("$label : running: '$cmd'..."); # my $out = `$cmd`; # not implemented yet system($cmd); my $rc = 0; my $out = slurp_file($outtmp); print "done (rc=$rc).\n"; if ($out ne $exp) { $*ERR.print("Expected:\n$exp"); $*ERR.print("Got:\n$out"); die("Oops, you failed.\n"); } } # ----------------------------------------------------- my $file1 = "1st line "; my $file2 = "1st line 2nd line "; my $file3 = "1st line 2nd line 3rd line "; my $file4 = "1st line 2nd line 3rd line 4th line "; my $file12 = "1st line 2nd line 3rd line 4th line 5th line 6th line 7th line 8th line 9th line 10th line 11th line 12th line "; my $file21 = "1st line 2nd line 3rd line 4th line 5th line 6th line 7th line 8th line 9th line 10th line 11th line 12th line "; # ----------------------------------------------------- sub check_head (Str $scr) { my @tt = ( [ 'file1', $file1, "1st line\n" ], [ 'file2', $file2, "1st line\n2nd line\n" ], [ 'file3', $file3, "1st line\n2nd line\n3rd line\n" ], [ 'file12', $file12, "1st line\n2nd line\n3rd line\n4th line\n5th line\n"~ "6th line\n7th line\n8th line\n9th line\n10th line\n" ] ); for @tt -> $f0, $f1, $f2 { check_one($scr, $f0, $f1, $f2) } } sub check_tail (Str $scr) { my @tt = ( [ 'file1', $file1, "1st line\n" ], [ 'file2', $file2, "1st line\n2nd line\n" ], [ 'file3', $file3, "1st line\n2nd line\n3rd line\n" ], [ 'file12', $file12, "3rd line\n4th line\n5th line\n6th line\n7th line\n"~ "8th line\n9th line\n10th line\n11th line\n12th line\n" ], [ 'file21', $file21, "12th line\n\n\n\n\n\n\n\n\n\n" ] ); for @tt -> $f0, $f1, $f2 { check_one($scr, $f0, $f1, $f2) } } sub check_rev (Str $scr) { my @tt = ( [ 'file1', $file1, "1st line\n" ], [ 'file2', $file2, "2nd line\n1st line\n" ], [ 'file3', $file3, "3rd line\n2nd line\n1st line\n" ], [ 'file21', $file21, "\n\n\n\n\n\n\n\n\n12th line\n11th line\n10th line\n"~ "9th line\n8th line\n7th line\n6th line\n5th line\n"~ "4th line\n3rd line\n2nd line\n1st line\n" ] ); for @tt -> $f0, $f1, $f2 { check_one($scr, $f0, $f1, $f2) } } sub check_mid (Str $scr) { my @tt = ( [ 'file1', $file1, "1st line\n" ], [ 'file2', $file2, "1st line\n2nd line\n" ], [ 'file3', $file3, "2nd line\n" ], [ 'file4', $file4, "2nd line\n3rd line\n" ], [ 'file12', $file12, "6th line\n7th line\n" ], [ 'file21', $file21, "11th line\n" ] ); for @tt -> $f0, $f1, $f2 { check_one($scr, $f0, $f1, $f2) } } sub check_wc (Str $scr) { my @tt = ( [ 'file1', $file1, "0000000001\n" ], [ 'file2', $file2, "0000000002\n" ], [ 'file3', $file3, "0000000003\n" ], [ 'file4', $file4, "0000000004\n" ], [ 'file12', $file12, "0000000012\n" ], [ 'file21', $file21, "0000000021\n" ] ); for @tt -> $f0, $f1, $f2 { check_one($scr, $f0, $f1, $f2) } } # ----------------------------------------------------- my $head = 'head.p6'; my $tail = 'tail.p6'; my $rev = 'rev.p6'; my $mid = 'mid.p6'; my $wc = 'wc.p6'; # -f $head or die("error: file '$head' not found"); # -f $tail or die("error: file '$tail' not found"); # -f $rev or die("error: file '$rev' not found"); # -f $mid or die("error: file '$mid' not found"); # -f $wc or die("error: file '$wc' not found"); print_golf_score($head, $tail, $rev, $mid, $wc); check_head($head); check_tail($tail); check_rev($rev); check_mid($mid); check_wc($wc); print_golf_score($head, $tail, $rev, $mid, $wc); say("Hooray, you passed.");