Source

shakespeare-parrot / t / example_test.pl

Full commit
#!/usr/bin/env perl
use strict;
use warnings;
use IO::Handle;
use IPC::Open2;
use Test::More;
our $STR="Hello World!\n";


plan tests=> 45+1+1+26+1+11+1+99*2;

sub with_prog {
  my ($name,$code) = @_;
  my $str= "../../parrot z2.pir example/$name.spl";
  print "$str\n";
  my ($in,$out);
  open2($out,$in,$str);
  &$code($out,$in);
}

with_prog 'fibonacci2', sub {
  my ($in,$out,$i) = (shift,shift);
  foreach my $e (<DATA>) {
    $i = <$in>; 
    is $i, $e;
  }
};


with_prog 'hello', sub{
  my ($in,$out,$i) = (shift,shift);
  $i =<$in>;
  is $i, $STR;
};

with_prog 'reverse', sub {
  my ($in,$out,$i) = (shift,shift);
  print $out $STR;
  close $out;
  $i = <$in>;
  $i .= <$in>;
  is $i,reverse($STR);
};

with_prog 'primes', sub {
  my @ary= qw(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97);
  my ($in,$out,$i) = (shift,shift);
  read $in, $i, 1;
  is $i, '>';
  print $out "100\n";
  close $out;
  foreach my $el (@ary) {
    $i = <$in>;
    chomp $i;
    is $i, $el;
  }
};


with_prog 'sum2', sub {
  my ($in,$out,$i) = (shift,shift);
  print $out "1\n";
  print $out "5\n";
  close $out;
  $i = <$in>;
  chomp $i;
  is $i,'6';
};
with_prog 'guess', sub {
  my ($in,$out,$i) = (shift,shift);
  my @inp=qw(> > < > < > > > < =);
  my @oup=qw(500?  750?  875?  812?  843?  827?  835?  839?  841?  840?  840);
  my $first = shift @oup;
  read $in, $i, 4;
  is $i, $first;
  foreach my $o (@oup) {
    my $str = shift @inp;
    print $out "$str\n";
    $i = <$in>;
    $i = <$in>;
    $i =~ s/\s+//;
    is $i, $o;
  }
};
with_prog 'fibonacci', sub {
  my ($in,$i, $exp)= shift;
  $exp = "1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903";
  read $in,$i, length($exp); 
  is $i,$exp;
};
with_prog 'shakesbeer', sub{
  my ($in,$i, $bottle)= shift;
  $bottle = sub {
    my $i=shift; 
    \("$i bottle". ($i==1 ? '' : 's'));
  };
  $/ = "\r\n";
  for(my $j=99; $j>0; $j--) {
    $i = <$in>;
    chomp $i; 
    is $i,"${&$bottle($j)} of beer on the wall, ${&$bottle($j)} of beer.";
    $i = <$in>;
    chomp $i; 
    is $i, "Take one down, pass it around, ${&$bottle($j-1)} of beer on the wall.";
  }
};

1;

__DATA__
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733