rework interpreter test framework

remove lots of copy paste
This commit is contained in:
Torsten Ruger 2015-11-08 14:30:28 +02:00
parent 37564d9c2e
commit cf05e7553a
6 changed files with 105 additions and 82 deletions

View File

@ -2,6 +2,25 @@ require_relative "../helper"
require "interpreter/interpreter" require "interpreter/interpreter"
module Ticker module Ticker
def setup
machine = Register.machine.boot
syntax = Parser::Salama.new.parse_with_debug(@string_input)
parts = Parser::Transform.new.apply(syntax)
#puts parts.inspect
Soml.compile( parts )
machine.collect
@interpreter = Interpreter::Interpreter.new
@interpreter.start Register.machine.init
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}"
end
end
def ticks num def ticks num
last = nil last = nil
num.times do num.times do
@ -13,7 +32,6 @@ module Ticker
def show_ticks def show_ticks
classes = [] classes = []
error = nil
tick = 1 tick = 1
begin begin
while true and (classes.length < 200) while true and (classes.length < 200)

View File

@ -1,27 +1,25 @@
require_relative "helper" require_relative "helper"
class AddTest < MiniTest::Test class AddTest < MiniTest::Test
include AST::Sexp
include Ticker include Ticker
def setup def setup
machine = Register.machine.boot @string_input = <<HERE
code = s(:class, :Object, class Object
s(:derives, nil), int main()
s(:statements, return 5 + 7
s(:function, :int, end
s(:name, :main), end
s(:parameters), HERE
s(:statements, super
s(:return, end
s(:operator_value, :+,
s(:int, 5),
s(:int, 7)))))))
Soml.compile( code ) def test_chain
machine.collect #show_ticks # get output of what is
@interpreter = Interpreter::Interpreter.new check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
@interpreter.start Register.machine.init "LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end end
def test_get def test_get
@ -58,15 +56,4 @@ class AddTest < MiniTest::Test
result = @interpreter.get_register(done_op.left) result = @interpreter.get_register(done_op.left)
assert_equal result , 12 assert_equal result , 12
end end
def test_chain
#show_ticks # get output of what is
["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"].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
end
end end

View File

@ -1,4 +1,4 @@
require_relative "test_add" require_relative "test_add"
require_relative "test_if" require_relative "test_if"
require_relative "test_puts" require_relative "test_puts"
require_relative "test_puti" require_relative "test_ops"

View File

@ -2,9 +2,8 @@ require_relative "helper"
class AddTest < MiniTest::Test class AddTest < MiniTest::Test
include Ticker include Ticker
include AST::Sexp
def test_if def setup
@string_input = <<HERE @string_input = <<HERE
class Object class Object
int itest(int n) int itest(int n)
@ -20,16 +19,12 @@ class Object
end end
end end
HERE HERE
machine = Register.machine.boot super
syntax = Parser::Salama.new.parse_with_debug(@string_input) end
parts = Parser::Transform.new.apply(syntax)
#puts parts.inspect def test_if
Soml.compile( parts )
machine.collect
@interpreter = Interpreter::Interpreter.new
@interpreter.start Register.machine.init
#show_ticks # get output of what is #show_ticks # get output of what is
["Branch","Label","LoadConstant","GetSlot","SetSlot", check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","GetSlot", "LoadConstant","SetSlot","FunctionCall","Label","GetSlot",
"GetSlot","SetSlot","LoadConstant","SetSlot","LoadConstant", "GetSlot","SetSlot","LoadConstant","SetSlot","LoadConstant",
"SetSlot","LoadConstant","SetSlot","LoadConstant","SetSlot", "SetSlot","LoadConstant","SetSlot","LoadConstant","SetSlot",
@ -41,11 +36,6 @@ HERE
"Label","FunctionReturn","RegisterTransfer","GetSlot","GetSlot", "Label","FunctionReturn","RegisterTransfer","GetSlot","GetSlot",
"Branch","Label","Label","FunctionReturn","RegisterTransfer", "Branch","Label","Label","FunctionReturn","RegisterTransfer",
"GetSlot","GetSlot","Label","FunctionReturn","RegisterTransfer", "GetSlot","GetSlot","Label","FunctionReturn","RegisterTransfer",
"Syscall","NilClass"].each_with_index do |name , index| "Syscall","NilClass"]
got = ticks(1)
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
end
#puts @interpreter.block.inspect
end end
end end

View File

@ -0,0 +1,42 @@
require_relative "helper"
class AddTest < MiniTest::Test
include Ticker
include AST::Sexp
def setup
@string_input = <<HERE
class Object
int main()
return 5 + 10
end
end
HERE
super
end
def test_add
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
def test_mult
@string_input = <<HERE
class Object
int main()
return 5 * 10
end
end
HERE
setup
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
"RegisterTransfer","Syscall","NilClass"]
end
end

View File

@ -1,27 +1,29 @@
require_relative "helper" require_relative "helper"
class TestPuts < MiniTest::Test class TestPuts < MiniTest::Test
include AST::Sexp
include Ticker include Ticker
def setup
machine = Register.machine.boot
code = s(:class, :Object,
s(:derives, nil),
s(:statements,
s(:function, :int,
s(:name, :main),
s(:parameters),
s(:statements,
s(:call,
s(:name, :putstring),
s(:arguments),
s(:receiver,
s(:string, "Hello again")))))))
Soml.compile( code ) def setup
machine.collect @string_input = <<HERE
@interpreter = Interpreter::Interpreter.new class Object
@interpreter.start Register.machine.init int main()
"Hello again".putstring()
end
end
HERE
super
end
def test_chain
#show_ticks # get output of what is
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","GetSlot",
"LoadConstant","SetSlot","LoadConstant","SetSlot","LoadConstant",
"SetSlot","LoadConstant","SetSlot","RegisterTransfer","FunctionCall",
"Label","GetSlot","RegisterTransfer","Syscall","RegisterTransfer",
"RegisterTransfer","SetSlot","Label","FunctionReturn","RegisterTransfer",
"GetSlot","GetSlot","Label","FunctionReturn","RegisterTransfer",
"Syscall","NilClass"]
end end
def test_branch def test_branch
@ -44,22 +46,6 @@ class TestPuts < MiniTest::Test
assert_equal Register::FunctionCall , ticks(8).class assert_equal Register::FunctionCall , ticks(8).class
end end
def test_chain
#show_ticks # get output of what is
["Branch","Label","LoadConstant","GetSlot","SetSlot",
"LoadConstant","SetSlot","FunctionCall","Label","GetSlot",
"LoadConstant","SetSlot","LoadConstant","SetSlot","LoadConstant",
"SetSlot","LoadConstant","SetSlot","RegisterTransfer","FunctionCall",
"Label","GetSlot","RegisterTransfer","Syscall","RegisterTransfer",
"RegisterTransfer","SetSlot","Label","FunctionReturn","RegisterTransfer",
"GetSlot","GetSlot","Label","FunctionReturn","RegisterTransfer",
"Syscall","NilClass"].each_with_index do |name , index|
got = ticks(1)
#puts "TICK #{index}"
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
end
end
def test_putstring def test_putstring
done = ticks(24) done = ticks(24)
assert_equal Register::Syscall , done.class assert_equal Register::Syscall , done.class