Added test for recursive calls
This commit is contained in:
2
makefile
2
makefile
@@ -18,7 +18,7 @@ runTest: ./src/test/test
|
||||
./src/test/test
|
||||
|
||||
./src/test/test: ./src/test/tests.s
|
||||
gcc -o ./src/test/test ./src/test/tests.s ./src/test/functionCall/functionCall.c ./src/test/testCode.c
|
||||
gcc -o ./src/test/test ./src/test/tests.s ./src/test/functionCall/functionCall.c ./src/test/recursive/recursive.c ./src/test/testCode.c
|
||||
|
||||
./src/test/tests.s: target/klang-1.0-jar-with-dependencies.jar
|
||||
java -cp target/klang-1.0-jar-with-dependencies.jar de.hsrm.compiler.Klang.Klang < ./src/test/tests.k > ./src/test/tests.s
|
||||
|
||||
28
src/test/recursive/recursive.c
Normal file
28
src/test/recursive/recursive.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include "recursive.h"
|
||||
|
||||
void printRecSuccess(char* name, int x, int expected, int result) {
|
||||
printf("SUCCESS:\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected);
|
||||
}
|
||||
|
||||
void printRecError(char* name, int x, int expected, int result) {
|
||||
printf("ERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\n", name, x, result, expected);
|
||||
}
|
||||
|
||||
int recursiveTest(char* name, int x, int expected, int result) {
|
||||
if (expected == result) {
|
||||
printRecSuccess(name, x, expected, result);
|
||||
return 0;
|
||||
} else {
|
||||
printRecError(name, x, expected, result);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int runRecursiveTests() {
|
||||
printf("\nRecursive Tests \n");
|
||||
int failed = 0;
|
||||
failed += recursiveTest("fac", 5, 120, fac(5));
|
||||
|
||||
return failed;
|
||||
}
|
||||
1
src/test/recursive/recursive.h
Normal file
1
src/test/recursive/recursive.h
Normal file
@@ -0,0 +1 @@
|
||||
int fac(int x);
|
||||
@@ -105,14 +105,18 @@ int main(){
|
||||
failed += testOneArg("id", cId, id, -1);
|
||||
failed += testOneArg("id", cId, id, 15);
|
||||
|
||||
// Test for passing arguments to functions
|
||||
// Tests for passing arguments to functions
|
||||
failed += runFunctionCallTests();
|
||||
|
||||
// Tests for recursive funtions
|
||||
failed += runRecursiveTests();
|
||||
|
||||
printf("\n=== Failed Tests: %d\n", failed);
|
||||
|
||||
if (failed > 0) {
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
Alle Test Module
|
||||
*/
|
||||
|
||||
int runFunctionCallTests ();
|
||||
int runFunctionCallTests();
|
||||
int runRecursiveTests();
|
||||
@@ -95,4 +95,11 @@ function get10() {
|
||||
return arg10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
}
|
||||
|
||||
function fac(x) {
|
||||
if (x) {
|
||||
return (x * fac((x - 1)));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
add(1, 1);
|
||||
|
||||
Reference in New Issue
Block a user