Source

shakespeare-parrot / t / test.rb

#!/usr/bin/env ruby
STR="Hello World!\n"
$i=0

DIR=File.join(File.dirname(__FILE__),'..')

puts "1..#{45+1+1+26+1+11+1+99*2}"
def with_prog(name)
  p str= %W[../../parrot shakespeare.pir example/#{name}.spl].map{|x| File.join(DIR,x)}*" "
  IO.popen(str,'r+') do |io|
    yield io
  end
end

def is(x,y)
  $i+=1
  if x==y
    puts "ok #$i"
  else
    puts "not ok #$i -- #{x.inspect} expected but got #{y.inspect}"
  end
end

with_prog 'fibonacci2' do |p|
  DATA.each do |o|
    is o, p.gets
  end
end


with_prog 'hello' do |prog|
  is STR, prog.gets
end

with_prog 'reverse' do |prog|
  prog.write(STR)
  prog.close_write
  is STR.reverse, prog.read
end

with_prog 'primes' do |prog|
  ary=%w[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];
  is '>',prog.read(1)
  prog.write('100\n')
  prog.close_write
  ary.each do |el|
    is el, prog.gets.chomp
  end
end

with_prog 'sum2' do |prog|
  prog.puts('1')
  prog.puts('5')
  prog.close_write
  is '6', prog.gets.chomp
end

with_prog 'guess' do |prog|
  inp=%w[> > < > < > > > < =]
  out=%w(500?  750?  875?  812?  843?  827?  835?  839?  841?  840?  840)
  o = out.shift
  is o,x=prog.read(4)
  out.each do |o|
    str= inp.shift
    prog.puts(str)
    prog.gets
    x=prog.gets
    is o,x.strip
  end
end

with_prog 'fibonacci' do |prog|
  out = "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"
  is out,prog.read(out.size)
end

with_prog 'shakesbeer' do |prog|
  bottle= proc {|i| "#{i} bottle#{i==1 ? ''  : 's'}"}
  99.downto(1) do |i|
    is "#{bottle[i]} of beer on the wall, #{bottle[i]} of beer.",prog.gets.chomp 
    is "Take one down, pass it around, #{bottle[i-1]} of beer on the wall.",prog.gets.chomp
  end
end
__END__
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