2014-05-28 14:03:54 +02:00
|
|
|
require_relative 'helper'
|
|
|
|
|
|
|
|
class TestWhileFragment < MiniTest::Test
|
|
|
|
include Fragments
|
|
|
|
|
2014-06-03 21:16:57 +02:00
|
|
|
def test_while_fibo
|
2014-05-28 14:03:54 +02:00
|
|
|
@string_input = <<HERE
|
2014-05-30 09:42:43 +02:00
|
|
|
def fibonaccit(n) # n == r0
|
2014-05-28 14:03:54 +02:00
|
|
|
a = 0 # a == r1
|
|
|
|
b = 1 # b = r2
|
|
|
|
while( n > 1 ) do #BUG comment lines + comments behind function calls
|
|
|
|
tmp = a # r3 <- r1
|
|
|
|
a = b # r1 <- r2
|
|
|
|
b = tmp + b # r4 = r2 + r3 (r4 transient) r2 <- r4
|
|
|
|
n = n - 1 # r0 <- r2 for call, #call ok
|
|
|
|
end #r5 <- r0 - 1 n=n-1 through r5 tmp
|
2014-06-07 22:22:32 +02:00
|
|
|
b.putint()
|
2014-05-28 14:03:54 +02:00
|
|
|
return b
|
2014-05-30 09:42:43 +02:00
|
|
|
end # r0 <- r5
|
2014-06-07 22:22:32 +02:00
|
|
|
|
|
|
|
fibonaccit( 10 )
|
2014-05-28 14:03:54 +02:00
|
|
|
HERE
|
2014-06-12 15:23:57 +02:00
|
|
|
@should = [0x0,0x40,0x2d,0xe9,0x0,0x40,0xa0,0xe3,0x1,0x50,0xa0,0xe3,0x1,0x0,0x53,0xe3,0x4,0x0,0x0,0xda,0x4,0x60,0xa0,0xe1,0x5,0x40,0xa0,0xe1,0x5,0x50,0x86,0xe0,0x1,0x30,0x43,0xe2,0xf8,0xff,0xff,0xea,0x20,0x0,0x2d,0xe9,0x5,0x20,0xa0,0xe1,0x13,0x0,0x0,0xeb,0x20,0x0,0xbd,0xe8,0x5,0x0,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
2014-05-28 14:03:54 +02:00
|
|
|
@output = " 55 "
|
2014-06-05 12:02:36 +02:00
|
|
|
@target = [:Object , :fibonaccit]
|
2014-05-28 14:03:54 +02:00
|
|
|
parse
|
|
|
|
write "while"
|
|
|
|
end
|
2014-06-05 15:27:25 +02:00
|
|
|
|
|
|
|
# a hand coded version of the fibonachi numbers (moved to kernel to be able to call it)
|
|
|
|
# not my hand off course, found in the net from a basic introduction
|
|
|
|
def test_kernel_fibo
|
2014-06-12 15:23:57 +02:00
|
|
|
int = Vm::Integer.new(Vm::RegisterMachine.instance.receiver_register)
|
2014-06-13 22:41:45 +02:00
|
|
|
fibo = @object_space.get_or_create_class(:Object).resolve_function(:fibo)
|
2014-06-07 16:59:44 +02:00
|
|
|
main = @object_space.main
|
|
|
|
main.mov int , 10
|
2014-06-12 15:23:57 +02:00
|
|
|
main.call( fibo )
|
|
|
|
main.mov( Vm::RegisterMachine.instance.receiver_register , Vm::RegisterMachine.instance.return_register )
|
2014-06-13 22:41:45 +02:00
|
|
|
putint = @object_space.get_or_create_class(:Object).resolve_function(:putint)
|
2014-06-12 15:23:57 +02:00
|
|
|
main.call( putint )
|
|
|
|
@should = [0x0,0x40,0x2d,0xe9,0x1,0x0,0x52,0xe3,0x2,0x0,0xa0,0xd1,0x7,0x0,0x0,0xda,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x42,0xe2,0x1,0x0,0x52,0xe3,0xfa,0xff,0xff,0x1a,0x3,0x0,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
2014-06-05 15:27:25 +02:00
|
|
|
@target = [:Object , :fibo]
|
|
|
|
write "fibo"
|
|
|
|
end
|
|
|
|
|
2014-05-28 14:03:54 +02:00
|
|
|
end
|
|
|
|
|