require 'stringio'
require 'timeout'
require "./run.rb"

things = "a"," ","-","0","1",",","\n"

`ghc -O engine.hs`

def run_engine(prog, input)
  File.open("prog.iog", "w"){|f| f << prog }
  if Array === input
    `(ruby main.rb prog.iog -e > test.ism && engine test.ism #{input.map{|i|"'"+i+"'"}*" "}) 2>&1`
  else
    File.open("input.txt", "w"){|f| f << input }
    `(ruby main.rb prog.iog -e > test.ism && engine test.ism < input.txt) 2>&1`
  end
end

been = {}
loop{
  program = "show"
  if rand(2) > 0
    input = [ rand(7).times.map{ things.sample }*"",
      rand(7).times.map{ things.sample }*""]
  else
    input = rand(7).times.map{ things.sample }*""
  end
  next if been[input]
  been[input] = true
  STDERR.puts

  STDERR.puts "input: %p" % [input]
  $stdout = StringIO.new("")
  run_and_do_io(program, input, false, false)

  out = $stdout.string
  $stdout = STDOUT

  puts "ruby output: %p" % [out]
  out_hs = run_engine(program, input)
  puts "engine output: %p" % [out_hs]

  exit if out != out_hs
}

