putting tests into module

This commit is contained in:
Torsten Ruger 2017-01-03 22:42:40 +02:00
parent da58ce30e3
commit 8aee761b73
11 changed files with 453 additions and 434 deletions

View File

@ -1,63 +1,65 @@
require_relative "../helper" require_relative "../helper"
require "register/interpreter" require "register/interpreter"
module Ticker module Register
include AST::Sexp module Ticker
include AST::Sexp
def setup def setup
Register.machine.boot Register.machine.boot
do_clean_compile do_clean_compile
Typed.compile( @input ) Typed.compile( @input )
Register::Collector.collect_space Register::Collector.collect_space
@interpreter = Register::Interpreter.new @interpreter = Register::Interpreter.new
@interpreter.start Register.machine.init @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}"
end end
end
def check_return val # must be after boot, but before main compile, to define method
assert_equal Parfait::Message , @interpreter.get_register(:r0).class def do_clean_compile
assert_equal val , @interpreter.get_register(:r0).return_value
end
def ticks num
last = nil
num.times do
last = @interpreter.instruction
@interpreter.tick
end end
return last
end
def show_ticks def check_chain should
classes = [] should.each_with_index do |name , index|
tick = 1 got = ticks(1)
begin assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
while true and (classes.length < 200)
cl = ticks(1).class
tick += 1
classes << cl
break if cl == NilClass
end end
rescue => e
puts "Error at tick #{tick}"
puts e
puts e.backtrace
end end
classes = classes.collect {|c| '"' + c.name.sub("Register::","") + '",' }
classes << "length = #{classes.length}" def check_return val
classes.each_slice(5).each do |line| assert_equal Parfait::Message , @interpreter.get_register(:r0).class
puts " " + line.join 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 end
exit(1)
end end
end end

View File

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

View File

@ -1,64 +1,66 @@
require_relative "helper" require_relative "helper"
class TestInterpretRegToByte < MiniTest::Test module Register
include Ticker class TestInterpretRegToByte < MiniTest::Test
include Ticker
def setup def setup
@string_input = <<HERE @string_input = <<HERE
class Space class Space
int main() int main()
"Hello".set_internal_byte(1,104) "Hello".set_internal_byte(1,104)
end
end end
end
HERE HERE
@input = s(:statements, s(:call, @input = s(:statements, s(:call,
s(:name, :set_internal_byte), s(:name, :set_internal_byte),
s(:arguments, s(:int, 1), s(:int, 104)), s(:arguments, s(:int, 1), s(:int, 104)),
s(:receiver, s(:string, "Hello")))) s(:receiver, s(:string, "Hello"))))
super super
end end
def test_chain def test_chain
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot", check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg", "LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant", "LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg", "SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","SlotToReg","RegToSlot","LoadConstant", "RegToSlot","LoadConstant","SlotToReg","RegToSlot","LoadConstant",
"RegToSlot","RegisterTransfer","FunctionCall","Label","SlotToReg", "RegToSlot","RegisterTransfer","FunctionCall","Label","SlotToReg",
"SlotToReg","SlotToReg","SlotToReg","SlotToReg","RegToByte", "SlotToReg","SlotToReg","SlotToReg","SlotToReg","RegToByte",
"Label","FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg", "Label","FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg",
"Label","FunctionReturn","RegisterTransfer","Syscall","NilClass"] "Label","FunctionReturn","RegisterTransfer","Syscall","NilClass"]
end end
def test_branch def test_branch
was = @interpreter.instruction was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction" assert @interpreter.instruction , "should have gone to next instruction"
end end
def test_load def test_load
assert_equal Register::LoadConstant , ticks(3).class assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol assert_equal :r2, @interpreter.instruction.array.symbol
end end
def test_get def test_get
assert_equal Register::SlotToReg , ticks(4).class assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 ) assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class assert Integer , @interpreter.get_register( :r1 ).class
end end
def test_call def test_call
assert_equal Register::FunctionCall , ticks(8).class assert_equal Register::FunctionCall , ticks(8).class
end end
def test_exit def test_exit
done = ticks(45) done = ticks(45)
assert_equal NilClass , done.class assert_equal NilClass , done.class
end end
def test_reg_to_byte def test_reg_to_byte
done = ticks(35) done = ticks(35)
assert_equal Register::RegToByte , done.class assert_equal Register::RegToByte , done.class
assert_equal "h".ord , @interpreter.get_register(done.register) assert_equal "h".ord , @interpreter.get_register(done.register)
end end
end
end end

View File

