move soml tests out, small cleanup

This commit is contained in:
Torsten Ruger
2016-12-06 15:08:29 +02:00
parent 5ac14ddccc
commit adca8b21c1
41 changed files with 32 additions and 21 deletions

View File

@ -0,0 +1,20 @@
require_relative '../helper'
# Fragments are small programs that we run through the interpreter and really only check
# - the no. of instructions processed
# - the stdout output
module Fragments
include RuntimeTests
# define setup to NOT load parfait.
def setup
@stdout = ""
@machine = Register.machine.boot
end
def main()
@string_input
end
end

View File

@ -0,0 +1,11 @@
require_relative "test_if"
require_relative "test_class"
require_relative "test_functions"
require_relative "test_hello"
require_relative "test_if"
require_relative "test_putint"
require_relative "test_recursive_fibo"
require_relative "test_return"
require_relative "test_while_fibo"
require_relative "test_word"

View File

@ -0,0 +1,24 @@
require_relative 'helper'
module Register
class TestBasicClass < MiniTest::Test
include Fragments
def test_class_def
@string_input = <<HERE
class Bar
int buh()
return 1
end
end
class Space
int main()
return 1
end
end
HERE
@length = 15
check
end
end
end

View File

@ -0,0 +1,65 @@
require_relative 'helper'
class TestFunctions < MiniTest::Test
include Fragments
def test_functions
@string_input = <<HERE
class Space
int times(int a, int b)
if_zero( b + 0)
a = 0
else
int m = b - 1
int t = times(a, m)
a = a + t
end
return a
end
int t_seven()
int tim = times(8,10)
tim.putint()
return tim
end
int main()
return t_seven()
end
end
HERE
@length = 505
check 80
end
def test_class_method
@string_input = <<HERE
class Space
int self.some()
return 5
end
int main()
return Object.some()
end
end
HERE
@length = 33
check 5
end
def test_class_method_fails
@string_input = <<HERE
class Space
int main()
return Object.som()
end
end
HERE
assert_raises {check}
end
end

View File

@ -0,0 +1,18 @@
require_relative 'helper'
class TestHello < MiniTest::Test
include Fragments
def test_hello
@string_input = <<HERE
class Space
int main()
"Hello Raisa, I am salama".putstring()
end
end
HERE
@length = 37
@stdout = "Hello Raisa, I am salama"
check
end
end

View File

@ -0,0 +1,60 @@
require_relative 'helper'
class TestIf < MiniTest::Test
include Fragments
def test_if_plus
@string_input = <<HERE
class Space
int main()
int n = 10
if_plus( n - 12)
return 3
else
return 4
end
end
end
HERE
@length = 25
check 4
end
def test_if_zero
@string_input = <<HERE
class Space
int main()
int n = 10
if_zero(n - 10 )
"10".putstring()
end
end
end
HERE
@length = 47
@stdout = "10"
check
end
def test_if_minus
@string_input = <<HERE
class Space
int itest(int n)
if_minus( n - 12)
"then".putstring()
else
"else".putstring()
end
end
int main()
itest(20)
end
end
HERE
@length = 62
@stdout = "else"
check
end
end

View File

@ -0,0 +1,22 @@
require_relative 'helper'
class TestPutint < MiniTest::Test
include Fragments
def test_putint
@string_input = <<HERE
class Integer
int putint()
return 1
end
end
class Space
int main()
42.putint()
end
end
HERE
@length = 32
check
end
end

View File

@ -0,0 +1,34 @@
require_relative 'helper'
class TestRecursinveFibo < MiniTest::Test
include Fragments
def test_recursive_fibo
@string_input = <<HERE
class Space
int fibonaccir( int n )
if_plus( n - 2 )
int tmp
tmp = n - 1
int a = fibonaccir( tmp )
tmp = n - 2
int b = fibonaccir( tmp )
return a + b
else
return n
end
end
int fib_print(int n)
int fib = fibonaccir( n )
fib.putint()
return fib
end
int main()
return fib_print(8)
end
end
HERE
@length = 2525
check 21
end
end

View File

@ -0,0 +1,51 @@
require_relative 'helper'
class TestReturn < MiniTest::Test
include Fragments
def test_return1
@string_input = <<HERE
class Space
int main()
return 5
end
end
HERE
@length = 15
check 5
end
def test_return2
@string_input = <<HERE
class Space
int foo(int x)
return x
end
int main()
return foo( 5 )
end
end
HERE
@length = 35
check 5
end
def test_return3
@string_input = <<HERE
class Space
int foo(int x)
int a = 5
return a
end
int main()
return foo( 4 )
end
end
HERE
@length = 39
check 5
end
end

View File

@ -0,0 +1,44 @@
require_relative 'helper'
class TestWhileFragment < MiniTest::Test
include Fragments
def fibo num
@string_input = <<HERE
class Space
int fibonaccit(int n)
int a = 0
int b = 1
while_plus( n - 2)
n = n - 1
int tmp = a
a = b
b = tmp + b
end
b.putint()
return b
end
int main()
return fibonaccit( 100 )
end
end
HERE
@string_input.sub!( "100" , num.to_s )
end
def test_while_fibo48
fibo 48
@length = 1241
# this is not the correct fibo, just what comes from wrapping (smaller than below)
check 512559680
end
# highest 32 bit fibo
def test_while_fibo47
fibo 47
@length = 1216
check 2971215073
end
end

View File

@ -0,0 +1,25 @@
require_relative 'helper'
module Soml
class TestWord < MiniTest::Test
include Fragments
def test_word_new
@string_input = <<HERE
class Space
Word self.new()
return nil
end
end
class Space
int main()
Word w = Word.new()
end
end
HERE
@length = 34
@stdout = ""
check
end
end
end