putting tests into module

This commit is contained in:
Torsten Ruger 2017-01-03 22:42:40 +02:00
parent da58ce30e3
commit 8aee761b73
11 changed files with 453 additions and 434 deletions

View File

@ -1,63 +1,65 @@
require_relative "../helper"
require "register/interpreter"
module Ticker
include AST::Sexp
module Register
module Ticker
include AST::Sexp
def setup
Register.machine.boot
do_clean_compile
Typed.compile( @input )
Register::Collector.collect_space
@interpreter = Register::Interpreter.new
@interpreter.start Register.machine.init
end
# must be after boot, but before main compile, to define method
def do_clean_compile
end
def check_chain should
should.each_with_index do |name , index|
got = ticks(1)
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
def setup
Register.machine.boot
do_clean_compile
Typed.compile( @input )
Register::Collector.collect_space
@interpreter = Register::Interpreter.new
@interpreter.start Register.machine.init
end
end
def check_return val
assert_equal Parfait::Message , @interpreter.get_register(:r0).class
assert_equal val , @interpreter.get_register(:r0).return_value
end
def ticks num
last = nil
num.times do
last = @interpreter.instruction
@interpreter.tick
# must be after boot, but before main compile, to define method
def do_clean_compile
end
return last
end
def show_ticks
classes = []
tick = 1
begin
while true and (classes.length < 200)
cl = ticks(1).class
tick += 1
classes << cl
break if cl == NilClass
def check_chain should
should.each_with_index do |name , index|
got = ticks(1)
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
end
rescue => e
puts "Error at tick #{tick}"
puts e
puts e.backtrace
end
classes = classes.collect {|c| '"' + c.name.sub("Register::","") + '",' }
classes << "length = #{classes.length}"
classes.each_slice(5).each do |line|
puts " " + line.join
def check_return val
assert_equal Parfait::Message , @interpreter.get_register(:r0).class
assert_equal val , @interpreter.get_register(:r0).return_value
end
def ticks num
last = nil
num.times do
last = @interpreter.instruction
@interpreter.tick
end
return last
end
def show_ticks
classes = []
tick = 1
begin
while true and (classes.length < 200)
cl = ticks(1).class
tick += 1
classes << cl
break if cl == NilClass
end
rescue => e
puts "Error at tick #{tick}"
puts e
puts e.backtrace
end
classes = classes.collect {|c| '"' + c.name.sub("Register::","") + '",' }
classes << "length = #{classes.length}"
classes.each_slice(5).each do |line|
puts " " + line.join
end
exit(1)
end
exit(1)
end
end

View File

@ -1,60 +1,62 @@
require_relative "helper"
class AddTest < MiniTest::Test
include Ticker
module Register
class AddTest < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
return 5 + 7
def setup
@string_input = <<HERE
class Space
int main()
return 5 + 7
end
end
end
HERE
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7))))
super
end
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7))))
super
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
def test_get
assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r2 )
assert Integer , @interpreter.get_register( :r2 ).class
end
def test_transfer
transfer = ticks 16
assert_equal Register::RegisterTransfer , transfer.class
assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from)
end
def test_call
ret = ticks(15)
assert_equal Register::FunctionReturn , ret.class
def test_get
assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r2 )
assert Integer , @interpreter.get_register( :r2 ).class
end
def test_transfer
transfer = ticks 16
assert_equal Register::RegisterTransfer , transfer.class
assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from)
end
def test_call
ret = ticks(15)
assert_equal Register::FunctionReturn , ret.class
object = @interpreter.get_register( ret.register )
link = object.get_internal_word( ret.index )
object = @interpreter.get_register( ret.register )
link = object.get_internal_word( ret.index )
assert_equal Register::Label , link.class
end
def test_adding
done_op = ticks(12)
assert_equal Register::OperatorInstruction , done_op.class
left = @interpreter.get_register(done_op.left)
rr = done_op.right
right = @interpreter.get_register(rr)
assert_equal Fixnum , left.class
assert_equal Fixnum , right.class
assert_equal 7 , right
assert_equal 12 , left
done_tr = ticks(4)
assert_equal Register::RegisterTransfer , done_tr.class
result = @interpreter.get_register(done_op.left)
assert_equal result , 12
assert_equal Register::Label , link.class
end
def test_adding
done_op = ticks(12)
assert_equal Register::OperatorInstruction , done_op.class
left = @interpreter.get_register(done_op.left)
rr = done_op.right
right = @interpreter.get_register(rr)
assert_equal Fixnum , left.class
assert_equal Fixnum , right.class
assert_equal 7 , right
assert_equal 12 , left
done_tr = ticks(4)
assert_equal Register::RegisterTransfer , done_tr.class
result = @interpreter.get_register(done_op.left)
assert_equal result , 12
end
end
end

