renames Typed to Vm
This commit is contained in:
3
test/vm/helper.rb
Normal file
3
test/vm/helper.rb
Normal file
@ -0,0 +1,3 @@
|
||||
require_relative "../helper"
|
||||
|
||||
Register.machine.boot unless Register.machine.booted
|
78
test/vm/method_compiler/helper.rb
Normal file
78
test/vm/method_compiler/helper.rb
Normal file
@ -0,0 +1,78 @@
|
||||
require_relative '../helper'
|
||||
|
||||
module Register
|
||||
module ExpressionHelper
|
||||
|
||||
def check
|
||||
Register.machine.boot unless Register.machine.booted
|
||||
compiler = Vm::MethodCompiler.new Parfait.object_space.get_main
|
||||
code = Vm.ast_to_code @input
|
||||
assert code.to_s , @input
|
||||
produced = compiler.process( code )
|
||||
assert @output , "No output given"
|
||||
assert_equal produced.class , @output , "Wrong class"
|
||||
produced
|
||||
end
|
||||
|
||||
# test hack to in place change object type
|
||||
def add_space_field(name,type)
|
||||
class_type = Parfait.object_space.get_class_by_name(:Space).instance_type
|
||||
class_type.send(:private_add_instance_variable, name , type)
|
||||
end
|
||||
end
|
||||
|
||||
module Statements
|
||||
include AST::Sexp
|
||||
include Compiling
|
||||
|
||||
def setup
|
||||
Register.machine.boot # force boot to reset main
|
||||
end
|
||||
|
||||
def preamble
|
||||
[Label, SlotToReg , LoadConstant, RegToSlot, LoadConstant,RegToSlot, LoadConstant, SlotToReg, SlotToReg ]
|
||||
end
|
||||
def postamble
|
||||
[ Label, FunctionReturn]
|
||||
end
|
||||
def check_nil
|
||||
assert @expect , "No output given"
|
||||
compiler = Vm::MethodCompiler.new
|
||||
code = Vm.ast_to_code( @input )
|
||||
assert code.to_s , @input
|
||||
produced = compiler.process( code )
|
||||
produced = Parfait.object_space.get_main.instructions
|
||||
compare_instructions produced , @expect
|
||||
end
|
||||
def check_return
|
||||
was = check_nil
|
||||
raise was if was
|
||||
Parfait.object_space.get_main.instructions
|
||||
end
|
||||
|
||||
def compare_instructions( instruction , expect )
|
||||
index = 0
|
||||
all = instruction.to_arr
|
||||
full_expect = preamble + expect + postamble
|
||||
full_expect = expect
|
||||
begin
|
||||
should = full_expect[index]
|
||||
return "No instruction at #{index}" unless should
|
||||
return "Expected at #{index+1}\n#{should(all)}" unless instruction.class == should
|
||||
index += 1
|
||||
instruction = instruction.next
|
||||
end while( instruction )
|
||||
nil
|
||||
end
|
||||
def should( all )
|
||||
#preamble.each {all.shift}
|
||||
#postamble.each {all.pop}
|
||||
str = all.to_s.gsub("Register::","")
|
||||
ret = ""
|
||||
str.split(",").each_slice(6).each do |line|
|
||||
ret += " " + line.join(",") + " ,\n"
|
||||
end
|
||||
ret
|
||||
end
|
||||
end
|
||||
end
|
13
test/vm/method_compiler/test_all.rb
Normal file
13
test/vm/method_compiler/test_all.rb
Normal file
@ -0,0 +1,13 @@
|
||||
#statements
|
||||
require_relative "test_assignment"
|
||||
require_relative "test_call_site"
|
||||
require_relative "test_class"
|
||||
require_relative "test_fields"
|
||||
require_relative "test_if_statement"
|
||||
require_relative "test_return_statement"
|
||||
require_relative "test_while_statement"
|
||||
#expressions
|
||||
require_relative "test_basic_values"
|
||||
require_relative "test_field_access"
|
||||
require_relative "test_operator_expression"
|
||||
require_relative "test_name_expression"
|
105
test/vm/method_compiler/test_assignment.rb
Normal file
105
test/vm/method_compiler/test_assignment.rb
Normal file
@ -0,0 +1,105 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestAssignStatement < MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_assign_op
|
||||
Parfait.object_space.get_main.add_local(:r , :Integer)
|
||||
|
||||
@input = s(:statements, s(:assignment, s(:name, :r), s(:operator_value, :+, s(:int, 10), s(:int, 1))))
|
||||
|
||||
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, SlotToReg, RegToSlot ,
|
||||
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_assign_local
|
||||
Parfait.object_space.get_main.add_local(:r , :Integer)
|
||||
@input =s(:statements, s(:assignment, s(:name, :r), s(:int, 5)))
|
||||
|
||||
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
||||
RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_assign_local_assign
|
||||
Parfait.object_space.get_main.add_local(:r , :Integer)
|
||||
|
||||
@input = s(:statements, s(:assignment, s(:name, :r), s(:int, 5)))
|
||||
|
||||
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
||||
RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_assign_call
|
||||
Parfait.object_space.get_main.add_local(:r , :Integer)
|
||||
@input = s(:statements, s(:assignment, s(:name, :r), s(:call, s(:name, :main), s(:arguments))))
|
||||
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
|
||||
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RegisterTransfer ,
|
||||
FunctionCall, Label, RegisterTransfer, SlotToReg, SlotToReg, SlotToReg ,
|
||||
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_named_list_get
|
||||
Parfait.object_space.get_main.add_local(:r , :Integer)
|
||||
@input = s(:statements, s(:assignment, s(:name, :r), s(:int, 5)), s(:return, s(:name, :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}"
|
||||
end
|
||||
|
||||
def test_assign_local_int
|
||||
Parfait.object_space.get_main.add_local(:r , :Integer)
|
||||
@input = s(:statements, s(:assignment, s(:name, :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}"
|
||||
end
|
||||
|
||||
def test_misassign_local
|
||||
Parfait.object_space.get_main.add_local(:r , :Integer)
|
||||
@input = s(:statements, s(:assignment, s(:name, :r), s(:string, "5")) )
|
||||
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_raises {check }
|
||||
end
|
||||
|
||||
def test_assign_arg
|
||||
Parfait.object_space.get_main.add_argument(:blar , :Integer)
|
||||
@input = s(:statements, s(:assignment, s(:name, :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}"
|
||||
end
|
||||
|
||||
def test_misassign_arg
|
||||
Parfait.object_space.get_main.add_argument(:blar , :Integer)
|
||||
@input = s(:statements, s(:assignment, s(:name, :blar), s(:string, "5")))
|
||||
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_raises {check }
|
||||
end
|
||||
|
||||
def test_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(:name, :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}"
|
||||
end
|
||||
end
|
||||
end
|
41
test/vm/method_compiler/test_basic_values.rb
Normal file
41
test/vm/method_compiler/test_basic_values.rb
Normal file
@ -0,0 +1,41 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Register
|
||||
|
||||
class TestBasic < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
include AST::Sexp
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@output = Register::RegisterValue
|
||||
end
|
||||
|
||||
def test_number
|
||||
@input = s(:int , 42)
|
||||
assert_equal 42 , check.value
|
||||
end
|
||||
|
||||
def test_true
|
||||
@input = s(:true)
|
||||
check
|
||||
end
|
||||
def test_false
|
||||
@input = s(:false)
|
||||
check
|
||||
end
|
||||
def test_nil
|
||||
@input = s(:nil)
|
||||
check
|
||||
end
|
||||
def test_self
|
||||
@input = s(:name, :self)
|
||||
check
|
||||
end
|
||||
def test_string
|
||||
@input = s(:string , "hello")
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
43
test/vm/method_compiler/test_call_expression.rb
Normal file
43
test/vm/method_compiler/test_call_expression.rb
Normal file
@ -0,0 +1,43 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Register
|
||||
class TestCall < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
include AST::Sexp
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@output = Register::RegisterValue
|
||||
end
|
||||
|
||||
def test_call_main_plain
|
||||
@input = s(:call,s(:name, :main),s(:arguments))
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_int
|
||||
Parfait.object_space.get_main.add_argument(:blar , :Integer)
|
||||
@input =s(:call,s(:name, :main),s(:arguments , s(:int, 1)))
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_string
|
||||
Parfait.object_space.get_main.add_argument(:blar , :Word)
|
||||
@input =s(:call,s(:name, :main),s(:arguments , s(:string, "1") ))
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_op
|
||||
Parfait.object_space.get_main.add_local(:bar , :Integer)
|
||||
Parfait.object_space.get_main.add_argument(:blar , :Integer)
|
||||
@input =s(:call,s(:name, :main),s(:arguments , s(:name, :bar) ))
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_string_put
|
||||
@input = s(:call,s(:name, :putstring),s(:arguments),s(:receiver,s(:string, "Hello Raisa, I am salama")))
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
68
test/vm/method_compiler/test_call_site.rb
Normal file
68
test/vm/method_compiler/test_call_site.rb
Normal file
@ -0,0 +1,68 @@
|
||||
require_relative 'helper'
|
||||
require_relative "test_call_expression"
|
||||
|
||||
module Register
|
||||
class TestCallStatement < MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_call_constant_int
|
||||
clean_compile :Integer, :puti, {}, s(:statements, s(:return, s(:int, 1)))
|
||||
@input = s(:call, s(:name, :puti), s(:arguments), s(:receiver, s(:int, 42)))
|
||||
@expect = [Label, SlotToReg, LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant ,
|
||||
SlotToReg, RegToSlot, LoadConstant, RegToSlot, RegisterTransfer, FunctionCall, Label ,
|
||||
RegisterTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot, Label ,
|
||||
FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
|
||||
def test_call_constant_string
|
||||
clean_compile :Word, :putstr,{}, s(:statements, s(:return, s(:int, 1)))
|
||||
|
||||
@input =s(:call, s(:name, :putstr), s(:arguments), s(:receiver, s(:string, "Hello")))
|
||||
@expect = [Label, SlotToReg, LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant ,
|
||||
SlotToReg, RegToSlot, LoadConstant, RegToSlot, RegisterTransfer, FunctionCall, Label ,
|
||||
RegisterTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot, Label ,
|
||||
FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_call_local_int
|
||||
Parfait.object_space.get_main.add_local(:testi , :Integer)
|
||||
clean_compile :Integer, :putint, {}, s(:statements, s(:return, s(:int, 1)))
|
||||
@input = s(:statements, s(:assignment, s(:name, :testi), s(:int, 20)), s(:call, s(:name, :putint), s(:arguments), s(:receiver, s(:name, :testi))))
|
||||
|
||||
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg, SlotToReg ,
|
||||
RegToSlot, LoadConstant, RegToSlot, LoadConstant, SlotToReg, RegToSlot, LoadConstant ,
|
||||
RegToSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer, SlotToReg, SlotToReg ,
|
||||
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_call_local_class
|
||||
Parfait.object_space.get_main.add_local(:test_l , :List)
|
||||
clean_compile :List, :add, {}, s(:statements, s(:return, s(:int, 1)))
|
||||
|
||||
@input =s(:statements, s(:call, s(:name, :add), s(:arguments), s(:receiver, s(:name, :test_l))))
|
||||
@expect = [Label, SlotToReg, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
|
||||
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RegisterTransfer, FunctionCall ,
|
||||
Label, RegisterTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot ,
|
||||
Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_call_puts
|
||||
clean_compile :Space, :putstr, {str: :Word}, s(:statements, s(:return, s(:name, :str)))
|
||||
@input =s(:call, s(:name, :putstr), s(:arguments, s(:string, "Hello") ) )
|
||||
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
|
||||
LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot ,
|
||||
LoadConstant, RegToSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
|
||||
SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot, Label ,
|
||||
FunctionReturn]
|
||||
was = check_return
|
||||
set = was.next(8)
|
||||
assert_equal RegToSlot , set.class
|
||||
assert_equal 1, set.index , "Set to message must be offset, not #{set.index}"
|
||||
end
|
||||
end
|
||||
end
|
31
test/vm/method_compiler/test_class.rb
Normal file
31
test/vm/method_compiler/test_class.rb
Normal file
@ -0,0 +1,31 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestClassStatements < MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def class_def
|
||||
clean_compile :Bar, :buh, {}, s(:statements, s(:return, s(:int, 1)))
|
||||
end
|
||||
|
||||
def test_class_call
|
||||
#FIXME class call
|
||||
# class_def
|
||||
# @input = s(:statements, s(:return, s(:call, s(:name, :buh), s(:arguments), s(:receiver, s(:class_name, :Bar)))))
|
||||
#
|
||||
# @expect = [Label, SlotToReg, LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant ,
|
||||
# RegToSlot, LoadConstant, RegToSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
|
||||
# SlotToReg, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
# check
|
||||
end
|
||||
|
||||
def test_class_field
|
||||
# clean_compile :Space, s(:class_field, :Integer, :boo2)
|
||||
#FIXME class_field handling unclear at the moment
|
||||
# @input =s(:statements, s(:return, s(:field_access, s(:receiver, s(:name, :self)),
|
||||
# s(:field,s(:name, :boo2)))))
|
||||
# @expect = [Label, SlotToReg,SlotToReg,RegToSlot,Label,FunctionReturn]
|
||||
# check
|
||||
end
|
||||
end
|
||||
end
|
34
test/vm/method_compiler/test_field_access.rb
Normal file
34
test/vm/method_compiler/test_field_access.rb
Normal file
@ -0,0 +1,34 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Register
|
||||
class TestFields < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
include AST::Sexp
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
end
|
||||
|
||||
def test_field_not_defined
|
||||
@root = :field_access
|
||||
@input = s(:field_access, s(:receiver, s(:name, :self)), s(:field, s(:name, :a)))
|
||||
assert_raises(RuntimeError) { check }
|
||||
end
|
||||
|
||||
def test_field_not_space
|
||||
@root = :field_access
|
||||
@input = s(:field_access, s(:receiver, s(:name, :self)), s(:field, s(:name, :space)))
|
||||
|
||||
assert_raises(RuntimeError) { check }
|
||||
end
|
||||
|
||||
def test_field
|
||||
add_space_field(:bro,:Object)
|
||||
@root = :field_access
|
||||
@input = s(:field_access,s(:receiver, s(:name, :self)),s(:field,s(:name, :bro)))
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
42
test/vm/method_compiler/test_fields.rb
Normal file
42
test/vm/method_compiler/test_fields.rb
Normal file
@ -0,0 +1,42 @@
|
||||
require_relative 'helper'
|
||||
|
||||
|
||||
module Register
|
||||
class TestFieldStatement < MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_field_named_list
|
||||
Parfait.object_space.get_main.add_local( :m , :Message)
|
||||
@input = s(:statements, s(:return, s(:field_access,
|
||||
s(:receiver, s(:name, :m)), s(:field, s(:name, :name)))))
|
||||
@expect = [Label, SlotToReg, SlotToReg, SlotToReg, RegToSlot, LoadConstant ,
|
||||
SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_field_arg
|
||||
Parfait.object_space.get_main.add_local( :m , :Message)
|
||||
clean_compile :Space, :get_name, { :main => :Message},
|
||||
s(:statements, s(:return, s(:field_access,
|
||||
s(:receiver, s(:name, :main)), s(:field, s(:name, :name)))))
|
||||
@input =s(:statements, s(:return, s(:call, s(:name, :get_name), s(:arguments, s(:name, :m)))))
|
||||
|
||||
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
|
||||
LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg, SlotToReg ,
|
||||
RegToSlot, LoadConstant, RegToSlot, RegisterTransfer, FunctionCall, Label ,
|
||||
RegisterTransfer, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
||||
RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_message_field
|
||||
Parfait.object_space.get_main.add_local(:name , :Word)
|
||||
@input = s(:statements, s(:assignment, s(:name, :name), s(:field_access, s(:receiver, s(:name, :message)), s(:field, s(:name, :name)))), s(:return, s(:name, :name)))
|
||||
|
||||
@expect = [Label, RegisterTransfer, SlotToReg, SlotToReg, RegToSlot, SlotToReg ,
|
||||
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label ,
|
||||
FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
end
|
||||
end
|
36
test/vm/method_compiler/test_if_statement.rb
Normal file
36
test/vm/method_compiler/test_if_statement.rb
Normal file
@ -0,0 +1,36 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestIfStatement < MiniTest::Test
|
||||
include Statements
|
||||
|
||||
def test_if_basicr
|
||||
@input = s(:statements, s(:if_statement, :plus, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, s(:return, s(:int, 4)))))
|
||||
|
||||
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsPlus, LoadConstant ,
|
||||
RegToSlot, Branch, Label, LoadConstant, RegToSlot, Label ,
|
||||
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
|
||||
def test_if_small_minus
|
||||
@input = s(:statements, s(:if_statement, :minus, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, nil)))
|
||||
|
||||
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsMinus, Branch ,
|
||||
Label, LoadConstant, RegToSlot, Label, LoadConstant, SlotToReg ,
|
||||
RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
|
||||
def test_if_small_zero
|
||||
@input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, nil)))
|
||||
|
||||
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsZero, Branch ,
|
||||
Label, LoadConstant, RegToSlot, Label, LoadConstant, SlotToReg ,
|
||||
RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
end
|
||||
end
|
34
test/vm/method_compiler/test_name_expression.rb
Normal file
34
test/vm/method_compiler/test_name_expression.rb
Normal file
@ -0,0 +1,34 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Register
|
||||
class TestFields < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
include AST::Sexp
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
end
|
||||
|
||||
def test_local
|
||||
Parfait.object_space.get_main.add_local(:bar , :Integer)
|
||||
@input = s(:name, :bar)
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_space
|
||||
@root = :name
|
||||
@input = s(:name, :space)
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_args
|
||||
Parfait.object_space.get_main.add_argument(:bar , :Integer)
|
||||
@input = s(:name, :bar)
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
46
test/vm/method_compiler/test_operator_expression.rb
Normal file
46
test/vm/method_compiler/test_operator_expression.rb
Normal file
@ -0,0 +1,46 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Register
|
||||
class TestOps < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
include AST::Sexp
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@root = :operator_value
|
||||
@output = Register::RegisterValue
|
||||
end
|
||||
|
||||
def operators
|
||||
[:+ , :- , :* , :/ , :== ]
|
||||
end
|
||||
def test_ints
|
||||
operators.each do |op|
|
||||
@input = s(:operator_value, op , s(:int, 2), s(:int, 3))
|
||||
check
|
||||
end
|
||||
end
|
||||
def test_local_int
|
||||
Parfait.object_space.get_main.add_local(:bar , :Integer)
|
||||
@input = s(:operator_value, :+, s(:name, :bar), s(:int, 3))
|
||||
check
|
||||
end
|
||||
def test_int_local
|
||||
Parfait.object_space.get_main.add_local(:bar , :Integer)
|
||||
@input = s(:operator_value, :+, s(:int, 3), s(:name, :bar))
|
||||
check
|
||||
end
|
||||
|
||||
def test_field_int
|
||||
add_space_field(:bro,:Integer)
|
||||
@input = s(:operator_value, :+, s(:field_access,s(:receiver, s(:name, :self)), s(:field, s(:name, :bro))), s(:int, 3))
|
||||
check
|
||||
end
|
||||
|
||||
def test_int_field
|
||||
add_space_field(:bro,:Integer)
|
||||
@input = s(:operator_value, :+, s(:int, 3), s(:field_access, s(:receiver, s(:name, :self)), s(:field,s(:name, :bro))))
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
47
test/vm/method_compiler/test_return_statement.rb
Normal file
47
test/vm/method_compiler/test_return_statement.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, RegToSlot, LoadConstant, SlotToReg, RegToSlot ,
|
||||
Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_return_local
|
||||
Parfait.object_space.get_main.add_local(:runner , :Integer)
|
||||
@input = s(:statements, s(:return, s(:name, :runner)))
|
||||
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
||||
RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_return_local_assign
|
||||
Parfait.object_space.get_main.add_local(:runner , :Integer)
|
||||
@input = s(:statements, s(:assignment, s(:name, :runner), s(:int, 5)), s(:return, s(:name, :runner)))
|
||||
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg ,
|
||||
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_return_call
|
||||
@input =s(:statements, s(:return, s(:call, s(:name, :main), s(:arguments))))
|
||||
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
|
||||
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RegisterTransfer ,
|
||||
FunctionCall, Label, RegisterTransfer, SlotToReg, SlotToReg, RegToSlot ,
|
||||
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def pest_return_space_length # need to add runtime first
|
||||
Parfait.object_space.get_main.add_local(:l , :Type)
|
||||
@input = s(:statements, s(:assignment, 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, SlotToReg,SlotToReg ,RegToSlot,Label,FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
end
|
||||
end
|
43
test/vm/method_compiler/test_while_statement.rb
Normal file
43
test/vm/method_compiler/test_while_statement.rb
Normal file
@ -0,0 +1,43 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Register
|
||||
class TestWhile < MiniTest::Test
|
||||
include Statements
|
||||
|
||||
|
||||
def test_while_mini
|
||||
@input = s(:statements, s(:while_statement, :plus, s(:conditional, s(:int, 1)), s(:statements, s(:return, s(:int, 3)))))
|
||||
|
||||
@expect = [Label, Branch, Label, LoadConstant, RegToSlot, Label ,
|
||||
LoadConstant, IsPlus, LoadConstant, SlotToReg, RegToSlot, Label ,
|
||||
FunctionReturn]
|
||||
assert_nil msg = check_nil , msg
|
||||
end
|
||||
|
||||
def test_while_assign
|
||||
Parfait.object_space.get_main.add_local(:n , :Integer)
|
||||
|
||||
@input = s(:statements, s(:assignment, s(:name, :n), s(:int, 5)), s(:while_statement, :plus, s(:conditional, s(:name, :n)), s(:statements, s(:assignment, s(:name, :n), s(:operator_value, :-, s(:name, :n), s(:int, 1))))), s(:return, s(:name, :n)))
|
||||
|
||||
@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
|
||||
end
|
||||
|
||||
|
||||
def test_while_return
|
||||
Parfait.object_space.get_main.add_local(:n , :Integer)
|
||||
|
||||
@input = s(:statements, s(:assignment, s(:name, :n), s(:int, 10)), s(:while_statement, :plus, s(:conditional, s(:operator_value, :-, s(:name, :n), s(:int, 5))), s(:statements, s(:assignment, s(:name, :n), s(:operator_value, :+, s(:name, :n), s(:int, 1))), s(:return, s(:name, :n)))))
|
||||
|
||||
@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
|
||||
end
|
||||
end
|
||||
end
|
10
test/vm/parfait/test_all.rb
Normal file
10
test/vm/parfait/test_all.rb
Normal file
@ -0,0 +1,10 @@
|
||||
require_relative "test_attributes"
|
||||
require_relative "test_class"
|
||||
require_relative "test_dictionary"
|
||||
require_relative "test_named_list"
|
||||
require_relative "test_list"
|
||||
require_relative "test_message"
|
||||
require_relative "test_typed_method"
|
||||
require_relative "test_object"
|
||||
require_relative "test_space"
|
||||
require_relative "test_word"
|
40
test/vm/parfait/test_attributes.rb
Normal file
40
test/vm/parfait/test_attributes.rb
Normal file
@ -0,0 +1,40 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestAttributes < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
@mess = @space.first_message
|
||||
@type = @mess.get_type
|
||||
end
|
||||
|
||||
def test_message_get_type
|
||||
assert_equal Parfait::Type , @type.class
|
||||
end
|
||||
|
||||
def test_message_name_nil
|
||||
last = @type.names.last
|
||||
assert_equal :arguments , last , @type.names.inspect
|
||||
assert_nil @mess.name
|
||||
end
|
||||
def test_message_next_set
|
||||
@mess.next_message = :next_message
|
||||
assert_equal :next_message , @mess.next_message
|
||||
end
|
||||
def test_message_type_set
|
||||
@mess.set_type @type
|
||||
assert_equal @type , @mess.get_type
|
||||
end
|
||||
def test_attribute_index
|
||||
@mess.next_message = :message
|
||||
assert_equal Parfait::Type , @mess.get_type.class
|
||||
end
|
||||
|
||||
def test_type_type
|
||||
assert_equal Parfait::Type , @type.get_type.get_type.class
|
||||
end
|
||||
def test_type_type_type
|
||||
assert_equal Parfait::Type , @type.get_type.get_type.get_type.class
|
||||
end
|
||||
end
|
37
test/vm/parfait/test_class.rb
Normal file
37
test/vm/parfait/test_class.rb
Normal file
@ -0,0 +1,37 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestClass < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
@try = @space.create_class :Try , :Object
|
||||
end
|
||||
|
||||
def test_type_forclass
|
||||
assert_equal "Class(Space)" , @space.get_type.object_class.inspect
|
||||
assert_equal :Space , @space.get_type.object_class.name
|
||||
end
|
||||
def test_new_superclass_name
|
||||
assert_equal :Object , @try.super_class_name
|
||||
end
|
||||
def test_new_superclass
|
||||
assert_equal "Class(Object)" , @try.super_class.inspect
|
||||
end
|
||||
def test_new_methods
|
||||
assert_equal @try.method_names.class, @try.instance_methods.class
|
||||
assert_equal @try.method_names.get_length , @try.instance_methods.get_length
|
||||
end
|
||||
def test_remove_nothere
|
||||
assert !@try.remove_instance_method(:foo)
|
||||
end
|
||||
def test_resolve
|
||||
assert_nil @try.resolve_method :foo
|
||||
end
|
||||
def test_remove_method
|
||||
assert_equal false , @try.remove_instance_method( :foo)
|
||||
end
|
||||
def test_add_method
|
||||
assert_raises{ @try.add_instance_method(nil)}
|
||||
end
|
||||
end
|
76
test/vm/parfait/test_dictionary.rb
Normal file
76
test/vm/parfait/test_dictionary.rb
Normal file
@ -0,0 +1,76 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestDictionary < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@lookup = ::Parfait::Dictionary.new
|
||||
end
|
||||
def test_dict_create
|
||||
assert_equal 0 , @lookup.length
|
||||
assert @lookup.empty?
|
||||
end
|
||||
def test_empty_dict_doesnt_return
|
||||
assert_nil @lookup.get(3)
|
||||
assert_nil @lookup.get(:any)
|
||||
end
|
||||
def test_one_set1
|
||||
assert_equal 1 , @lookup.set(1,1)
|
||||
assert_equal 1 , @lookup.length
|
||||
end
|
||||
def test_one_double
|
||||
assert_equal 1 , @lookup.set(1,1)
|
||||
assert_equal 3 , @lookup.set(1,3)
|
||||
assert_equal 1 , @lookup.length
|
||||
end
|
||||
def test_one_double2
|
||||
assert_equal 1 , @lookup.set(:one,1)
|
||||
assert_equal 3 , @lookup.set(:one,3)
|
||||
assert_equal 1 , @lookup.length
|
||||
end
|
||||
def test_one_set2
|
||||
assert_equal :some , @lookup.set(1,:some)
|
||||
end
|
||||
def test_two_sets
|
||||
assert_equal 1 , @lookup.set(1,1)
|
||||
assert_equal :some , @lookup.set(1,:some)
|
||||
end
|
||||
def test_one_get1
|
||||
test_one_set1
|
||||
assert_equal 1 , @lookup.get(1)
|
||||
end
|
||||
def test_one_get2
|
||||
test_one_set2
|
||||
assert_equal :some , @lookup.get(1)
|
||||
end
|
||||
def test_inspect1
|
||||
@lookup[:key] = :value
|
||||
assert_equal "Dictionary{key => value ,}" , @lookup.inspect
|
||||
end
|
||||
def test_inspect2
|
||||
@lookup[:key1] = :value1
|
||||
@lookup[:key2] = :value2
|
||||
assert_equal "Dictionary{key1 => value1 ,key2 => value2 ,}" , @lookup.inspect
|
||||
end
|
||||
def test_many_get
|
||||
shouldda = { :one => 1 , :two => 2 , :three => 3}
|
||||
shouldda.each do |k,v|
|
||||
@lookup.set(k,v)
|
||||
end
|
||||
@lookup.each do |k,v|
|
||||
assert_equal v , shouldda[k]
|
||||
end
|
||||
end
|
||||
def test_values
|
||||
@lookup[2] = 2
|
||||
assert @lookup.values.get_length == 1
|
||||
end
|
||||
def test_keys
|
||||
@lookup[2] = 2
|
||||
assert @lookup.keys.get_length == 1
|
||||
end
|
||||
def test_override_exising
|
||||
@lookup[2] = 2
|
||||
@lookup[2] = :two
|
||||
assert @lookup[2] == :two
|
||||
end
|
||||
end
|
198
test/vm/parfait/test_list.rb
Normal file
198
test/vm/parfait/test_list.rb
Normal file
@ -0,0 +1,198 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestList < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@list = ::Parfait::List.new
|
||||
end
|
||||
def test_isa
|
||||
assert @list.is_a? Parfait::List
|
||||
end
|
||||
def test_old_type
|
||||
assert_equal Parfait::Type , Parfait.object_space.classes.keys.get_type.class
|
||||
end
|
||||
def test_old_type_push
|
||||
list = Parfait.object_space.classes.keys
|
||||
assert_equal Parfait::Type , list.get_type.class
|
||||
end
|
||||
def test_new_type
|
||||
assert_equal Parfait::Type , @list.get_type.class
|
||||
end
|
||||
def test_new_type_push
|
||||
@list.push(1)
|
||||
assert_equal Parfait::Type , @list.get_type.class
|
||||
end
|
||||
def notest_type_is_first
|
||||
type = @list.get_type
|
||||
assert_equal 1 , type.variable_index(:type)
|
||||
end
|
||||
def notest_type_is_first_old
|
||||
type = Parfait.object_space.classes.keys.get_type
|
||||
assert_equal 1 , type.variable_index(:type)
|
||||
end
|
||||
|
||||
def test_length0
|
||||
assert_equal 0 , @list.get_length
|
||||
assert_equal 0, @list.indexed_length
|
||||
end
|
||||
def test_offset
|
||||
assert_equal 2 , @list.get_offset
|
||||
end
|
||||
def test_indexed_index
|
||||
# 1 type , 2 indexed_length
|
||||
assert_equal 2 , @list.get_type.variable_index(:indexed_length)
|
||||
end
|
||||
def test_length1
|
||||
@list.push :one
|
||||
assert_equal 1 , @list.get_length
|
||||
assert_equal 1 , @list.indexed_length
|
||||
assert_equal 1 , @list.get_internal_word(Parfait::TYPE_INDEX + 1)
|
||||
end
|
||||
def test_list_inspect
|
||||
@list.set(1,1)
|
||||
assert_equal "1" , @list.inspect
|
||||
end
|
||||
def test_list_equal
|
||||
@list.set(1,1)
|
||||
list = ::Parfait::List.new
|
||||
list.set(1,1)
|
||||
assert @list.equal? list
|
||||
end
|
||||
def test_list_create
|
||||
assert @list.empty?
|
||||
end
|
||||
def test_list_len
|
||||
assert_equal 0 , @list.get_length
|
||||
end
|
||||
def test_empty_list_doesnt_return
|
||||
assert_nil @list.get(3)
|
||||
end
|
||||
def test_one_set1
|
||||
assert_equal 2 , @list.set(1,2)
|
||||
assert_equal 1 , @list.get_internal_word(2)
|
||||
end
|
||||
def test_set1_len
|
||||
@list.set(1,1)
|
||||
assert_equal 1 , @list.get_length
|
||||
end
|
||||
def test_one_set2
|
||||
assert_equal :some , @list.set(2,:some)
|
||||
end
|
||||
def test_set2_len
|
||||
@list.set(2,:some)
|
||||
assert_equal 2 , @list.get_length
|
||||
end
|
||||
def test_two_sets
|
||||
assert_equal 1 , @list.set(1,1)
|
||||
assert_equal :some , @list.set(1,:some)
|
||||
end
|
||||
def test_one_get1
|
||||
test_one_set1
|
||||
assert_equal 2 , @list.get(1)
|
||||
end
|
||||
def test_one_get2
|
||||
test_one_set2
|
||||
assert_equal :some , @list.get(2)
|
||||
end
|
||||
def set_shouldda
|
||||
shouldda = { 1 => :one , 2 => :two , 3 => :three}
|
||||
shouldda.each do |k,v|
|
||||
@list.set(k,v)
|
||||
end
|
||||
shouldda
|
||||
end
|
||||
def test_many_get
|
||||
shouldda = set_shouldda
|
||||
shouldda.each do |k,v|
|
||||
assert_equal v , @list.get(k)
|
||||
end
|
||||
end
|
||||
def test_each
|
||||
shouldda_values = set_shouldda.values
|
||||
@list.each do |val|
|
||||
shouldda_values.delete val
|
||||
end
|
||||
assert_equal 0 , shouldda_values.length
|
||||
end
|
||||
def test_each_index
|
||||
set_shouldda
|
||||
@list.each_with_index do |val , index|
|
||||
assert_equal @list[index] , val
|
||||
end
|
||||
end
|
||||
def test_each_pair_length
|
||||
shouldda_values = set_shouldda.values
|
||||
@list.each_pair do |key,val|
|
||||
shouldda_values.delete key
|
||||
shouldda_values.delete val
|
||||
end
|
||||
assert_equal 0 , shouldda_values.length
|
||||
end
|
||||
def test_each_pair_count
|
||||
set_shouldda.values
|
||||
counter = 0
|
||||
@list.each_pair do |key,val|
|
||||
counter += 1
|
||||
end
|
||||
assert_equal 2 , counter
|
||||
end
|
||||
def test_find
|
||||
@list.set(1,1)
|
||||
@list.set(2,2)
|
||||
assert_equal 2, @list.find{|i| i == 2}
|
||||
end
|
||||
def test_not_find
|
||||
@list.set(1,1)
|
||||
assert_nil @list.find{|i| i == 3}
|
||||
end
|
||||
def test_delete_at
|
||||
test_many_get
|
||||
assert @list.delete_at 2
|
||||
assert_equal 2 , @list.get_length
|
||||
assert_equal 2 , @list.index_of( :three )
|
||||
end
|
||||
|
||||
def test_delete
|
||||
test_many_get
|
||||
assert @list.delete :two
|
||||
assert_equal 2 , @list.get_length
|
||||
assert_equal 2 , @list.index_of( :three )
|
||||
end
|
||||
def test_index_of
|
||||
test_many_get
|
||||
assert_equal 2 , @list.index_of( :two )
|
||||
assert_equal 3 , @list.index_of( :three )
|
||||
assert_nil @list.index_of( :four )
|
||||
end
|
||||
def test_inspect
|
||||
test_many_get
|
||||
assert @list.inspect.include?("one") , @list.inspect
|
||||
assert @list.inspect.include?("three") , @list.inspect
|
||||
end
|
||||
def test_inlcude
|
||||
test_many_get
|
||||
assert_equal true , @list.include?( :two )
|
||||
assert_equal false , @list.include?( :four )
|
||||
end
|
||||
def test_empty_empty
|
||||
assert_equal true , @list.empty?
|
||||
end
|
||||
def test_empty_notempty
|
||||
assert_equal 1 , @list.set(1,1)
|
||||
assert_equal false , @list.empty?
|
||||
end
|
||||
def test_first
|
||||
test_many_get
|
||||
assert_equal :one , @list.first
|
||||
end
|
||||
def test_first_empty
|
||||
assert_nil @list.first
|
||||
end
|
||||
def test_last
|
||||
test_many_get
|
||||
assert_equal :three , @list.last
|
||||
end
|
||||
def test_last_empty
|
||||
assert_nil @list.last
|
||||
end
|
||||
end
|
27
test/vm/parfait/test_message.rb
Normal file
27
test/vm/parfait/test_message.rb
Normal file
@ -0,0 +1,27 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestMessage < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
@mess = @space.first_message
|
||||
end
|
||||
|
||||
def test_length
|
||||
assert_equal 9 , @mess.get_type.instance_length , @mess.get_type.inspect
|
||||
end
|
||||
|
||||
def test_attribute_set
|
||||
@mess.set_receiver( 55)
|
||||
assert_equal 55 , @mess.receiver
|
||||
end
|
||||
|
||||
def test_indexed
|
||||
assert_equal 9 , @mess.get_type.variable_index(:arguments)
|
||||
end
|
||||
|
||||
def test_next
|
||||
assert @mess.next_message
|
||||
end
|
||||
end
|
27
test/vm/parfait/test_named_list.rb
Normal file
27
test/vm/parfait/test_named_list.rb
Normal file
@ -0,0 +1,27 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestNamedLists < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
@named_list = @space.first_message.locals
|
||||
@type = @named_list.get_type
|
||||
end
|
||||
|
||||
def test_named_list_get_type
|
||||
assert_equal Parfait::Type , @type.class
|
||||
assert @type.names
|
||||
assert @named_list.get_instance_variables
|
||||
end
|
||||
|
||||
def test_new
|
||||
list = Parfait::NamedList.new
|
||||
assert list.get_type
|
||||
end
|
||||
|
||||
def test_var_names
|
||||
list = Parfait::NamedList.new
|
||||
assert list.get_instance_variables
|
||||
end
|
||||
end
|
17
test/vm/parfait/test_object.rb
Normal file
17
test/vm/parfait/test_object.rb
Normal file
@ -0,0 +1,17 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestObject < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@object = ::Parfait::Object.new
|
||||
end
|
||||
|
||||
def test_empty_object_doesnt_return
|
||||
assert_nil @object.get_internal_word(3)
|
||||
end
|
||||
|
||||
def test_one_set1
|
||||
assert_equal @object.get_type , @object.set_internal_word(1, @object.get_type)
|
||||
end
|
||||
|
||||
end
|
148
test/vm/parfait/test_space.rb
Normal file
148
test/vm/parfait/test_space.rb
Normal file
@ -0,0 +1,148 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestSpace < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@machine = Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
end
|
||||
|
||||
def classes
|
||||
[:Kernel,:Word,:List,:Message,:NamedList,:Type,:Object,:Class,:Dictionary,:TypedMethod , :Integer]
|
||||
end
|
||||
def test_booted
|
||||
assert_equal true , @machine.booted
|
||||
end
|
||||
|
||||
def test_global_space
|
||||
assert_equal Parfait::Space , Parfait.object_space.class
|
||||
end
|
||||
|
||||
def test_integer
|
||||
int = Parfait.object_space.get_class_by_name :Integer
|
||||
assert_equal 3, int.instance_type.method_names.get_length
|
||||
end
|
||||
|
||||
def test_classes_class
|
||||
classes.each do |name|
|
||||
assert_equal :Class , @space.classes[name].get_class.name
|
||||
assert_equal Parfait::Class , @space.classes[name].class
|
||||
end
|
||||
end
|
||||
|
||||
def test_types
|
||||
assert @space.instance_variable_get("@types").is_a? Parfait::Dictionary
|
||||
end
|
||||
|
||||
def test_types_each
|
||||
@space.each_type do |type|
|
||||
assert type.is_a?(Parfait::Type)
|
||||
end
|
||||
end
|
||||
|
||||
def test_types_hashes
|
||||
types = @space.instance_variable_get("@types")
|
||||
types.each do |has , type|
|
||||
assert has.is_a?(Fixnum) , has.inspect
|
||||
end
|
||||
end
|
||||
|
||||
def test_classes_types_in_space_types
|
||||
@space.classes do |name , clazz|
|
||||
assert_equal clazz.instance_type , @space.get_type_for(clazz.instance_type.hash) , clazz.name
|
||||
end
|
||||
end
|
||||
|
||||
def test_word_class
|
||||
word = @space.classes[:Word]
|
||||
assert word.instance_type
|
||||
t_word = @space.get_type_for(word.instance_type.hash)
|
||||
assert_equal word.instance_type.hash , t_word.hash
|
||||
assert_equal word.instance_type.object_id , t_word.object_id
|
||||
end
|
||||
|
||||
def test_classes_type
|
||||
classes.each do |name|
|
||||
assert_equal Parfait::Type , @space.classes[name].get_type.class
|
||||
end
|
||||
end
|
||||
|
||||
def test_classes_name
|
||||
classes.each do |name|
|
||||
assert_equal name , @space.classes[name].name
|
||||
end
|
||||
end
|
||||
|
||||
def test_method_name
|
||||
classes.each do |name|
|
||||
cl = @space.classes[name]
|
||||
cl.method_names.each do |mname|
|
||||
method = cl.get_instance_method(mname)
|
||||
assert_equal mname , method.name
|
||||
assert_equal name , method.for_class.name
|
||||
end
|
||||
end
|
||||
end
|
||||
def test_messages
|
||||
mess = @space.first_message
|
||||
all = []
|
||||
while mess
|
||||
all << mess
|
||||
assert mess.locals
|
||||
mess = mess.next_message
|
||||
end
|
||||
assert_equal all.length , all.uniq.length
|
||||
# there is a 5.times in space, but one Message gets created before
|
||||
assert_equal 50 + 1 , all.length
|
||||
end
|
||||
def test_message_vars
|
||||
mess = @space.first_message
|
||||
all = mess.get_instance_variables
|
||||
assert all
|
||||
assert all.include?(:next_message)
|
||||
end
|
||||
|
||||
def test_create_class
|
||||
assert @space.create_class( :NewClass )
|
||||
end
|
||||
|
||||
def test_created_class_is_stored
|
||||
@space.create_class( :NewerClass )
|
||||
assert @space.get_class_by_name(:NewerClass)
|
||||
end
|
||||
|
||||
def test_class_types_are_stored
|
||||
@space.classes.each do |name,clazz|
|
||||
assert @space.get_type_for(clazz.instance_type.hash)
|
||||
end
|
||||
end
|
||||
|
||||
def test_class_types_are_identical
|
||||
@space.classes.each do |name , clazz|
|
||||
cl_type = @space.get_type_for(clazz.instance_type.hash)
|
||||
assert_equal cl_type.object_id , clazz.instance_type.object_id
|
||||
end
|
||||
end
|
||||
|
||||
def test_remove_methods
|
||||
@space.each_type do | type |
|
||||
type.method_names.each do |method|
|
||||
type.remove_method(method)
|
||||
end
|
||||
end
|
||||
assert_equal 0 , @space.collect_methods.length
|
||||
end
|
||||
def test_no_methods_in_types
|
||||
test_remove_methods
|
||||
@space.each_type do |type|
|
||||
assert_equal 0 , type.methods.get_length , "name #{type.name}"
|
||||
end
|
||||
end
|
||||
def ttest_no_methods_in_classes
|
||||
test_remove_methods
|
||||
@space.classes.each do |name , cl|
|
||||
assert_equal 0 , cl.instance_type.methods.get_length , "name #{cl.name}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
95
test/vm/parfait/test_typed_method.rb
Normal file
95
test/vm/parfait/test_typed_method.rb
Normal file
@ -0,0 +1,95 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestMethod < MiniTest::Test
|
||||
|
||||
def setup
|
||||
obj = Parfait.object_space.get_class_by_name(:Object).instance_type
|
||||
args = Parfait::Type.for_hash( obj.object_class , { bar: :Integer , foo: :Type})
|
||||
@method = ::Parfait::TypedMethod.new obj , :meth , args
|
||||
@method.add_local :local_bar , :Integer
|
||||
@method.add_local :local_foo , :Type
|
||||
end
|
||||
|
||||
def test_method_name
|
||||
assert_equal :meth , @method.name
|
||||
end
|
||||
|
||||
def test_class_for
|
||||
assert_equal :Object , @method.for_type.object_class.name
|
||||
end
|
||||
|
||||
def test_arg1
|
||||
assert_equal 2 , @method.arguments_length , @method.arguments.inspect
|
||||
assert_equal Symbol , @method.arguments.names.first.class
|
||||
assert_equal :bar , @method.argument_name(1)
|
||||
end
|
||||
|
||||
def test_has_arg
|
||||
assert_equal 1 , @method.has_arg(:bar)
|
||||
assert_equal 2 , @method.has_arg(:foo)
|
||||
end
|
||||
|
||||
def test_add_arg
|
||||
@method.add_argument(:foo2 , :Object)
|
||||
assert_equal 3 , @method.arguments_length
|
||||
assert_equal :foo2 , @method.argument_name(3)
|
||||
assert_equal :Object , @method.argument_type(3)
|
||||
end
|
||||
|
||||
def test_get_arg_name1
|
||||
index = @method.has_arg(:bar)
|
||||
assert_equal 1 , index
|
||||
assert_equal :bar , @method.argument_name(index)
|
||||
end
|
||||
def test_get_arg_type1
|
||||
index = @method.has_arg(:bar)
|
||||
assert_equal :Integer , @method.argument_type(index)
|
||||
end
|
||||
def test_get_arg_name2
|
||||
index = @method.has_arg(:foo)
|
||||
assert_equal 2 , index
|
||||
assert_equal :foo , @method.argument_name(index)
|
||||
end
|
||||
def test_get_arg_type2
|
||||
index = @method.has_arg(:foo)
|
||||
assert_equal :Type , @method.argument_type(index)
|
||||
end
|
||||
|
||||
def test_local1
|
||||
assert_equal 2 , @method.locals_length , @method.locals.inspect
|
||||
assert_equal Symbol , @method.locals.names.first.class
|
||||
assert_equal :local_bar , @method.locals_name(1)
|
||||
end
|
||||
|
||||
def test_has_local
|
||||
assert_equal 1 , @method.has_local(:local_bar)
|
||||
assert_equal 2 , @method.has_local(:local_foo)
|
||||
end
|
||||
|
||||
def test_add_local
|
||||
@method.add_local(:foo2 , :Object)
|
||||
assert_equal 3 , @method.locals_length
|
||||
assert_equal :foo2 , @method.locals_name(3)
|
||||
assert_equal :Object , @method.locals_type(3)
|
||||
end
|
||||
|
||||
def test_get_locals_name1
|
||||
index = @method.has_local(:local_bar)
|
||||
assert_equal 1 , index
|
||||
assert_equal :local_bar , @method.locals_name(index)
|
||||
end
|
||||
def test_get_locals_type1
|
||||
index = @method.has_local(:local_bar)
|
||||
assert_equal :Integer , @method.locals_type(index)
|
||||
end
|
||||
def test_get_locals_name2
|
||||
index = @method.has_local(:local_foo)
|
||||
assert_equal 2 , index
|
||||
assert_equal :local_foo , @method.locals_name(index)
|
||||
end
|
||||
def test_get_locals_type2
|
||||
index = @method.has_local(:local_bar)
|
||||
assert_equal :Integer , @method.locals_type(index)
|
||||
end
|
||||
|
||||
end
|
113
test/vm/parfait/test_word.rb
Normal file
113
test/vm/parfait/test_word.rb
Normal file
@ -0,0 +1,113 @@
|
||||
require_relative "../helper"
|
||||
module Parfait
|
||||
class TestEmptyWord < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@word = Parfait::Word.new(0)
|
||||
end
|
||||
def test_word_create
|
||||
assert @word.empty?
|
||||
end
|
||||
def test_empty_is_zero
|
||||
assert_equal 0 , @word.length
|
||||
end
|
||||
def test_empty_is_zero_internal
|
||||
assert_equal 0 , @word.char_length
|
||||
end
|
||||
def test_index_check_get
|
||||
assert_raises RuntimeError do
|
||||
@word.get_char(0)
|
||||
end
|
||||
end
|
||||
def test_index_check_set
|
||||
assert_raises RuntimeError do
|
||||
@word.set_char(1 , 32)
|
||||
end
|
||||
end
|
||||
end
|
||||
class TestWord < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@word = Parfait::Word.new(5)
|
||||
end
|
||||
def test_len
|
||||
assert_equal 5 , @word.length
|
||||
end
|
||||
def test_len_internal
|
||||
assert_equal 5 , @word.char_length
|
||||
end
|
||||
def test_first_char
|
||||
assert_equal 32 , @word.get_char(1)
|
||||
end
|
||||
def test_equals_copy
|
||||
assert_equal @word.copy , @word
|
||||
end
|
||||
def test_equals_copy2
|
||||
@word.set_char(1 , 2)
|
||||
@word.set_char(5 , 6)
|
||||
assert_equal @word.copy , @word
|
||||
end
|
||||
def test_equals_same
|
||||
assert_equal ::Parfait::Word.new(5) , @word
|
||||
end
|
||||
def test_index_check_get
|
||||
assert_raises RuntimeError do
|
||||
@word.get_char(-6)
|
||||
end
|
||||
end
|
||||
def test_index_check_set
|
||||
assert_raises RuntimeError do
|
||||
@word.set_char(6 , 32)
|
||||
end
|
||||
end
|
||||
def test_index_check_get
|
||||
assert_raises RuntimeError do
|
||||
@word.get_char(6)
|
||||
end
|
||||
end
|
||||
def test_index_check_set
|
||||
assert_raises RuntimeError do
|
||||
@word.set_char(-6 , 32)
|
||||
end
|
||||
end
|
||||
def test_one_char
|
||||
assert_equal 32 , @word.set_char(1 , 32)
|
||||
end
|
||||
def test_one_char_doesnt_cause_problems
|
||||
@word.set_char(1 , 32)
|
||||
assert_equal 32 , @word.get_char(1)
|
||||
end
|
||||
def test_one_set1
|
||||
assert_equal 44 , @word.set_char(1, 44 )
|
||||
end
|
||||
def test_two_sets
|
||||
assert_equal 1 , @word.set_char(1,1)
|
||||
assert_equal 44 , @word.set_char(1,44)
|
||||
end
|
||||
def test_one_get1
|
||||
test_one_set1
|
||||
assert_equal 44 , @word.get_char(1)
|
||||
end
|
||||
def test_many_get
|
||||
shouldda = { 1 => 11 , 2 => 22 , 3 => 33}
|
||||
shouldda.each do |k,v|
|
||||
@word.set_char(k,v)
|
||||
end
|
||||
shouldda.each do |k,v|
|
||||
assert_equal v , @word.get_char(k)
|
||||
end
|
||||
end
|
||||
def test_not_same
|
||||
one = Parfait.new_word("one")
|
||||
two = Parfait.new_word("two")
|
||||
assert !one.compare(two)
|
||||
end
|
||||
def test_is_same
|
||||
one = Parfait.new_word("one")
|
||||
two = Parfait.new_word("one")
|
||||
assert one.compare(two)
|
||||
end
|
||||
end
|
||||
end
|
7
test/vm/test_all.rb
Normal file
7
test/vm/test_all.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require_relative "parfait/test_all"
|
||||
|
||||
require_relative "type/test_all"
|
||||
|
||||
require_relative "method_compiler/test_all"
|
||||
|
||||
require_relative "test_to_code"
|
43
test/vm/test_to_code.rb
Normal file
43
test/vm/test_to_code.rb
Normal file
@ -0,0 +1,43 @@
|
||||
require_relative "helper"
|
||||
|
||||
class ToCodeTest < MiniTest::Test
|
||||
include AST::Sexp
|
||||
|
||||
def check clazz
|
||||
tree = Vm.ast_to_code @statement
|
||||
assert_equal tree.class , Vm::Tree.const_get( clazz )
|
||||
end
|
||||
|
||||
def test_field_access
|
||||
@statement = s(:field_access, s(:receiver, s(:name, :m)), s(:field, s(:name, :index)))
|
||||
check "FieldAccess"
|
||||
end
|
||||
def test_simple_while
|
||||
@statement = s(:while_statement, :false, s(:conditional,s(:int, 1)), s(:statements))
|
||||
check "WhileStatement"
|
||||
end
|
||||
def test_assignment
|
||||
@statement = s(:assignment, s(:name, :i), s(:int, 0))
|
||||
check "Assignment"
|
||||
end
|
||||
def test_nil
|
||||
@statement = s(:nil)
|
||||
check "NilExpression"
|
||||
end
|
||||
def test_true
|
||||
@statement = s(:true)
|
||||
check "TrueExpression"
|
||||
end
|
||||
def test_false
|
||||
@statement = s(:false)
|
||||
check "FalseExpression"
|
||||
end
|
||||
def test_name
|
||||
@statement = s(:name, :foo)
|
||||
check "NameExpression"
|
||||
end
|
||||
def test_class_name
|
||||
@statement =s(:class_name, :FooBar)
|
||||
check "ClassExpression"
|
||||
end
|
||||
end
|
5
test/vm/type/test_all.rb
Normal file
5
test/vm/type/test_all.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require_relative "test_basic"
|
||||
require_relative "test_hash"
|
||||
require_relative "test_message"
|
||||
require_relative "test_method_api"
|
||||
require_relative "test_type_api"
|
75
test/vm/type/test_basic.rb
Normal file
75
test/vm/type/test_basic.rb
Normal file
@ -0,0 +1,75 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class BasicType < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
@mess = @space.first_message
|
||||
assert @mess
|
||||
@type = @mess.get_type()
|
||||
end
|
||||
|
||||
def test_type_index
|
||||
assert_equal @mess.get_type , @mess.get_internal_word(Parfait::TYPE_INDEX) , "mess"
|
||||
end
|
||||
|
||||
def test_type_is_first
|
||||
type = @mess.get_type
|
||||
assert_equal 1 , type.variable_index(:type)
|
||||
end
|
||||
|
||||
def test_length
|
||||
assert @mess
|
||||
assert @mess.get_type
|
||||
assert_equal 9 , @mess.get_type.instance_length , @mess.get_type.inspect
|
||||
end
|
||||
|
||||
def test_names
|
||||
assert @type.names
|
||||
end
|
||||
def test_types
|
||||
assert @type.types
|
||||
end
|
||||
|
||||
def test_type_length
|
||||
assert_equal 9 , @mess.get_type.instance_length , @mess.get_type.inspect
|
||||
end
|
||||
|
||||
def test_type_length_index
|
||||
type = @mess.get_type.get_type
|
||||
assert_equal 5 , type.variable_index(:methods)
|
||||
assert_equal type.object_class , type.get_internal_word(4)
|
||||
end
|
||||
|
||||
def test_no_index_below_1
|
||||
type = @mess.get_type
|
||||
names = type.names
|
||||
assert_equal 9 , names.get_length , names.inspect
|
||||
names.each do |n|
|
||||
assert type.variable_index(n) >= 1
|
||||
end
|
||||
end
|
||||
|
||||
def test_attribute_set
|
||||
@mess.set_receiver( 55)
|
||||
assert_equal 55 , @mess.receiver
|
||||
end
|
||||
|
||||
# not really parfait test, but related and no other place currently
|
||||
def test_reg_index
|
||||
message_ind = Register.resolve_to_index( :message , :receiver )
|
||||
assert_equal 3 , message_ind
|
||||
@mess.set_receiver( 55)
|
||||
assert_equal 55 , @mess.get_internal_word(message_ind)
|
||||
end
|
||||
|
||||
def test_instance_type
|
||||
assert_equal 2 , @mess.get_type.variable_index(:next_message)
|
||||
end
|
||||
|
||||
def test_remove_me
|
||||
type = @mess.get_type
|
||||
assert_equal type , @mess.get_internal_word(1)
|
||||
end
|
||||
end
|
45
test/vm/type/test_hash.rb
Normal file
45
test/vm/type/test_hash.rb
Normal file
@ -0,0 +1,45 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TypeHash < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
@types = @space.instance_variable_get("@types")
|
||||
@first = @types.values.first
|
||||
end
|
||||
|
||||
def test_hash
|
||||
assert_equal Parfait::Dictionary , @types.class
|
||||
end
|
||||
|
||||
def test_length
|
||||
assert @types.length > 11
|
||||
end
|
||||
|
||||
def test_two_hashs_not_equal
|
||||
assert @types.keys.last != @types.keys.first
|
||||
end
|
||||
|
||||
def test_name
|
||||
assert_equal "Word_Type" , @types.values.first.name
|
||||
end
|
||||
|
||||
def test_to_hash
|
||||
hash = @first.to_hash
|
||||
assert_equal hash[:type] , :Type
|
||||
assert_equal 2 , hash.length
|
||||
end
|
||||
def test_add_is_different
|
||||
type = @first.add_instance_variable :random , :Integer
|
||||
assert type != @first , "new: #{type.inspect} , old: #{@first.inspect}"
|
||||
assert @first.hash != type.hash
|
||||
end
|
||||
|
||||
def test_hash_for_no_ivars
|
||||
list = @space.get_class_by_name(:NamedList )
|
||||
t1 = Parfait::Type.for_hash( list , type: :NewInt)
|
||||
t2 = Parfait::Type.for_hash( list , type: :NewObj)
|
||||
assert t1.hash != t2.hash , "Hashes should differ"
|
||||
end
|
||||
end
|
29
test/vm/type/test_message.rb
Normal file
29
test/vm/type/test_message.rb
Normal file
@ -0,0 +1,29 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TypeMessages < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
@mess = @space.first_message
|
||||
end
|
||||
|
||||
def test_message_type
|
||||
type = @mess.get_type
|
||||
assert type
|
||||
assert @mess.instance_variable_defined :next_message
|
||||
assert_equal @mess.next_message , @mess.get_instance_variable(:next_message)
|
||||
end
|
||||
|
||||
def test_message_by_index
|
||||
assert_equal @mess.next_message , @mess.get_instance_variable(:next_message)
|
||||
index = @mess.get_type.variable_index :next_message
|
||||
assert_equal 2 , index
|
||||
assert_equal @mess.next_message , @mess.get_internal_word(index)
|
||||
end
|
||||
|
||||
def test_type_methods
|
||||
assert_equal 5 , @mess.get_type.get_type.variable_index(:methods)
|
||||
end
|
||||
|
||||
end
|
58
test/vm/type/test_method_api.rb
Normal file
58
test/vm/type/test_method_api.rb
Normal file
@ -0,0 +1,58 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestMethodApi < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
@try_class = @space.create_class( :Try )
|
||||
@try_type = @try_class.instance_type
|
||||
end
|
||||
|
||||
def foo_method for_class = :Try
|
||||
args = Parfait::Type.for_hash( @try_class , { bar: :Integer})
|
||||
::Parfait::TypedMethod.new @space.get_class_by_name(for_class).instance_type , :foo , args
|
||||
end
|
||||
|
||||
def test_new_methods
|
||||
assert_equal @try_type.method_names.class, @try_type.methods.class
|
||||
assert_equal @try_type.method_names.get_length , @try_type.methods.get_length
|
||||
end
|
||||
|
||||
def test_add_method
|
||||
before = @try_type.methods.get_length
|
||||
foo = foo_method
|
||||
assert_equal foo , @try_type.add_method(foo)
|
||||
assert_equal 1 , @try_type.methods.get_length - before
|
||||
assert @try_type.method_names.inspect.include?(":foo")
|
||||
end
|
||||
def test_remove_method
|
||||
test_add_method
|
||||
assert_equal true , @try_type.remove_method(:foo)
|
||||
end
|
||||
def test_remove_nothere
|
||||
assert_raises RuntimeError do
|
||||
@try_type.remove_method(:foo)
|
||||
end
|
||||
end
|
||||
def test_create_method
|
||||
args = Parfait::Type.for_hash( @try_class , { bar: :Integer})
|
||||
@try_type.create_method :bar, args
|
||||
assert @try_type.method_names.inspect.include?("bar")
|
||||
end
|
||||
def test_method_get
|
||||
test_add_method
|
||||
assert_equal Parfait::TypedMethod , @try_type.get_method(:foo).class
|
||||
end
|
||||
def test_method_get_nothere
|
||||
assert_nil @try_type.get_method(:foo)
|
||||
test_remove_method
|
||||
assert_nil @try_type.get_method(:foo)
|
||||
end
|
||||
def test_get_instance
|
||||
foo = foo_method :Object
|
||||
type = @space.get_class_by_name(:Object).instance_type
|
||||
type.add_method(foo)
|
||||
assert_equal :foo , type.get_method(:foo).name
|
||||
end
|
||||
end
|
93
test/vm/type/test_type_api.rb
Normal file
93
test/vm/type/test_type_api.rb
Normal file
@ -0,0 +1,93 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TypeApi < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@space = Parfait.object_space
|
||||
tc = @space.get_class_by_name( :NamedList )
|
||||
@type = tc.instance_type
|
||||
end
|
||||
|
||||
def test_inspect
|
||||
assert @type.inspect.start_with?("Type")
|
||||
end
|
||||
|
||||
def test_class_type
|
||||
oc = @space.get_class_by_name( :Object )
|
||||
assert_equal Parfait::Class , oc.class
|
||||
type = oc.instance_type
|
||||
assert_equal Parfait::Type , type.class
|
||||
assert_equal 1 , type.names.get_length , type.names.inspect
|
||||
assert_equal type.names.first , :type
|
||||
end
|
||||
|
||||
def test_class_space
|
||||
space = Parfait.object_space
|
||||
assert_equal Parfait::Space , space.class
|
||||
type = space.get_type
|
||||
assert_equal Parfait::Type , type.class
|
||||
assert_equal 4 , type.names.get_length
|
||||
assert_equal type.object_class.class , Parfait::Class
|
||||
assert_equal type.object_class.name , :Space
|
||||
end
|
||||
|
||||
def test_add_name
|
||||
t = @type.add_instance_variable( :boo , :Object)
|
||||
assert t
|
||||
t
|
||||
end
|
||||
|
||||
def test_added_name_length
|
||||
type = test_add_name
|
||||
assert_equal 2 , type.names.get_length , type.inspect
|
||||
assert_equal :type , type.names.get(1)
|
||||
assert_equal :boo , type.names.get(2)
|
||||
end
|
||||
|
||||
def test_added_name_index
|
||||
type = test_add_name
|
||||
assert_equal 2 , type.variable_index(:boo)
|
||||
assert_equal :Object , type.type_at(2)
|
||||
end
|
||||
|
||||
def test_basic_var_index
|
||||
assert_equal 1 , @type.variable_index(:type)
|
||||
end
|
||||
def test_basic_type_index
|
||||
assert_equal :Type , @type.type_at(1)
|
||||
end
|
||||
|
||||
def test_inspect_added
|
||||
type = test_add_name
|
||||
assert type.inspect.include?("boo") , type.inspect
|
||||
end
|
||||
|
||||
def test_added_names
|
||||
type = test_add_name
|
||||
assert_equal :type , type.names.get(1)
|
||||
assert_equal :boo , type.names.get(2)
|
||||
assert_equal 2 , type.names.get_length
|
||||
end
|
||||
|
||||
def test_each_name
|
||||
type = test_add_name
|
||||
assert_equal 2 , type.get_length
|
||||
counter = [ :boo , :type ]
|
||||
type.names.each do |item|
|
||||
assert_equal item , counter.delete(item)
|
||||
end
|
||||
assert counter.empty? , counter.inspect
|
||||
end
|
||||
|
||||
def test_each_type
|
||||
type = test_add_name
|
||||
assert_equal 2 , type.get_length
|
||||
counter = [ :Object , :Type]
|
||||
type.types.each do |item|
|
||||
assert_equal item , counter.delete(item)
|
||||
end
|
||||
assert counter.empty? , counter.inspect
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user