test interpreters event firing

This commit is contained in:
Torsten Ruger 2018-04-01 22:50:15 +03:00
parent 599679d6e8
commit ad497b34f1
4 changed files with 63 additions and 104 deletions

View File

@ -3,7 +3,6 @@ require "risc/interpreter"
module Risc
module Ticker
include AST::Sexp
include InterpreterHelpers
include CompilerHelper

View File

@ -0,0 +1,63 @@
require_relative "helper"
module Risc
class InterpreterEvents < MiniTest::Test
include Ticker
def setup
@string_input = as_main("return 5 + 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 88
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 88
assert_equal 88 , @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, LoadConstant,
SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg,
SlotToReg, SlotToReg, SlotToReg, RegToSlot, LoadConstant,
SlotToReg, SlotToReg, SlotToReg, SlotToReg, RegToSlot,
SlotToReg, RegToSlot, LoadConstant, RegToSlot, FunctionCall,
Label, LoadConstant, SlotToReg, SlotToReg, RegToSlot,
LoadConstant, SlotToReg, SlotToReg, SlotToReg, SlotToReg,
RegToSlot, LoadConstant, SlotToReg, SlotToReg, SlotToReg,
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, SlotToReg, SlotToReg, RegToSlot, LoadConstant,
SlotToReg, RegToSlot, SlotToReg, LoadConstant, FunctionCall,
Label, SlotToReg, SlotToReg, SlotToReg, SlotToReg,
SlotToReg, OperatorInstruction, LoadConstant, SlotToReg, SlotToReg,
RegToSlot, RegToSlot, RegToSlot, SlotToReg, SlotToReg,
RegToSlot, SlotToReg, SlotToReg, FunctionReturn, SlotToReg,
SlotToReg, RegToSlot, SlotToReg, SlotToReg, RegToSlot,
SlotToReg, SlotToReg, RegToSlot, SlotToReg, SlotToReg,
FunctionReturn, Transfer, Syscall, NilClass]
assert_equal Parfait::Integer , get_return.class
assert_equal 12 , get_return.value
end
end
end

View File

@ -1,55 +0,0 @@
require_relative "../helper"
module Risc
class IfCalledTest < MiniTest::Test
include Ticker
include CleanCompile
def setup
@string_input = <<HERE
class Space
int itest(int n)
if_zero( n - 12)
"then".putstring()
else
"else".putstring()
end
end
int main()
itest(20)
end
end
HERE
@input = s(:statements, s(:call, :itest , s(:arguments, s(:int, 20))))
super
end
# 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(:arg, :n), s(:int, 12))),
s(:true_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "then")))),
s(:false_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "else"))))))
end
def pest_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, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, Transfer, FunctionCall, Label,
LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg,
LoadConstant, OperatorInstruction, IsZero, SlotToReg, LoadConstant,
RegToSlot, LoadConstant, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, LoadConstant, RegToSlot, Transfer, FunctionCall,
Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg,
SlotToReg, Transfer, Syscall, Transfer, Transfer,
RegToSlot, Label, FunctionReturn, Transfer, SlotToReg,
SlotToReg, Branch, Label, Label, FunctionReturn,
Transfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, Transfer, Syscall,
NilClass]
end
end
end

View File

@ -1,48 +0,0 @@
require_relative "../helper"
module Risc
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 pest_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 pest_instruction_events
@interpreter.register_event :instruction_changed , self
ticks 30
assert_equal 20 , @instruction_events.length
@interpreter.unregister_event :instruction_changed , self
end
def pest_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, Label, FunctionReturn, Transfer, Syscall,
NilClass]
end
end
end