better way to test object registers

also some cleaning, by using more helpers
This commit is contained in:
Torsten 2020-03-02 17:50:03 +02:00
parent ece1e8c87b
commit d0036ed95b
8 changed files with 73 additions and 136 deletions

View File

@ -9,10 +9,7 @@ module Risc
Risc.load_constant("source" , const)
end
def test_const
assert_equal LoadConstant , load.class
end
def test_const_reg
assert load.register.is_object?
assert_load load , SlotMachine::StringConstant , "id_"
end
end
class TestLoadConstant1 < MiniTest::Test
@ -23,10 +20,7 @@ module Risc
Risc.load_constant("source" , const)
end
def test_parf
assert_equal LoadConstant , load.class
end
def test_parf_reg
assert load.register.is_object?
assert_load load , Parfait::Word , "id_"
end
end
end

View File

@ -52,10 +52,11 @@ module Risc
end
def test_operator
ret = @r0.op :<< , @r1
assert_equal OperatorInstruction , ret.class
assert_equal @r0 , ret.left
assert_equal @r1 , ret.right
assert_equal :<< , ret.operator
assert_operator ret , :<< , :message , "id_"
end
def test_operator_slot
ret = @r0.op :<< , @r1[:type]
assert_operator ret , :<< , :message , "id_.type"
end
def test_byte_to_reg
instr = @r0 <= @r1[1]
@ -66,10 +67,7 @@ module Risc
end
def test_slot_to_reg
instr = @r0 << @r2[:next_object]
assert_equal SlotToReg , instr.class
assert_equal :message , instr.register.symbol
assert_equal 2 , instr.index
assert instr.array.is_object?
assert_slot_to_reg instr , "id_" , 2 , :message
end
def test_reg_to_byte
instr = @r1[1] <= @r0

View File

@ -20,32 +20,17 @@ module SlotMachine
assert_equal Risc::RegToSlot , @instructions.next(3).class
assert_equal NilClass , @instructions.next(4).class
end
def test_ins_next_reg
assert_equal :"message.caller.type" , @instructions.next.register.symbol
def test_ins
assert_slot_to_reg @instructions ,:message , 6 , "message.caller"
end
def test_ins_next_arr
assert_equal :"message.caller" , @instructions.next.array.symbol
def test_ins_next
assert_slot_to_reg @instructions.next ,"message.caller" , 0 , "message.caller.type"
end
def test_ins_next_index
assert_equal 0 , @instructions.next.index
def test_ins_next_2
assert_slot_to_reg @instructions.next(2) , :message , 6 , "message.caller"
end
def test_ins_next_2_reg
assert_equal :"message.caller" , @instructions.next(2).register.symbol
end
def test_ins_next_2_arr
assert_equal :"message" , @instructions.next(2).array.symbol
end
def test_ins_next_2_index
assert_equal 6 , @instructions.next(2).index
end
def test_ins_next_3_reg
assert_equal :"message.caller.type" , @instructions.next(3).register.symbol
end
def test_ins_next_3_arr
assert_equal :"message.caller" , @instructions.next(3).array.symbol
end
def test_ins_next_3_index
assert_equal 0 , @instructions.next(3).index
def test_ins_next_3
assert_reg_to_slot @instructions.next(3) ,"message.caller.type" , "message.caller" , 0
end
end
end

View File

@ -17,34 +17,18 @@ module SlotMachine
assert_equal Risc::SlotToReg , @instructions.class
assert_equal Risc::LoadConstant, @instructions.next.class
end
def test_ins_next_class
assert_equal Risc::RegToSlot , @instructions.next(2).class
assert_equal NilClass , @instructions.next(3).class
end
def test_ins_load
assert @instructions.next.register.is_object?
assert_equal Parfait::CacheEntry , @instructions.next.constant.class
def test_ins
assert_slot_to_reg @instructions , :message , 0 , "message.type"
end
def test_ins_next_reg
assert_equal :"message.type" , @instructions.register.symbol
def test_ins_next
assert_load @instructions.next , Parfait::CacheEntry , "id_"
end
def test_ins_next_arr
assert_equal :message , @instructions.array.symbol
end
def test_ins_next_index
assert_equal 0 , @instructions.index
end
def test_ins_next_2_reg
assert_equal :"message.type" , @instructions.next(2).register.symbol
end
def test_ins_next_2_arr
assert @instructions.next(2).array.is_object?
end
def test_ins_next_2_index
assert_equal 1 , @instructions.next(2).index
def test_ins_next_2
assert_reg_to_slot @instructions.next(2) , :"message.type" , "id_", 1
end
end