View File

@ -1,64 +1,66 @@
require_relative "helper"
class TestInterpretRegToByte < MiniTest::Test
include Ticker
module Register
class TestInterpretRegToByte < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
"Hello".set_internal_byte(1,104)
def setup
@string_input = <<HERE
class Space
int main()
"Hello".set_internal_byte(1,104)
end
end
end
HERE
@input = s(:statements, s(:call,
s(:name, :set_internal_byte),
s(:arguments, s(:int, 1), s(:int, 104)),
s(:receiver, s(:string, "Hello"))))
super
end
@input = s(:statements, s(:call,
s(:name, :set_internal_byte),
s(:arguments, s(:int, 1), s(:int, 104)),
s(:receiver, s(:string, "Hello"))))
super
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","SlotToReg","RegToSlot","LoadConstant",
"RegToSlot","RegisterTransfer","FunctionCall","Label","SlotToReg",
"SlotToReg","SlotToReg","SlotToReg","SlotToReg","RegToByte",
"Label","FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg",
"Label","FunctionReturn","RegisterTransfer","Syscall","NilClass"]
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","SlotToReg","RegToSlot","LoadConstant",
"RegToSlot","RegisterTransfer","FunctionCall","Label","SlotToReg",
"SlotToReg","SlotToReg","SlotToReg","SlotToReg","RegToByte",
"Label","FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg",
"Label","FunctionReturn","RegisterTransfer","Syscall","NilClass"]
end
def test_branch
was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal Register::FunctionCall , ticks(8).class
end
def test_exit
done = ticks(45)
assert_equal NilClass , done.class
end
def test_branch
was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal Register::FunctionCall , ticks(8).class
end
def test_exit
done = ticks(45)
assert_equal NilClass , done.class
end
def test_reg_to_byte
done = ticks(35)
assert_equal Register::RegToByte , done.class
assert_equal "h".ord , @interpreter.get_register(done.register)
end
def test_reg_to_byte
done = ticks(35)
assert_equal Register::RegToByte , done.class
assert_equal "h".ord , @interpreter.get_register(done.register)
end
end
end

View File

