first interpreted tests, fix branch issues

whole branch logic wobbly
better syntax needed, but working(ish) for now
This commit is contained in:
Torsten Ruger
2015-10-19 14:46:12 +03:00
parent d767caf479
commit bdcd0f297d
12 changed files with 99 additions and 123 deletions

View File

@ -1,17 +0,0 @@
module CodeChecker
def check
machine = Virtual.machine.boot
machine.parse_and_compile @string_input
produced = Virtual.machine.space.get_main.source
assert @output , "No output given"
assert_equal @output.length , produced.blocks.length , "Block length"
produced.blocks.each_with_index do |b,i|
codes = @output[i]
assert codes , "No codes for block #{i}"
assert_equal b.codes.length , codes.length , "Code length for block #{i}"
b.codes.each_with_index do |c , ii |
assert_equal codes[ii] , c.class , "Block #{i} , code #{ii}"
end
end
end
end

View File

@ -1,54 +1,31 @@
require_relative '../../helper'
require "interpreter/interpreter"
# Fragments are small programs that we run through the interpreter and really only check
# - the no. of instructions processed
# - the stdout output
# simple tests to check parsing pworks and the first classes come out right.
#
# build up from small to check larger statements are correct
module Fragments
def setup
@stdout = ""
end
def check
machine = Virtual.machine.boot
machine.parse_and_compile @string_input
produced = Virtual.machine.space.get_main.source
assert @expect , "No output given"
assert_equal @expect.length , produced.blocks.length , "Block length"
produced.blocks.each_with_index do |b,i|
codes = @expect[i]
assert codes , "No codes for block #{i}"
assert_equal b.codes.length , codes.length , "Code length for block #{i+1}"
b.codes.each_with_index do |c , ii |
assert_equal codes[ii] , c.class , "Block #{i+1} , code #{ii+1}"
end
end
end
# helper to write the file
def write name
writer = Elf::ObjectWriter.new(@object_machine , Elf::Constants::TARGET_ARM)
assembly = writer.text
writer.save("#{name}.o")
function = @object_machine.classes[@target[0]]
assert function , "no class #{@target[0]}"
function = function.get_function(@target[1])
assert function , "no function #{@target[1]} for class #{@target[0]}"
io = StringIO.new
function.assemble io
assembly = io.string
# use this for getting the bytes to compare to :
puts bytes(assembly)
assembly.bytes.each_with_index do |byte , index|
is = @should[index]
assert_equal Fixnum , is.class , "@#{index.to_s(16)} = #{is}"
assert_equal byte , is , "@#{index.to_s(16)} #{byte.to_s(16)} != #{is.to_s(16)}"
end
if( RbConfig::CONFIG["build_cpu"] == "arm")
system "ld -N #{name}.o"
result = %x[./a.out]
assert_equal @output , result
end
end
def bytes string
"[" + string.bytes.collect{|b| "0x"+ b.to_s(16)}.join(",") + "]"
machine.collect
interpreter = Interpreter::Interpreter.new
interpreter.start machine.init
count = 0
begin
count += 1
#puts interpreter.instruction
interpreter.tick
end while( ! interpreter.instruction.nil?)
assert_equal @length , count
assert_equal @stdout , interpreter.stdout
end
end

View File

@ -1,9 +1,9 @@
require_relative "test_foo"
require_relative "test_if"
require_relative "test_hello"
require_relative "test_class"
require_relative "test_putint"
require_relative "test_functions"
require_relative "test_recursive_fibo"
require_relative "test_while_fibo"
#require_relative "test_foo"
#require_relative "test_hello"
#require_relative "test_class"
#require_relative "test_putint"
#require_relative "test_functions"
#require_relative "test_recursive_fibo"
#require_relative "test_while_fibo"

View File

@ -8,7 +8,7 @@ class TestIf < MiniTest::Test
class Object
int main()
int n = 10
if( n < 12)
if( n - 12)
return 3
else
return 4
@ -16,30 +16,32 @@ class Object
end
end
HERE
@expect = [[SaveReturn,Virtual::Set,Virtual::Set,Register::GetSlot,
Register::GetSlot,Register::OperatorInstruction,Register::IsZeroBranch] ,
[Virtual::Set,Register::AlwaysBranch] ,[Virtual::Set] ,[] ,[RegisterTransfer,GetSlot,FunctionReturn] ]
@length = 17
check
end
def test_return
def test_if_small
@string_input = <<HERE
class Object
int main()
return 5
int n = 10
if(8 - n )
"10".putstring()
end
end
end
HERE
@expect = [[SaveReturn,Virtual::Set] , [RegisterTransfer,GetSlot,FunctionReturn]]
@length = 33
@stdout = "10"
check
end
def test_if_function
def test_if_puts
@string_input = <<HERE
class Object
int itest(int n)
if( n < 12)
if( n - 12)
"then".putstring()
else
"else".putstring()
@ -51,8 +53,8 @@ class Object
end
end
HERE
@expect = [ [SaveReturn,Register::GetSlot,Virtual::Set,Virtual::Set,
Virtual::Set,Virtual::Set,RegisterTransfer,FunctionCall] ,[RegisterTransfer,GetSlot,FunctionReturn] ]
@length = 40
@stdout = "else"
check
end
end

View File

@ -2,4 +2,4 @@ require_relative "expressions/test_all"
require_relative "statements/test_all"
#require_relative "fragments/test_all"
require_relative "fragments/test_all"