2015-11-07 16:37:41 +01:00
|
|
|
require_relative '../helper'
|
|
|
|
require "interpreter/interpreter"
|
2015-11-16 22:23:08 +01:00
|
|
|
require "rye"
|
|
|
|
Rye::Cmd.add_command :ld, '/usr/bin/ld'
|
|
|
|
Rye::Cmd.add_command :aout, './a.out'
|
2015-11-07 16:37:41 +01:00
|
|
|
|
|
|
|
module RuntimeTests
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@stdout = ""
|
|
|
|
end
|
|
|
|
def main
|
|
|
|
<<HERE
|
|
|
|
class Object
|
|
|
|
int main()
|
|
|
|
PROGRAM
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
end
|
2015-11-16 22:23:08 +01:00
|
|
|
def check ret = nil
|
2015-11-07 16:37:41 +01:00
|
|
|
machine = Register.machine.boot
|
|
|
|
Soml::Compiler.load_parfait
|
|
|
|
machine.parse_and_compile main.sub("PROGRAM" , @string_input )
|
|
|
|
machine.collect
|
|
|
|
@interpreter = Interpreter::Interpreter.new
|
|
|
|
@interpreter.start machine.init
|
|
|
|
count = 0
|
|
|
|
begin
|
|
|
|
count += 1
|
|
|
|
#puts interpreter.instruction
|
|
|
|
@interpreter.tick
|
|
|
|
end while( ! @interpreter.instruction.nil?)
|
2015-11-16 22:23:08 +01:00
|
|
|
assert_equal @stdout , @interpreter.stdout , "stdout wrong locally"
|
|
|
|
if ret
|
|
|
|
assert_equal Parfait::Message , @interpreter.get_register(:r0).class
|
|
|
|
assert_equal ret , @interpreter.get_register(:r0).return_value , "exit wrong #{@string_input}"
|
|
|
|
end
|
|
|
|
check_remote ret
|
2015-11-18 10:29:37 +01:00
|
|
|
file = write_object_file
|
2015-11-12 19:03:57 +01:00
|
|
|
end
|
|
|
|
|
2015-11-16 22:23:08 +01:00
|
|
|
def connected
|
|
|
|
return @@conn if defined?(@@conn)
|
2015-11-17 01:28:47 +01:00
|
|
|
return @@conn = false
|
2015-11-16 22:23:08 +01:00
|
|
|
begin
|
|
|
|
box = Rye::Box.new("localhost" , :port => 2222 , :user => "pi")
|
|
|
|
box.pwd
|
|
|
|
puts "connected, testing also remotely"
|
|
|
|
@@conn = box
|
|
|
|
rescue Rye::Err
|
|
|
|
@@conn = false
|
|
|
|
end
|
|
|
|
return @@conn
|
|
|
|
end
|
|
|
|
def check_remote ret
|
|
|
|
return unless box = connected
|
2015-11-18 10:29:37 +01:00
|
|
|
return unless ret.is_a?(Numeric)
|
2015-11-16 22:23:08 +01:00
|
|
|
file = write_object_file
|
|
|
|
r_file = file.sub("./" , "salama/")
|
|
|
|
box.file_upload file , r_file
|
|
|
|
box.ld "-N", r_file
|
|
|
|
begin #need to rescue here as rye throws if no return 0
|
|
|
|
ret = box.aout # and we use return to mean something
|
|
|
|
rescue Rye::Err => e # so it's basically never 0
|
|
|
|
ret = e.rap
|
|
|
|
end
|
|
|
|
assert_equal @stdout , ret.stdout.join , "remote std was #{ret.stdout}" if @stdout
|
|
|
|
assert_equal "" , ret.stderr.join , "remote had error"
|
|
|
|
if ret
|
|
|
|
should = @interpreter.get_register(:r0).return_value
|
2015-11-18 10:29:37 +01:00
|
|
|
assert_equal should , ret.exit_status.to_i , "remote exit failed for #{@string_input}"
|
2015-11-16 22:23:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_object_file
|
2015-11-16 17:02:13 +01:00
|
|
|
file_name = caller(3).first.split("in ").last.chop.sub("`","")
|
|
|
|
return if file_name.include?("run")
|
|
|
|
file_name = "./tmp/" + file_name + ".o"
|
2015-11-12 19:03:57 +01:00
|
|
|
Register.machine.translate_arm
|
|
|
|
writer = Elf::ObjectWriter.new
|
|
|
|
writer.save file_name
|
2015-11-16 22:23:08 +01:00
|
|
|
file_name
|
2015-11-07 16:37:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_return val
|
2015-11-16 22:23:08 +01:00
|
|
|
check val
|
2015-11-07 21:26:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_return_class val
|
|
|
|
check
|
|
|
|
assert_equal val , @interpreter.get_register(:r0).return_value.class
|
2015-11-07 16:37:41 +01:00
|
|
|
end
|
|
|
|
end
|