rubyx/stash/soml/statements/test_assign.rb

129 lines
2.9 KiB
Ruby
Raw Normal View History

require_relative 'helper'
module Register
class TestAssignStatement < MiniTest::Test
include Statements
def setup
2015-10-22 17:16:29 +02:00
Register.machine.boot
end
def test_assign_op
@string_input = <<HERE
2015-11-30 15:20:39 +01:00
class Space
int main()
int n = 10 + 1
end
end
HERE
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, GetSlot, SetSlot, Label ,
FunctionReturn]
check
end
def test_assign_local
@string_input = <<HERE
2015-11-30 15:20:39 +01:00
class Space
int main()
int runner
runner = 5
end
end
HERE
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
check
end
def test_assign_local_assign
@string_input = <<HERE
2015-11-30 15:20:39 +01:00
class Space
int main()
int runner = 5
end
end
HERE
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
check
end
def test_assign_call
@string_input = <<HERE
2015-11-30 15:20:39 +01:00
class Space
int main()
int r = main()
end
end
HERE
@expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant ,
SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
GetSlot, GetSlot, GetSlot, SetSlot, Label, FunctionReturn]
2015-11-02 19:12:01 +01:00
check
end
2015-10-22 10:02:57 +02:00
def test_frame_get
@string_input = <<HERE
2015-11-30 15:20:39 +01:00
class Space
2015-10-22 10:02:57 +02:00
int main()
int r = 5
return r
end
end
HERE
@expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, SetSlot ,
Label, FunctionReturn]
2015-10-22 10:02:57 +02:00
was = check
2015-11-02 19:12:01 +01:00
get = was.next(5)
2015-10-22 10:02:57 +02:00
assert_equal GetSlot , get.class
assert_equal 4, get.index , "Get to frame index must be offset, not #{get.index}"
2015-10-22 10:02:57 +02:00
end
def test_assign_arg
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :blar)
2015-10-22 10:02:57 +02:00
@string_input = <<HERE
2015-11-30 15:20:39 +01:00
class Space
int main(int blar)
blar = 5
2015-10-22 10:02:57 +02:00
end
end
HERE
@expect = [Label, LoadConstant, SetSlot, Label, FunctionReturn]
2015-10-22 10:02:57 +02:00
was = check
2015-11-02 19:12:01 +01:00
set = was.next(2)
2015-10-22 10:02:57 +02:00
assert_equal SetSlot , set.class
assert_equal 10, set.index , "Set to args index must be offset, not #{set.index}"
2015-10-22 10:02:57 +02:00
end
def test_assign_int
@string_input = <<HERE
2015-11-30 15:20:39 +01:00
class Space
2015-10-22 10:02:57 +02:00
int main()
int r = 5
end
end
HERE
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
2015-10-22 10:02:57 +02:00
was = check
2015-11-02 19:12:01 +01:00
set = was.next(3)
2015-10-22 10:02:57 +02:00
assert_equal SetSlot , set.class
assert_equal 4, set.index , "Set to frame index must be offset, not #{set.index}"
2015-10-22 10:02:57 +02:00
end
def test_arg_get
# have to define bar externally, just because redefining main. Otherwise that would be automatic
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :balr)
2015-10-22 10:02:57 +02:00
@string_input = <<HERE
2015-11-30 15:20:39 +01:00
class Space
int main(int balr)
return balr
2015-10-22 10:02:57 +02:00
end
end
HERE
@expect = [Label, GetSlot, SetSlot, Label, FunctionReturn]
2015-10-22 10:02:57 +02:00
was = check
2015-11-02 19:12:01 +01:00
get = was.next(1)
2015-10-22 10:02:57 +02:00
assert_equal GetSlot , get.class
assert_equal 10, get.index , "Get to frame index must be offset, not #{get.index}"
2015-10-22 10:02:57 +02:00
end
end
end