@ -1,51 +1,53 @@
require_relative "helper"
class IfCalledTest < MiniTest::Test
include Ticker
include Compiling
module Register
class IfCalledTest < MiniTest::Test
include Ticker
include Compiling
def setup
@string_input = <<HERE
class Space
int itest(int n)
if_zero( n - 12)
"then".putstring()
else
"else".putstring()
def setup
@string_input = <<HERE
class Space
int itest(int n)
if_zero( n - 12)
"then".putstring()
else
"else".putstring()
end
end
int main()
itest(20)
end
end
int main()
itest(20)
end
end
HERE
@input = s(:statements, s(:call, s(:name, :itest), s(:arguments, s(:int, 20))))
super
end
@input = s(:statements, s(:call, s(:name, :itest), s(:arguments, s(:int, 20))))
super
end
# must be after boot, but before main compile, to define method
def do_clean_compile
clean_compile :Space , :itest , {:n => :Integer} ,
s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:name, :n), s(:int, 12))),
s(:true_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "then")))),
s(:false_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "else"))))))
end
def test_if
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"SlotToReg","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall",
"Label","SlotToReg","SlotToReg","LoadConstant","OperatorInstruction",
"IsZero","SlotToReg","LoadConstant","RegToSlot","LoadConstant",
"RegToSlot","LoadConstant","SlotToReg","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","RegisterTransfer","FunctionCall","Label",
"SlotToReg","SlotToReg","RegisterTransfer","Syscall","RegisterTransfer",
"RegisterTransfer","RegToSlot","Label","FunctionReturn","RegisterTransfer",
"SlotToReg","SlotToReg","Branch","Label","Label",
"FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg","Label",
"FunctionReturn","RegisterTransfer","Syscall","NilClass"]
# must be after boot, but before main compile, to define method
def do_clean_compile
clean_compile :Space , :itest , {:n => :Integer} ,
s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:name, :n), s(:int, 12))),
s(:true_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "then")))),
s(:false_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "else"))))))
end
def test_if
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"SlotToReg","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall",
"Label","SlotToReg","SlotToReg","LoadConstant","OperatorInstruction",
"IsZero","SlotToReg","LoadConstant","RegToSlot","LoadConstant",
"RegToSlot","LoadConstant","SlotToReg","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","RegisterTransfer","FunctionCall","Label",
"SlotToReg","SlotToReg","RegisterTransfer","Syscall","RegisterTransfer",
"RegisterTransfer","RegToSlot","Label","FunctionReturn","RegisterTransfer",
"SlotToReg","SlotToReg","Branch","Label","Label",
"FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg","Label",
"FunctionReturn","RegisterTransfer","Syscall","NilClass"]
end
end
end

View File

@ -1,45 +1,47 @@
require_relative "helper"
class AddChange < MiniTest::Test
include Ticker
module Register
class AddChange < MiniTest::Test
include Ticker
def setup
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7))))
@state_events = {}
@instruction_events = []
super
end
def state_changed( a , b)
@state_events[:state_changed] = [a , b]
end
def instruction_changed(was , is )
@instruction_events << was
end
def test_state_change
@interpreter.register_event :state_changed , self
ticks 30
assert @state_events[:state_changed]
assert_equal 2 , @state_events[:state_changed].length
assert_equal :running, @state_events[:state_changed][0]
@interpreter.unregister_event :state_changed , self
end
def test_instruction_events
@interpreter.register_event :instruction_changed , self
ticks 30
assert_equal 17 , @instruction_events.length
@interpreter.unregister_event :instruction_changed , self
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
def setup
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7))))
@state_events = {}
@instruction_events = []
super
end
def state_changed( a , b)
@state_events[:state_changed] = [a , b]
end
def instruction_changed(was , is )
@instruction_events << was
end
def test_state_change
@interpreter.register_event :state_changed , self
ticks 30
assert @state_events[:state_changed]
assert_equal 2 , @state_events[:state_changed].length
assert_equal :running, @state_events[:state_changed][0]
@interpreter.unregister_event :state_changed , self
end
def test_instruction_events
@interpreter.register_event :instruction_changed , self
ticks 30
assert_equal 17 , @instruction_events.length
@interpreter.unregister_event :instruction_changed , self
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
end

View File

@ -1,38 +1,39 @@
require_relative "helper"
class MultTest < MiniTest::Test
include Ticker
include AST::Sexp
module Register
class MultTest < MiniTest::Test
include Ticker
include AST::Sexp
def setup
@string_input = <<HERE
class Space
int main()
return #{2**31} * #{2**31}
def setup
@string_input = <<HERE
class Space
int main()
return #{2**31} * #{2**31}
end
end
end
HERE
@input = s(:statements, s(:return, s(:operator_value, :*, s(:int, 2147483648), s(:int, 2147483648))))
super
end
@input = s(:statements, s(:return, s(:operator_value, :*, s(:int, 2147483648), s(:int, 2147483648))))
super
end
def test_mult
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
check_return 0
end
def test_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_mult
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
check_return 0
end
def test_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end
def test_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end
end
end

