X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=support%2Fregression%2Ftests%2Ffunptrs.c;h=546eb6b57c22170d210c36508d87a5be7db7bdab;hb=f3a44fc094dc5ce393e2029d3c7c6587a4b1a835;hp=3e584cf5269ebcc60e59bcc23543cdfc0fa9327d;hpb=ab0dc591b7c0b233ddd162a81b736381a65a2d4d;p=fw%2Fsdcc diff --git a/support/regression/tests/funptrs.c b/support/regression/tests/funptrs.c index 3e584cf5..546eb6b5 100644 --- a/support/regression/tests/funptrs.c +++ b/support/regression/tests/funptrs.c @@ -1,14 +1,27 @@ /** Function pointer tests. + + type: BOOL, char, int, long */ #include +#include + +#ifndef BOOL +#define BOOL bool +#endif + +#define TYPE_{type} /* Must use a typedef as there is no way of adding the code modifier on the z80. */ typedef void (*NOARGFUNPTR)(void); -typedef void (*ONEARGFUNPTR)(int) REENTRANT; +typedef void (*ONEARGFUNPTR)({type}) REENTRANT; +typedef long int (*FOURARGFUNPTR)(char, char, long int, long int) REENTRANT; +typedef {type} (*TYPEFUNPTR)({type}, {type}) REENTRANT; int count; +FOURARGFUNPTR fafp; +TYPEFUNPTR tfp; void incCount(void) @@ -17,11 +30,24 @@ incCount(void) } void -incBy(int a) REENTRANT +incBy({type} a) REENTRANT { count += a; } +long int f6(char a, char b, long int c, long int d) REENTRANT +{ + switch (a) + { + case 0: return a; + case 1: return b; + case 2: return c; + case 3: return d; + } + return 0; +} + + void callViaPtr(NOARGFUNPTR fptr) { @@ -29,7 +55,7 @@ callViaPtr(NOARGFUNPTR fptr) } void -callViaPtr2(ONEARGFUNPTR fptr, int arg) +callViaPtr2(ONEARGFUNPTR fptr, {type} arg) { (*fptr)(arg); } @@ -40,12 +66,75 @@ callViaPtr3(void (*fptr)(void)) (*fptr)(); } +void +callViaPtrAnsi(NOARGFUNPTR fptr) +{ + fptr(); +} + +void +callViaPtr2Ansi(ONEARGFUNPTR fptr, {type} arg) +{ + fptr(arg); +} + +void +callViaPtr3Ansi(void (*fptr)(void)) +{ + fptr(); +} + +{type} f_ret({type} arg1, {type} arg2) REENTRANT +{ + {type} local; + local = !arg1; + return (local & arg2); +} + + + void testFunPtr(void) { + fafp = f6; + ASSERT(count == 0); callViaPtr(incCount); ASSERT(count == 1); callViaPtr2(incBy, 7); - ASSERT(count == 8); + ASSERT(count == 8 || count == 2); + + ASSERT((*fafp)(0, 0x55, 0x12345678, 0x9abcdef0) == 0); + ASSERT((*fafp)(1, 0x55, 0x12345678, 0x9abcdef0) == 0x55); + ASSERT((*fafp)(2, 0x55, 0x12345678, 0x9abcdef0) == 0x12345678); + ASSERT((*fafp)(3, 0x55, 0x12345678, 0x9abcdef0) == 0x9abcdef0); } + +void +testFunPtrAnsi(void) +{ + fafp = f6; + + count = 0; + callViaPtrAnsi(incCount); + ASSERT(count == 1); + callViaPtr2Ansi(incBy, 7); + ASSERT(count == 8 || count == 2); + + ASSERT(fafp(0, 0x55, 0x12345678, 0x9abcdef0) == 0); + ASSERT(fafp(1, 0x55, 0x12345678, 0x9abcdef0) == 0x55); + ASSERT(fafp(2, 0x55, 0x12345678, 0x9abcdef0) == 0x12345678); + ASSERT(fafp(3, 0x55, 0x12345678, 0x9abcdef0) == 0x9abcdef0); +} + +void +testFunPtrReturn(void) +{ + tfp = f_ret; + + ASSERT(tfp(0, 0) == 0); + ASSERT(tfp(0, 1) == 1); + ASSERT(tfp(1, 0) == 0); + ASSERT(tfp(1, 1) == 0); +} +