Tests in abitest.c now work on Win32.
[tinycc.git] / tests / abitest.c
blob90f4cb86a2e02b5dcd49b53ed798525737cae29f
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;
41 * reg_pack_test: return a small struct which should be packed into
42 * registers (Win32) during return.
44 typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;
45 typedef reg_pack_test_type (*reg_pack_test_function_type) (reg_pack_test_type);
47 static int reg_pack_test_callback(void *ptr) {
48 reg_pack_test_function_type f = (reg_pack_test_function_type)ptr;
49 reg_pack_test_type a = {10, 35};
50 reg_pack_test_type r;
51 r = f(a);
52 return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1;
55 static int reg_pack_test(void) {
56 const char *src =
57 "typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;"
58 "reg_pack_test_type f(reg_pack_test_type a) {\n"
59 " reg_pack_test_type r = {a.x*5, a.y*3};\n"
60 " return r;\n"
61 "}\n";
63 return run_callback(src, reg_pack_test_callback);
67 * sret_test: Create a struct large enough to be returned via sret
68 * (hidden pointer as first function argument)
70 typedef struct sret_test_type_s {
71 long long a;
72 long long b;
73 } sret_test_type;
75 typedef sret_test_type (*sret_test_function_type) (sret_test_type);
77 static int sret_test_callback(void *ptr) {
78 sret_test_function_type f = (sret_test_function_type)(ptr);
79 sret_test_type x = {5436LL, 658277698LL};
80 sret_test_type r = f(x);
81 return ((r.a==x.a*35)&&(r.b==x.b*19)) ? 0 : -1;
84 static int sret_test(void) {
85 const char *src =
86 "typedef struct sret_test_type_s {\n"
87 " long long a;\n"
88 " long long b;\n"
89 "} sret_test_type;\n"
90 "sret_test_type f(sret_test_type x) {\n"
91 " sret_test_type r = {x.a*35, x.b*19};\n"
92 " return r;\n"
93 "}\n";
95 return run_callback(src, sret_test_callback);
98 #define RUN_TEST(t) \
99 do { \
100 fputs(#t "... ", stdout); \
101 fflush(stdout); \
102 if (t() == 0) { \
103 fputs("success\n", stdout); \
104 } else { \
105 fputs("failure\n", stdout); \
106 retval = EXIT_FAILURE; \
108 } while (0);
110 int main(int argc, char **argv) {
111 int retval = EXIT_SUCCESS;
112 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
113 if (argc == 2 && !memcmp(argv[1], "lib_path=",9))
114 tccdir = argv[1] + 9;
116 RUN_TEST(reg_pack_test);
117 RUN_TEST(sret_test);
118 return retval;