View File

@ -1,36 +1,38 @@
require_relative "helper"
class PlusTest < MiniTest::Test
include Ticker
module Register
class PlusTest < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
return #{2**62 - 1} + 1
def setup
@string_input = <<HERE
class Space
int main()
return #{2**62 - 1} + 1
end
end
end
HERE
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 4611686018427387903), s(:int, 1))))
super
end
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 4611686018427387903), s(:int, 1))))
super
end
def test_add
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
check_return 0
end
def test_add
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","RegToSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
check_return 0
end
def test_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_zero
ticks( 12 )
assert @interpreter.flags[:zero]
def test_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end
end
end

View File

@ -1,68 +1,70 @@
require_relative "helper"
class TestPuts < MiniTest::Test
include Ticker
module Register
class TestPuts < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
"Hello again".putstring()
def setup
@string_input = <<HERE
class Space
int main()
"Hello again".putstring()
end
end
end
HERE
@input = s(:statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "Hello again"))))
super
end
@input = s(:statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "Hello again"))))
super
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","RegToSlot",
"RegisterTransfer","FunctionCall","Label","SlotToReg","SlotToReg",
"RegisterTransfer","Syscall","RegisterTransfer","RegisterTransfer","RegToSlot",
"Label","FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg",
"Label","FunctionReturn","RegisterTransfer","Syscall","NilClass"]
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","RegToSlot",
"RegisterTransfer","FunctionCall","Label","SlotToReg","SlotToReg",
"RegisterTransfer","Syscall","RegisterTransfer","RegisterTransfer","RegToSlot",
"Label","FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg",
"Label","FunctionReturn","RegisterTransfer","Syscall","NilClass"]
end
def test_branch
was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal Register::FunctionCall , ticks(8).class
end
def test_branch
was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal Register::FunctionCall , ticks(8).class
end
def test_putstring
done = ticks(27)
assert_equal Register::Syscall , done.class
assert_equal "Hello again" , @interpreter.stdout
end
def test_putstring
done = ticks(27)
assert_equal Register::Syscall , done.class
assert_equal "Hello again" , @interpreter.stdout
end
def test_return
done = ticks(32)
assert_equal Register::FunctionReturn , done.class
assert Register::Label , @interpreter.instruction.class
assert @interpreter.instruction.is_a?(Register::Instruction) , "not instruction #{@interpreter.instruction}"
end
def test_return
done = ticks(32)
assert_equal Register::FunctionReturn , done.class
assert Register::Label , @interpreter.instruction.class
assert @interpreter.instruction.is_a?(Register::Instruction) , "not instruction #{@interpreter.instruction}"
end
def test_exit
done = ticks(42)
assert_equal NilClass , done.class
assert_equal "Hello again" , @interpreter.stdout
def test_exit
done = ticks(42)
assert_equal NilClass , done.class
assert_equal "Hello again" , @interpreter.stdout
end
end
end

View File