View File

@ -14,26 +14,20 @@ module SlotMachine
assert_slot_to_reg risc(1) ,:message , 6 , :"message.caller"
end
def test_2_load
assert_load risc(2) , Parfait::FalseClass
assert_load risc(2) , Parfait::FalseClass, "id_"
end
def test_3_op
assert_equal Risc::OperatorInstruction , risc(3).class
assert risc(3).left.is_object?
assert_equal :"message.caller" , risc(3).right.symbol
assert_equal :- , risc(3).operator
assert_operator risc(3) , :- , "id_" , "message.caller"
end
def test_4_zero
assert_equal Risc::IsZero , risc(4).class
assert_label risc(4).label , "target"
end
def test_5_load
assert_load risc(5) , Parfait::NilClass
assert_load risc(5) , Parfait::NilClass , "id_"
end
def test_6_op
assert_equal Risc::OperatorInstruction , risc(6).class
assert risc(6).left.is_object?
assert_equal :"message.caller" , risc(6).right.symbol
assert_equal :- , risc(6).operator
assert_operator risc(6), :- , "id_", "message.caller"
end
def test_7_zero
assert_equal Risc::IsZero , risc(7).class

View File

@ -10,11 +10,8 @@ module SlotMachine
register = @slotted.to_register(compiler , InstructionMock.new)
@instruction = compiler.risc_instructions.next
end
def test_def_class
assert_equal Risc::LoadConstant , @instruction.class
end
def test_def_register
assert @instruction.register.is_object?
def test_load
assert_load @instruction , Parfait::Word , "id_"
end
def test_def_const
assert_equal "hi" , @instruction.constant.to_string
@ -28,11 +25,8 @@ module SlotMachine
register = @slotted.to_register(compiler , InstructionMock.new)
@instruction = compiler.risc_instructions.next
end
def test_def_class
assert_equal Risc::LoadConstant , @instruction.class
end
def test_def_register
assert @instruction.register.is_object?
def test_load
assert_load @instruction , Parfait::Word , "id_"
end
def test_def_const
assert_equal "hi" , @instruction.constant.to_string
@ -41,10 +35,7 @@ module SlotMachine
assert_equal "StringConstant.type" , @slotted.to_s
end
def test_def_register2
assert @instruction.next.register.is_object?
end
def test_def_next_index
assert_equal 0 , @instruction.next.index
assert_slot_to_reg @instruction.next , "id_" , 0 , "id_.0"
end
end
end

View File

