rubyx/test/mom/test_assignment.rb

90 lines
3.7 KiB
Ruby
Raw Normal View History

2018-03-15 16:21:46 +01:00
require_relative 'helper'
2017-04-10 15:12:15 +02:00
2018-03-15 16:21:46 +01:00
module Risc
class TestAssignStatement < MiniTest::Test
include Statements
2017-04-10 15:12:15 +02:00
2018-03-15 16:21:46 +01:00
def pest_assign_op
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = "r = 10.mod4"
@expect = [Label, Label, Label, Label, Label]
2018-03-15 16:21:46 +01:00
assert_nil msg = check_nil , msg
2017-09-10 11:36:16 +02:00
end
2018-03-15 16:21:46 +01:00
def pest_assign_ivar
@input = "@r = 5"
@expect = [Label]
2018-03-15 16:21:46 +01:00
assert_nil msg = check_nil , msg
end
2018-03-15 16:21:46 +01:00
def pest_assign_call
Parfait.object_space.get_main.add_local(:r , :Object)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:call, :main, s(:arguments))))
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer ,
FunctionCall, Label, RiscTransfer, SlotToReg, SlotToReg, SlotToReg ,
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
2017-09-10 18:48:46 +02:00
end
2018-03-15 16:21:46 +01:00
def pest_named_list_get
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)), s(:return, s(:local, :r)))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg ,
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
was = check_return
get = was.next(5)
assert_equal SlotToReg , get.class
assert_equal 1 + 1, get.index , "Get to named_list index must be offset, not #{get.index}"
2017-09-10 18:48:46 +02:00
end
2018-03-15 16:21:46 +01:00
def pest_assign_local_int
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)) )
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
was = check_return
set = was.next(3)
assert_equal RegToSlot , set.class
assert_equal 1 + 1, set.index , "Set to named_list index must be offset, not #{set.index}"
2017-09-10 18:48:46 +02:00
end
2018-03-15 16:21:46 +01:00
def pest_misassign_local
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:string, "5")) )
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_raises {check }
2017-09-10 18:48:46 +02:00
end
2018-03-15 16:21:46 +01:00
def pest_assign_arg
Parfait.object_space.get_main.add_argument(:blar , :Integer)
@input = s(:statements, s(:a_assignment, s(:arg, :blar), s(:int, 5)))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
was = check_return
set = was.next(3)
assert_equal RegToSlot , set.class
assert_equal 1 + 1, set.index , "Set to args index must be offset, not #{set.index}"
2017-09-10 18:48:46 +02:00
end
2018-03-15 16:21:46 +01:00
def pest_misassign_arg
Parfait.object_space.get_main.add_argument(:blar , :Integer)
@input = s(:statements, s(:a_assignment, s(:arg, :blar), s(:string, "5")))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_raises {check }
2017-09-10 18:48:46 +02:00
end
2018-03-15 16:21:46 +01:00
def pest_arg_get
# have to define bar externally, just because redefining main. Otherwise that would be automatic
Parfait.object_space.get_main.add_argument(:balr , :Integer)
@input = s(:statements, s(:return, s(:arg, :balr)))
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
was = check_return
get = was.next(2)
assert_equal SlotToReg , get.class
assert_equal 1 + 1, get.index , "Get to args index must be offset, not #{get.index}"
2017-09-10 18:48:46 +02:00
end
end
2017-04-10 15:12:15 +02:00
end