@ -1,64 +1,66 @@
require_relative "helper"
class TestInterpretByteToReg < MiniTest::Test
include Ticker
module Register
class TestInterpretByteToReg < MiniTest::Test
include Ticker
def setup
@string_input = <<HERE
class Space
int main()
"Hello".get_internal_byte(1)
def setup
@string_input = <<HERE
class Space
int main()
"Hello".get_internal_byte(1)
end
end
end
HERE
@input = s(:statements, s(:call,
s(:name, :get_internal_byte),
s(:arguments, s(:int, 1)),
s(:receiver, s(:string, "Hello"))))
super
end
@input = s(:statements, s(:call,
s(:name, :get_internal_byte),
s(:arguments, s(:int, 1)),
s(:receiver, s(:string, "Hello"))))
super
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall",
"Label","SlotToReg","SlotToReg","SlotToReg","ByteToReg",
"RegToSlot","Label","FunctionReturn","RegisterTransfer","SlotToReg",
"SlotToReg","Label","FunctionReturn","RegisterTransfer","Syscall",
"NilClass"]
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","SlotToReg",
"LoadConstant","RegToSlot","LoadConstant","RegToSlot","LoadConstant",
"SlotToReg","SlotToReg","RegToSlot","LoadConstant","SlotToReg",
"RegToSlot","LoadConstant","RegToSlot","RegisterTransfer","FunctionCall",
"Label","SlotToReg","SlotToReg","SlotToReg","ByteToReg",
"RegToSlot","Label","FunctionReturn","RegisterTransfer","SlotToReg",
"SlotToReg","Label","FunctionReturn","RegisterTransfer","Syscall",
"NilClass"]
end
def test_branch
was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal Register::FunctionCall , ticks(8).class
end
def test_exit
done = ticks(41)
assert_equal NilClass , done.class
end
def test_branch
was = @interpreter.instruction
assert_equal Register::Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
assert_equal Register::LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
assert_equal Register::SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
assert_equal Register::FunctionCall , ticks(8).class
end
def test_exit
done = ticks(41)
assert_equal NilClass , done.class
end
def test_byte_to_reg
done = ticks(30)
assert_equal Register::ByteToReg , done.class
assert_equal "H".ord , @interpreter.get_register(done.register)
end
def test_byte_to_reg
done = ticks(30)
assert_equal Register::ByteToReg , done.class
assert_equal "H".ord , @interpreter.get_register(done.register)
end
end
end

View File

@ -1,39 +1,41 @@
require_relative "helper"
class IfSimpleTest < MiniTest::Test
include Ticker
include Compiling
module Register
class IfSimpleTest < MiniTest::Test
include Ticker
include Compiling
def setup
@string_input = <<HERE
class Space
int main()
if_zero( 10 - 12)
"then".putstring()
else
"else".putstring()
def setup
@string_input = <<HERE
class Space
int main()
if_zero( 10 - 12)
"then".putstring()
else
"else".putstring()
end
end
end
HERE
@input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))),
s(:true_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "then")))),
s(:false_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "else"))))))
super
end
def test_if
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","IsZero","SlotToReg","LoadConstant",
"RegToSlot","LoadConstant","RegToSlot","LoadConstant","SlotToReg",
"SlotToReg","RegToSlot","LoadConstant","RegToSlot","RegisterTransfer",
"FunctionCall","Label","SlotToReg","SlotToReg","RegisterTransfer",
"Syscall","RegisterTransfer","RegisterTransfer","RegToSlot","Label",
"FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg","Branch",
"Label","Label","FunctionReturn","RegisterTransfer","Syscall",
"NilClass"]
end
end
end
HERE
@input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))),
s(:true_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "then")))),
s(:false_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "else"))))))
super
end
def test_if
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","SlotToReg","RegToSlot",
"LoadConstant","RegToSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","IsZero","SlotToReg","LoadConstant",
"RegToSlot","LoadConstant","RegToSlot","LoadConstant","SlotToReg",
"SlotToReg","RegToSlot","LoadConstant","RegToSlot","RegisterTransfer",
"FunctionCall","Label","SlotToReg","SlotToReg","RegisterTransfer",
"Syscall","RegisterTransfer","RegisterTransfer","RegToSlot","Label",
"FunctionReturn","RegisterTransfer","SlotToReg","SlotToReg","Branch",
"Label","Label","FunctionReturn","RegisterTransfer","Syscall",
"NilClass"]
end
end

View File

@ -5,7 +5,7 @@ module Register
def test_simple_collect
Machine.new.boot
objects = Register::Collector.collect_space
assert ((352 == objects.length) or (420 == objects.length)) , objects.length.to_s
assert ((352 == objects.length) or (419 == objects.length)) , objects.length.to_s
end
end
end