2018-07-31 17:00:42 +02:00
|
|
|
module Minitest
|
2018-04-05 19:10:00 +02:00
|
|
|
module Assertions
|
2019-10-02 16:42:24 +02:00
|
|
|
def assert_tos string , object
|
|
|
|
assert_equal string , object.to_s.gsub("\n",";").gsub(/\s+/," ").gsub("; ",";")
|
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_register( kind , pattern , register , at = 0)
|
2020-03-02 16:50:03 +01:00
|
|
|
return unless register
|
|
|
|
if(pattern.is_a?(Symbol))
|
2020-03-06 20:26:23 +01:00
|
|
|
assert_equal( pattern , register.symbol , "wrong #{kind} register:#{register} , at:#{at}")
|
2020-03-02 16:50:03 +01:00
|
|
|
else
|
|
|
|
is_parts = pattern.split(".")
|
|
|
|
reg_parts = register.symbol.to_s.split(".")
|
2020-03-06 20:26:23 +01:00
|
|
|
assert_equal reg_parts.length , is_parts.length , "wrong dot length for #{pattern} , at:#{at}"
|
2020-03-02 16:50:03 +01:00
|
|
|
is_parts.each_with_index do |part , index|
|
2020-03-06 20:26:23 +01:00
|
|
|
assert reg_parts[index].start_with?(part) , "wrong #{kind}, part:#{part} register:#{register}, at:#{at}"
|
2020-03-02 16:50:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_slot_to_reg( slot_i , array = nil, index = nil , register = nil)
|
|
|
|
assert_equal Integer , slot_i.class , "assert_slot_to_reg #{slot_i}"
|
|
|
|
slot = risc(slot_i)
|
|
|
|
assert_equal Risc::SlotToReg , slot.class , "Class at #{slot_i}"
|
|
|
|
assert_register( :source , array , slot.array , slot_i)
|
|
|
|
assert_register( :destination , register , slot.register , slot_i )
|
|
|
|
assert_index( :source , slot_i , index)
|
2018-04-05 19:37:03 +02:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_reg_to_slot( slot_i , register = nil, array = nil, index = nil )
|
|
|
|
assert_equal Integer , slot_i.class, "assert_reg_to_slot #{slot_i}"
|
|
|
|
slot = risc(slot_i)
|
|
|
|
assert_equal Risc::RegToSlot , slot.class, "Class at #{slot_i}"
|
|
|
|
assert_register( :source , register , slot.register , slot_i)
|
|
|
|
assert_register( :destination , array , slot.array , slot_i)
|
|
|
|
assert_index( :destination , slot_i , index )
|
2020-03-06 17:09:56 +01:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_index(kind , slot_i , index)
|
2020-03-06 17:09:56 +01:00
|
|
|
return unless index
|
2020-03-06 20:26:23 +01:00
|
|
|
assert_equal Integer , slot_i.class , "assert_index #{slot_i}"
|
|
|
|
slot = risc(slot_i)
|
2020-03-06 17:09:56 +01:00
|
|
|
if(slot.index.is_a?(Risc::RegisterValue))
|
2020-03-06 20:26:23 +01:00
|
|
|
assert_equal( Symbol , index.class, "wrong #{kind} index class, at:#{slot_i}")
|
|
|
|
assert_equal( index , slot.index.symbol, "wrong #{kind} index, at#{slot_i}")
|
2020-03-06 17:09:56 +01:00
|
|
|
else
|
2020-03-06 20:26:23 +01:00
|
|
|
assert_equal( index , slot.index, "wrong #{kind} index, at:#{slot_i}")
|
2020-03-06 17:09:56 +01:00
|
|
|
end
|
2018-04-05 19:10:00 +02:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_load(load_i , clazz = nil , register = nil)
|
|
|
|
assert_equal Integer , load_i.class, "assert_load #{load_i}"
|
|
|
|
load = risc(load_i)
|
2018-07-31 17:00:42 +02:00
|
|
|
assert_equal Risc::LoadConstant , load.class
|
2018-04-05 19:10:00 +02:00
|
|
|
assert_equal( clazz , load.constant.class) if clazz
|
2020-03-01 17:07:42 +01:00
|
|
|
if register
|
2020-03-02 16:50:03 +01:00
|
|
|
assert_register(:source , register , load.register)
|
2020-03-01 17:07:42 +01:00
|
|
|
else
|
2020-03-06 20:26:23 +01:00
|
|
|
raise "add register at:#{load_i}, as third operand at #{load_i}"
|
2020-03-01 17:07:42 +01:00
|
|
|
end
|
2018-04-05 19:10:00 +02:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_transfer( transfer_i , from , to)
|
|
|
|
assert_equal Integer , transfer_i.class, "assert_transfer #{transfer_i}"
|
|
|
|
transfer = risc(transfer_i)
|
|
|
|
assert_equal Risc::Transfer , transfer.class, "Class at #{transfer_i}"
|
|
|
|
assert_register( :source , from , transfer.from , transfer_i)
|
|
|
|
assert_register( :destination , to , transfer.to , transfer_i)
|
2018-08-15 16:52:21 +02:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_label( label_i , name , at = nil)
|
|
|
|
if(at)
|
|
|
|
label = label_i
|
|
|
|
label_i = at
|
|
|
|
else
|
|
|
|
assert_equal Integer , label_i.class, "assert_label #{label_i}"
|
|
|
|
label = risc(label_i)
|
|
|
|
end
|
|
|
|
assert_equal Risc::Label , label.class, "Class at:#{label_i}"
|
2019-09-15 16:44:35 +02:00
|
|
|
if(name[-1] == "_")
|
2020-03-06 20:26:23 +01:00
|
|
|
assert label.name.start_with?(name) , "Label at:#{label_i} does not start with #{name}:#{label.name}"
|
2019-09-15 16:44:35 +02:00
|
|
|
else
|
2020-03-06 20:26:23 +01:00
|
|
|
assert_equal name , label.name , "Label at:#{label_i}"
|
2019-09-15 16:44:35 +02:00
|
|
|
end
|
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_branch( branch_i , label_name )
|
|
|
|
assert_equal Integer , branch_i.class, "assert_branch #{branch_i}"
|
|
|
|
branch = risc(branch_i)
|
|
|
|
assert_equal Risc::Branch , branch.class , "Class at:#{branch_i}"
|
|
|
|
assert_label branch.label , label_name , "Label at #{branch_i}"
|
2019-09-17 09:18:45 +02:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_operator ins_i , op , left , right
|
|
|
|
assert_equal Integer , ins_i.class, "assert_operator #{ins_i}"
|
|
|
|
ins = risc(ins_i)
|
|
|
|
assert_equal Risc::OperatorInstruction , ins.class , "Class at:#{ins_i}"
|
2019-09-17 09:18:45 +02:00
|
|
|
assert_equal op , ins.operator
|
2020-03-06 20:26:23 +01:00
|
|
|
assert_register :left , left , ins.left , ins_i
|
|
|
|
assert_register :right , right , ins.right, ins_i
|
2019-09-17 09:18:45 +02:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_zero ins_i , label
|
|
|
|
assert_equal Integer , ins_i.class, "assert_zero #{ins_i}"
|
|
|
|
ins = risc(ins_i)
|
|
|
|
assert_equal Risc::IsNotZero , ins.class , "Class at:#{ins_i}"
|
|
|
|
assert_label ins.label , label , "Label at:#{ins_i}"
|
2020-03-06 17:09:56 +01:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_not_zero ins_i , label
|
|
|
|
assert_equal Integer , ins_i.class, "assert_not_zero #{ins_i}"
|
|
|
|
ins = risc(ins_i)
|
|
|
|
assert_equal Risc::IsZero , ins.class, "Class at:#{ins_i}"
|
|
|
|
assert_label ins.label , label, "Label at:#{ins_i}"
|
2019-09-17 09:18:45 +02:00
|
|
|
end
|
|
|
|
def assert_syscall ins , name
|
2020-03-06 20:26:23 +01:00
|
|
|
assert_equal Integer , ins_i.class, "assert_syscall #{ins_i}"
|
|
|
|
ins = risc(ins_i)
|
|
|
|
assert_equal Risc::Syscall , ins.class, "Class at:#{ins_i}"
|
2019-09-17 09:18:45 +02:00
|
|
|
assert_equal ins.name , name
|
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_minus ins_i , label
|
|
|
|
assert_equal Integer , ins_i.class, "assert_minus #{ins_i}"
|
|
|
|
ins = risc(ins_i)
|
|
|
|
assert_equal Risc::IsMinus , ins.class, "Class at:#{ins_i}"
|
|
|
|
assert_label ins.label , label, ins_i
|
2020-03-06 17:09:56 +01:00
|
|
|
end
|
2020-03-06 20:26:23 +01:00
|
|
|
def assert_data(ins_i , data)
|
|
|
|
assert_equal Integer , ins_i.class, "assert_data #{ins_i}"
|
|
|
|
ins = risc(ins_i)
|
|
|
|
assert_equal Risc::LoadData , ins.class, "Class at:#{ins_i}"
|
|
|
|
assert_equal data , ins.constant , "Data at:#{ins_i}"
|
2020-03-06 17:09:56 +01:00
|
|
|
end
|
2018-04-05 19:10:00 +02:00
|
|
|
end
|
|
|
|
end
|