2015-10-16 13:17:02 +03:00
|
|
|
require_relative 'helper'
|
|
|
|
|
2017-01-19 09:02:29 +02:00
|
|
|
module Risc
|
2016-12-10 22:42:02 +02:00
|
|
|
class TestWhile < MiniTest::Test
|
2015-10-16 13:17:02 +03:00
|
|
|
include Statements
|
|
|
|
|
|
|
|
|
|
|
|
def test_while_mini
|
2016-12-10 23:07:04 +02:00
|
|
|
@input = s(:statements, s(:while_statement, :plus, s(:conditional, s(:int, 1)), s(:statements, s(:return, s(:int, 3)))))
|
2016-12-10 22:42:02 +02:00
|
|
|
|
2017-01-04 21:38:38 +02:00
|
|
|
@expect = [Label, Branch, Label, LoadConstant, RegToSlot, Label ,
|
|
|
|
LoadConstant, IsPlus, LoadConstant, SlotToReg, RegToSlot, Label ,
|
|
|
|
FunctionReturn]
|
|
|
|
assert_nil msg = check_nil , msg
|
2015-10-16 13:17:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_while_assign
|
2016-12-30 14:10:49 +02:00
|
|
|
Parfait.object_space.get_main.add_local(:n , :Integer)
|
2016-12-17 13:12:49 +02:00
|
|
|
|
2017-01-16 09:34:47 +02:00
|
|
|
@input = s(:statements, s(:l_assignment, s(:local, :n), s(:int, 5)),
|
|
|
|
s(:while_statement, :plus, s(:conditional, s(:local, :n)),
|
|
|
|
s(:statements, s(:l_assignment, s(:local, :n),
|
|
|
|
s(:operator_value, :-, s(:local, :n), s(:int, 1))))),
|
|
|
|
s(:return, s(:local, :n)))
|
2016-12-10 22:42:02 +02:00
|
|
|
|
2017-01-04 21:38:38 +02:00
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Branch, Label ,
|
|
|
|
SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, SlotToReg, RegToSlot ,
|
|
|
|
Label, SlotToReg, SlotToReg, IsPlus, SlotToReg, SlotToReg ,
|
|
|
|
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
|
|
|
assert_nil msg = check_nil , msg
|
2015-10-16 13:17:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_while_return
|
2016-12-30 14:10:49 +02:00
|
|
|
Parfait.object_space.get_main.add_local(:n , :Integer)
|
2016-12-17 13:12:49 +02:00
|
|
|
|
2017-01-16 09:34:47 +02:00
|
|
|
@input = s(:statements, s(:l_assignment, s(:local, :n), s(:int, 10)), s(:while_statement, :plus, s(:conditional, s(:operator_value, :-, s(:local, :n), s(:int, 5))),
|
|
|
|
s(:statements, s(:l_assignment, s(:local, :n), s(:operator_value, :+, s(:local, :n), s(:int, 1))),
|
|
|
|
s(:return, s(:local, :n)))))
|
2016-12-10 22:42:02 +02:00
|
|
|
|
2017-01-04 21:38:38 +02:00
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Branch, Label ,
|
|
|
|
SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, SlotToReg, RegToSlot ,
|
|
|
|
SlotToReg, SlotToReg, RegToSlot, Label, SlotToReg, SlotToReg ,
|
|
|
|
LoadConstant, OperatorInstruction, IsPlus, LoadConstant, SlotToReg, RegToSlot ,
|
|
|
|
Label, FunctionReturn]
|
|
|
|
assert_nil msg = check_nil , msg
|
2015-10-16 13:17:02 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|