|
| 1 | +/*_ exe4.c */ |
| 2 | +/* Copyright (C) 1986-1992 by Walter Bright */ |
| 3 | +/* All Rights Reserved */ |
| 4 | +/* Test alignment (compile with alignment off (-a)) */ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <assert.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#undef strlen |
| 12 | +#define __CLIB |
| 13 | + |
| 14 | +typedef int (*PFI) (void); |
| 15 | +typedef int (__CLIB *PFI2) (void); |
| 16 | +typedef unsigned (__CLIB *PFC) (const char *); |
| 17 | + |
| 18 | +int func1(void) { return 51; } |
| 19 | +static int func2(void) { return 52; } |
| 20 | +int func4(void),func5(void),atoi(const char*); |
| 21 | + |
| 22 | +int callpfunc(PFC pf,char *s); |
| 23 | + |
| 24 | +PFI array[] = {func1, func2, (PFI) strlen, func4}; |
| 25 | + |
| 26 | +void testpfunc() |
| 27 | +{ |
| 28 | + static int (*pfunc[])(void) = {func1,func2,(PFI)strlen,func4,func5,(PFI)atoi}; |
| 29 | + static char L123[] = "123",L56[] = "56"; |
| 30 | + int (__CLIB *pf)(void),i; |
| 31 | + int (*pf2)(void); |
| 32 | + PFI get_func(); |
| 33 | + |
| 34 | + i = 0x10; |
| 35 | + assert(func1() == 51); |
| 36 | + assert(func2() == 52); |
| 37 | + assert(strlen(L123) == 3); |
| 38 | + assert(func4() == 54); |
| 39 | + assert(func5() == 55); |
| 40 | + assert(atoi(L56) == 56); |
| 41 | + |
| 42 | + assert((*pfunc[0])() == 51); |
| 43 | + assert((*pfunc[1])() == 52); |
| 44 | + assert((*(PFC)(pfunc[2]))(L123) == 3); |
| 45 | + assert((*pfunc[3])() == 54); |
| 46 | + assert((*pfunc[4])() == 55); |
| 47 | + assert((*(PFC)(pfunc[5]))(L56) == 56); |
| 48 | + |
| 49 | + //pf = (PFI2) strlen + 0x10; |
| 50 | + //assert((*((int (*)(char *))((char *)pf - i)))(L123) == 3); |
| 51 | + //pf -= i; |
| 52 | + //callpfunc((PFC)pf,"123"); |
| 53 | + //callpfunc(strlen,"123"); |
| 54 | + |
| 55 | + pf2 = get_func (); |
| 56 | + i = (*pf2) (); |
| 57 | + assert(i == 54); |
| 58 | + i = 0; |
| 59 | + i = pf2(); |
| 60 | + assert(i == 54); |
| 61 | + |
| 62 | + pf2 = &func1; /* try alternative syntax */ |
| 63 | + assert(pf2() == 51); |
| 64 | +} |
| 65 | + |
| 66 | +/*static*/ int func4() { return 54; } // the static should work |
| 67 | +int func5() { return 55; } |
| 68 | + |
| 69 | +int callpfunc(pf,s) |
| 70 | +PFC pf; |
| 71 | +char *s; |
| 72 | +{ |
| 73 | + assert((*pf)(s) == 3); |
| 74 | +} |
| 75 | + |
| 76 | +PFI get_func () |
| 77 | +{ |
| 78 | + return array[3]; |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +/***************************************/ |
| 83 | + |
| 84 | +extern void PostASM(); |
| 85 | + |
| 86 | +void testp2() |
| 87 | +{ |
| 88 | + unsigned dist; |
| 89 | + static int cnt = 1; |
| 90 | + void (* post1)(); |
| 91 | + void (* post2)(); |
| 92 | + |
| 93 | + post1 = (void(*)()) ( ((unsigned char *)PostASM) + 6 ); |
| 94 | + post2 = (void(*)()) ( ((unsigned char *)PostASM) + (6 * cnt) ); |
| 95 | +// assert(post1 == post2); // doesn't work on Win32 for some reason |
| 96 | +} |
| 97 | + |
| 98 | +void PostASM() {} |
| 99 | + |
| 100 | + |
| 101 | +/***************************************/ |
| 102 | + |
| 103 | +int main() |
| 104 | +{ |
| 105 | + printf("Test file '%s'\n",__FILE__); |
| 106 | + |
| 107 | + testpfunc(); |
| 108 | + testp2(); |
| 109 | + |
| 110 | + printf("Success\n"); |
| 111 | + return EXIT_SUCCESS; |
| 112 | +} |
0 commit comments