move interpreter tests to register

to shadow the interpreter move / the lib structure
This commit is contained in:
Torsten Ruger
2015-11-18 12:07:37 +02:00
parent 979660f282
commit cab7e61f8b
9 changed files with 2 additions and 3 deletions

View File

@ -0,0 +1,59 @@
require_relative "../../helper"
require "register/interpreter"
module Ticker
def setup
machine = Register.machine.boot
syntax = Parser::Salama.new.parse_with_debug(@string_input)
parts = Parser::Transform.new.apply(syntax)
#puts parts.inspect
Soml.compile( parts )
machine.collect
@interpreter = Register::Interpreter.new
@interpreter.start Register.machine.init
end
def check_chain should
should.each_with_index do |name , index|
got = ticks(1)
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
end
end
def check_return val
assert_equal Parfait::Message , @interpreter.get_register(:r0).class
assert_equal val , @interpreter.get_register(:r0).return_value
end
def ticks num
last = nil
num.times do
last = @interpreter.instruction
@interpreter.tick
end
return last
end
def show_ticks
classes = []
tick = 1
begin
while true and (classes.length < 200)
cl = ticks(1).class
tick += 1
classes << cl
break if cl == NilClass
end
rescue => e
puts "Error at tick #{tick}"
puts e
end
classes = classes.collect {|c| '"' + c.name.sub("Register::","") + '",' }
classes << "length = #{classes.length}"
classes.each_slice(5).each do |line|
puts " " + line.join
end
exit(1)
end
end

View File

@ -0,0 +1,59 @@
require_relative "helper"
class AddTest < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Object
int main()
return 5 + 7
end
end
HERE
super
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
def test_get
assert_equal Register::GetSlot , ticks(4).class
assert @interpreter.get_register( :r2 )
assert Integer , @interpreter.get_register( :r2 ).class
end
def test_transfer
transfer = ticks 16
assert_equal Register::RegisterTransfer , transfer.class
assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from)
end
def test_call
ret = ticks(15)
assert_equal Register::FunctionReturn , ret.class
object = @interpreter.get_register( ret.register )
link = object.get_internal( ret.index )
assert_equal Register::Label , link.class
end
def test_adding
done_op = ticks(12)
assert_equal Register::OperatorInstruction , done_op.class
left = @interpreter.get_register(done_op.left)
rr = done_op.right
right = @interpreter.get_register(rr)
assert_equal Fixnum , left.class
assert_equal Fixnum , right.class
assert_equal 7 , right
assert_equal 12 , left
done_tr = ticks(4)
assert_equal Register::RegisterTransfer , done_tr.class
result = @interpreter.get_register(done_op.left)
assert_equal result , 12
end
end

View File

@ -0,0 +1,5 @@
require_relative "test_add"
require_relative "test_if"
require_relative "test_puts"
require_relative "test_plus"
require_relative "test_mult"

View File

@ -0,0 +1,41 @@
require_relative "helper"
class IfTest < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Object
int itest(int n)
if_zero( n - 12)
"then".putstring()
else
"else".putstring()
end
end
int main()
itest(20)
end
end
HERE
super
end
def test_if
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","GetSlot",
"GetSlot","SetSlot","LoadConstant","SetSlot","LoadConstant",
"SetSlot","LoadConstant","SetSlot","LoadConstant","SetSlot",
"RegisterTransfer","FunctionCall","Label","GetSlot","LoadConstant",
"OperatorInstruction","IsZero","GetSlot","LoadConstant","SetSlot",
"LoadConstant","SetSlot","LoadConstant","SetSlot","LoadConstant",
"SetSlot","RegisterTransfer","FunctionCall","Label","GetSlot",
"GetSlot","RegisterTransfer","Syscall","RegisterTransfer","RegisterTransfer",
"SetSlot","Label","FunctionReturn","RegisterTransfer","GetSlot",
"GetSlot","Branch","Label","Label","FunctionReturn",
"RegisterTransfer","GetSlot","GetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
end

View File

@ -0,0 +1,36 @@
require_relative "helper"
class MultTest < MiniTest::Test
include Ticker
include AST::Sexp
def setup
@string_input = <<HERE
class Object
int main()
return #{2**31} * #{2**31}
end
end
HERE
super
end
def test_mult
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
check_return 0
end
def test_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end
end

View File

@ -0,0 +1,35 @@
require_relative "helper"
class PlusTest < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Object
int main()
return #{2**62 - 1} + 1
end
end
HERE
super
end
def test_add
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
check_return 0
end
def test_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end
end

View File

@ -0,0 +1,67 @@
require_relative "helper"
class TestPuts < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Object
int main()
"Hello again".putstring()
end
end
HERE
super
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","GetSlot",
"LoadConstant","SetSlot","LoadConstant","SetSlot","LoadConstant",
"SetSlot","LoadConstant","SetSlot","RegisterTransfer","FunctionCall",
"Label","GetSlot","GetSlot","RegisterTransfer","Syscall",
"RegisterTransfer","RegisterTransfer","SetSlot","Label","FunctionReturn",
"RegisterTransfer","GetSlot","GetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
def test_branch
was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal Register::GetSlot , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal Register::FunctionCall , ticks(8).class
end
def test_putstring
done = ticks(25)
assert_equal Register::Syscall , done.class
assert_equal "Hello again" , @interpreter.stdout
end
def test_return
done = ticks(30)
assert_equal Register::FunctionReturn , done.class
assert Register::Label , @interpreter.instruction.class
assert @interpreter.instruction.is_a?(Register::Instruction) , "not instruction #{@interpreter.instruction}"
end
def test_exit
done = ticks(42)
assert_equal NilClass , done.class
assert_equal "Hello again" , @interpreter.stdout
end
end