move stashed tests
This commit is contained in:
42
test/typed/statements/helper.rb
Normal file
42
test/typed/statements/helper.rb
Normal file
@ -0,0 +1,42 @@
|
||||
require_relative '../helper'
|
||||
|
||||
|
||||
module Statements
|
||||
include AST::Sexp
|
||||
|
||||
def check
|
||||
assert @expect , "No output given"
|
||||
Register.machine.boot # force boot to reset main
|
||||
compiler = Typed::Compiler.new Register.machine.space.get_main
|
||||
produced = compiler.process( Typed.ast_to_code( @input) )
|
||||
assert_nil produced.first , "Statements should result in nil"
|
||||
produced = Register.machine.space.get_main.instructions
|
||||
compare_instructions produced , @expect
|
||||
produced
|
||||
end
|
||||
|
||||
def as_main(statements)
|
||||
s(:statements, s(:class, :Space, s(:derives, nil), statements ))
|
||||
end
|
||||
|
||||
def compare_instructions instruction , expect
|
||||
index = 0
|
||||
start = instruction
|
||||
begin
|
||||
should = expect[index]
|
||||
assert should , "No instruction at #{index}"
|
||||
assert_equal instruction.class , should , "Expected at #{index+1}\n#{should(start)}"
|
||||
index += 1
|
||||
instruction = instruction.next
|
||||
end while( instruction )
|
||||
end
|
||||
def should start
|
||||
str = start.to_ac.to_s
|
||||
str.gsub!("Register::","")
|
||||
ret = ""
|
||||
str.split(",").each_slice(7).each do |line|
|
||||
ret += " " + line.join(",") + " ,\n"
|
||||
end
|
||||
ret
|
||||
end
|
||||
end
|
7
test/typed/statements/test_all.rb
Normal file
7
test/typed/statements/test_all.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require_relative "test_assign"
|
||||
require_relative "test_call"
|
||||
require_relative "test_class"
|
||||
require_relative "test_fields"
|
||||
require_relative "test_if"
|
||||
require_relative "test_return"
|
||||
require_relative "test_while"
|
119
test/typed/statements/test_assign.rb
Normal file
119
test/typed/statements/test_assign.rb
Normal file
@ -0,0 +1,119 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestAssignStatement < MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
end
|
||||
|
||||
def _test_assign_op
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :n),
|
||||
s(:operator_value, :+, s(:int, 10), s(:int, 1))))
|
||||
|
||||
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, GetSlot, SetSlot, Label ,
|
||||
FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def _test_assign_local
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :runner)),
|
||||
s(:assignment, s(:name, :runner), s(:int, 5)))
|
||||
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def _test_assign_local_assign
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int runner = 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def _test_assign_call
|
||||
@input = <<HERE
|
||||
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]
|
||||
check
|
||||
end
|
||||
|
||||
def _test_frame_get
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int r = 5
|
||||
return r
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, SetSlot ,
|
||||
Label, FunctionReturn]
|
||||
was = check
|
||||
get = was.next(5)
|
||||
assert_equal GetSlot , get.class
|
||||
assert_equal 4, get.index , "Get to frame index must be offset, not #{get.index}"
|
||||
end
|
||||
|
||||
def _test_assign_int
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int r = 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
was = check
|
||||
set = was.next(3)
|
||||
assert_equal SetSlot , set.class
|
||||
assert_equal 4, set.index , "Set to frame index must be offset, not #{set.index}"
|
||||
end
|
||||
|
||||
def _test_assign_arg
|
||||
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :blar)
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main(int blar)
|
||||
blar = 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant, SetSlot, Label, FunctionReturn]
|
||||
was = check
|
||||
set = was.next(2)
|
||||
assert_equal SetSlot , set.class
|
||||
assert_equal 10, set.index , "Set to args index must be offset, not #{set.index}"
|
||||
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)
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main(int balr)
|
||||
return balr
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
was = check
|
||||
get = was.next(1)
|
||||
assert_equal GetSlot , get.class
|
||||
assert_equal 10, get.index , "Get to frame index must be offset, not #{get.index}"
|
||||
end
|
||||
end
|
||||
end
|
107
test/typed/statements/test_call.rb
Normal file
107
test/typed/statements/test_call.rb
Normal file
@ -0,0 +1,107 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestCallStatement #< MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_call_constant_int
|
||||
@input = <<HERE
|
||||
class Integer
|
||||
int putint()
|
||||
return 1
|
||||
end
|
||||
end
|
||||
class Space
|
||||
int main()
|
||||
42.putint()
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant ,
|
||||
SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
|
||||
GetSlot, GetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
|
||||
def test_call_constant_string
|
||||
@input = <<HERE
|
||||
class Word
|
||||
int putstring()
|
||||
return 1
|
||||
end
|
||||
end
|
||||
class Space
|
||||
int main()
|
||||
"Hello".putstring()
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant ,
|
||||
SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
|
||||
GetSlot, GetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_local_int
|
||||
@input = <<HERE
|
||||
class Integer
|
||||
int putint()
|
||||
return 1
|
||||
end
|
||||
end
|
||||
class Space
|
||||
int main()
|
||||
int testi = 20
|
||||
testi.putint()
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, GetSlot ,
|
||||
SetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot ,
|
||||
RegisterTransfer, FunctionCall, Label, RegisterTransfer, GetSlot, GetSlot, Label ,
|
||||
FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_local_class
|
||||
@input = <<HERE
|
||||
class List < Object
|
||||
int add()
|
||||
return 1
|
||||
end
|
||||
end
|
||||
class Space
|
||||
int main()
|
||||
List test_l
|
||||
test_l.add()
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot ,
|
||||
LoadConstant, SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label ,
|
||||
RegisterTransfer, GetSlot, GetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_puts
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int puts(Word str)
|
||||
return str
|
||||
end
|
||||
int main()
|
||||
puts("Hello")
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant ,
|
||||
SetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall ,
|
||||
Label, RegisterTransfer, GetSlot, GetSlot, Label, FunctionReturn]
|
||||
was = check
|
||||
set = was.next(7)
|
||||
assert_equal SetSlot , set.class
|
||||
assert_equal 9, set.index , "Set to message must be offset, not #{set.index}"
|
||||
end
|
||||
end
|
||||
end
|
56
test/typed/statements/test_class.rb
Normal file
56
test/typed/statements/test_class.rb
Normal file
@ -0,0 +1,56 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestClassStatements #< MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_class_defs
|
||||
@input = <<HERE
|
||||
class Bar
|
||||
int self.buh()
|
||||
return 1
|
||||
end
|
||||
end
|
||||
class Space
|
||||
int main()
|
||||
return 1
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant,SetSlot,Label,FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_class_call
|
||||
@input = <<HERE
|
||||
class Bar
|
||||
int self.buh()
|
||||
return 1
|
||||
end
|
||||
end
|
||||
class Space
|
||||
int main()
|
||||
return Bar.buh()
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant ,
|
||||
SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
|
||||
GetSlot, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_class_field
|
||||
@input = <<HERE
|
||||
class Space
|
||||
field int boo2
|
||||
int main()
|
||||
return self.boo2
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot,GetSlot,SetSlot,Label,FunctionReturn]
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
68
test/typed/statements/test_fields.rb
Normal file
68
test/typed/statements/test_fields.rb
Normal file
@ -0,0 +1,68 @@
|
||||
require_relative 'helper'
|
||||
|
||||
|
||||
module Register
|
||||
class TestFieldStatement #< MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_field_frame
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
Message m
|
||||
return m.name
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, GetSlot, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_field_arg
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int get_name(Message main)
|
||||
return main.name
|
||||
end
|
||||
int main()
|
||||
Message m
|
||||
return get_name(m)
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant ,
|
||||
SetSlot, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, RegisterTransfer ,
|
||||
FunctionCall, Label, RegisterTransfer, GetSlot, GetSlot, SetSlot, Label ,
|
||||
FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_self_field
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
Type l = self.type
|
||||
return 1
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, GetSlot, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot ,
|
||||
Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_message_field
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
Word name = message.name
|
||||
return name
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, RegisterTransfer, GetSlot, GetSlot, SetSlot, GetSlot, GetSlot ,
|
||||
SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
58
test/typed/statements/test_if.rb
Normal file
58
test/typed/statements/test_if.rb
Normal file
@ -0,0 +1,58 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestIfStatement #< MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_if_basicr
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
if_plus( 10 - 12)
|
||||
return 3
|
||||
else
|
||||
return 4
|
||||
end
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant,LoadConstant, OperatorInstruction,IsPlus ,
|
||||
LoadConstant,SetSlot,Branch , Label , LoadConstant ,SetSlot,
|
||||
Label,Label,FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
|
||||
def test_if_small_minus
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
if_minus( 10 - 12)
|
||||
return 3
|
||||
end
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsMinus, Branch, Label ,
|
||||
LoadConstant, SetSlot, Label, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
|
||||
def test_if_small_zero
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
if_zero( 10 - 12)
|
||||
return 3
|
||||
end
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant,LoadConstant,OperatorInstruction,IsZero ,
|
||||
Branch , Label , LoadConstant ,SetSlot,
|
||||
Label,Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
47
test/typed/statements/test_return.rb
Normal file
47
test/typed/statements/test_return.rb
Normal file
@ -0,0 +1,47 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestReturnStatement < MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_return_int
|
||||
@input = s(:statements, s(:return, s(:int, 5)))
|
||||
@expect = [Label, LoadConstant ,SetSlot,Label,FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_return_local
|
||||
@input = s(:statements,s(:field_def, :Integer, s(:name, :runner)),
|
||||
s(:return, s(:name, :runner)))
|
||||
@expect = [Label, GetSlot,GetSlot ,SetSlot,Label,FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_return_local_assign
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :runner), s(:int, 5)),
|
||||
s(:return, s(:name, :runner)))
|
||||
@expect = [Label, LoadConstant,GetSlot,SetSlot,GetSlot,GetSlot ,SetSlot,
|
||||
Label,FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_return_call
|
||||
@input = s(:statements, s(:return, s(:call, s(:name, :main), s(:arguments))))
|
||||
@expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant ,
|
||||
SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
|
||||
GetSlot, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def pest_return_space_length # need to add runtime first
|
||||
@input = s(:statements, s(:field_def, :Type, s(:name, :l),
|
||||
s(:call, s(:name, :get_type), s(:arguments), s(:receiver, s(:name, :space)))),
|
||||
s(:return, s(:field_access,
|
||||
s(:receiver, s(:name, :self)),
|
||||
s(:field, s(:name, :runner)))))
|
||||
@expect = [Label, GetSlot,GetSlot ,SetSlot,Label,FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
61
test/typed/statements/test_while.rb
Normal file
61
test/typed/statements/test_while.rb
Normal file
@ -0,0 +1,61 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestWhile #< MiniTest::Test
|
||||
include Statements
|
||||
|
||||
|
||||
def test_while_mini
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
while_plus(1)
|
||||
return 3
|
||||
end
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, Branch, Label, LoadConstant, SetSlot, Label, LoadConstant ,
|
||||
IsPlus, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def test_while_assign
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int n = 5
|
||||
while_plus(n)
|
||||
n = n - 1
|
||||
end
|
||||
return n
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, Branch, Label, GetSlot ,
|
||||
GetSlot, LoadConstant, OperatorInstruction, GetSlot, SetSlot, Label, GetSlot ,
|
||||
GetSlot, IsPlus, GetSlot, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
|
||||
def test_while_return
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int n = 10
|
||||
while_plus( n - 5)
|
||||
n = n + 1
|
||||
return n
|
||||
end
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, Branch, Label, GetSlot ,
|
||||
GetSlot, LoadConstant, OperatorInstruction, GetSlot, SetSlot, GetSlot, GetSlot ,
|
||||
SetSlot, Label, GetSlot, GetSlot, LoadConstant, OperatorInstruction, IsPlus ,
|
||||
Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user