a small revers for bcheck.o changes (d80593bc4d43)
[tinycc.git] / tests / tcctest.c
blob49fa587fb1e71726426f3d3a0ead0b00f096d0fc
1 /*
2 * TCC auto test program
3 */
4 #include "config.h"
6 #if GCC_MAJOR >= 3
8 /* Unfortunately, gcc version < 3 does not handle that! */
9 #define ALL_ISOC99
11 /* only gcc 3 handles _Bool correctly */
12 #define BOOL_ISOC99
14 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
15 #define CORRECT_CR_HANDLING
17 #endif
19 #if defined(_WIN32)
20 #define LONG_LONG_FORMAT "%lld"
21 #define ULONG_LONG_FORMAT "%llu"
22 #else
23 #define LONG_LONG_FORMAT "%Ld"
24 #define ULONG_LONG_FORMAT "%Lu"
25 #endif
27 // MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC
28 #if defined(_WIN32) && defined(__GNUC__)
29 #define LONG_DOUBLE double
30 #define LONG_DOUBLE_LITERAL(x) x
31 #else
32 #define LONG_DOUBLE long double
33 #define LONG_DOUBLE_LITERAL(x) x ## L
34 #endif
36 /* deprecated and no longer supported in gcc 3.3 */
37 //#define ACCEPT_CR_IN_STRINGS
39 /* __VA_ARGS__ and __func__ support */
40 #define C99_MACROS
42 /* test various include syntaxes */
44 #define TCCLIB_INC <tcclib.h>
45 #define TCCLIB_INC1 <tcclib
46 #define TCCLIB_INC2 h>
47 #define TCCLIB_INC3 "tcclib"
49 #include TCCLIB_INC
51 #include TCCLIB_INC1.TCCLIB_INC2
53 #include TCCLIB_INC1.h>
55 /* gcc 3.2 does not accept that (bug ?) */
56 //#include TCCLIB_INC3 ".h"
58 #include <tcclib.h>
60 #include "tcclib.h"
62 void intdiv_test();
63 void string_test();
64 void expr_test();
65 void macro_test();
66 void recursive_macro_test();
67 void scope_test();
68 void forward_test();
69 void funcptr_test();
70 void loop_test();
71 void switch_test();
72 void goto_test();
73 void enum_test();
74 void typedef_test();
75 void struct_test();
76 void array_test();
77 void expr_ptr_test();
78 void bool_test();
79 void expr2_test();
80 void constant_expr_test();
81 void expr_cmp_test();
82 void char_short_test();
83 void init_test(void);
84 void compound_literal_test(void);
85 int kr_test();
86 void struct_assign_test(void);
87 void cast_test(void);
88 void bitfield_test(void);
89 void c99_bool_test(void);
90 void float_test(void);
91 void longlong_test(void);
92 void manyarg_test(void);
93 void stdarg_test(void);
94 void whitespace_test(void);
95 void relocation_test(void);
96 void old_style_function(void);
97 void alloca_test(void);
98 void c99_vla_test(int size1, int size2);
99 void sizeof_test(void);
100 void typeof_test(void);
101 void local_label_test(void);
102 void statement_expr_test(void);
103 void asm_test(void);
104 void builtin_test(void);
105 void weak_test(void);
106 void global_data_test(void);
107 void cmp_comparison_test(void);
108 void math_cmp_test(void);
109 void callsave_test(void);
110 void builtin_frame_address_test(void);
112 int fib(int n);
113 void num(int n);
114 void forward_ref(void);
115 int isid(int c);
117 #define A 2
118 #define N 1234 + A
119 #define pf printf
120 #define M1(a, b) (a) + (b)
122 #define str\
123 (s) # s
124 #define glue(a, b) a ## b
125 #define xglue(a, b) glue(a, b)
126 #define HIGHLOW "hello"
127 #define LOW LOW ", world"
129 static int onetwothree = 123;
130 #define onetwothree4 onetwothree
131 #define onetwothree xglue(onetwothree,4)
133 #define min(a, b) ((a) < (b) ? (a) : (b))
135 #ifdef C99_MACROS
136 #define dprintf(level,...) printf(__VA_ARGS__)
137 #endif
139 /* gcc vararg macros */
140 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
142 #define MACRO_NOARGS()
144 #define AAA 3
145 #undef AAA
146 #define AAA 4
148 #if 1
149 #define B3 1
150 #elif 1
151 #define B3 2
152 #elif 0
153 #define B3 3
154 #else
155 #define B3 4
156 #endif
158 #define __INT64_C(c) c ## LL
159 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
161 int qq(int x)
163 return x + 40;
165 #define qq(x) x
167 #define spin_lock(lock) do { } while (0)
168 #define wq_spin_lock spin_lock
169 #define TEST2() wq_spin_lock(a)
171 #define UINT_MAX ((unsigned) -1)
173 void intdiv_test(void)
175 printf("18/21=%u\n", 18/21);
176 printf("18%21=%u\n", 18%21);
177 printf("41/21=%u\n", 41/21);
178 printf("41%21=%u\n", 41%21);
179 printf("42/21=%u\n", 42/21);
180 printf("42%21=%u\n", 42%21);
181 printf("43/21=%u\n", 43/21);
182 printf("43%21=%u\n", 43%21);
183 printf("126/21=%u\n", 126/21);
184 printf("12%/21=%u\n", 126%21);
185 printf("131/21=%u\n", 131/21);
186 printf("131%21=%u\n", 131%21);
187 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
188 printf("(UINT_MAX/2+3)%2=%u\n", (UINT_MAX/2+3)%2);
190 printf("18/-21=%u\n", 18/-21);
191 printf("18%-21=%u\n", 18%-21);
192 printf("41/-21=%u\n", 41/-21);
193 printf("41%-21=%u\n", 41%-21);
194 printf("42/-21=%u\n", 42/-21);
195 printf("42%-21=%u\n", 42%-21);
196 printf("43/-21=%u\n", 43/-21);
197 printf("43%-21=%u\n", 43%-21);
198 printf("126/-21=%u\n", 126/-21);
199 printf("12%/-21=%u\n", 126%-21);
200 printf("131/-21=%u\n", 131/-21);
201 printf("131%-21=%u\n", 131%-21);
202 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
203 printf("(UINT_MAX/2+3)%-2=%u\n", (UINT_MAX/2+3)%-2);
205 printf("-18/21=%u\n", -18/21);
206 printf("-18%21=%u\n", -18%21);
207 printf("-41/21=%u\n", -41/21);
208 printf("-41%21=%u\n", -41%21);
209 printf("-42/21=%u\n", -42/21);
210 printf("-42%21=%u\n", -42%21);
211 printf("-43/21=%u\n", -43/21);
212 printf("-43%21=%u\n", -43%21);
213 printf("-126/21=%u\n", -126/21);
214 printf("-12%/21=%u\n", -126%21);
215 printf("-131/21=%u\n", -131/21);
216 printf("-131%21=%u\n", -131%21);
217 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
218 printf("-(UINT_MAX/2+3)%2=%u\n", (0-(UINT_MAX/2+3))%2);
220 printf("-18/-21=%u\n", -18/-21);
221 printf("-18%-21=%u\n", -18%-21);
222 printf("-41/-21=%u\n", -41/-21);
223 printf("-41%-21=%u\n", -41%-21);
224 printf("-42/-21=%u\n", -42/-21);
225 printf("-42%-21=%u\n", -42%-21);
226 printf("-43/-21=%u\n", -43/-21);
227 printf("-43%-21=%u\n", -43%-21);
228 printf("-126/-21=%u\n", -126/-21);
229 printf("-12%/-21=%u\n", -126%-21);
230 printf("-131/-21=%u\n", -131/-21);
231 printf("-131%-21=%u\n", -131%-21);
232 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
233 printf("-(UINT_MAX/2+3)%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
236 void macro_test(void)
238 printf("macro:\n");\f\v
239 pf("N=%d\n", N);
240 printf("aaa=%d\n", AAA);
242 printf("min=%d\n", min(1, min(2, -1)));
244 printf("s1=%s\n", glue(HIGH, LOW));
245 printf("s2=%s\n", xglue(HIGH, LOW));
246 printf("s3=%s\n", str("c"));
247 printf("s4=%s\n", str(a1));
248 printf("B3=%d\n", B3);
250 printf("onetwothree=%d\n", onetwothree);
252 #ifdef A
253 printf("A defined\n");
254 #endif
255 #ifdef B
256 printf("B defined\n");
257 #endif
258 #ifdef A
259 printf("A defined\n");
260 #else
261 printf("A not defined\n");
262 #endif
263 #ifdef B
264 printf("B defined\n");
265 #else
266 printf("B not defined\n");
267 #endif
269 #ifdef A
270 printf("A defined\n");
271 #ifdef B
272 printf("B1 defined\n");
273 #else
274 printf("B1 not defined\n");
275 #endif
276 #else
277 printf("A not defined\n");
278 #ifdef B
279 printf("B2 defined\n");
280 #else
281 printf("B2 not defined\n");
282 #endif
283 #endif
285 #if 1+1
286 printf("test true1\n");
287 #endif
288 #if 0
289 printf("test true2\n");
290 #endif
291 #if 1-1
292 printf("test true3\n");
293 #endif
294 #if defined(A)
295 printf("test trueA\n");
296 #endif
297 #if defined(B)
298 printf("test trueB\n");
299 #endif
301 #if 0
302 printf("test 0\n");
303 #elif 0
304 printf("test 1\n");
305 #elif 2
306 printf("test 2\n");
307 #else
308 printf("test 3\n");
309 #endif
311 MACRO_NOARGS();
313 #ifdef __LINE__
314 printf("__LINE__ defined\n");
315 #endif
317 printf("__LINE__=%d __FILE__=%s\n",
318 __LINE__, __FILE__);
319 #line 200
320 printf("__LINE__=%d __FILE__=%s\n",
321 __LINE__, __FILE__);
322 #line 203 "test"
323 printf("__LINE__=%d __FILE__=%s\n",
324 __LINE__, __FILE__);
325 #line 227 "tcctest.c"
327 /* not strictly preprocessor, but we test it there */
328 #ifdef C99_MACROS
329 printf("__func__ = %s\n", __func__);
330 dprintf(1, "vaarg=%d\n", 1);
331 #endif
332 dprintf1(1, "vaarg1\n");
333 dprintf1(1, "vaarg1=%d\n", 2);
334 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
336 /* gcc extension */
337 printf("func='%s'\n", __FUNCTION__);
339 /* complicated macros in glibc */
340 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
342 int a;
343 a = 1;
344 glue(a+, +);
345 printf("a=%d\n", a);
346 glue(a <, <= 2);
347 printf("a=%d\n", a);
350 /* macro function with argument outside the macro string */
351 #define MF_s MF_hello
352 #define MF_hello(msg) printf("%s\n",msg)
354 #define MF_t printf("tralala\n"); MF_hello
356 MF_s("hi");
357 MF_t("hi");
359 /* test macro substituion inside args (should not eat stream) */
360 printf("qq=%d\n", qq(qq)(2));
362 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
363 null argument without a space. gcc 3.2 fixes that. */
365 #define qq1(x) 1
366 printf("qq1=%d\n", qq1( ));
368 /* comment with stray handling *\
370 /* this is a valid *\/ comment */
371 /* this is a valid comment *\*/
372 // this is a valid\
373 comment
375 /* test function macro substitution when the function name is
376 substituted */
377 TEST2();
379 /* And again when the name and parenthes are separated by a
380 comment. */
381 TEST2 /* the comment */ ();
385 static void print_num(char *fn, int line, int num) {
386 printf("fn %s, line %d, num %d\n", fn, line, num);
389 void recursive_macro_test(void)
392 #define ELF32_ST_TYPE(val) ((val) & 0xf)
393 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
394 #define STB_WEAK 2 /* Weak symbol */
395 #define ELFW(type) ELF##32##_##type
396 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
398 #define WRAP(x) x
400 #define print_num(x) print_num(__FILE__,__LINE__,x)
401 print_num(123);
402 WRAP(print_num(123));
403 WRAP(WRAP(print_num(123)));
405 static struct recursive_macro { int rm_field; } G;
406 #define rm_field (G.rm_field)
407 printf("rm_field = %d\n", rm_field);
408 printf("rm_field = %d\n", WRAP(rm_field));
409 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
412 int op(a,b)
414 return a / b;
417 int ret(a)
419 if (a == 2)
420 return 1;
421 if (a == 3)
422 return 2;
423 return 0;
426 void ps(const char *s)
428 int c;
429 while (1) {
430 c = *s;
431 if (c == 0)
432 break;
433 printf("%c", c);
434 s++;
438 const char foo1_string[] = "\
439 bar\n\
440 test\14\
443 void string_test()
445 unsigned int b;
446 printf("string:\n");
447 printf("\141\1423\143\n");/* dezdez test */
448 printf("\x41\x42\x43\x3a\n");
449 printf("c=%c\n", 'r');
450 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
451 printf("foo1_string='%s'\n", foo1_string);
452 #if 0
453 printf("wstring=%S\n", L"abc");
454 printf("wstring=%S\n", L"abc" L"def" "ghi");
455 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
456 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
457 #endif
458 ps("test\n");
459 b = 32;
460 while ((b = b + 1) < 96) {
461 printf("%c", b);
463 printf("\n");
464 printf("fib=%d\n", fib(33));
465 b = 262144;
466 while (b != 0x80000000) {
467 num(b);
468 b = b * 2;
472 void loop_test()
474 int i;
475 i = 0;
476 while (i < 10)
477 printf("%d", i++);
478 printf("\n");
479 for(i = 0; i < 10;i++)
480 printf("%d", i);
481 printf("\n");
482 i = 0;
483 do {
484 printf("%d", i++);
485 } while (i < 10);
486 printf("\n");
488 char count = 123;
489 /* c99 for loop init test */
490 for (size_t count = 1; count < 3; count++)
491 printf("count=%d\n", count);
492 printf("count = %d\n", count);
494 /* break/continue tests */
495 i = 0;
496 while (1) {
497 if (i == 6)
498 break;
499 i++;
500 if (i == 3)
501 continue;
502 printf("%d", i);
504 printf("\n");
506 /* break/continue tests */
507 i = 0;
508 do {
509 if (i == 6)
510 break;
511 i++;
512 if (i == 3)
513 continue;
514 printf("%d", i);
515 } while(1);
516 printf("\n");
518 for(i = 0;i < 10;i++) {
519 if (i == 3)
520 continue;
521 printf("%d", i);
523 printf("\n");
526 typedef int typedef_and_label;
528 void goto_test()
530 int i;
531 static void *label_table[3] = { &&label1, &&label2, &&label3 };
533 printf("goto:\n");
534 i = 0;
535 /* This needs to parse as label, not as start of decl. */
536 typedef_and_label:
537 s_loop:
538 if (i >= 10)
539 goto s_end;
540 printf("%d", i);
541 i++;
542 goto s_loop;
543 s_end:
544 printf("\n");
546 /* we also test computed gotos (GCC extension) */
547 for(i=0;i<3;i++) {
548 goto *label_table[i];
549 label1:
550 printf("label1\n");
551 goto next;
552 label2:
553 printf("label2\n");
554 goto next;
555 label3:
556 printf("label3\n");
557 next: ;
561 enum {
563 E1 = 2,
564 E2 = 4,
569 enum test {
570 E5 = 1000,
573 void enum_test()
575 enum test b1;
576 printf("enum:\n%d %d %d %d %d %d\n",
577 E0, E1, E2, E3, E4, E5);
578 b1 = 1;
579 printf("b1=%d\n", b1);
582 typedef int *my_ptr;
584 typedef int mytype1;
585 typedef int mytype2;
587 void typedef_test()
589 my_ptr a;
590 mytype1 mytype2;
591 int b;
593 a = &b;
594 *a = 1234;
595 printf("typedef:\n");
596 printf("a=%d\n", *a);
597 mytype2 = 2;
598 printf("mytype2=%d\n", mytype2);
601 void forward_test()
603 printf("forward:\n");
604 forward_ref();
605 forward_ref();
609 void forward_ref(void)
611 printf("forward ok\n");
614 typedef struct struct1 {
615 int f1;
616 int f2, f3;
617 union union1 {
618 int v1;
619 int v2;
620 } u;
621 char str[3];
622 } struct1;
624 struct struct2 {
625 int a;
626 char b;
629 union union2 {
630 int w1;
631 int w2;
634 struct struct1 st1, st2;
636 int main(int argc, char **argv)
638 string_test();
639 expr_test();
640 macro_test();
641 recursive_macro_test();
642 scope_test();
643 forward_test();
644 funcptr_test();
645 loop_test();
646 switch_test();
647 goto_test();
648 enum_test();
649 typedef_test();
650 struct_test();
651 array_test();
652 expr_ptr_test();
653 bool_test();
654 expr2_test();
655 constant_expr_test();
656 expr_cmp_test();
657 char_short_test();
658 init_test();
659 compound_literal_test();
660 kr_test();
661 struct_assign_test();
662 cast_test();
663 bitfield_test();
664 c99_bool_test();
665 float_test();
666 longlong_test();
667 manyarg_test();
668 stdarg_test();
669 whitespace_test();
670 relocation_test();
671 old_style_function();
672 alloca_test();
673 c99_vla_test(5, 2);
674 sizeof_test();
675 typeof_test();
676 statement_expr_test();
677 local_label_test();
678 asm_test();
679 builtin_test();
680 #ifndef _WIN32
681 weak_test();
682 #endif
683 global_data_test();
684 cmp_comparison_test();
685 math_cmp_test();
686 callsave_test();
687 builtin_frame_address_test();
688 intdiv_test();
689 return 0;
692 int tab[3];
693 int tab2[3][2];
695 int g;
697 void f1(g)
699 printf("g1=%d\n", g);
702 void scope_test()
704 printf("scope:\n");
705 g = 2;
706 f1(1);
707 printf("g2=%d\n", g);
709 int g;
710 g = 3;
711 printf("g3=%d\n", g);
713 int g;
714 g = 4;
715 printf("g4=%d\n", g);
718 printf("g5=%d\n", g);
721 void array_test()
723 int i, j, a[4];
725 printf("array:\n");
726 printf("sizeof(a) = %d\n", sizeof(a));
727 printf("sizeof(\"a\") = %d\n", sizeof("a"));
728 #ifdef C99_MACROS
729 printf("sizeof(__func__) = %d\n", sizeof(__func__));
730 #endif
731 printf("sizeof tab %d\n", sizeof(tab));
732 printf("sizeof tab2 %d\n", sizeof tab2);
733 tab[0] = 1;
734 tab[1] = 2;
735 tab[2] = 3;
736 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
737 for(i=0;i<3;i++)
738 for(j=0;j<2;j++)
739 tab2[i][j] = 10 * i + j;
740 for(i=0;i<3*2;i++) {
741 printf(" %3d", ((int *)tab2)[i]);
743 printf("\n");
744 printf("sizeof(size_t)=%d\n", sizeof(size_t));
745 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
748 void expr_test()
750 int a, b;
751 a = 0;
752 printf("%d\n", a += 1);
753 printf("%d\n", a -= 2);
754 printf("%d\n", a *= 31232132);
755 printf("%d\n", a /= 4);
756 printf("%d\n", a %= 20);
757 printf("%d\n", a &= 6);
758 printf("%d\n", a ^= 7);
759 printf("%d\n", a |= 8);
760 printf("%d\n", a >>= 3);
761 printf("%d\n", a <<= 4);
763 a = 22321;
764 b = -22321;
765 printf("%d\n", a + 1);
766 printf("%d\n", a - 2);
767 printf("%d\n", a * 312);
768 printf("%d\n", a / 4);
769 printf("%d\n", b / 4);
770 printf("%d\n", (unsigned)b / 4);
771 printf("%d\n", a % 20);
772 printf("%d\n", b % 20);
773 printf("%d\n", (unsigned)b % 20);
774 printf("%d\n", a & 6);
775 printf("%d\n", a ^ 7);
776 printf("%d\n", a | 8);
777 printf("%d\n", a >> 3);
778 printf("%d\n", b >> 3);
779 printf("%d\n", (unsigned)b >> 3);
780 printf("%d\n", a << 4);
781 printf("%d\n", ~a);
782 printf("%d\n", -a);
783 printf("%d\n", +a);
785 printf("%d\n", 12 + 1);
786 printf("%d\n", 12 - 2);
787 printf("%d\n", 12 * 312);
788 printf("%d\n", 12 / 4);
789 printf("%d\n", 12 % 20);
790 printf("%d\n", 12 & 6);
791 printf("%d\n", 12 ^ 7);
792 printf("%d\n", 12 | 8);
793 printf("%d\n", 12 >> 2);
794 printf("%d\n", 12 << 4);
795 printf("%d\n", ~12);
796 printf("%d\n", -12);
797 printf("%d\n", +12);
798 printf("%d %d %d %d\n",
799 isid('a'),
800 isid('g'),
801 isid('T'),
802 isid('('));
805 int isid(int c)
807 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
810 /**********************/
812 int vstack[10], *vstack_ptr;
814 void vpush(int vt, int vc)
816 *vstack_ptr++ = vt;
817 *vstack_ptr++ = vc;
820 void vpop(int *ft, int *fc)
822 *fc = *--vstack_ptr;
823 *ft = *--vstack_ptr;
826 void expr2_test()
828 int a, b;
830 printf("expr2:\n");
831 vstack_ptr = vstack;
832 vpush(1432432, 2);
833 vstack_ptr[-2] &= ~0xffffff80;
834 vpop(&a, &b);
835 printf("res= %d %d\n", a, b);
838 void constant_expr_test()
840 int a;
841 printf("constant_expr:\n");
842 a = 3;
843 printf("%d\n", a * 16);
844 printf("%d\n", a * 1);
845 printf("%d\n", a + 0);
848 int tab4[10];
850 void expr_ptr_test()
852 int *p, *q;
853 int i = -1;
855 printf("expr_ptr:\n");
856 p = tab4;
857 q = tab4 + 10;
858 printf("diff=%d\n", q - p);
859 p++;
860 printf("inc=%d\n", p - tab4);
861 p--;
862 printf("dec=%d\n", p - tab4);
863 ++p;
864 printf("inc=%d\n", p - tab4);
865 --p;
866 printf("dec=%d\n", p - tab4);
867 printf("add=%d\n", p + 3 - tab4);
868 printf("add=%d\n", 3 + p - tab4);
870 /* check if 64bit support is ok */
871 q = p = 0;
872 q += i;
873 printf("%p %p %ld\n", q, p, p-q);
874 printf("%d %d %d %d %d %d\n",
875 p == q, p != q, p < q, p <= q, p >= q, p > q);
876 i = 0xf0000000;
877 p += i;
878 printf("%p %p %ld\n", q, p, p-q);
879 printf("%d %d %d %d %d %d\n",
880 p == q, p != q, p < q, p <= q, p >= q, p > q);
881 p = (int *)((char *)p + 0xf0000000);
882 printf("%p %p %ld\n", q, p, p-q);
883 printf("%d %d %d %d %d %d\n",
884 p == q, p != q, p < q, p <= q, p >= q, p > q);
885 p += 0xf0000000;
886 printf("%p %p %ld\n", q, p, p-q);
887 printf("%d %d %d %d %d %d\n",
888 p == q, p != q, p < q, p <= q, p >= q, p > q);
890 struct size12 {
891 int i, j, k;
893 struct size12 s[2], *sp = s;
894 int i, j;
895 sp->i = 42;
896 sp++;
897 j = -1;
898 printf("%d\n", sp[j].i);
902 void expr_cmp_test()
904 int a, b;
905 printf("constant_expr:\n");
906 a = -1;
907 b = 1;
908 printf("%d\n", a == a);
909 printf("%d\n", a != a);
911 printf("%d\n", a < b);
912 printf("%d\n", a <= b);
913 printf("%d\n", a <= a);
914 printf("%d\n", b >= a);
915 printf("%d\n", a >= a);
916 printf("%d\n", b > a);
918 printf("%d\n", (unsigned)a < b);
919 printf("%d\n", (unsigned)a <= b);
920 printf("%d\n", (unsigned)a <= a);
921 printf("%d\n", (unsigned)b >= a);
922 printf("%d\n", (unsigned)a >= a);
923 printf("%d\n", (unsigned)b > a);
926 struct empty {
929 struct aligntest1 {
930 char a[10];
933 struct aligntest2 {
934 int a;
935 char b[10];
938 struct aligntest3 {
939 double a, b;
942 struct aligntest4 {
943 double a[0];
946 void struct_test()
948 struct1 *s;
949 union union2 u;
951 printf("struct:\n");
952 printf("sizes: %d %d %d %d\n",
953 sizeof(struct struct1),
954 sizeof(struct struct2),
955 sizeof(union union1),
956 sizeof(union union2));
957 st1.f1 = 1;
958 st1.f2 = 2;
959 st1.f3 = 3;
960 printf("st1: %d %d %d\n",
961 st1.f1, st1.f2, st1.f3);
962 st1.u.v1 = 1;
963 st1.u.v2 = 2;
964 printf("union1: %d\n", st1.u.v1);
965 u.w1 = 1;
966 u.w2 = 2;
967 printf("union2: %d\n", u.w1);
968 s = &st2;
969 s->f1 = 3;
970 s->f2 = 2;
971 s->f3 = 1;
972 printf("st2: %d %d %d\n",
973 s->f1, s->f2, s->f3);
974 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
976 /* align / size tests */
977 printf("aligntest1 sizeof=%d alignof=%d\n",
978 sizeof(struct aligntest1), __alignof__(struct aligntest1));
979 printf("aligntest2 sizeof=%d alignof=%d\n",
980 sizeof(struct aligntest2), __alignof__(struct aligntest2));
981 printf("aligntest3 sizeof=%d alignof=%d\n",
982 sizeof(struct aligntest3), __alignof__(struct aligntest3));
983 printf("aligntest4 sizeof=%d alignof=%d\n",
984 sizeof(struct aligntest4), __alignof__(struct aligntest4));
986 /* empty structures (GCC extension) */
987 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
988 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
991 /* XXX: depend on endianness */
992 void char_short_test()
994 int var1, var2;
996 printf("char_short:\n");
998 var1 = 0x01020304;
999 var2 = 0xfffefdfc;
1000 printf("s8=%d %d\n",
1001 *(char *)&var1, *(char *)&var2);
1002 printf("u8=%d %d\n",
1003 *(unsigned char *)&var1, *(unsigned char *)&var2);
1004 printf("s16=%d %d\n",
1005 *(short *)&var1, *(short *)&var2);
1006 printf("u16=%d %d\n",
1007 *(unsigned short *)&var1, *(unsigned short *)&var2);
1008 printf("s32=%d %d\n",
1009 *(int *)&var1, *(int *)&var2);
1010 printf("u32=%d %d\n",
1011 *(unsigned int *)&var1, *(unsigned int *)&var2);
1012 *(char *)&var1 = 0x08;
1013 printf("var1=%x\n", var1);
1014 *(short *)&var1 = 0x0809;
1015 printf("var1=%x\n", var1);
1016 *(int *)&var1 = 0x08090a0b;
1017 printf("var1=%x\n", var1);
1020 /******************/
1022 typedef struct Sym {
1023 int v;
1024 int t;
1025 int c;
1026 struct Sym *next;
1027 struct Sym *prev;
1028 } Sym;
1030 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1031 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1033 static int toupper1(int a)
1035 return TOUPPER(a);
1038 void bool_test()
1040 int *s, a, b, t, f, i;
1042 a = 0;
1043 s = (void*)0;
1044 printf("!s=%d\n", !s);
1046 if (!s || !s[0])
1047 a = 1;
1048 printf("a=%d\n", a);
1050 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1051 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1052 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1053 #if 1 && 1
1054 printf("a1\n");
1055 #endif
1056 #if 1 || 0
1057 printf("a2\n");
1058 #endif
1059 #if 1 ? 0 : 1
1060 printf("a3\n");
1061 #endif
1062 #if 0 ? 0 : 1
1063 printf("a4\n");
1064 #endif
1066 a = 4;
1067 printf("b=%d\n", a + (0 ? 1 : a / 2));
1069 /* test register spilling */
1070 a = 10;
1071 b = 10;
1072 a = (a + b) * ((a < b) ?
1073 ((b - a) * (a - b)): a + b);
1074 printf("a=%d\n", a);
1076 /* test complex || or && expressions */
1077 t = 1;
1078 f = 0;
1079 a = 32;
1080 printf("exp=%d\n", f == (32 <= a && a <= 3));
1081 printf("r=%d\n", (t || f) + (t && f));
1083 /* test ? : cast */
1085 int aspect_on;
1086 int aspect_native = 65536;
1087 double bfu_aspect = 1.0;
1088 int aspect;
1089 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1090 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1091 printf("aspect=%d\n", aspect);
1095 /* test ? : GCC extension */
1097 static int v1 = 34 ? : -1; /* constant case */
1098 static int v2 = 0 ? : -1; /* constant case */
1099 int a = 30;
1101 printf("%d %d\n", v1, v2);
1102 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1105 /* again complex expression */
1106 for(i=0;i<256;i++) {
1107 if (toupper1 (i) != TOUPPER (i))
1108 printf("error %d\n", i);
1112 /* GCC accepts that */
1113 static int tab_reinit[];
1114 static int tab_reinit[10];
1116 //int cinit1; /* a global variable can be defined several times without error ! */
1117 int cinit1;
1118 int cinit1;
1119 int cinit1 = 0;
1120 int *cinit2 = (int []){3, 2, 1};
1122 void compound_literal_test(void)
1124 int *p, i;
1125 char *q, *q3;
1127 printf("compound_test:\n");
1129 p = (int []){1, 2, 3};
1130 for(i=0;i<3;i++)
1131 printf(" %d", p[i]);
1132 printf("\n");
1134 for(i=0;i<3;i++)
1135 printf("%d", cinit2[i]);
1136 printf("\n");
1138 q = "tralala1";
1139 printf("q1=%s\n", q);
1141 q = (char *){ "tralala2" };
1142 printf("q2=%s\n", q);
1144 q3 = (char *){ q };
1145 printf("q3=%s\n", q3);
1147 q = (char []){ "tralala3" };
1148 printf("q4=%s\n", q);
1150 #ifdef ALL_ISOC99
1151 p = (int []){1, 2, cinit1 + 3};
1152 for(i=0;i<3;i++)
1153 printf(" %d", p[i]);
1154 printf("\n");
1156 for(i=0;i<3;i++) {
1157 p = (int []){1, 2, 4 + i};
1158 printf("%d %d %d\n",
1159 p[0],
1160 p[1],
1161 p[2]);
1163 #endif
1166 /* K & R protos */
1168 kr_func1(a, b)
1170 return a + b;
1173 int kr_func2(a, b)
1175 return a + b;
1178 kr_test()
1180 printf("kr_test:\n");
1181 printf("func1=%d\n", kr_func1(3, 4));
1182 printf("func2=%d\n", kr_func2(3, 4));
1183 return 0;
1186 void num(int n)
1188 char *tab, *p;
1189 tab = (char*)malloc(20);
1190 p = tab;
1191 while (1) {
1192 *p = 48 + (n % 10);
1193 p++;
1194 n = n / 10;
1195 if (n == 0)
1196 break;
1198 while (p != tab) {
1199 p--;
1200 printf("%c", *p);
1202 printf("\n");
1203 free(tab);
1206 /* structure assignment tests */
1207 struct structa1 {
1208 int f1;
1209 char f2;
1212 struct structa1 ssta1;
1214 void struct_assign_test1(struct structa1 s1, int t, float f)
1216 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1219 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1221 s1.f1 += t;
1222 s1.f2 -= t;
1223 return s1;
1226 void struct_assign_test(void)
1228 struct S {
1229 struct structa1 lsta1, lsta2;
1230 int i;
1231 } s, *ps;
1233 ps = &s;
1234 ps->i = 4;
1235 #if 0
1236 printf("struct_assign_test:\n");
1238 s.lsta1.f1 = 1;
1239 s.lsta1.f2 = 2;
1240 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1241 s.lsta2 = s.lsta1;
1242 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1243 #else
1244 s.lsta2.f1 = 1;
1245 s.lsta2.f2 = 2;
1246 #endif
1247 struct_assign_test1(ps->lsta2, 3, 4.5);
1249 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1250 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1251 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1253 static struct {
1254 void (*elem)();
1255 } t[] = {
1256 /* XXX: we should allow this even without braces */
1257 { struct_assign_test }
1259 printf("%d\n", struct_assign_test == t[0].elem);
1262 /* casts to short/char */
1264 void cast1(char a, short b, unsigned char c, unsigned short d)
1266 printf("%d %d %d %d\n", a, b, c, d);
1269 char bcast;
1270 short scast;
1272 void cast_test()
1274 int a;
1275 char c;
1276 char tab[10];
1277 unsigned b,d;
1278 short s;
1279 char *p = NULL;
1280 p -= 0x700000000042;
1282 printf("cast_test:\n");
1283 a = 0xfffff;
1284 cast1(a, a, a, a);
1285 a = 0xffffe;
1286 printf("%d %d %d %d\n",
1287 (char)(a + 1),
1288 (short)(a + 1),
1289 (unsigned char)(a + 1),
1290 (unsigned short)(a + 1));
1291 printf("%d %d %d %d\n",
1292 (char)0xfffff,
1293 (short)0xfffff,
1294 (unsigned char)0xfffff,
1295 (unsigned short)0xfffff);
1297 a = (bcast = 128) + 1;
1298 printf("%d\n", a);
1299 a = (scast = 65536) + 1;
1300 printf("%d\n", a);
1302 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1304 /* test cast from unsigned to signed short to int */
1305 b = 0xf000;
1306 d = (short)b;
1307 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1308 b = 0xf0f0;
1309 d = (char)b;
1310 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1312 /* test implicit int casting for array accesses */
1313 c = 0;
1314 tab[1] = 2;
1315 tab[c] = 1;
1316 printf("%d %d\n", tab[0], tab[1]);
1318 /* test implicit casting on some operators */
1319 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1320 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1321 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1323 /* from pointer to integer types */
1324 printf("%d %d %ld %ld %lld %lld\n",
1325 (int)p, (unsigned int)p,
1326 (long)p, (unsigned long)p,
1327 (long long)p, (unsigned long long)p);
1329 /* from integers to pointers */
1330 printf("%p %p %p %p\n",
1331 (void *)a, (void *)b, (void *)c, (void *)d);
1334 /* initializers tests */
1335 struct structinit1 {
1336 int f1;
1337 char f2;
1338 short f3;
1339 int farray[3];
1342 int sinit1 = 2;
1343 int sinit2 = { 3 };
1344 int sinit3[3] = { 1, 2, {{3}}, };
1345 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1346 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1347 int sinit6[] = { 1, 2, 3 };
1348 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1349 char sinit8[] = "hello" "trala";
1351 struct structinit1 sinit9 = { 1, 2, 3 };
1352 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1353 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1354 #ifdef ALL_ISOC99
1355 .farray[0] = 10,
1356 .farray[1] = 11,
1357 .farray[2] = 12,
1358 #endif
1361 char *sinit12 = "hello world";
1362 char *sinit13[] = {
1363 "test1",
1364 "test2",
1365 "test3",
1367 char sinit14[10] = { "abc" };
1368 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1370 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1372 struct bar {
1373 char *s;
1374 int len;
1375 } sinit17[] = {
1376 "a1", 4,
1377 "a2", 1
1380 int sinit18[10] = {
1381 [2 ... 5] = 20,
1383 [8] = 10,
1386 struct complexinit0 {
1387 int a;
1388 int b;
1391 struct complexinit {
1392 int a;
1393 const struct complexinit0 *b;
1396 const static struct complexinit cix[] = {
1397 [0] = {
1398 .a = 2000,
1399 .b = (const struct complexinit0[]) {
1400 { 2001, 2002 },
1401 { 2003, 2003 },
1407 struct complexinit2 {
1408 int a;
1409 int b[];
1412 struct complexinit2 cix20;
1414 struct complexinit2 cix21 = {
1415 .a = 3000,
1416 .b = { 3001, 3002, 3003 }
1419 struct complexinit2 cix22 = {
1420 .a = 4000,
1421 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1424 void init_test(void)
1426 int linit1 = 2;
1427 int linit2 = { 3 };
1428 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1429 int linit6[] = { 1, 2, 3 };
1430 int i, j;
1431 char linit8[] = "hello" "trala";
1432 int linit12[10] = { 1, 2 };
1433 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1434 char linit14[10] = "abc";
1435 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1436 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1437 int linit17 = sizeof(linit17);
1439 printf("init_test:\n");
1441 printf("sinit1=%d\n", sinit1);
1442 printf("sinit2=%d\n", sinit2);
1443 printf("sinit3=%d %d %d %d\n",
1444 sizeof(sinit3),
1445 sinit3[0],
1446 sinit3[1],
1447 sinit3[2]
1449 printf("sinit6=%d\n", sizeof(sinit6));
1450 printf("sinit7=%d %d %d %d\n",
1451 sizeof(sinit7),
1452 sinit7[0],
1453 sinit7[1],
1454 sinit7[2]
1456 printf("sinit8=%s\n", sinit8);
1457 printf("sinit9=%d %d %d\n",
1458 sinit9.f1,
1459 sinit9.f2,
1460 sinit9.f3
1462 printf("sinit10=%d %d %d\n",
1463 sinit10.f1,
1464 sinit10.f2,
1465 sinit10.f3
1467 printf("sinit11=%d %d %d %d %d %d\n",
1468 sinit11.f1,
1469 sinit11.f2,
1470 sinit11.f3,
1471 sinit11.farray[0],
1472 sinit11.farray[1],
1473 sinit11.farray[2]
1476 for(i=0;i<3;i++)
1477 for(j=0;j<2;j++)
1478 printf("[%d][%d] = %d %d %d\n",
1479 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1480 printf("linit1=%d\n", linit1);
1481 printf("linit2=%d\n", linit2);
1482 printf("linit6=%d\n", sizeof(linit6));
1483 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1485 printf("sinit12=%s\n", sinit12);
1486 printf("sinit13=%d %s %s %s\n",
1487 sizeof(sinit13),
1488 sinit13[0],
1489 sinit13[1],
1490 sinit13[2]);
1491 printf("sinit14=%s\n", sinit14);
1493 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1494 printf("\n");
1495 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1496 printf("\n");
1497 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1498 printf("\n");
1499 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1500 printf("\n");
1501 printf("%d %d %d %d\n",
1502 linit16.a1,
1503 linit16.a2,
1504 linit16.a3,
1505 linit16.a4);
1506 /* test that initialisation is done after variable declare */
1507 printf("linit17=%d\n", linit17);
1508 printf("sinit15=%d\n", sinit15[0]);
1509 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1510 printf("sinit17=%s %d %s %d\n",
1511 sinit17[0].s, sinit17[0].len,
1512 sinit17[1].s, sinit17[1].len);
1513 for(i=0;i<10;i++)
1514 printf("%x ", sinit18[i]);
1515 printf("\n");
1516 /* complex init check */
1517 printf("cix: %d %d %d %d %d %d %d\n",
1518 cix[0].a,
1519 cix[0].b[0].a, cix[0].b[0].b,
1520 cix[0].b[1].a, cix[0].b[1].b,
1521 cix[0].b[2].a, cix[0].b[2].b);
1522 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1523 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1527 void switch_test()
1529 int i;
1531 for(i=0;i<15;i++) {
1532 switch(i) {
1533 case 0:
1534 case 1:
1535 printf("a");
1536 break;
1537 default:
1538 printf("%d", i);
1539 break;
1540 case 8 ... 12:
1541 printf("c");
1542 break;
1543 case 3:
1544 printf("b");
1545 break;
1548 printf("\n");
1551 /* ISOC99 _Bool type */
1552 void c99_bool_test(void)
1554 #ifdef BOOL_ISOC99
1555 int a;
1556 _Bool b;
1558 printf("bool_test:\n");
1559 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1560 a = 3;
1561 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1562 b = 3;
1563 printf("b = %d\n", b);
1564 b++;
1565 printf("b = %d\n", b);
1566 #endif
1569 void bitfield_test(void)
1571 int a;
1572 short sa;
1573 unsigned char ca;
1574 struct sbf1 {
1575 int f1 : 3;
1576 int : 2;
1577 int f2 : 1;
1578 int : 0;
1579 int f3 : 5;
1580 int f4 : 7;
1581 unsigned int f5 : 7;
1582 } st1;
1583 printf("bitfield_test:");
1584 printf("sizeof(st1) = %d\n", sizeof(st1));
1586 st1.f1 = 3;
1587 st1.f2 = 1;
1588 st1.f3 = 15;
1589 a = 120;
1590 st1.f4 = a;
1591 st1.f5 = a;
1592 st1.f5++;
1593 printf("%d %d %d %d %d\n",
1594 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1595 sa = st1.f5;
1596 ca = st1.f5;
1597 printf("%d %d\n", sa, ca);
1599 st1.f1 = 7;
1600 if (st1.f1 == -1)
1601 printf("st1.f1 == -1\n");
1602 else
1603 printf("st1.f1 != -1\n");
1604 if (st1.f2 == -1)
1605 printf("st1.f2 == -1\n");
1606 else
1607 printf("st1.f2 != -1\n");
1609 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1610 long-long bitfields whose size is not bigger than int */
1611 struct sbf2 {
1612 long long f1 : 45;
1613 long long : 2;
1614 long long f2 : 35;
1615 unsigned long long f3 : 38;
1616 } st2;
1617 st2.f1 = 0x123456789ULL;
1618 a = 120;
1619 st2.f2 = (long long)a << 25;
1620 st2.f3 = a;
1621 st2.f2++;
1622 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1625 #ifdef __x86_64__
1626 #define FLOAT_FMT "%f\n"
1627 #else
1628 /* x86's float isn't compatible with GCC */
1629 #define FLOAT_FMT "%.5f\n"
1630 #endif
1632 /* declare strto* functions as they are C99 */
1633 double strtod(const char *nptr, char **endptr);
1635 #if defined(_WIN32)
1636 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
1637 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
1638 #else
1639 float strtof(const char *nptr, char **endptr);
1640 LONG_DOUBLE strtold(const char *nptr, char **endptr);
1641 #endif
1643 #define FTEST(prefix, typename, type, fmt)\
1644 void prefix ## cmp(type a, type b)\
1646 printf("%d %d %d %d %d %d\n",\
1647 a == b,\
1648 a != b,\
1649 a < b,\
1650 a > b,\
1651 a >= b,\
1652 a <= b);\
1653 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1656 a + b,\
1657 a - b,\
1658 a * b,\
1659 a / b,\
1660 -a);\
1661 printf(fmt "\n", ++a);\
1662 printf(fmt "\n", a++);\
1663 printf(fmt "\n", a);\
1664 b = 0;\
1665 printf("%d %d\n", !a, !b);\
1667 void prefix ## fcast(type a)\
1669 float fa;\
1670 double da;\
1671 LONG_DOUBLE la;\
1672 int ia;\
1673 long long llia;\
1674 unsigned int ua;\
1675 unsigned long long llua;\
1676 type b;\
1677 fa = a;\
1678 da = a;\
1679 la = a;\
1680 printf("ftof: %f %f %Lf\n", fa, da, la);\
1681 ia = (int)a;\
1682 llia = (long long)a;\
1683 a = (a >= 0) ? a : -a;\
1684 ua = (unsigned int)a;\
1685 llua = (unsigned long long)a;\
1686 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1687 ia = -1234;\
1688 ua = 0x81234500;\
1689 llia = -0x123456789012345LL;\
1690 llua = 0xf123456789012345LLU;\
1691 b = ia;\
1692 printf("itof: " fmt "\n", b);\
1693 b = ua;\
1694 printf("utof: " fmt "\n", b);\
1695 b = llia;\
1696 printf("lltof: " fmt "\n", b);\
1697 b = llua;\
1698 printf("ulltof: " fmt "\n", b);\
1701 float prefix ## retf(type a) { return a; }\
1702 double prefix ## retd(type a) { return a; }\
1703 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1705 void prefix ## call(void)\
1707 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1708 printf("double: %f\n", prefix ## retd(42.123456789));\
1709 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1710 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1713 void prefix ## signed_zeros(void) \
1715 type x = 0.0, y = -0.0, n, p;\
1716 if (x == y)\
1717 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1718 1.0 / x != 1.0 / y);\
1719 else\
1720 printf ("x != y; this is wrong!\n");\
1722 n = -x;\
1723 if (x == n)\
1724 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1725 1.0 / x != 1.0 / n);\
1726 else\
1727 printf ("x != -x; this is wrong!\n");\
1729 p = +y;\
1730 if (x == p)\
1731 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1732 1.0 / x != 1.0 / p);\
1733 else\
1734 printf ("x != +y; this is wrong!\n");\
1735 p = -y;\
1736 if (x == p)\
1737 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1738 1.0 / x != 1.0 / p);\
1739 else\
1740 printf ("x != -y; this is wrong!\n");\
1742 void prefix ## test(void)\
1744 printf("testing '%s'\n", #typename);\
1745 prefix ## cmp(1, 2.5);\
1746 prefix ## cmp(2, 1.5);\
1747 prefix ## cmp(1, 1);\
1748 prefix ## fcast(234.6);\
1749 prefix ## fcast(-2334.6);\
1750 prefix ## call();\
1751 prefix ## signed_zeros();\
1754 FTEST(f, float, float, "%f")
1755 FTEST(d, double, double, "%f")
1756 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
1758 double ftab1[3] = { 1.2, 3.4, -5.6 };
1761 void float_test(void)
1763 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1764 float fa, fb;
1765 double da, db;
1766 int a;
1767 unsigned int b;
1769 printf("float_test:\n");
1770 printf("sizeof(float) = %d\n", sizeof(float));
1771 printf("sizeof(double) = %d\n", sizeof(double));
1772 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
1773 ftest();
1774 dtest();
1775 ldtest();
1776 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1777 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1778 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1779 da = 123;
1780 printf("da=%f\n", da);
1781 fa = 123;
1782 printf("fa=%f\n", fa);
1783 a = 4000000000;
1784 da = a;
1785 printf("da = %f\n", da);
1786 b = 4000000000;
1787 db = b;
1788 printf("db = %f\n", db);
1789 #endif
1792 int fib(int n)
1794 if (n <= 2)
1795 return 1;
1796 else
1797 return fib(n-1) + fib(n-2);
1800 void funcptr_test()
1802 void (*func)(int);
1803 int a;
1804 struct {
1805 int dummy;
1806 void (*func)(int);
1807 } st1;
1809 printf("funcptr:\n");
1810 func = &num;
1811 (*func)(12345);
1812 func = num;
1813 a = 1;
1814 a = 1;
1815 func(12345);
1816 /* more complicated pointer computation */
1817 st1.func = num;
1818 st1.func(12346);
1819 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1820 printf("sizeof2 = %d\n", sizeof funcptr_test);
1821 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1822 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1825 void lloptest(long long a, long long b)
1827 unsigned long long ua, ub;
1829 ua = a;
1830 ub = b;
1831 /* arith */
1832 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1833 a + b,
1834 a - b,
1835 a * b);
1837 if (b != 0) {
1838 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1839 a / b,
1840 a % b);
1843 /* binary */
1844 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1845 a & b,
1846 a | b,
1847 a ^ b);
1849 /* tests */
1850 printf("test: %d %d %d %d %d %d\n",
1851 a == b,
1852 a != b,
1853 a < b,
1854 a > b,
1855 a >= b,
1856 a <= b);
1858 printf("utest: %d %d %d %d %d %d\n",
1859 ua == ub,
1860 ua != ub,
1861 ua < ub,
1862 ua > ub,
1863 ua >= ub,
1864 ua <= ub);
1866 /* arith2 */
1867 a++;
1868 b++;
1869 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1870 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
1871 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
1872 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1873 b = ub = 0;
1874 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1877 void llshift(long long a, int b)
1879 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1880 (unsigned long long)a >> b,
1881 a >> b,
1882 a << b);
1883 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1884 (unsigned long long)a >> 3,
1885 a >> 3,
1886 a << 3);
1887 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1888 (unsigned long long)a >> 35,
1889 a >> 35,
1890 a << 35);
1893 void llfloat(void)
1895 float fa;
1896 double da;
1897 LONG_DOUBLE lda;
1898 long long la, lb, lc;
1899 unsigned long long ula, ulb, ulc;
1900 la = 0x12345678;
1901 ula = 0x72345678;
1902 la = (la << 20) | 0x12345;
1903 ula = ula << 33;
1904 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
1906 fa = la;
1907 da = la;
1908 lda = la;
1909 printf("lltof: %f %f %Lf\n", fa, da, lda);
1911 la = fa;
1912 lb = da;
1913 lc = lda;
1914 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
1916 fa = ula;
1917 da = ula;
1918 lda = ula;
1919 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1921 ula = fa;
1922 ulb = da;
1923 ulc = lda;
1924 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
1927 long long llfunc1(int a)
1929 return a * 2;
1932 struct S {
1933 int id;
1934 char item;
1937 long long int value(struct S *v)
1939 return ((long long int)v->item);
1942 void longlong_test(void)
1944 long long a, b, c;
1945 int ia;
1946 unsigned int ua;
1947 printf("longlong_test:\n");
1948 printf("sizeof(long long) = %d\n", sizeof(long long));
1949 ia = -1;
1950 ua = -2;
1951 a = ia;
1952 b = ua;
1953 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1954 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
1955 (long long)1,
1956 (long long)-2,
1957 1LL,
1958 0x1234567812345679);
1959 a = llfunc1(-3);
1960 printf(LONG_LONG_FORMAT "\n", a);
1962 lloptest(1000, 23);
1963 lloptest(0xff, 0x1234);
1964 b = 0x72345678 << 10;
1965 lloptest(-3, b);
1966 llshift(0x123, 5);
1967 llshift(-23, 5);
1968 b = 0x72345678LL << 10;
1969 llshift(b, 47);
1971 llfloat();
1972 #if 1
1973 b = 0x12345678;
1974 a = -1;
1975 c = a + b;
1976 printf("%Lx\n", c);
1977 #endif
1979 /* long long reg spill test */
1981 struct S a;
1983 a.item = 3;
1984 printf("%lld\n", value(&a));
1986 lloptest(0x80000000, 0);
1988 /* another long long spill test */
1990 long long *p, v;
1991 v = 1;
1992 p = &v;
1993 p[0]++;
1994 printf("%lld\n", *p);
1997 a = 68719476720LL;
1998 b = 4294967295LL;
1999 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2001 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2004 void manyarg_test(void)
2006 LONG_DOUBLE ld = 1234567891234LL;
2007 printf("manyarg_test:\n");
2008 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2009 1, 2, 3, 4, 5, 6, 7, 8,
2010 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2011 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2012 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2013 1, 2, 3, 4, 5, 6, 7, 8,
2014 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2015 1234567891234LL, 987654321986LL,
2016 42.0, 43.0);
2017 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2018 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2019 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2020 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2021 1234567891234LL, 987654321986LL,
2022 42.0, 43.0);
2023 printf("%d %d %d %d %d %d %d %d %Lf\n",
2024 1, 2, 3, 4, 5, 6, 7, 8, ld);
2025 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2026 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2027 1, 2, 3, 4, 5, 6, 7, 8,
2028 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2029 1234567891234LL, 987654321986LL,
2030 42.0, 43.0, ld);
2031 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2032 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2033 1, 2, 3, 4, 5, 6, 7, 8,
2034 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2035 ld, 1234567891234LL, 987654321986LL,
2036 42.0, 43.0, ld);
2039 void vprintf1(const char *fmt, ...)
2041 va_list ap, aq;
2042 const char *p;
2043 int c, i;
2044 double d;
2045 long long ll;
2046 LONG_DOUBLE ld;
2048 va_start(aq, fmt);
2049 va_copy(ap, aq);
2051 p = fmt;
2052 for(;;) {
2053 c = *p;
2054 if (c == '\0')
2055 break;
2056 p++;
2057 if (c == '%') {
2058 c = *p;
2059 switch(c) {
2060 case '\0':
2061 goto the_end;
2062 case 'd':
2063 i = va_arg(ap, int);
2064 printf("%d", i);
2065 break;
2066 case 'f':
2067 d = va_arg(ap, double);
2068 printf("%f", d);
2069 break;
2070 case 'l':
2071 ll = va_arg(ap, long long);
2072 printf(LONG_LONG_FORMAT, ll);
2073 break;
2074 case 'F':
2075 ld = va_arg(ap, LONG_DOUBLE);
2076 printf("%Lf", ld);
2077 break;
2079 p++;
2080 } else {
2081 putchar(c);
2084 the_end:
2085 va_end(aq);
2086 va_end(ap);
2089 struct myspace {
2090 short int profile;
2093 void stdarg_for_struct(struct myspace bob, ...)
2095 struct myspace george, bill;
2096 va_list ap;
2097 short int validate;
2099 va_start(ap, bob);
2100 bill = va_arg(ap, struct myspace);
2101 george = va_arg(ap, struct myspace);
2102 validate = va_arg(ap, int);
2103 printf("stdarg_for_struct: %d %d %d %d\n",
2104 bob.profile, bill.profile, george.profile, validate);
2105 va_end(ap);
2108 void stdarg_test(void)
2110 LONG_DOUBLE ld = 1234567891234LL;
2111 struct myspace bob;
2113 vprintf1("%d %d %d\n", 1, 2, 3);
2114 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2115 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2116 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2117 vprintf1("%d %f %l %F %d %f %l %F\n",
2118 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2119 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2120 1, 2, 3, 4, 5, 6, 7, 8,
2121 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2122 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2123 1, 2, 3, 4, 5, 6, 7, 8,
2124 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2125 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2126 "%l %l %f %f\n",
2127 1, 2, 3, 4, 5, 6, 7, 8,
2128 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2129 1234567891234LL, 987654321986LL,
2130 42.0, 43.0);
2131 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2132 "%l %l %f %f\n",
2133 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2134 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2135 1234567891234LL, 987654321986LL,
2136 42.0, 43.0);
2137 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2138 1, 2, 3, 4, 5, 6, 7, 8, ld);
2139 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2140 "%l %l %f %f %F\n",
2141 1, 2, 3, 4, 5, 6, 7, 8,
2142 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2143 1234567891234LL, 987654321986LL,
2144 42.0, 43.0, ld);
2145 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2146 "%F %l %l %f %f %F\n",
2147 1, 2, 3, 4, 5, 6, 7, 8,
2148 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2149 ld, 1234567891234LL, 987654321986LL,
2150 42.0, 43.0, ld);
2152 bob.profile = 42;
2153 stdarg_for_struct(bob, bob, bob, bob.profile);
2156 void whitespace_test(void)
2158 char *str;
2160 \f\v #if 1
2161 pri\
2162 ntf("whitspace:\n");\f\v
2163 #endif
2164 pf("N=%d\n", 2);
2166 #ifdef CORRECT_CR_HANDLING
2167 pri\
2168 ntf("aaa=%d\n", 3);
2169 #endif
2171 pri\
2173 ntf("min=%d\n", 4);
2175 #ifdef ACCEPT_CR_IN_STRINGS
2176 printf("len1=%d\n", strlen("
2177 "));
2178 #ifdef CORRECT_CR_HANDLING
2179 str = "
2181 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2182 #endif
2183 printf("len1=%d\n", strlen(" a
2184 "));
2185 #endif /* ACCEPT_CR_IN_STRINGS */
2188 int reltab[3] = { 1, 2, 3 };
2190 int *rel1 = &reltab[1];
2191 int *rel2 = &reltab[2];
2193 void relocation_test(void)
2195 printf("*rel1=%d\n", *rel1);
2196 printf("*rel2=%d\n", *rel2);
2199 void old_style_f(a,b,c)
2200 int a, b;
2201 double c;
2203 printf("a=%d b=%d b=%f\n", a, b, c);
2206 void decl_func1(int cmpfn())
2208 printf("cmpfn=%lx\n", (long)cmpfn);
2211 void decl_func2(cmpfn)
2212 int cmpfn();
2214 printf("cmpfn=%lx\n", (long)cmpfn);
2217 void old_style_function(void)
2219 old_style_f((void *)1, 2, 3.0);
2220 decl_func1(NULL);
2221 decl_func2(NULL);
2224 void alloca_test()
2226 #if defined __i386__ || defined __x86_64__ || defined __arm__
2227 char *p = alloca(16);
2228 strcpy(p,"123456789012345");
2229 printf("alloca: p is %s\n", p);
2230 char *demo = "This is only a test.\n";
2231 /* Test alloca embedded in a larger expression */
2232 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2233 #endif
2236 void *bounds_checking_is_enabled()
2238 char ca[10], *cp = ca-1;
2239 return (ca != cp + 1) ? cp : NULL;
2242 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2244 void c99_vla_test(int size1, int size2)
2246 #if defined __i386__ || defined __x86_64__
2247 int size = size1 * size2;
2248 int tab1[size][2], tab2[10][2];
2249 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2251 /* "size" should have been 'captured' at tab1 declaration,
2252 so modifying it should have no effect on VLA behaviour. */
2253 size = size-1;
2255 printf("Test C99 VLA 1 (sizeof): ");
2256 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2257 tab1_ptr = tab1;
2258 tab2_ptr = tab2;
2259 printf("Test C99 VLA 2 (ptrs subtract): ");
2260 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2261 printf("Test C99 VLA 3 (ptr add): ");
2262 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2263 printf("Test C99 VLA 4 (ptr access): ");
2264 tab1[size1][1] = 42;
2265 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2267 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2268 if (bad_ptr = bounds_checking_is_enabled()) {
2269 int *t1 = &tab1[size1 * size2 - 1][3];
2270 int *t2 = &tab2[9][3];
2271 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2272 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2274 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2275 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2276 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2277 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2279 int *i1 = tab1[-1];
2280 int *i2 = tab2[-1];
2281 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2282 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2284 int *x1 = tab1[size1 * size2 + 1];
2285 int *x2 = tab2[10 + 1];
2286 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2287 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2288 } else {
2289 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2291 printf("\n");
2292 #endif
2295 typedef __SIZE_TYPE__ uintptr_t;
2297 void sizeof_test(void)
2299 int a;
2300 int **ptr;
2302 printf("sizeof(int) = %d\n", sizeof(int));
2303 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2304 printf("sizeof(long) = %d\n", sizeof(long));
2305 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2306 printf("sizeof(short) = %d\n", sizeof(short));
2307 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2308 printf("sizeof(char) = %d\n", sizeof(char));
2309 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2310 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2311 a = 1;
2312 printf("sizeof(a++) = %d\n", sizeof a++);
2313 printf("a=%d\n", a);
2314 ptr = NULL;
2315 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2317 /* The type of sizeof should be as large as a pointer, actually
2318 it should be size_t. */
2319 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2320 uintptr_t t = 1;
2321 uintptr_t t2;
2322 /* Effectively <<32, but defined also on 32bit machines. */
2323 t <<= 16;
2324 t <<= 16;
2325 t++;
2326 /* This checks that sizeof really can be used to manipulate
2327 uintptr_t objects, without truncation. */
2328 t2 = t & -sizeof(uintptr_t);
2329 printf ("%lu %lu\n", t, t2);
2331 /* some alignof tests */
2332 printf("__alignof__(int) = %d\n", __alignof__(int));
2333 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2334 printf("__alignof__(short) = %d\n", __alignof__(short));
2335 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2336 printf("__alignof__(char) = %d\n", __alignof__(char));
2337 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2338 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2341 void typeof_test(void)
2343 double a;
2344 typeof(a) b;
2345 typeof(float) c;
2347 a = 1.5;
2348 b = 2.5;
2349 c = 3.5;
2350 printf("a=%f b=%f c=%f\n", a, b, c);
2353 void statement_expr_test(void)
2355 int a, i;
2357 a = 0;
2358 for(i=0;i<10;i++) {
2359 a += 1 +
2360 ( { int b, j;
2361 b = 0;
2362 for(j=0;j<5;j++)
2363 b += j; b;
2364 } );
2366 printf("a=%d\n", a);
2370 void local_label_test(void)
2372 int a;
2373 goto l1;
2375 a = 1 + ({
2376 __label__ l1, l2, l3, l4;
2377 goto l1;
2379 printf("aa1\n");
2380 goto l3;
2382 printf("aa3\n");
2383 goto l4;
2385 printf("aa2\n");
2386 goto l2;
2387 l3:;
2390 printf("a=%d\n", a);
2391 return;
2393 printf("bb1\n");
2394 goto l2;
2396 printf("bb2\n");
2397 goto l4;
2400 /* inline assembler test */
2401 #ifdef __i386__
2403 /* from linux kernel */
2404 static char * strncat1(char * dest,const char * src,size_t count)
2406 int d0, d1, d2, d3;
2407 __asm__ __volatile__(
2408 "repne\n\t"
2409 "scasb\n\t"
2410 "decl %1\n\t"
2411 "movl %8,%3\n"
2412 "1:\tdecl %3\n\t"
2413 "js 2f\n\t"
2414 "lodsb\n\t"
2415 "stosb\n\t"
2416 "testb %%al,%%al\n\t"
2417 "jne 1b\n"
2418 "2:\txorl %2,%2\n\t"
2419 "stosb"
2420 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2421 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2422 : "memory");
2423 return dest;
2426 static char * strncat2(char * dest,const char * src,size_t count)
2428 int d0, d1, d2, d3;
2429 __asm__ __volatile__(
2430 "repne scasb\n\t" /* one-line repne prefix + string op */
2431 "decl %1\n\t"
2432 "movl %8,%3\n"
2433 "1:\tdecl %3\n\t"
2434 "js 2f\n\t"
2435 "lodsb\n\t"
2436 "stosb\n\t"
2437 "testb %%al,%%al\n\t"
2438 "jne 1b\n"
2439 "2:\txorl %2,%2\n\t"
2440 "stosb"
2441 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2442 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2443 : "memory");
2444 return dest;
2447 static inline void * memcpy1(void * to, const void * from, size_t n)
2449 int d0, d1, d2;
2450 __asm__ __volatile__(
2451 "rep ; movsl\n\t"
2452 "testb $2,%b4\n\t"
2453 "je 1f\n\t"
2454 "movsw\n"
2455 "1:\ttestb $1,%b4\n\t"
2456 "je 2f\n\t"
2457 "movsb\n"
2458 "2:"
2459 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2460 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2461 : "memory");
2462 return (to);
2465 static inline void * memcpy2(void * to, const void * from, size_t n)
2467 int d0, d1, d2;
2468 __asm__ __volatile__(
2469 "rep movsl\n\t" /* one-line rep prefix + string op */
2470 "testb $2,%b4\n\t"
2471 "je 1f\n\t"
2472 "movsw\n"
2473 "1:\ttestb $1,%b4\n\t"
2474 "je 2f\n\t"
2475 "movsb\n"
2476 "2:"
2477 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2478 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2479 : "memory");
2480 return (to);
2483 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2485 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2488 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2490 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2493 static __inline__ __const__ unsigned int swab32(unsigned int x)
2495 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2496 "rorl $16,%0\n\t" /* swap words */
2497 "xchgb %b0,%h0" /* swap higher bytes */
2498 :"=q" (x)
2499 : "0" (x));
2500 return x;
2503 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2505 unsigned long long res;
2506 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2507 return res;
2510 static __inline__ unsigned long long inc64(unsigned long long a)
2512 unsigned long long res;
2513 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2514 return res;
2517 unsigned int set;
2519 void asm_test(void)
2521 char buf[128];
2522 unsigned int val;
2524 printf("inline asm:\n");
2525 /* test the no operand case */
2526 asm volatile ("xorl %eax, %eax");
2528 memcpy1(buf, "hello", 6);
2529 strncat1(buf, " worldXXXXX", 3);
2530 printf("%s\n", buf);
2532 memcpy2(buf, "hello", 6);
2533 strncat2(buf, " worldXXXXX", 3);
2534 printf("%s\n", buf);
2536 /* 'A' constraint test */
2537 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2538 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2540 set = 0xff;
2541 sigdelset1(&set, 2);
2542 sigaddset1(&set, 16);
2543 /* NOTE: we test here if C labels are correctly restored after the
2544 asm statement */
2545 goto label1;
2546 label2:
2547 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2548 #ifdef __GNUC__ // works strange with GCC 4.3
2549 set=0x1080fd;
2550 #endif
2551 printf("set=0x%x\n", set);
2552 val = 0x01020304;
2553 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2554 return;
2555 label1:
2556 goto label2;
2559 #else
2561 void asm_test(void)
2565 #endif
2567 #define COMPAT_TYPE(type1, type2) \
2569 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2570 __builtin_types_compatible_p (type1, type2));\
2573 int constant_p_var;
2575 void builtin_test(void)
2577 #if GCC_MAJOR >= 3
2578 COMPAT_TYPE(int, int);
2579 COMPAT_TYPE(int, unsigned int);
2580 COMPAT_TYPE(int, char);
2581 COMPAT_TYPE(int, const int);
2582 COMPAT_TYPE(int, volatile int);
2583 COMPAT_TYPE(int *, int *);
2584 COMPAT_TYPE(int *, void *);
2585 COMPAT_TYPE(int *, const int *);
2586 COMPAT_TYPE(char *, unsigned char *);
2587 COMPAT_TYPE(char *, signed char *);
2588 COMPAT_TYPE(char *, char *);
2589 /* space is needed because tcc preprocessor introduces a space between each token */
2590 COMPAT_TYPE(char * *, void *);
2591 #endif
2592 printf("res = %d\n", __builtin_constant_p(1));
2593 printf("res = %d\n", __builtin_constant_p(1 + 2));
2594 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2595 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2598 #ifndef _WIN32
2599 extern int __attribute__((weak)) weak_f1(void);
2600 extern int __attribute__((weak)) weak_f2(void);
2601 extern int weak_f3(void);
2602 extern int __attribute__((weak)) weak_v1;
2603 extern int __attribute__((weak)) weak_v2;
2604 extern int weak_v3;
2606 extern int (*weak_fpa)() __attribute__((weak));
2607 extern int __attribute__((weak)) (*weak_fpb)();
2608 extern __attribute__((weak)) int (*weak_fpc)();
2610 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
2611 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2612 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
2613 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
2614 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
2615 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
2617 static const size_t dummy = 0;
2618 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
2619 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
2620 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
2622 int some_lib_func(void);
2623 int dummy_impl_of_slf(void) { return 444; }
2624 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
2626 int weak_toolate() __attribute__((weak));
2627 int weak_toolate() { return 0; }
2629 void __attribute__((weak)) weak_test(void)
2631 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2632 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2633 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
2634 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2635 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2636 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
2638 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
2639 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
2640 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
2642 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
2643 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
2644 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
2645 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
2646 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
2647 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
2650 int __attribute__((weak)) weak_f2() { return 222; }
2651 int __attribute__((weak)) weak_f3() { return 333; }
2652 int __attribute__((weak)) weak_v2 = 222;
2653 int __attribute__((weak)) weak_v3 = 333;
2654 #endif
2656 void const_func(const int a)
2660 void const_warn_test(void)
2662 const_func(1);
2665 struct condstruct {
2666 int i;
2669 int getme (struct condstruct *s, int i)
2671 int i1 = (i == 0 ? 0 : s)->i;
2672 int i2 = (i == 0 ? s : 0)->i;
2673 int i3 = (i == 0 ? (void*)0 : s)->i;
2674 int i4 = (i == 0 ? s : (void*)0)->i;
2675 return i1 + i2 + i3 + i4;
2678 struct global_data
2680 int a[40];
2681 int *b[40];
2684 struct global_data global_data;
2686 int global_data_getstuff (int *, int);
2688 void global_data_callit (int i)
2690 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
2693 int global_data_getstuff (int *p, int i)
2695 return *p + i;
2698 void global_data_test (void)
2700 global_data.a[0] = 42;
2701 global_data.b[0] = &global_data.a[0];
2702 global_data_callit (0);
2703 printf ("%d\n", global_data.a[0]);
2706 struct cmpcmpS
2708 unsigned char fill : 3;
2709 unsigned char b1 : 1;
2710 unsigned char b2 : 1;
2711 unsigned char fill2 : 3;
2714 int glob1, glob2, glob3;
2716 void compare_comparisons (struct cmpcmpS *s)
2718 if (s->b1 != (glob1 == glob2)
2719 || (s->b2 != (glob1 == glob3)))
2720 printf ("comparing comparisons broken\n");
2723 void cmp_comparison_test(void)
2725 struct cmpcmpS s;
2726 s.b1 = 1;
2727 glob1 = 42; glob2 = 42;
2728 s.b2 = 0;
2729 glob3 = 43;
2730 compare_comparisons (&s);
2733 int fcompare (double a, double b, int code)
2735 switch (code) {
2736 case 0: return a == b;
2737 case 1: return a != b;
2738 case 2: return a < b;
2739 case 3: return a >= b;
2740 case 4: return a > b;
2741 case 5: return a <= b;
2745 void math_cmp_test(void)
2747 double nan = 0.0/0.0;
2748 double one = 1.0;
2749 double two = 2.0;
2750 int comp = 0;
2751 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
2753 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
2754 And it does this in various ways so that all code generation paths
2755 are checked (generating inverted tests, or non-inverted tests, or
2756 producing a 0/1 value without jumps (that's done in the fcompare
2757 function). */
2758 #define FCMP(a,b,op,iop,code) \
2759 if (fcompare (a,b,code)) \
2760 bug (a,b,op,iop,1); \
2761 if (a op b) \
2762 bug (a,b,op,iop,2); \
2763 if (a iop b) \
2765 else \
2766 bug (a,b,op,iop,3); \
2767 if ((a op b) || comp) \
2768 bug (a,b,op,iop,4); \
2769 if ((a iop b) || comp) \
2771 else \
2772 bug (a,b,op,iop,5);
2774 /* Equality tests. */
2775 FCMP(nan, nan, ==, !=, 0);
2776 FCMP(one, two, ==, !=, 0);
2777 FCMP(one, one, !=, ==, 1);
2778 /* Non-equality is a bit special. */
2779 if (!fcompare (nan, nan, 1))
2780 bug (nan, nan, !=, ==, 6);
2782 /* Relational tests on numbers. */
2783 FCMP(two, one, <, >=, 2);
2784 FCMP(one, two, >=, <, 3);
2785 FCMP(one, two, >, <=, 4);
2786 FCMP(two, one, <=, >, 5);
2788 /* Relational tests on NaNs. Note that the inverse op here is
2789 always !=, there's no operator in C that is equivalent to !(a < b),
2790 when NaNs are involved, same for the other relational ops. */
2791 FCMP(nan, nan, <, !=, 2);
2792 FCMP(nan, nan, >=, !=, 3);
2793 FCMP(nan, nan, >, !=, 4);
2794 FCMP(nan, nan, <=, !=, 5);
2797 double get100 () { return 100.0; }
2799 void callsave_test(void)
2801 #if defined __i386__ || defined __x86_64__ || defined __arm__
2802 int i, s; double *d; double t;
2803 s = sizeof (double);
2804 printf ("callsavetest: %d\n", s);
2805 d = alloca (sizeof(double));
2806 d[0] = 10.0;
2807 /* x86-64 had a bug were the next call to get100 would evict
2808 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
2809 in int type, not pointer type. When alloca returns a pointer
2810 with the high 32 bit set (which is likely on x86-64) the access
2811 generates a segfault. */
2812 i = d[0] > get100 ();
2813 printf ("%d\n", i);
2814 #endif
2818 void bfa3(ptrdiff_t str_offset)
2820 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
2822 void bfa2(ptrdiff_t str_offset)
2824 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
2825 bfa3(str_offset);
2827 void bfa1(ptrdiff_t str_offset)
2829 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
2830 bfa2(str_offset);
2833 void builtin_frame_address_test(void)
2835 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
2836 #ifndef __arm__
2837 char str[] = "__builtin_frame_address";
2838 char *fp0 = __builtin_frame_address(0);
2840 printf("str: %s\n", str);
2841 bfa1(str-fp0);
2842 #endif