rubyx/test/compiler/test_methods.rb

197 lines
3.2 KiB
Ruby
Raw Normal View History

2015-07-19 09:54:36 +02:00
require_relative "compiler_helper"
2015-09-27 10:27:30 +02:00
require_relative "code_checker"
2015-07-18 14:28:57 +02:00
module Virtual
class TestMethods < MiniTest::Test
2015-09-27 10:27:30 +02:00
include CodeChecker
2015-07-18 14:28:57 +02:00
2015-07-19 09:36:06 +02:00
def test_module
@string_input = <<HERE
class Some
2015-09-20 16:33:05 +02:00
int foo()
return 5
2015-07-19 09:36:06 +02:00
end
end
HERE
@output = [[MethodEnter] ,[MethodReturn]]
check
end
2015-07-18 14:28:57 +02:00
def test_simplest_function
@string_input = <<HERE
class Object
int foo(int x)
return x
end
2014-07-14 10:29:38 +02:00
end
2015-07-18 14:28:57 +02:00
HERE
@output = [[MethodEnter] ,[MethodReturn]]
check
end
2015-09-20 16:33:05 +02:00
def test_second_simplest_function
2015-07-18 14:28:57 +02:00
@string_input = <<HERE
class Object
ref foo(ref x)
return x
end
2015-07-18 14:28:57 +02:00
end
2014-07-14 10:29:38 +02:00
HERE
2015-09-20 16:33:05 +02:00
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end
2015-09-20 16:33:05 +02:00
def test_puts_string
@string_input = <<HERE
class Object
2015-10-07 09:04:55 +02:00
int puts(ref str)
return str
end
int main()
puts("Hello")
end
end
HERE
@output = [[MethodEnter , NewMessage, Set, Set , Set, Set, MethodCall],[MethodReturn]]
2014-07-14 10:29:38 +02:00
check
end
def test_int_function
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
class Integer < Object
int times(int x)
self * x
end
2014-07-14 10:29:38 +02:00
end
HERE
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
2014-07-14 13:06:09 +02:00
check
cla = Virtual.machine.space.get_class_by_name :Integer
assert cla.get_instance_method( :times )
2014-07-14 10:29:38 +02:00
end
2015-10-07 09:19:25 +02:00
def test_function_ops
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
2015-10-07 09:19:25 +02:00
class Object
int foo(int abba)
abba = 5
2 + 5
2014-07-15 17:27:13 +02:00
end
end
HERE
2015-10-07 09:19:25 +02:00
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
2014-07-14 15:19:47 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-10-07 09:19:25 +02:00
def test_function_if
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
2015-10-07 09:19:25 +02:00
class Object
int ofthen(int n)
if(0)
int isit = 42
else
int maybenot = 667
end
2014-07-14 10:29:38 +02:00
end
end
HERE
2015-10-07 09:19:25 +02:00
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
2015-05-06 14:15:53 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-07-18 18:02:54 +02:00
def test_while
@string_input = <<HERE
class Object
int foo()
while(1)
3
end
end
2015-07-18 18:02:54 +02:00
end
HERE
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
2015-07-18 18:02:54 +02:00
check
end
2015-10-07 09:19:25 +02:00
def test_function_while
@string_input = <<HERE
2015-10-07 09:19:25 +02:00
class Object
int fibonaccit(int n)
int a = 0
while(n)
int some = 43
int other = some * 4
end
end
end
HERE
2015-10-07 09:19:25 +02:00
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end
2015-10-07 09:19:25 +02:00
def test_function_return
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
2015-10-07 09:19:25 +02:00
class Object
int retvar(int n)
int i = 5
return i
end
2014-07-14 10:29:38 +02:00
end
HERE
2015-10-07 09:19:25 +02:00
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
2014-07-14 15:19:47 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-10-07 09:19:25 +02:00
def test_function_return_if
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
2015-10-07 09:19:25 +02:00
class Object
int retvar(int n)
if( n > 5)
return 10
else
return 20
end
2015-05-06 07:38:29 +02:00
end
2014-07-14 10:29:38 +02:00
end
HERE
2015-10-07 09:19:25 +02:00
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
2014-07-14 15:19:47 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-10-07 09:19:25 +02:00
def test_function_return_while
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
2015-10-07 09:19:25 +02:00
class Object
int retvar(int n)
while( n > 5) do
n = n + 1
return n
end
2015-05-06 07:38:29 +02:00
end
2014-07-14 10:29:38 +02:00
end
HERE
2015-10-07 09:19:25 +02:00
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
2014-07-14 15:19:47 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-10-07 09:19:25 +02:00
def test_function_big_while
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
2015-10-07 09:19:25 +02:00
class Object
int fibonaccit(int n)
int a = 0
int b = 1
while( n > 1 ) do
int tmp = a
a = b
b = tmp + b
puts(b)
n = n - 1
end
2014-07-14 10:29:38 +02:00
end
end
HERE
2015-10-07 09:19:25 +02:00
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
2014-07-14 15:19:47 +02:00
check
2015-05-06 07:38:29 +02:00
end
end
2015-07-18 14:28:57 +02:00
end