boppi/doc/report-test-program.tex

86 lines
2.3 KiB
TeX

% Extended test program. The listing of one (correct) extended test program, as well as the generated
% target code for that program and one or more example executions showing the correct functioning of
% the generated code.
As an example of a test program, we will look at a memoizing recursive fibonacci program. This program is well-suited because it contains I/O, variables, loops, functions, arrays, function references and closures. See \cref{test-example} for the source code and the text file \verb|doc/fibonacciRecursiveExample.iloc.txt| for the generated ILOC. The compiled form is quite long, mostly because it heavily increments and decrements reference counts and does so in-line and without optimizations.\\
The program works by repeatedly asking for a number. If the user provides a positive number, it returns the value of the Fibonacci sequence at that position. If the user provides a zero or negative number, the program terminates. See \cref{test-example-runs} for a few runs of the program.
\begin{figure}
\caption{Source code of \emph{fibonacciRecursive.boppi}}
\label{test-example}
\begin{minted}{boppi}
function (int)->int memoizedFib() {
var int[] memo;
memo := array(int, 50, 0);
function int fib(int n) {
if n < 1 || n > 46 then
0
else
if n < 2 then
1
else
if memo[n] > 0 then
memo[n]
else
memo[n] := fib(n-1)+fib(n-2)
fi
fi
fi
};
fib
};
var (int)->int myFib;
myFib := memoizedFib();
var int n;
while read(n) > 0 do
print(myFib(n))
od;
\end{minted}
\end{figure}
\begin{figure}
\caption{Examples of input and output on \emph{fibonacciRecursive}.}
\label{test-example-runs}
\begin{subfigure}{0.2\textwidth}
\begin{minted}{text}
> 1
<<< 1
> 0
\end{minted}
\end{subfigure}
\hfill
\begin{subfigure}{0.2\textwidth}
\begin{minted}{text}
> -5
\end{minted}
\end{subfigure}
\hfill
\begin{subfigure}{0.2\textwidth}
\begin{minted}{text}
> 3
<<< 2
> 4
<<< 3
> 5
<<< 5
> 6
<<< 8
> 0
\end{minted}
\end{subfigure}
\hfill
\begin{subfigure}{0.2\textwidth}
\begin{minted}{text}
> 46
<<< 1836311903
> 47
<<< 0
> -1337
\end{minted}
\end{subfigure}
\end{figure}