From 35de3c7de4256552d83542d0095208514025798b Mon Sep 17 00:00:00 2001 From: nitrix Date: Mon, 9 Mar 2020 17:10:50 +0100 Subject: [PATCH] implement test for tail call optimization --- src/test/functionCall/functionCall.c | 24 ++++++ src/test/functionCall/functionCall.h | 22 +++++ src/test/test.k | 121 +++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) diff --git a/src/test/functionCall/functionCall.c b/src/test/functionCall/functionCall.c index c438ddf..73f23bb 100644 --- a/src/test/functionCall/functionCall.c +++ b/src/test/functionCall/functionCall.c @@ -92,4 +92,28 @@ int runFunctionCallTests () { argumentTest("fgetMix8(...args)", 8, fgetMix8()); argumentTest_f("fgetMix9(...args)", 9.0, fgetMix9()); argumentTest("fgetMix10(...args)", 10, fgetMix10()); + + printf("\nTail Call Tests \n"); + // Checks that tails calls are properly invoked + argumentTest("arg1Tail(...args)", 1, arg1Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg2Tail(...args)", 2, arg2Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg3Tail(...args)", 3, arg3Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg4Tail(...args)", 4, arg4Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg5Tail(...args)", 5, arg5Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg6Tail(...args)", 6, arg6Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg7Tail(...args)", 7, arg7Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg8Tail(...args)", 8, arg8Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg9Tail(...args)", 9, arg9Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + argumentTest("arg10Tail(...args)", 10, arg10Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)); + // Checks that parameters are correctly passed from klang to functions + argumentTest("get1Tail(...args)", 1, get1Tail(10)); + argumentTest("get2Tail(...args)", 2, get2Tail(10)); + argumentTest("get3Tail(...args)", 3, get3Tail(10)); + argumentTest("get4Tail(...args)", 4, get4Tail(10)); + argumentTest("get5Tail(...args)", 5, get5Tail(10)); + argumentTest("get6Tail(...args)", 6, get6Tail(10)); + argumentTest("get7Tail(...args)", 7, get7Tail(10)); + argumentTest("get8Tail(...args)", 8, get8Tail(10)); + argumentTest("get9Tail(...args)", 9, get9Tail(10)); + argumentTest("get10Tail(...args)", 10, get10Tail(10)); } \ No newline at end of file diff --git a/src/test/functionCall/functionCall.h b/src/test/functionCall/functionCall.h index 7aac291..5238453 100644 --- a/src/test/functionCall/functionCall.h +++ b/src/test/functionCall/functionCall.h @@ -20,6 +20,28 @@ long get8(); long get9(); long get10(); +long arg1Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg2Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg3Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg4Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg5Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg6Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg7Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg8Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg9Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); +long arg10Tail(long a, long b, long c, long d, long e, long f, long g, long h, long i, long j, long count); + +long get1Tail(long count); +long get2Tail(long count); +long get3Tail(long count); +long get4Tail(long count); +long get5Tail(long count); +long get6Tail(long count); +long get7Tail(long count); +long get8Tail(long count); +long get9Tail(long count); +long get10Tail(long count); + double farg1(double a, double b, double c, double d, double e, double f, double g, double h, double i, double j); double farg2(double a, double b, double c, double d, double e, double f, double g, double h, double i, double j); double farg3(double a, double b, double c, double d, double e, double f, double g, double h, double i, double j); diff --git a/src/test/test.k b/src/test/test.k index 395afa2..5f24c1d 100644 --- a/src/test/test.k +++ b/src/test/test.k @@ -106,6 +106,127 @@ function get10(): int { return arg10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } +// TAIL CALL +function arg1Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return a; + } + + return arg1Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get1Tail(count: int): int { + return arg1Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg2Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return b; + } + + return arg2Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get2Tail(count: int): int { + return arg2Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg3Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return c; + } + + return arg3Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get3Tail(count: int): int { + return arg3Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg4Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return d; + } + + return arg4Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get4Tail(count: int): int { + return arg4Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg5Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return e; + } + + return arg5Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get5Tail(count: int): int { + return arg5Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg6Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return f; + } + + return arg6Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get6Tail(count: int): int { + return arg6Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg7Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return g; + } + + return arg7Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get7Tail(count: int): int { + return arg7Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg8Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return h; + } + + return arg8Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get8Tail(count: int): int { + return arg8Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg9Tail(a: int, b: int, c: int,d: int,e: int,f: int,g: int, h: int,i: int,j: int, count: int): int { + if (count <= 0) { + return i; + } + + return arg9Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get9Tail(count: int): int { + return arg9Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + +function arg10Tail(a: int, b: int, c: int, d: int, e: int, f: int, g: int, h: int, i: int, j: int, count: int): int { + if (count <= 0) { + return j; + } + + return arg10Tail(a, b, c, d, e, f, g, h, i, j, count - 1); +} + +function get10Tail(count: int): int { + return arg10Tail(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10); +} + // FLOATS function farg1(a: float, b: float, c: float, d: float, e: float, f: float, g: float, h: float, i: float ,j: float): float {