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-10-07 09:43:08 +02:00
|
|
|
def test_simplest_function
|
2015-07-19 09:36:06 +02:00
|
|
|
@string_input = <<HERE
|
2015-10-07 09:43:08 +02:00
|
|
|
class Object
|
|
|
|
int main()
|
2015-09-20 16:33:05 +02:00
|
|
|
return 5
|
2015-07-19 09:36:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2015-10-07 09:43:08 +02:00
|
|
|
@output = [[MethodEnter,Set] ,[MethodReturn]]
|
2015-07-19 09:36:06 +02:00
|
|
|
check
|
|
|
|
end
|
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
|
|
|
|
def test_second_simplest_function
|
2015-07-18 14:28:57 +02:00
|
|
|
@string_input = <<HERE
|
2015-10-05 23:27:13 +02:00
|
|
|
class Object
|
2015-10-07 09:43:08 +02:00
|
|
|
int main()
|
|
|
|
int x = 5
|
2015-10-05 23:27:13 +02:00
|
|
|
return x
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
2015-07-18 14:28:57 +02:00
|
|
|
HERE
|
2015-10-07 09:43:08 +02:00
|
|
|
@output = [[Virtual::MethodEnter,Set],[Virtual::MethodReturn]]
|
2015-07-18 14:28:57 +02:00
|
|
|
check
|
|
|
|
end
|
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
def test_puts_string
|
|
|
|
@string_input = <<HERE
|
2015-10-05 23:27:13 +02:00
|
|
|
class Object
|
2015-10-07 09:04:55 +02:00
|
|
|
int puts(ref str)
|
|
|
|
return str
|
|
|
|
end
|
2015-10-05 23:27:13 +02:00
|
|
|
int main()
|
|
|
|
puts("Hello")
|
|
|
|
end
|
2014-08-20 16:14:52 +02:00
|
|
|
end
|
|
|
|
HERE
|
2015-10-07 09:43:08 +02:00
|
|
|
@output = [[MethodEnter , NewMessage, Set, Set , Set, Set, MethodCall],[MethodReturn]]
|
|
|
|
check
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
def test_int_function
|
|
|
|
@string_input = <<HERE
|
2015-10-05 23:27:13 +02:00
|
|
|
class Integer < Object
|
2015-10-09 17:06:00 +02:00
|
|
|
int times(int x)
|
|
|
|
return x
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
HERE
|
2015-10-07 09:43:08 +02:00
|
|
|
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
|
|
|
|
check
|
|
|
|
cla = Virtual.machine.space.get_class_by_name :Integer
|
|
|
|
assert cla.get_instance_method( :times )
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
def test_function_ops
|
|
|
|
@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:43:08 +02:00
|
|
|
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
|
|
|
|
check
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
def test_function_if
|
|
|
|
@string_input = <<HERE
|
2015-10-07 09:19:25 +02:00
|
|
|
class Object
|
2015-10-07 09:43:08 +02:00
|
|
|
int main()
|
2015-10-07 09:19:25 +02:00
|
|
|
if(0)
|
2015-10-07 09:43:08 +02:00
|
|
|
return 42
|
2015-10-07 09:19:25 +02:00
|
|
|
else
|
2015-10-07 09:43:08 +02:00
|
|
|
return 667
|
2015-10-07 09:19:25 +02:00
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2015-10-07 09:43:08 +02:00
|
|
|
@output = [[MethodEnter,Set,Register::IsZeroBranch] , [Set,Register::AlwaysBranch],
|
|
|
|
[Set],[],[MethodReturn]]
|
|
|
|
check
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
def test_while
|
|
|
|
@string_input = <<HERE
|
2015-10-05 23:27:13 +02:00
|
|
|
class Object
|
|
|
|
int foo()
|
|
|
|
while(1)
|
|
|
|
3
|
|
|
|
end
|
|
|
|
end
|
2015-07-18 18:02:54 +02:00
|
|
|
end
|
|
|
|
HERE
|
2015-10-07 09:43:08 +02:00
|
|
|
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
|
|
|
check
|
|
|
|
end
|
2015-07-18 18:02:54 +02:00
|
|
|
|
2015-10-07 09:43:08 +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
|
2014-07-17 15:46:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2015-10-07 09:43:08 +02:00
|
|
|
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
|
|
|
check
|
|
|
|
end
|
2014-07-17 15:46:17 +02:00
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
def test_function_return
|
|
|
|
@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:43:08 +02:00
|
|
|
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
|
|
|
check
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
def test_function_return_if
|
|
|
|
@string_input = <<HERE
|
2015-10-07 09:19:25 +02:00
|
|
|
class Object
|
2015-10-07 09:43:08 +02:00
|
|
|
int main()
|
|
|
|
int n = 10
|
2015-10-07 09:19:25 +02:00
|
|
|
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:43:08 +02:00
|
|
|
@output = [[MethodEnter,Set,Set,Register::GetSlot,Register::GetSlot,
|
|
|
|
Register::OperatorInstruction,Register::IsZeroBranch],
|
|
|
|
[Set,Register::AlwaysBranch],[Set],[],[MethodReturn]]
|
|
|
|
check
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
|
2015-10-07 09:43:08 +02:00
|
|
|
def test_function_return_while
|
|
|
|
@string_input = <<HERE
|
2015-10-07 09:19:25 +02:00
|
|
|
class Object
|
2015-10-07 09:43:08 +02:00
|
|
|
int main()
|
|
|
|
int n = 10
|
|
|
|
while( n > 5)
|
2015-10-07 09:19:25 +02:00
|
|
|
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:43:08 +02:00
|
|
|
@output = [[MethodEnter,Set],
|
|
|
|
[Set,Register::GetSlot,Register::GetSlot,Register::OperatorInstruction,
|
|
|
|
Register::IsZeroBranch,Set,Register::GetSlot,Register::GetSlot,
|
|
|
|
Register::OperatorInstruction,Set,Register::AlwaysBranch] ,
|
|
|
|
[],[MethodReturn]]
|
|
|
|
check
|
2015-10-07 09:19:25 +02:00
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
end
|