@ -1,51 +1,53 @@
require_relative "helper" require_relative "helper"
class IfCalledTest < MiniTest::Test module Register
include Ticker class IfCalledTest < MiniTest::Test
include Compiling include Ticker
include Compiling
def setup def setup
@string_input = <<HERE @string_input = <<HERE
class Space class Space
int itest(int n) int itest(int n)
if_zero( n - 12) if_zero( n - 12)
"then".putstring() "then".putstring()
else else
"else".putstring() "else".putstring()
end
end
int main()
itest(20)
end end
end end
int main()
itest(20)
end
end
HERE HERE
@input = s(:statements, s(:call, s(:name, :itest), s(:arguments, s(:int, 20)))) @input = s(:statements, s(:call, s(:name, :itest), s(:arguments, s(:int, 20))))
super super
end end
# must be after boot, but before main compile, to define method # must be after boot, but before main compile, to define method
def do_clean_compile def do_clean_compile
clean_compile :Space , :itest , {:n => :Integer} , clean_compile :Space , :itest , {:n => :Integer} ,
s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:name, :n), s(:int, 12))), 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(: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")))))) s(:false_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "else"))))))
end end
def test_if def test_if
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot", check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg", "LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"SlotToReg","RegToSlot","LoadConstant","RegToSlot","LoadConstant", "SlotToReg","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg", "SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall", "RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall",
"Label","SlotToReg","SlotToReg","LoadConstant","OperatorInstruction", "Label","SlotToReg","SlotToReg","LoadConstant","OperatorInstruction",
"IsZero","SlotToReg","LoadConstant","RegToSlot","LoadConstant", "IsZero","SlotToReg","LoadConstant","RegToSlot","LoadConstant",
"RegToSlot","LoadConstant","SlotToReg","SlotToReg","RegToSlot", "RegToSlot","LoadConstant","SlotToReg","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","RegisterTransfer","FunctionCall","Label", "LoadConstant","RegToSlot","RegisterTransfer","FunctionCall","Label",
"SlotToReg","SlotToReg","RegisterTransfer","Syscall","RegisterTransfer", "SlotToReg","SlotToReg","RegisterTransfer","Syscall","RegisterTransfer",
"RegisterTransfer","RegToSlot","Label","FunctionReturn","RegisterTransfer", "RegisterTransfer","RegToSlot","Label","FunctionReturn","RegisterTransfer",
"SlotToReg","SlotToReg","Branch","Label","Label", "SlotToReg","SlotToReg","Branch","Label","Label",
"FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg","Label", "FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg","Label",
"FunctionReturn","RegisterTransfer","Syscall","NilClass"] "FunctionReturn","RegisterTransfer","Syscall","NilClass"]
end
end end
end end

View File

@ -1,45 +1,47 @@
require_relative "helper" require_relative "helper"
class AddChange < MiniTest::Test module Register
include Ticker 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 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 end

View File

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

View File

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

View File

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

View File

@ -1,64 +1,66 @@
require_relative "helper" require_relative "helper"
class TestInterpretByteToReg < MiniTest::Test module Register
include Ticker class TestInterpretByteToReg < MiniTest::Test
include Ticker
def setup def setup
@string_input = <<HERE @string_input = <<HERE
class Space class Space
int main() int main()
"Hello".get_internal_byte(1) "Hello".get_internal_byte(1)
end
end end
end
HERE HERE
@input = s(:statements, s(:call, @input = s(:statements, s(:call,
s(:name, :get_internal_byte), s(:name, :get_internal_byte),
s(:arguments, s(:int, 1)), s(:arguments, s(:int, 1)),
s(:receiver, s(:string, "Hello")))) s(:receiver, s(:string, "Hello"))))
super super
end end
def test_chain def test_chain
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot", check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg", "LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant", "LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg", "SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall", "RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall",
"Label","SlotToReg","SlotToReg","SlotToReg","ByteToReg", "Label","SlotToReg","SlotToReg","SlotToReg","ByteToReg",
"RegToSlot","Label","FunctionReturn","RegisterTransfer","SlotToReg", "RegToSlot","Label","FunctionReturn","RegisterTransfer","SlotToReg",
"SlotToReg","Label","FunctionReturn","RegisterTransfer","Syscall", "SlotToReg","Label","FunctionReturn","RegisterTransfer","Syscall",
"NilClass"] "NilClass"]
end end
def test_branch def test_branch
was = @interpreter.instruction was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction" assert @interpreter.instruction , "should have gone to next instruction"
end end
def test_load def test_load
assert_equal Register::LoadConstant , ticks(3).class assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol assert_equal :r2, @interpreter.instruction.array.symbol
end end
def test_get def test_get
assert_equal Register::SlotToReg , ticks(4).class assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 ) assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class assert Integer , @interpreter.get_register( :r1 ).class
end end
def test_call def test_call
assert_equal Register::FunctionCall , ticks(8).class assert_equal Register::FunctionCall , ticks(8).class
end end
def test_exit def test_exit
done = ticks(41) done = ticks(41)
assert_equal NilClass , done.class assert_equal NilClass , done.class
end end
def test_byte_to_reg def test_byte_to_reg
done = ticks(30) done = ticks(30)
assert_equal Register::ByteToReg , done.class assert_equal Register::ByteToReg , done.class
assert_equal "H".ord , @interpreter.get_register(done.register) assert_equal "H".ord , @interpreter.get_register(done.register)
end end
end
end end

View File

@ -1,39 +1,41 @@
require_relative "helper" require_relative "helper"
class IfSimpleTest < MiniTest::Test module Register
include Ticker class IfSimpleTest < MiniTest::Test
include Compiling include Ticker
include Compiling
def setup def setup
@string_input = <<HERE @string_input = <<HERE
class Space class Space
int main() int main()
if_zero( 10 - 12) if_zero( 10 - 12)
"then".putstring() "then".putstring()
else else
"else".putstring() "else".putstring()
end
end
end
HERE
@input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), 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"))))))
super
end
def test_if
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"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","Syscall",
"NilClass"]
end end
end end
end end
HERE
@input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), 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"))))))
super
end
def test_if
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"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","Syscall",
"NilClass"]
end
end

View File

@ -5,7 +5,7 @@ module Register
def test_simple_collect def test_simple_collect
Machine.new.boot Machine.new.boot
objects = Register::Collector.collect_space objects = Register::Collector.collect_space
assert ((352 == objects.length) or (420 == objects.length)) , objects.length.to_s assert ((352 == objects.length) or (419 == objects.length)) , objects.length.to_s
end end
end end
end end