Added ABI compatibility tests with native compiler using libtcc.
[tinycc.git] / tests / abitest.c
blob51a30ba7be71c6f79f235156b3a77d2d75f9d259
1 #include <libtcc.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
6 static const char *tccdir = NULL;
8 typedef int (*callback_type) (void*);
11 * Compile source code and call a callback with a pointer to the symbol "f".
13 static int run_callback(const char *src, callback_type callback) {
14 TCCState *s;
15 int result;
16 void *ptr;
18 s = tcc_new();
19 if (!s)
20 return -1;
21 if (tccdir)
22 tcc_set_lib_path(s, tccdir);
23 if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1)
24 return -1;
25 if (tcc_compile_string(s, src) == -1)
26 return -1;
27 if (tcc_relocate(s, TCC_RELOCATE_AUTO) == -1)
28 return -1;
30 ptr = tcc_get_symbol(s, "f");
31 if (!ptr)
32 return -1;
33 result = callback(ptr);
35 tcc_delete(s);
37 return result;
40 typedef struct test1_type_s {int x, y;} test1_type;
41 typedef test1_type (*test1_function_type) ();
43 static int test1_callback(void *ptr) {
44 test1_type r;
45 r = ((test1_function_type)ptr)();
46 return ((r.x == 10) && (r.y == 35)) ? 0 : -1;
49 static int test1() {
50 const char *src =
51 "typedef struct test1_type_s {int x, y;} test1_type;"
52 "test1_type f() {\n"
53 " test1_type r = {10, 35};\n"
54 " return r;\n"
55 "}\n";
57 return run_callback(src, test1_callback);
60 #define RUN_TEST(t) \
61 do { \
62 fputs(#t "... ", stdout); \
63 fflush(stdout); \
64 if (t() == 0) { \
65 fputs("success\n", stdout); \
66 } else { \
67 fputs("failure\n", stdout); \
68 retval = EXIT_FAILURE; \
69 } \
70 } while (0);
72 int main(int argc, char **argv) {
73 int retval = EXIT_SUCCESS;
74 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
75 if (argc == 2 && !memcmp(argv[1], "lib_path=",9))
76 tccdir = argv[1] + 9;
78 RUN_TEST(test1);
79 return retval;