@ -6,59 +6,37 @@ module SlotMachine
def setup
Parfait.boot!(Parfait.default_test_options)
compiler = Risc.test_compiler
@slotted = Slotted.for(Parfait.object_space , [:type])
@slotted = Slotted.for(Parfait.object_space , slot2)
register = @slotted.to_register(compiler , InstructionMock.new)
@instruction = compiler.risc_instructions.next
end
def test_def_class
assert_equal Risc::LoadConstant , @instruction.class
def slot2
[:type]
end
def test_def_register
assert @instruction.register.is_object?
end
def test_def_const
assert_equal Parfait::Space , @instruction.constant.class
def test_load
assert_load @instruction , Parfait::Space , "id_"
end
def test_to_s
assert_equal "Space.type" , @slotted.to_s
assert_equal "Space." + slot2.join(".") , @slotted.to_s
end
def test_def_register2
reg = @instruction.next.register
assert reg.is_object?
assert reg.symbol.to_s.index(".") , reg.symbol.to_s
end
def test_def_next_index
assert_equal 0 , @instruction.next.index
assert_slot_to_reg @instruction.next , "id_" , 0 , "id_.type"
end
end
class TestSlottedObjectType2 < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
compiler = Risc.test_compiler
@slotted = Slotted.for(Parfait.object_space , [:type , :type])
register = @slotted.to_register(compiler , InstructionMock.new)
@instruction = compiler.risc_instructions.next
end
def test_def_register2
reg = @instruction.next.register
assert reg.is_object?
assert_equal "type", reg.symbol.to_s.split(".").last
assert_equal 2, reg.symbol.to_s.split(".").length
end
end
class TestSlottedObjectType3 < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
compiler = Risc.test_compiler
@slotted = Slotted.for(Parfait.object_space , [:type , :type , :type])
register = @slotted.to_register(compiler , InstructionMock.new)
@instruction = compiler.risc_instructions.next
class TestSlottedObjectType2 < TestSlottedObjectType
def slot2
[:type , :type]
end
def test_def_register3
reg = @instruction.next.next.register
assert reg.is_object?
assert_equal "type", reg.symbol.to_s.split(".").last
assert_equal 3, reg.symbol.to_s.split(".").length
assert_slot_to_reg @instruction.next(2) , "id_.type" , 0 , "id_.type.type"
end
end
class TestSlottedObjectType3 < TestSlottedObjectType
def slot2
[:type , :type , :type ]
end
def test_def_register3
assert_slot_to_reg @instruction.next(3) , "id_.type.type" , 0 , "id_.type.type.type"
end
end
end

View File

@ -3,31 +3,44 @@ module Minitest
def assert_tos string , object
assert_equal string , object.to_s.gsub("\n",";").gsub(/\s+/," ").gsub("; ",";")
end
def assert_register( kind , pattern , register)
return unless register
if(pattern.is_a?(Symbol))
assert_equal( pattern , register.symbol , "wrong #{kind} register:#{register}")
else
is_parts = pattern.split(".")
reg_parts = register.symbol.to_s.split(".")
assert_equal reg_parts.length , is_parts.length , "wrong dot length for #{pattern}"
is_parts.each_with_index do |part , index|
assert reg_parts[index].start_with?(part) , "wrong #{kind}, at:#{part} register:#{register}"
end
end
end
def assert_slot_to_reg( slot , array = nil, index = nil , register = nil)
assert_equal Risc::SlotToReg , slot.class
assert_equal( array , slot.array.symbol , "wrong source register") if array
assert_register( :source , array , slot.array)
assert_equal( index , slot.index, "wrong source index") if index
assert_equal( register , slot.register.symbol, "wrong destination") if register
assert_register( :destination , register , slot.register )
end
def assert_reg_to_slot( slot , register = nil, array = nil, index = nil )
assert_equal Risc::RegToSlot , slot.class
assert_equal( register , slot.register.symbol, "wrong source register") if register
assert_equal( array , slot.array.symbol, "wrong destination register") if array
assert_register( :source , register , slot.register )
assert_register( :destination , array , slot.array)
assert_equal( index , slot.index, "wrong destination index") if index
end
def assert_load(load , clazz = nil , register = nil)
assert_equal Risc::LoadConstant , load.class
assert_equal( clazz , load.constant.class) if clazz
if register
assert_equal( register , load.register.symbol, "wrong destination register") if register
assert_register(:source , register , load.register)
else
assert load.register.is_object? , "reg #{load.register.symbol} is not object (ie no id_xx)"
raise "rewrite"
end
end
def assert_transfer( transfer , from , to)
assert_equal Risc::Transfer , transfer.class
assert_equal from , transfer.from.symbol
assert_equal to , transfer.to.symbol
assert_register( :source , from , transfer.from )
assert_register( :destination , to , transfer.to )
end
def assert_label( label , name )
assert_equal Risc::Label , label.class
@ -44,8 +57,8 @@ module Minitest
def assert_operator ins , op , left , right
assert_equal Risc::OperatorInstruction , ins.class
assert_equal op , ins.operator
assert_equal left , ins.left.symbol
assert_equal right , ins.right.symbol
assert_register :left , left , ins.left
assert_register :right , right , ins.right
end
def assert_zero ins , label
assert_equal Risc::IsZero , ins.class