diff --git a/test/register/interpreter/helper.rb b/test/register/interpreter/helper.rb index 9fe638f3..c1129506 100644 --- a/test/register/interpreter/helper.rb +++ b/test/register/interpreter/helper.rb @@ -1,63 +1,65 @@ require_relative "../helper" require "register/interpreter" -module Ticker - include AST::Sexp +module Register + module Ticker + include AST::Sexp - def setup - Register.machine.boot - do_clean_compile - Typed.compile( @input ) - Register::Collector.collect_space - @interpreter = Register::Interpreter.new - @interpreter.start Register.machine.init - end - - # must be after boot, but before main compile, to define method - def do_clean_compile - 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}" + def setup + Register.machine.boot + do_clean_compile + Typed.compile( @input ) + Register::Collector.collect_space + @interpreter = Register::Interpreter.new + @interpreter.start Register.machine.init 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 + # must be after boot, but before main compile, to define method + def do_clean_compile 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 + 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 - rescue => e - puts "Error at tick #{tick}" - puts e - puts e.backtrace end - classes = classes.collect {|c| '"' + c.name.sub("Register::","") + '",' } - classes << "length = #{classes.length}" - classes.each_slice(5).each do |line| - puts " " + line.join + + 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 + puts e.backtrace + 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 - exit(1) end end diff --git a/test/register/interpreter/test_add.rb b/test/register/interpreter/test_add.rb index b22fbb92..f37e43c5 100644 --- a/test/register/interpreter/test_add.rb +++ b/test/register/interpreter/test_add.rb @@ -1,60 +1,62 @@ require_relative "helper" -class AddTest < MiniTest::Test - include Ticker +module Register + class AddTest < MiniTest::Test + include Ticker - def setup - @string_input = < :Integer} , - s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:name, :n), s(:int, 12))), - s(:true_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "then")))), - s(:false_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "else")))))) - end - def test_if - #show_ticks # get output of what is - check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot", - "LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg", - "SlotToReg","RegToSlot","LoadConstant","RegToSlot","LoadConstant", - "SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg", - "RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall", - "Label","SlotToReg","SlotToReg","LoadConstant","OperatorInstruction", - "IsZero","SlotToReg","LoadConstant","RegToSlot","LoadConstant", - "RegToSlot","LoadConstant","SlotToReg","SlotToReg","RegToSlot", - "LoadConstant","RegToSlot","RegisterTransfer","FunctionCall","Label", - "SlotToReg","SlotToReg","RegisterTransfer","Syscall","RegisterTransfer", - "RegisterTransfer","RegToSlot","Label","FunctionReturn","RegisterTransfer", - "SlotToReg","SlotToReg","Branch","Label","Label", - "FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg","Label", - "FunctionReturn","RegisterTransfer","Syscall","NilClass"] + # must be after boot, but before main compile, to define method + def do_clean_compile + clean_compile :Space , :itest , {:n => :Integer} , + s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:name, :n), s(:int, 12))), + s(:true_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "then")))), + s(:false_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "else")))))) + end + def test_if + #show_ticks # get output of what is + check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot", + "LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg", + "SlotToReg","RegToSlot","LoadConstant","RegToSlot","LoadConstant", + "SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg", + "RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall", + "Label","SlotToReg","SlotToReg","LoadConstant","OperatorInstruction", + "IsZero","SlotToReg","LoadConstant","RegToSlot","LoadConstant", + "RegToSlot","LoadConstant","SlotToReg","SlotToReg","RegToSlot", + "LoadConstant","RegToSlot","RegisterTransfer","FunctionCall","Label", + "SlotToReg","SlotToReg","RegisterTransfer","Syscall","RegisterTransfer", + "RegisterTransfer","RegToSlot","Label","FunctionReturn","RegisterTransfer", + "SlotToReg","SlotToReg","Branch","Label","Label", + "FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg","Label", + "FunctionReturn","RegisterTransfer","Syscall","NilClass"] + end end end diff --git a/test/register/interpreter/test_change.rb b/test/register/interpreter/test_change.rb index 20df0350..d1985953 100644 --- a/test/register/interpreter/test_change.rb +++ b/test/register/interpreter/test_change.rb @@ -1,45 +1,47 @@ require_relative "helper" -class AddChange < MiniTest::Test - include Ticker +module Register + class AddChange < MiniTest::Test + include Ticker + + def setup + @input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7)))) + @state_events = {} + @instruction_events = [] + super + end + + def state_changed( a , b) + @state_events[:state_changed] = [a , b] + end + + def instruction_changed(was , is ) + @instruction_events << was + end + + def test_state_change + @interpreter.register_event :state_changed , self + ticks 30 + assert @state_events[:state_changed] + assert_equal 2 , @state_events[:state_changed].length + assert_equal :running, @state_events[:state_changed][0] + @interpreter.unregister_event :state_changed , self + end + + def test_instruction_events + @interpreter.register_event :instruction_changed , self + ticks 30 + assert_equal 17 , @instruction_events.length + @interpreter.unregister_event :instruction_changed , self + end + + def test_chain + #show_ticks # get output of what is + check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot", + "LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant", + "LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn", + "RegisterTransfer","Syscall","NilClass"] + end - def setup - @input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7)))) - @state_events = {} - @instruction_events = [] - super end - - def state_changed( a , b) - @state_events[:state_changed] = [a , b] - end - - def instruction_changed(was , is ) - @instruction_events << was - end - - def test_state_change - @interpreter.register_event :state_changed , self - ticks 30 - assert @state_events[:state_changed] - assert_equal 2 , @state_events[:state_changed].length - assert_equal :running, @state_events[:state_changed][0] - @interpreter.unregister_event :state_changed , self - end - - def test_instruction_events - @interpreter.register_event :instruction_changed , self - ticks 30 - assert_equal 17 , @instruction_events.length - @interpreter.unregister_event :instruction_changed , self - end - - def test_chain - #show_ticks # get output of what is - check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot", - "LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant", - "LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn", - "RegisterTransfer","Syscall","NilClass"] - end - end diff --git a/test/register/interpreter/test_mult.rb b/test/register/interpreter/test_mult.rb index 127ff8e5..0c6a00e1 100644 --- a/test/register/interpreter/test_mult.rb +++ b/test/register/interpreter/test_mult.rb @@ -1,38 +1,39 @@ require_relative "helper" -class MultTest < MiniTest::Test - include Ticker - include AST::Sexp +module Register + class MultTest < MiniTest::Test + include Ticker + include AST::Sexp - def setup - @string_input = <