Delete a = (a > = 0)? A: -a; \
[tinycc.git] / tests / tcctest.c
blob22a82782f50d422f457badae91aebac69e10d606
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 */ ();
383 /* macro_push and macro_pop test */
384 #undef MACRO_TEST
385 #ifdef MACRO_TEST
386 printf("define MACRO_TEST\n");
387 #else
388 printf("undef MACRO_TEST\n");
389 #endif
391 #pragma push_macro("MACRO_TEST")
392 #define MACRO_TEST
393 #pragma push_macro("MACRO_TEST")
394 #undef MACRO_TEST
395 #pragma push_macro("MACRO_TEST")
397 #pragma pop_macro("MACRO_TEST")
398 #ifdef MACRO_TEST
399 printf("define MACRO_TEST\n");
400 #else
401 printf("undef MACRO_TEST\n");
402 #endif
404 #pragma pop_macro("MACRO_TEST")
405 #ifdef MACRO_TEST
406 printf("define MACRO_TEST\n");
407 #else
408 printf("undef MACRO_TEST\n");
409 #endif
411 #pragma pop_macro("MACRO_TEST")
412 #ifdef MACRO_TEST
413 printf("define MACRO_TEST\n");
414 #else
415 printf("undef MACRO_TEST\n");
416 #endif
418 /* pack test */
419 #pragma pack(push,8)
420 #pragma pack(pop)
422 /* gcc does not support
423 #define MACRO_TEST_MACRO "MACRO_TEST"
424 #pragma push_macro(MACRO_TEST_MACRO)
425 #undef MACRO_TEST
426 #define MACRO_TEST "macro_test3\n"
427 printf(MACRO_TEST);
428 #pragma pop_macro(MACRO_TEST_MACRO)
429 printf(MACRO_TEST);
434 static void print_num(char *fn, int line, int num) {
435 printf("fn %s, line %d, num %d\n", fn, line, num);
438 void recursive_macro_test(void)
441 #define ELF32_ST_TYPE(val) ((val) & 0xf)
442 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
443 #define STB_WEAK 2 /* Weak symbol */
444 #define ELFW(type) ELF##32##_##type
445 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
447 #define WRAP(x) x
449 #define print_num(x) print_num(__FILE__,__LINE__,x)
450 print_num(123);
451 WRAP(print_num(123));
452 WRAP(WRAP(print_num(123)));
454 static struct recursive_macro { int rm_field; } G;
455 #define rm_field (G.rm_field)
456 printf("rm_field = %d\n", rm_field);
457 printf("rm_field = %d\n", WRAP(rm_field));
458 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
461 int op(a,b)
463 return a / b;
466 int ret(a)
468 if (a == 2)
469 return 1;
470 if (a == 3)
471 return 2;
472 return 0;
475 void ps(const char *s)
477 int c;
478 while (1) {
479 c = *s;
480 if (c == 0)
481 break;
482 printf("%c", c);
483 s++;
487 const char foo1_string[] = "\
488 bar\n\
489 test\14\
492 void string_test()
494 unsigned int b;
495 printf("string:\n");
496 printf("\141\1423\143\n");/* dezdez test */
497 printf("\x41\x42\x43\x3a\n");
498 printf("c=%c\n", 'r');
499 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
500 printf("foo1_string='%s'\n", foo1_string);
501 #if 0
502 printf("wstring=%S\n", L"abc");
503 printf("wstring=%S\n", L"abc" L"def" "ghi");
504 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
505 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
506 #endif
507 ps("test\n");
508 b = 32;
509 while ((b = b + 1) < 96) {
510 printf("%c", b);
512 printf("\n");
513 printf("fib=%d\n", fib(33));
514 b = 262144;
515 while (b != 0x80000000) {
516 num(b);
517 b = b * 2;
521 void loop_test()
523 int i;
524 i = 0;
525 while (i < 10)
526 printf("%d", i++);
527 printf("\n");
528 for(i = 0; i < 10;i++)
529 printf("%d", i);
530 printf("\n");
531 i = 0;
532 do {
533 printf("%d", i++);
534 } while (i < 10);
535 printf("\n");
537 char count = 123;
538 /* c99 for loop init test */
539 for (size_t count = 1; count < 3; count++)
540 printf("count=%d\n", count);
541 printf("count = %d\n", count);
543 /* break/continue tests */
544 i = 0;
545 while (1) {
546 if (i == 6)
547 break;
548 i++;
549 if (i == 3)
550 continue;
551 printf("%d", i);
553 printf("\n");
555 /* break/continue tests */
556 i = 0;
557 do {
558 if (i == 6)
559 break;
560 i++;
561 if (i == 3)
562 continue;
563 printf("%d", i);
564 } while(1);
565 printf("\n");
567 for(i = 0;i < 10;i++) {
568 if (i == 3)
569 continue;
570 printf("%d", i);
572 printf("\n");
575 typedef int typedef_and_label;
577 void goto_test()
579 int i;
580 static void *label_table[3] = { &&label1, &&label2, &&label3 };
582 printf("goto:\n");
583 i = 0;
584 /* This needs to parse as label, not as start of decl. */
585 typedef_and_label:
586 s_loop:
587 if (i >= 10)
588 goto s_end;
589 printf("%d", i);
590 i++;
591 goto s_loop;
592 s_end:
593 printf("\n");
595 /* we also test computed gotos (GCC extension) */
596 for(i=0;i<3;i++) {
597 goto *label_table[i];
598 label1:
599 printf("label1\n");
600 goto next;
601 label2:
602 printf("label2\n");
603 goto next;
604 label3:
605 printf("label3\n");
606 next: ;
610 enum {
612 E1 = 2,
613 E2 = 4,
618 enum test {
619 E5 = 1000,
622 void enum_test()
624 enum test b1;
625 printf("enum:\n%d %d %d %d %d %d\n",
626 E0, E1, E2, E3, E4, E5);
627 b1 = 1;
628 printf("b1=%d\n", b1);
631 typedef int *my_ptr;
633 typedef int mytype1;
634 typedef int mytype2;
636 void typedef_test()
638 my_ptr a;
639 mytype1 mytype2;
640 int b;
642 a = &b;
643 *a = 1234;
644 printf("typedef:\n");
645 printf("a=%d\n", *a);
646 mytype2 = 2;
647 printf("mytype2=%d\n", mytype2);
650 void forward_test()
652 printf("forward:\n");
653 forward_ref();
654 forward_ref();
658 void forward_ref(void)
660 printf("forward ok\n");
663 typedef struct struct1 {
664 int f1;
665 int f2, f3;
666 union union1 {
667 int v1;
668 int v2;
669 } u;
670 char str[3];
671 } struct1;
673 struct struct2 {
674 int a;
675 char b;
678 union union2 {
679 int w1;
680 int w2;
683 struct struct1 st1, st2;
685 int main(int argc, char **argv)
687 string_test();
688 expr_test();
689 macro_test();
690 recursive_macro_test();
691 scope_test();
692 forward_test();
693 funcptr_test();
694 loop_test();
695 switch_test();
696 goto_test();
697 enum_test();
698 typedef_test();
699 struct_test();
700 array_test();
701 expr_ptr_test();
702 bool_test();
703 expr2_test();
704 constant_expr_test();
705 expr_cmp_test();
706 char_short_test();
707 init_test();
708 compound_literal_test();
709 kr_test();
710 struct_assign_test();
711 cast_test();
712 bitfield_test();
713 c99_bool_test();
714 float_test();
715 longlong_test();
716 manyarg_test();
717 stdarg_test();
718 whitespace_test();
719 relocation_test();
720 old_style_function();
721 alloca_test();
722 c99_vla_test(5, 2);
723 sizeof_test();
724 typeof_test();
725 statement_expr_test();
726 local_label_test();
727 asm_test();
728 builtin_test();
729 #ifndef _WIN32
730 weak_test();
731 #endif
732 global_data_test();
733 cmp_comparison_test();
734 math_cmp_test();
735 callsave_test();
736 builtin_frame_address_test();
737 intdiv_test();
738 return 0;
741 int tab[3];
742 int tab2[3][2];
744 int g;
746 void f1(g)
748 printf("g1=%d\n", g);
751 void scope_test()
753 printf("scope:\n");
754 g = 2;
755 f1(1);
756 printf("g2=%d\n", g);
758 int g;
759 g = 3;
760 printf("g3=%d\n", g);
762 int g;
763 g = 4;
764 printf("g4=%d\n", g);
767 printf("g5=%d\n", g);
770 void array_test()
772 int i, j, a[4];
774 printf("array:\n");
775 printf("sizeof(a) = %d\n", sizeof(a));
776 printf("sizeof(\"a\") = %d\n", sizeof("a"));
777 #ifdef C99_MACROS
778 printf("sizeof(__func__) = %d\n", sizeof(__func__));
779 #endif
780 printf("sizeof tab %d\n", sizeof(tab));
781 printf("sizeof tab2 %d\n", sizeof tab2);
782 tab[0] = 1;
783 tab[1] = 2;
784 tab[2] = 3;
785 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
786 for(i=0;i<3;i++)
787 for(j=0;j<2;j++)
788 tab2[i][j] = 10 * i + j;
789 for(i=0;i<3*2;i++) {
790 printf(" %3d", ((int *)tab2)[i]);
792 printf("\n");
793 printf("sizeof(size_t)=%d\n", sizeof(size_t));
794 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
797 void expr_test()
799 int a, b;
800 a = 0;
801 printf("%d\n", a += 1);
802 printf("%d\n", a -= 2);
803 printf("%d\n", a *= 31232132);
804 printf("%d\n", a /= 4);
805 printf("%d\n", a %= 20);
806 printf("%d\n", a &= 6);
807 printf("%d\n", a ^= 7);
808 printf("%d\n", a |= 8);
809 printf("%d\n", a >>= 3);
810 printf("%d\n", a <<= 4);
812 a = 22321;
813 b = -22321;
814 printf("%d\n", a + 1);
815 printf("%d\n", a - 2);
816 printf("%d\n", a * 312);
817 printf("%d\n", a / 4);
818 printf("%d\n", b / 4);
819 printf("%d\n", (unsigned)b / 4);
820 printf("%d\n", a % 20);
821 printf("%d\n", b % 20);
822 printf("%d\n", (unsigned)b % 20);
823 printf("%d\n", a & 6);
824 printf("%d\n", a ^ 7);
825 printf("%d\n", a | 8);
826 printf("%d\n", a >> 3);
827 printf("%d\n", b >> 3);
828 printf("%d\n", (unsigned)b >> 3);
829 printf("%d\n", a << 4);
830 printf("%d\n", ~a);
831 printf("%d\n", -a);
832 printf("%d\n", +a);
834 printf("%d\n", 12 + 1);
835 printf("%d\n", 12 - 2);
836 printf("%d\n", 12 * 312);
837 printf("%d\n", 12 / 4);
838 printf("%d\n", 12 % 20);
839 printf("%d\n", 12 & 6);
840 printf("%d\n", 12 ^ 7);
841 printf("%d\n", 12 | 8);
842 printf("%d\n", 12 >> 2);
843 printf("%d\n", 12 << 4);
844 printf("%d\n", ~12);
845 printf("%d\n", -12);
846 printf("%d\n", +12);
847 printf("%d %d %d %d\n",
848 isid('a'),
849 isid('g'),
850 isid('T'),
851 isid('('));
854 int isid(int c)
856 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
859 /**********************/
861 int vstack[10], *vstack_ptr;
863 void vpush(int vt, int vc)
865 *vstack_ptr++ = vt;
866 *vstack_ptr++ = vc;
869 void vpop(int *ft, int *fc)
871 *fc = *--vstack_ptr;
872 *ft = *--vstack_ptr;
875 void expr2_test()
877 int a, b;
879 printf("expr2:\n");
880 vstack_ptr = vstack;
881 vpush(1432432, 2);
882 vstack_ptr[-2] &= ~0xffffff80;
883 vpop(&a, &b);
884 printf("res= %d %d\n", a, b);
887 void constant_expr_test()
889 int a;
890 printf("constant_expr:\n");
891 a = 3;
892 printf("%d\n", a * 16);
893 printf("%d\n", a * 1);
894 printf("%d\n", a + 0);
897 int tab4[10];
899 void expr_ptr_test()
901 int *p, *q;
902 int i = -1;
904 printf("expr_ptr:\n");
905 p = tab4;
906 q = tab4 + 10;
907 printf("diff=%d\n", q - p);
908 p++;
909 printf("inc=%d\n", p - tab4);
910 p--;
911 printf("dec=%d\n", p - tab4);
912 ++p;
913 printf("inc=%d\n", p - tab4);
914 --p;
915 printf("dec=%d\n", p - tab4);
916 printf("add=%d\n", p + 3 - tab4);
917 printf("add=%d\n", 3 + p - tab4);
919 /* check if 64bit support is ok */
920 q = p = 0;
921 q += i;
922 printf("%p %p %ld\n", q, p, p-q);
923 printf("%d %d %d %d %d %d\n",
924 p == q, p != q, p < q, p <= q, p >= q, p > q);
925 i = 0xf0000000;
926 p += i;
927 printf("%p %p %ld\n", q, p, p-q);
928 printf("%d %d %d %d %d %d\n",
929 p == q, p != q, p < q, p <= q, p >= q, p > q);
930 p = (int *)((char *)p + 0xf0000000);
931 printf("%p %p %ld\n", q, p, p-q);
932 printf("%d %d %d %d %d %d\n",
933 p == q, p != q, p < q, p <= q, p >= q, p > q);
934 p += 0xf0000000;
935 printf("%p %p %ld\n", q, p, p-q);
936 printf("%d %d %d %d %d %d\n",
937 p == q, p != q, p < q, p <= q, p >= q, p > q);
939 struct size12 {
940 int i, j, k;
942 struct size12 s[2], *sp = s;
943 int i, j;
944 sp->i = 42;
945 sp++;
946 j = -1;
947 printf("%d\n", sp[j].i);
951 void expr_cmp_test()
953 int a, b;
954 printf("constant_expr:\n");
955 a = -1;
956 b = 1;
957 printf("%d\n", a == a);
958 printf("%d\n", a != a);
960 printf("%d\n", a < b);
961 printf("%d\n", a <= b);
962 printf("%d\n", a <= a);
963 printf("%d\n", b >= a);
964 printf("%d\n", a >= a);
965 printf("%d\n", b > a);
967 printf("%d\n", (unsigned)a < b);
968 printf("%d\n", (unsigned)a <= b);
969 printf("%d\n", (unsigned)a <= a);
970 printf("%d\n", (unsigned)b >= a);
971 printf("%d\n", (unsigned)a >= a);
972 printf("%d\n", (unsigned)b > a);
975 struct empty {
978 struct aligntest1 {
979 char a[10];
982 struct aligntest2 {
983 int a;
984 char b[10];
987 struct aligntest3 {
988 double a, b;
991 struct aligntest4 {
992 double a[0];
995 void struct_test()
997 struct1 *s;
998 union union2 u;
1000 printf("struct:\n");
1001 printf("sizes: %d %d %d %d\n",
1002 sizeof(struct struct1),
1003 sizeof(struct struct2),
1004 sizeof(union union1),
1005 sizeof(union union2));
1006 st1.f1 = 1;
1007 st1.f2 = 2;
1008 st1.f3 = 3;
1009 printf("st1: %d %d %d\n",
1010 st1.f1, st1.f2, st1.f3);
1011 st1.u.v1 = 1;
1012 st1.u.v2 = 2;
1013 printf("union1: %d\n", st1.u.v1);
1014 u.w1 = 1;
1015 u.w2 = 2;
1016 printf("union2: %d\n", u.w1);
1017 s = &st2;
1018 s->f1 = 3;
1019 s->f2 = 2;
1020 s->f3 = 1;
1021 printf("st2: %d %d %d\n",
1022 s->f1, s->f2, s->f3);
1023 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
1025 /* align / size tests */
1026 printf("aligntest1 sizeof=%d alignof=%d\n",
1027 sizeof(struct aligntest1), __alignof__(struct aligntest1));
1028 printf("aligntest2 sizeof=%d alignof=%d\n",
1029 sizeof(struct aligntest2), __alignof__(struct aligntest2));
1030 printf("aligntest3 sizeof=%d alignof=%d\n",
1031 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1032 printf("aligntest4 sizeof=%d alignof=%d\n",
1033 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1035 /* empty structures (GCC extension) */
1036 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1037 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1040 /* XXX: depend on endianness */
1041 void char_short_test()
1043 int var1, var2;
1045 printf("char_short:\n");
1047 var1 = 0x01020304;
1048 var2 = 0xfffefdfc;
1049 printf("s8=%d %d\n",
1050 *(char *)&var1, *(char *)&var2);
1051 printf("u8=%d %d\n",
1052 *(unsigned char *)&var1, *(unsigned char *)&var2);
1053 printf("s16=%d %d\n",
1054 *(short *)&var1, *(short *)&var2);
1055 printf("u16=%d %d\n",
1056 *(unsigned short *)&var1, *(unsigned short *)&var2);
1057 printf("s32=%d %d\n",
1058 *(int *)&var1, *(int *)&var2);
1059 printf("u32=%d %d\n",
1060 *(unsigned int *)&var1, *(unsigned int *)&var2);
1061 *(char *)&var1 = 0x08;
1062 printf("var1=%x\n", var1);
1063 *(short *)&var1 = 0x0809;
1064 printf("var1=%x\n", var1);
1065 *(int *)&var1 = 0x08090a0b;
1066 printf("var1=%x\n", var1);
1069 /******************/
1071 typedef struct Sym {
1072 int v;
1073 int t;
1074 int c;
1075 struct Sym *next;
1076 struct Sym *prev;
1077 } Sym;
1079 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1080 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1082 static int toupper1(int a)
1084 return TOUPPER(a);
1087 void bool_test()
1089 int *s, a, b, t, f, i;
1091 a = 0;
1092 s = (void*)0;
1093 printf("!s=%d\n", !s);
1095 if (!s || !s[0])
1096 a = 1;
1097 printf("a=%d\n", a);
1099 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1100 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1101 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1102 #if 1 && 1
1103 printf("a1\n");
1104 #endif
1105 #if 1 || 0
1106 printf("a2\n");
1107 #endif
1108 #if 1 ? 0 : 1
1109 printf("a3\n");
1110 #endif
1111 #if 0 ? 0 : 1
1112 printf("a4\n");
1113 #endif
1115 a = 4;
1116 printf("b=%d\n", a + (0 ? 1 : a / 2));
1118 /* test register spilling */
1119 a = 10;
1120 b = 10;
1121 a = (a + b) * ((a < b) ?
1122 ((b - a) * (a - b)): a + b);
1123 printf("a=%d\n", a);
1125 /* test complex || or && expressions */
1126 t = 1;
1127 f = 0;
1128 a = 32;
1129 printf("exp=%d\n", f == (32 <= a && a <= 3));
1130 printf("r=%d\n", (t || f) + (t && f));
1132 /* test ? : cast */
1134 int aspect_on;
1135 int aspect_native = 65536;
1136 double bfu_aspect = 1.0;
1137 int aspect;
1138 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1139 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1140 printf("aspect=%d\n", aspect);
1144 /* test ? : GCC extension */
1146 static int v1 = 34 ? : -1; /* constant case */
1147 static int v2 = 0 ? : -1; /* constant case */
1148 int a = 30;
1150 printf("%d %d\n", v1, v2);
1151 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1154 /* again complex expression */
1155 for(i=0;i<256;i++) {
1156 if (toupper1 (i) != TOUPPER (i))
1157 printf("error %d\n", i);
1161 /* GCC accepts that */
1162 static int tab_reinit[];
1163 static int tab_reinit[10];
1165 //int cinit1; /* a global variable can be defined several times without error ! */
1166 int cinit1;
1167 int cinit1;
1168 int cinit1 = 0;
1169 int *cinit2 = (int []){3, 2, 1};
1171 void compound_literal_test(void)
1173 int *p, i;
1174 char *q, *q3;
1176 printf("compound_test:\n");
1178 p = (int []){1, 2, 3};
1179 for(i=0;i<3;i++)
1180 printf(" %d", p[i]);
1181 printf("\n");
1183 for(i=0;i<3;i++)
1184 printf("%d", cinit2[i]);
1185 printf("\n");
1187 q = "tralala1";
1188 printf("q1=%s\n", q);
1190 q = (char *){ "tralala2" };
1191 printf("q2=%s\n", q);
1193 q3 = (char *){ q };
1194 printf("q3=%s\n", q3);
1196 q = (char []){ "tralala3" };
1197 printf("q4=%s\n", q);
1199 #ifdef ALL_ISOC99
1200 p = (int []){1, 2, cinit1 + 3};
1201 for(i=0;i<3;i++)
1202 printf(" %d", p[i]);
1203 printf("\n");
1205 for(i=0;i<3;i++) {
1206 p = (int []){1, 2, 4 + i};
1207 printf("%d %d %d\n",
1208 p[0],
1209 p[1],
1210 p[2]);
1212 #endif
1215 /* K & R protos */
1217 kr_func1(a, b)
1219 return a + b;
1222 int kr_func2(a, b)
1224 return a + b;
1227 kr_test()
1229 printf("kr_test:\n");
1230 printf("func1=%d\n", kr_func1(3, 4));
1231 printf("func2=%d\n", kr_func2(3, 4));
1232 return 0;
1235 void num(int n)
1237 char *tab, *p;
1238 tab = (char*)malloc(20);
1239 p = tab;
1240 while (1) {
1241 *p = 48 + (n % 10);
1242 p++;
1243 n = n / 10;
1244 if (n == 0)
1245 break;
1247 while (p != tab) {
1248 p--;
1249 printf("%c", *p);
1251 printf("\n");
1252 free(tab);
1255 /* structure assignment tests */
1256 struct structa1 {
1257 int f1;
1258 char f2;
1261 struct structa1 ssta1;
1263 void struct_assign_test1(struct structa1 s1, int t, float f)
1265 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1268 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1270 s1.f1 += t;
1271 s1.f2 -= t;
1272 return s1;
1275 void struct_assign_test(void)
1277 struct S {
1278 struct structa1 lsta1, lsta2;
1279 int i;
1280 } s, *ps;
1282 ps = &s;
1283 ps->i = 4;
1284 #if 0
1285 printf("struct_assign_test:\n");
1287 s.lsta1.f1 = 1;
1288 s.lsta1.f2 = 2;
1289 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1290 s.lsta2 = s.lsta1;
1291 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1292 #else
1293 s.lsta2.f1 = 1;
1294 s.lsta2.f2 = 2;
1295 #endif
1296 struct_assign_test1(ps->lsta2, 3, 4.5);
1298 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1299 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1300 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1302 static struct {
1303 void (*elem)();
1304 } t[] = {
1305 /* XXX: we should allow this even without braces */
1306 { struct_assign_test }
1308 printf("%d\n", struct_assign_test == t[0].elem);
1311 /* casts to short/char */
1313 void cast1(char a, short b, unsigned char c, unsigned short d)
1315 printf("%d %d %d %d\n", a, b, c, d);
1318 char bcast;
1319 short scast;
1321 void cast_test()
1323 int a;
1324 char c;
1325 char tab[10];
1326 unsigned b,d;
1327 short s;
1328 char *p = NULL;
1329 p -= 0x700000000042;
1331 printf("cast_test:\n");
1332 a = 0xfffff;
1333 cast1(a, a, a, a);
1334 a = 0xffffe;
1335 printf("%d %d %d %d\n",
1336 (char)(a + 1),
1337 (short)(a + 1),
1338 (unsigned char)(a + 1),
1339 (unsigned short)(a + 1));
1340 printf("%d %d %d %d\n",
1341 (char)0xfffff,
1342 (short)0xfffff,
1343 (unsigned char)0xfffff,
1344 (unsigned short)0xfffff);
1346 a = (bcast = 128) + 1;
1347 printf("%d\n", a);
1348 a = (scast = 65536) + 1;
1349 printf("%d\n", a);
1351 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1353 /* test cast from unsigned to signed short to int */
1354 b = 0xf000;
1355 d = (short)b;
1356 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1357 b = 0xf0f0;
1358 d = (char)b;
1359 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1361 /* test implicit int casting for array accesses */
1362 c = 0;
1363 tab[1] = 2;
1364 tab[c] = 1;
1365 printf("%d %d\n", tab[0], tab[1]);
1367 /* test implicit casting on some operators */
1368 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1369 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1370 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1372 /* from pointer to integer types */
1373 printf("%d %d %ld %ld %lld %lld\n",
1374 (int)p, (unsigned int)p,
1375 (long)p, (unsigned long)p,
1376 (long long)p, (unsigned long long)p);
1378 /* from integers to pointers */
1379 printf("%p %p %p %p\n",
1380 (void *)a, (void *)b, (void *)c, (void *)d);
1383 /* initializers tests */
1384 struct structinit1 {
1385 int f1;
1386 char f2;
1387 short f3;
1388 int farray[3];
1391 int sinit1 = 2;
1392 int sinit2 = { 3 };
1393 int sinit3[3] = { 1, 2, {{3}}, };
1394 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1395 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1396 int sinit6[] = { 1, 2, 3 };
1397 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1398 char sinit8[] = "hello" "trala";
1400 struct structinit1 sinit9 = { 1, 2, 3 };
1401 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1402 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1403 #ifdef ALL_ISOC99
1404 .farray[0] = 10,
1405 .farray[1] = 11,
1406 .farray[2] = 12,
1407 #endif
1410 char *sinit12 = "hello world";
1411 char *sinit13[] = {
1412 "test1",
1413 "test2",
1414 "test3",
1416 char sinit14[10] = { "abc" };
1417 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1419 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1421 struct bar {
1422 char *s;
1423 int len;
1424 } sinit17[] = {
1425 "a1", 4,
1426 "a2", 1
1429 int sinit18[10] = {
1430 [2 ... 5] = 20,
1432 [8] = 10,
1435 struct complexinit0 {
1436 int a;
1437 int b;
1440 struct complexinit {
1441 int a;
1442 const struct complexinit0 *b;
1445 const static struct complexinit cix[] = {
1446 [0] = {
1447 .a = 2000,
1448 .b = (const struct complexinit0[]) {
1449 { 2001, 2002 },
1450 { 2003, 2003 },
1456 struct complexinit2 {
1457 int a;
1458 int b[];
1461 struct complexinit2 cix20;
1463 struct complexinit2 cix21 = {
1464 .a = 3000,
1465 .b = { 3001, 3002, 3003 }
1468 struct complexinit2 cix22 = {
1469 .a = 4000,
1470 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1473 void init_test(void)
1475 int linit1 = 2;
1476 int linit2 = { 3 };
1477 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1478 int linit6[] = { 1, 2, 3 };
1479 int i, j;
1480 char linit8[] = "hello" "trala";
1481 int linit12[10] = { 1, 2 };
1482 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1483 char linit14[10] = "abc";
1484 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1485 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1486 int linit17 = sizeof(linit17);
1488 printf("init_test:\n");
1490 printf("sinit1=%d\n", sinit1);
1491 printf("sinit2=%d\n", sinit2);
1492 printf("sinit3=%d %d %d %d\n",
1493 sizeof(sinit3),
1494 sinit3[0],
1495 sinit3[1],
1496 sinit3[2]
1498 printf("sinit6=%d\n", sizeof(sinit6));
1499 printf("sinit7=%d %d %d %d\n",
1500 sizeof(sinit7),
1501 sinit7[0],
1502 sinit7[1],
1503 sinit7[2]
1505 printf("sinit8=%s\n", sinit8);
1506 printf("sinit9=%d %d %d\n",
1507 sinit9.f1,
1508 sinit9.f2,
1509 sinit9.f3
1511 printf("sinit10=%d %d %d\n",
1512 sinit10.f1,
1513 sinit10.f2,
1514 sinit10.f3
1516 printf("sinit11=%d %d %d %d %d %d\n",
1517 sinit11.f1,
1518 sinit11.f2,
1519 sinit11.f3,
1520 sinit11.farray[0],
1521 sinit11.farray[1],
1522 sinit11.farray[2]
1525 for(i=0;i<3;i++)
1526 for(j=0;j<2;j++)
1527 printf("[%d][%d] = %d %d %d\n",
1528 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1529 printf("linit1=%d\n", linit1);
1530 printf("linit2=%d\n", linit2);
1531 printf("linit6=%d\n", sizeof(linit6));
1532 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1534 printf("sinit12=%s\n", sinit12);
1535 printf("sinit13=%d %s %s %s\n",
1536 sizeof(sinit13),
1537 sinit13[0],
1538 sinit13[1],
1539 sinit13[2]);
1540 printf("sinit14=%s\n", sinit14);
1542 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1543 printf("\n");
1544 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1545 printf("\n");
1546 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1547 printf("\n");
1548 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1549 printf("\n");
1550 printf("%d %d %d %d\n",
1551 linit16.a1,
1552 linit16.a2,
1553 linit16.a3,
1554 linit16.a4);
1555 /* test that initialisation is done after variable declare */
1556 printf("linit17=%d\n", linit17);
1557 printf("sinit15=%d\n", sinit15[0]);
1558 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1559 printf("sinit17=%s %d %s %d\n",
1560 sinit17[0].s, sinit17[0].len,
1561 sinit17[1].s, sinit17[1].len);
1562 for(i=0;i<10;i++)
1563 printf("%x ", sinit18[i]);
1564 printf("\n");
1565 /* complex init check */
1566 printf("cix: %d %d %d %d %d %d %d\n",
1567 cix[0].a,
1568 cix[0].b[0].a, cix[0].b[0].b,
1569 cix[0].b[1].a, cix[0].b[1].b,
1570 cix[0].b[2].a, cix[0].b[2].b);
1571 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1572 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1576 void switch_test()
1578 int i;
1580 for(i=0;i<15;i++) {
1581 switch(i) {
1582 case 0:
1583 case 1:
1584 printf("a");
1585 break;
1586 default:
1587 printf("%d", i);
1588 break;
1589 case 8 ... 12:
1590 printf("c");
1591 break;
1592 case 3:
1593 printf("b");
1594 break;
1597 printf("\n");
1600 /* ISOC99 _Bool type */
1601 void c99_bool_test(void)
1603 #ifdef BOOL_ISOC99
1604 int a;
1605 _Bool b;
1607 printf("bool_test:\n");
1608 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1609 a = 3;
1610 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1611 b = 3;
1612 printf("b = %d\n", b);
1613 b++;
1614 printf("b = %d\n", b);
1615 #endif
1618 void bitfield_test(void)
1620 int a;
1621 short sa;
1622 unsigned char ca;
1623 struct sbf1 {
1624 int f1 : 3;
1625 int : 2;
1626 int f2 : 1;
1627 int : 0;
1628 int f3 : 5;
1629 int f4 : 7;
1630 unsigned int f5 : 7;
1631 } st1;
1632 printf("bitfield_test:");
1633 printf("sizeof(st1) = %d\n", sizeof(st1));
1635 st1.f1 = 3;
1636 st1.f2 = 1;
1637 st1.f3 = 15;
1638 a = 120;
1639 st1.f4 = a;
1640 st1.f5 = a;
1641 st1.f5++;
1642 printf("%d %d %d %d %d\n",
1643 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1644 sa = st1.f5;
1645 ca = st1.f5;
1646 printf("%d %d\n", sa, ca);
1648 st1.f1 = 7;
1649 if (st1.f1 == -1)
1650 printf("st1.f1 == -1\n");
1651 else
1652 printf("st1.f1 != -1\n");
1653 if (st1.f2 == -1)
1654 printf("st1.f2 == -1\n");
1655 else
1656 printf("st1.f2 != -1\n");
1658 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1659 long-long bitfields whose size is not bigger than int */
1660 struct sbf2 {
1661 long long f1 : 45;
1662 long long : 2;
1663 long long f2 : 35;
1664 unsigned long long f3 : 38;
1665 } st2;
1666 st2.f1 = 0x123456789ULL;
1667 a = 120;
1668 st2.f2 = (long long)a << 25;
1669 st2.f3 = a;
1670 st2.f2++;
1671 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1674 #ifdef __x86_64__
1675 #define FLOAT_FMT "%f\n"
1676 #else
1677 /* x86's float isn't compatible with GCC */
1678 #define FLOAT_FMT "%.5f\n"
1679 #endif
1681 /* declare strto* functions as they are C99 */
1682 double strtod(const char *nptr, char **endptr);
1684 #if defined(_WIN32)
1685 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
1686 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
1687 #else
1688 float strtof(const char *nptr, char **endptr);
1689 LONG_DOUBLE strtold(const char *nptr, char **endptr);
1690 #endif
1692 #define FTEST(prefix, typename, type, fmt)\
1693 void prefix ## cmp(type a, type b)\
1695 printf("%d %d %d %d %d %d\n",\
1696 a == b,\
1697 a != b,\
1698 a < b,\
1699 a > b,\
1700 a >= b,\
1701 a <= b);\
1702 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1705 a + b,\
1706 a - b,\
1707 a * b,\
1708 a / b,\
1709 -a);\
1710 printf(fmt "\n", ++a);\
1711 printf(fmt "\n", a++);\
1712 printf(fmt "\n", a);\
1713 b = 0;\
1714 printf("%d %d\n", !a, !b);\
1716 void prefix ## fcast(type a)\
1718 float fa;\
1719 double da;\
1720 LONG_DOUBLE la;\
1721 int ia;\
1722 long long llia;\
1723 unsigned int ua;\
1724 unsigned long long llua;\
1725 type b;\
1726 fa = a;\
1727 da = a;\
1728 la = a;\
1729 printf("ftof: %f %f %Lf\n", fa, da, la);\
1730 ia = (int)a;\
1731 llia = (long long)a;\
1732 ua = (unsigned int)a;\
1733 llua = (unsigned long long)a;\
1734 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1735 ia = -1234;\
1736 ua = 0x81234500;\
1737 llia = -0x123456789012345LL;\
1738 llua = 0xf123456789012345LLU;\
1739 b = ia;\
1740 printf("itof: " fmt "\n", b);\
1741 b = ua;\
1742 printf("utof: " fmt "\n", b);\
1743 b = llia;\
1744 printf("lltof: " fmt "\n", b);\
1745 b = llua;\
1746 printf("ulltof: " fmt "\n", b);\
1749 float prefix ## retf(type a) { return a; }\
1750 double prefix ## retd(type a) { return a; }\
1751 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1753 void prefix ## call(void)\
1755 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1756 printf("double: %f\n", prefix ## retd(42.123456789));\
1757 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1758 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1761 void prefix ## calc(type x, type y)\
1763 x=x*x;y=y*y;\
1764 printf("%d, %d\n", (int)x, (int)y);\
1765 x=x-y;y=y-x;\
1766 printf("%d, %d\n", (int)x, (int)y);\
1767 x=x/y;y=y/x;\
1768 printf("%d, %d\n", (int)x, (int)y);\
1769 x=x+x;y=y+y;\
1770 printf("%d, %d\n", (int)x, (int)y);\
1773 void prefix ## signed_zeros(void) \
1775 type x = 0.0, y = -0.0, n, p;\
1776 if (x == y)\
1777 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1778 1.0 / x != 1.0 / y);\
1779 else\
1780 printf ("x != y; this is wrong!\n");\
1782 n = -x;\
1783 if (x == n)\
1784 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1785 1.0 / x != 1.0 / n);\
1786 else\
1787 printf ("x != -x; this is wrong!\n");\
1789 p = +y;\
1790 if (x == p)\
1791 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1792 1.0 / x != 1.0 / p);\
1793 else\
1794 printf ("x != +y; this is wrong!\n");\
1795 p = -y;\
1796 if (x == p)\
1797 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1798 1.0 / x != 1.0 / p);\
1799 else\
1800 printf ("x != -y; this is wrong!\n");\
1802 void prefix ## test(void)\
1804 printf("testing '%s'\n", #typename);\
1805 prefix ## cmp(1, 2.5);\
1806 prefix ## cmp(2, 1.5);\
1807 prefix ## cmp(1, 1);\
1808 prefix ## fcast(234.6);\
1809 prefix ## fcast(-2334.6);\
1810 prefix ## call();\
1811 prefix ## calc(1, 1.0000000000000001);\
1812 prefix ## signed_zeros();\
1815 FTEST(f, float, float, "%f")
1816 FTEST(d, double, double, "%f")
1817 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
1819 double ftab1[3] = { 1.2, 3.4, -5.6 };
1822 void float_test(void)
1824 float fa, fb;
1825 double da, db;
1826 int a;
1827 unsigned int b;
1829 printf("float_test:\n");
1830 printf("sizeof(float) = %d\n", sizeof(float));
1831 printf("sizeof(double) = %d\n", sizeof(double));
1832 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
1833 ftest();
1834 dtest();
1835 ldtest();
1836 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1837 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1838 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1839 da = 123;
1840 printf("da=%f\n", da);
1841 fa = 123;
1842 printf("fa=%f\n", fa);
1843 a = 4000000000;
1844 da = a;
1845 printf("da = %f\n", da);
1846 b = 4000000000;
1847 db = b;
1848 printf("db = %f\n", db);
1851 int fib(int n)
1853 if (n <= 2)
1854 return 1;
1855 else
1856 return fib(n-1) + fib(n-2);
1859 void funcptr_test()
1861 void (*func)(int);
1862 int a;
1863 struct {
1864 int dummy;
1865 void (*func)(int);
1866 } st1;
1868 printf("funcptr:\n");
1869 func = &num;
1870 (*func)(12345);
1871 func = num;
1872 a = 1;
1873 a = 1;
1874 func(12345);
1875 /* more complicated pointer computation */
1876 st1.func = num;
1877 st1.func(12346);
1878 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1879 printf("sizeof2 = %d\n", sizeof funcptr_test);
1880 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1881 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1884 void lloptest(long long a, long long b)
1886 unsigned long long ua, ub;
1888 ua = a;
1889 ub = b;
1890 /* arith */
1891 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1892 a + b,
1893 a - b,
1894 a * b);
1896 if (b != 0) {
1897 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1898 a / b,
1899 a % b);
1902 /* binary */
1903 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1904 a & b,
1905 a | b,
1906 a ^ b);
1908 /* tests */
1909 printf("test: %d %d %d %d %d %d\n",
1910 a == b,
1911 a != b,
1912 a < b,
1913 a > b,
1914 a >= b,
1915 a <= b);
1917 printf("utest: %d %d %d %d %d %d\n",
1918 ua == ub,
1919 ua != ub,
1920 ua < ub,
1921 ua > ub,
1922 ua >= ub,
1923 ua <= ub);
1925 /* arith2 */
1926 a++;
1927 b++;
1928 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1929 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
1930 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
1931 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1932 b = ub = 0;
1933 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1936 void llshift(long long a, int b)
1938 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1939 (unsigned long long)a >> b,
1940 a >> b,
1941 a << b);
1942 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1943 (unsigned long long)a >> 3,
1944 a >> 3,
1945 a << 3);
1946 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1947 (unsigned long long)a >> 35,
1948 a >> 35,
1949 a << 35);
1952 void llfloat(void)
1954 float fa;
1955 double da;
1956 LONG_DOUBLE lda;
1957 long long la, lb, lc;
1958 unsigned long long ula, ulb, ulc;
1959 la = 0x12345678;
1960 ula = 0x72345678;
1961 la = (la << 20) | 0x12345;
1962 ula = ula << 33;
1963 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
1965 fa = la;
1966 da = la;
1967 lda = la;
1968 printf("lltof: %f %f %Lf\n", fa, da, lda);
1970 la = fa;
1971 lb = da;
1972 lc = lda;
1973 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
1975 fa = ula;
1976 da = ula;
1977 lda = ula;
1978 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1980 ula = fa;
1981 ulb = da;
1982 ulc = lda;
1983 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
1986 long long llfunc1(int a)
1988 return a * 2;
1991 struct S {
1992 int id;
1993 char item;
1996 long long int value(struct S *v)
1998 return ((long long int)v->item);
2001 void longlong_test(void)
2003 long long a, b, c;
2004 int ia;
2005 unsigned int ua;
2006 printf("longlong_test:\n");
2007 printf("sizeof(long long) = %d\n", sizeof(long long));
2008 ia = -1;
2009 ua = -2;
2010 a = ia;
2011 b = ua;
2012 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2013 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2014 (long long)1,
2015 (long long)-2,
2016 1LL,
2017 0x1234567812345679);
2018 a = llfunc1(-3);
2019 printf(LONG_LONG_FORMAT "\n", a);
2021 lloptest(1000, 23);
2022 lloptest(0xff, 0x1234);
2023 b = 0x72345678 << 10;
2024 lloptest(-3, b);
2025 llshift(0x123, 5);
2026 llshift(-23, 5);
2027 b = 0x72345678LL << 10;
2028 llshift(b, 47);
2030 llfloat();
2031 #if 1
2032 b = 0x12345678;
2033 a = -1;
2034 c = a + b;
2035 printf("%Lx\n", c);
2036 #endif
2038 /* long long reg spill test */
2040 struct S a;
2042 a.item = 3;
2043 printf("%lld\n", value(&a));
2045 lloptest(0x80000000, 0);
2047 /* another long long spill test */
2049 long long *p, v;
2050 v = 1;
2051 p = &v;
2052 p[0]++;
2053 printf("%lld\n", *p);
2056 a = 68719476720LL;
2057 b = 4294967295LL;
2058 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2060 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2063 void manyarg_test(void)
2065 LONG_DOUBLE ld = 1234567891234LL;
2066 printf("manyarg_test:\n");
2067 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2068 1, 2, 3, 4, 5, 6, 7, 8,
2069 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2070 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2071 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2072 1, 2, 3, 4, 5, 6, 7, 8,
2073 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2074 1234567891234LL, 987654321986LL,
2075 42.0, 43.0);
2076 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2077 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2078 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2079 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2080 1234567891234LL, 987654321986LL,
2081 42.0, 43.0);
2082 printf("%d %d %d %d %d %d %d %d %Lf\n",
2083 1, 2, 3, 4, 5, 6, 7, 8, ld);
2084 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2085 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2086 1, 2, 3, 4, 5, 6, 7, 8,
2087 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2088 1234567891234LL, 987654321986LL,
2089 42.0, 43.0, ld);
2090 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2091 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2092 1, 2, 3, 4, 5, 6, 7, 8,
2093 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2094 ld, 1234567891234LL, 987654321986LL,
2095 42.0, 43.0, ld);
2098 void vprintf1(const char *fmt, ...)
2100 va_list ap, aq;
2101 const char *p;
2102 int c, i;
2103 double d;
2104 long long ll;
2105 LONG_DOUBLE ld;
2107 va_start(aq, fmt);
2108 va_copy(ap, aq);
2110 p = fmt;
2111 for(;;) {
2112 c = *p;
2113 if (c == '\0')
2114 break;
2115 p++;
2116 if (c == '%') {
2117 c = *p;
2118 switch(c) {
2119 case '\0':
2120 goto the_end;
2121 case 'd':
2122 i = va_arg(ap, int);
2123 printf("%d", i);
2124 break;
2125 case 'f':
2126 d = va_arg(ap, double);
2127 printf("%f", d);
2128 break;
2129 case 'l':
2130 ll = va_arg(ap, long long);
2131 printf(LONG_LONG_FORMAT, ll);
2132 break;
2133 case 'F':
2134 ld = va_arg(ap, LONG_DOUBLE);
2135 printf("%Lf", ld);
2136 break;
2138 p++;
2139 } else {
2140 putchar(c);
2143 the_end:
2144 va_end(aq);
2145 va_end(ap);
2148 struct myspace {
2149 short int profile;
2152 void stdarg_for_struct(struct myspace bob, ...)
2154 struct myspace george, bill;
2155 va_list ap;
2156 short int validate;
2158 va_start(ap, bob);
2159 bill = va_arg(ap, struct myspace);
2160 george = va_arg(ap, struct myspace);
2161 validate = va_arg(ap, int);
2162 printf("stdarg_for_struct: %d %d %d %d\n",
2163 bob.profile, bill.profile, george.profile, validate);
2164 va_end(ap);
2167 void stdarg_test(void)
2169 LONG_DOUBLE ld = 1234567891234LL;
2170 struct myspace bob;
2172 vprintf1("%d %d %d\n", 1, 2, 3);
2173 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2174 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2175 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2176 vprintf1("%d %f %l %F %d %f %l %F\n",
2177 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2178 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n",
2179 1, 2, 3, 4, 5, 6, 7, 8,
2180 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8);
2181 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2182 1, 2, 3, 4, 5, 6, 7, 8,
2183 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2184 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2185 "%l %l %f %f\n",
2186 1, 2, 3, 4, 5, 6, 7, 8,
2187 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2188 1234567891234LL, 987654321986LL,
2189 42.0, 43.0);
2190 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2191 "%l %l %f %f\n",
2192 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2193 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2194 1234567891234LL, 987654321986LL,
2195 42.0, 43.0);
2196 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2197 1, 2, 3, 4, 5, 6, 7, 8, ld);
2198 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2199 "%l %l %f %f %F\n",
2200 1, 2, 3, 4, 5, 6, 7, 8,
2201 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2202 1234567891234LL, 987654321986LL,
2203 42.0, 43.0, ld);
2204 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2205 "%F %l %l %f %f %F\n",
2206 1, 2, 3, 4, 5, 6, 7, 8,
2207 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2208 ld, 1234567891234LL, 987654321986LL,
2209 42.0, 43.0, ld);
2211 bob.profile = 42;
2212 stdarg_for_struct(bob, bob, bob, bob.profile);
2215 void whitespace_test(void)
2217 char *str;
2219 \f\v
2220 #if 1
2221 pri\
2222 ntf("whitspace:\n");\f\v
2223 #endif
2224 pf("N=%d\n", 2);
2226 #ifdef CORRECT_CR_HANDLING
2227 pri\
2228 ntf("aaa=%d\n", 3);
2229 #endif
2231 pri\
2233 ntf("min=%d\n", 4);
2235 #ifdef ACCEPT_CR_IN_STRINGS
2236 printf("len1=%d\n", strlen("
2237 "));
2238 #ifdef CORRECT_CR_HANDLING
2239 str = "
2241 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2242 #endif
2243 printf("len1=%d\n", strlen("
2245 "));
2246 #endif /* ACCEPT_CR_IN_STRINGS */
2249 int reltab[3] = { 1, 2, 3 };
2251 int *rel1 = &reltab[1];
2252 int *rel2 = &reltab[2];
2254 void relocation_test(void)
2256 printf("*rel1=%d\n", *rel1);
2257 printf("*rel2=%d\n", *rel2);
2260 void old_style_f(a,b,c)
2261 int a, b;
2262 double c;
2264 printf("a=%d b=%d b=%f\n", a, b, c);
2267 void decl_func1(int cmpfn())
2269 printf("cmpfn=%lx\n", (long)cmpfn);
2272 void decl_func2(cmpfn)
2273 int cmpfn();
2275 printf("cmpfn=%lx\n", (long)cmpfn);
2278 void old_style_function(void)
2280 old_style_f((void *)1, 2, 3.0);
2281 decl_func1(NULL);
2282 decl_func2(NULL);
2285 void alloca_test()
2287 #if defined __i386__ || defined __x86_64__ || defined __arm__
2288 char *p = alloca(16);
2289 strcpy(p,"123456789012345");
2290 printf("alloca: p is %s\n", p);
2291 char *demo = "This is only a test.\n";
2292 /* Test alloca embedded in a larger expression */
2293 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2294 #endif
2297 void *bounds_checking_is_enabled()
2299 char ca[10], *cp = ca-1;
2300 return (ca != cp + 1) ? cp : NULL;
2303 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2305 void c99_vla_test(int size1, int size2)
2307 #if defined __i386__ || defined __x86_64__
2308 int size = size1 * size2;
2309 int tab1[size][2], tab2[10][2];
2310 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2312 /* "size" should have been 'captured' at tab1 declaration,
2313 so modifying it should have no effect on VLA behaviour. */
2314 size = size-1;
2316 printf("Test C99 VLA 1 (sizeof): ");
2317 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2318 tab1_ptr = tab1;
2319 tab2_ptr = tab2;
2320 printf("Test C99 VLA 2 (ptrs subtract): ");
2321 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2322 printf("Test C99 VLA 3 (ptr add): ");
2323 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2324 printf("Test C99 VLA 4 (ptr access): ");
2325 tab1[size1][1] = 42;
2326 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2328 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2329 if (bad_ptr = bounds_checking_is_enabled()) {
2330 int *t1 = &tab1[size1 * size2 - 1][3];
2331 int *t2 = &tab2[9][3];
2332 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2333 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2335 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2336 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2337 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2338 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2340 int *i1 = tab1[-1];
2341 int *i2 = tab2[-1];
2342 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2343 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2345 int *x1 = tab1[size1 * size2 + 1];
2346 int *x2 = tab2[10 + 1];
2347 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2348 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2349 } else {
2350 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2352 printf("\n");
2353 #endif
2356 typedef __SIZE_TYPE__ uintptr_t;
2358 void sizeof_test(void)
2360 int a;
2361 int **ptr;
2363 printf("sizeof(int) = %d\n", sizeof(int));
2364 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2365 printf("sizeof(long) = %d\n", sizeof(long));
2366 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2367 printf("sizeof(short) = %d\n", sizeof(short));
2368 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2369 printf("sizeof(char) = %d\n", sizeof(char));
2370 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2371 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2372 a = 1;
2373 printf("sizeof(a++) = %d\n", sizeof a++);
2374 printf("a=%d\n", a);
2375 ptr = NULL;
2376 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2378 /* The type of sizeof should be as large as a pointer, actually
2379 it should be size_t. */
2380 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2381 uintptr_t t = 1;
2382 uintptr_t t2;
2383 /* Effectively <<32, but defined also on 32bit machines. */
2384 t <<= 16;
2385 t <<= 16;
2386 t++;
2387 /* This checks that sizeof really can be used to manipulate
2388 uintptr_t objects, without truncation. */
2389 t2 = t & -sizeof(uintptr_t);
2390 printf ("%lu %lu\n", t, t2);
2392 /* some alignof tests */
2393 printf("__alignof__(int) = %d\n", __alignof__(int));
2394 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2395 printf("__alignof__(short) = %d\n", __alignof__(short));
2396 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2397 printf("__alignof__(char) = %d\n", __alignof__(char));
2398 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2399 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2402 void typeof_test(void)
2404 double a;
2405 typeof(a) b;
2406 typeof(float) c;
2408 a = 1.5;
2409 b = 2.5;
2410 c = 3.5;
2411 printf("a=%f b=%f c=%f\n", a, b, c);
2414 void statement_expr_test(void)
2416 int a, i;
2418 a = 0;
2419 for(i=0;i<10;i++) {
2420 a += 1 +
2421 ( { int b, j;
2422 b = 0;
2423 for(j=0;j<5;j++)
2424 b += j; b;
2425 } );
2427 printf("a=%d\n", a);
2431 void local_label_test(void)
2433 int a;
2434 goto l1;
2436 a = 1 + ({
2437 __label__ l1, l2, l3, l4;
2438 goto l1;
2440 printf("aa1\n");
2441 goto l3;
2443 printf("aa3\n");
2444 goto l4;
2446 printf("aa2\n");
2447 goto l2;
2448 l3:;
2451 printf("a=%d\n", a);
2452 return;
2454 printf("bb1\n");
2455 goto l2;
2457 printf("bb2\n");
2458 goto l4;
2461 /* inline assembler test */
2462 #ifdef __i386__
2464 /* from linux kernel */
2465 static char * strncat1(char * dest,const char * src,size_t count)
2467 int d0, d1, d2, d3;
2468 __asm__ __volatile__(
2469 "repne\n\t"
2470 "scasb\n\t"
2471 "decl %1\n\t"
2472 "movl %8,%3\n"
2473 "1:\tdecl %3\n\t"
2474 "js 2f\n\t"
2475 "lodsb\n\t"
2476 "stosb\n\t"
2477 "testb %%al,%%al\n\t"
2478 "jne 1b\n"
2479 "2:\txorl %2,%2\n\t"
2480 "stosb"
2481 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2482 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2483 : "memory");
2484 return dest;
2487 static char * strncat2(char * dest,const char * src,size_t count)
2489 int d0, d1, d2, d3;
2490 __asm__ __volatile__(
2491 "repne scasb\n\t" /* one-line repne prefix + string op */
2492 "decl %1\n\t"
2493 "movl %8,%3\n"
2494 "1:\tdecl %3\n\t"
2495 "js 2f\n\t"
2496 "lodsb\n\t"
2497 "stosb\n\t"
2498 "testb %%al,%%al\n\t"
2499 "jne 1b\n"
2500 "2:\txorl %2,%2\n\t"
2501 "stosb"
2502 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2503 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2504 : "memory");
2505 return dest;
2508 static inline void * memcpy1(void * to, const void * from, size_t n)
2510 int d0, d1, d2;
2511 __asm__ __volatile__(
2512 "rep ; movsl\n\t"
2513 "testb $2,%b4\n\t"
2514 "je 1f\n\t"
2515 "movsw\n"
2516 "1:\ttestb $1,%b4\n\t"
2517 "je 2f\n\t"
2518 "movsb\n"
2519 "2:"
2520 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2521 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2522 : "memory");
2523 return (to);
2526 static inline void * memcpy2(void * to, const void * from, size_t n)
2528 int d0, d1, d2;
2529 __asm__ __volatile__(
2530 "rep movsl\n\t" /* one-line rep prefix + string op */
2531 "testb $2,%b4\n\t"
2532 "je 1f\n\t"
2533 "movsw\n"
2534 "1:\ttestb $1,%b4\n\t"
2535 "je 2f\n\t"
2536 "movsb\n"
2537 "2:"
2538 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2539 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2540 : "memory");
2541 return (to);
2544 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2546 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2549 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2551 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2554 static __inline__ __const__ unsigned int swab32(unsigned int x)
2556 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2557 "rorl $16,%0\n\t" /* swap words */
2558 "xchgb %b0,%h0" /* swap higher bytes */
2559 :"=q" (x)
2560 : "0" (x));
2561 return x;
2564 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2566 unsigned long long res;
2567 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2568 return res;
2571 static __inline__ unsigned long long inc64(unsigned long long a)
2573 unsigned long long res;
2574 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2575 return res;
2578 unsigned int set;
2580 void asm_test(void)
2582 char buf[128];
2583 unsigned int val;
2585 printf("inline asm:\n");
2586 /* test the no operand case */
2587 asm volatile ("xorl %eax, %eax");
2589 memcpy1(buf, "hello", 6);
2590 strncat1(buf, " worldXXXXX", 3);
2591 printf("%s\n", buf);
2593 memcpy2(buf, "hello", 6);
2594 strncat2(buf, " worldXXXXX", 3);
2595 printf("%s\n", buf);
2597 /* 'A' constraint test */
2598 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2599 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2601 set = 0xff;
2602 sigdelset1(&set, 2);
2603 sigaddset1(&set, 16);
2604 /* NOTE: we test here if C labels are correctly restored after the
2605 asm statement */
2606 goto label1;
2607 label2:
2608 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2609 #ifdef __GNUC__ // works strange with GCC 4.3
2610 set=0x1080fd;
2611 #endif
2612 printf("set=0x%x\n", set);
2613 val = 0x01020304;
2614 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2615 return;
2616 label1:
2617 goto label2;
2620 #else
2622 void asm_test(void)
2626 #endif
2628 #define COMPAT_TYPE(type1, type2) \
2630 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2631 __builtin_types_compatible_p (type1, type2));\
2634 int constant_p_var;
2636 void builtin_test(void)
2638 COMPAT_TYPE(int, int);
2639 COMPAT_TYPE(int, unsigned int);
2640 COMPAT_TYPE(int, char);
2641 COMPAT_TYPE(int, const int);
2642 COMPAT_TYPE(int, volatile int);
2643 COMPAT_TYPE(int *, int *);
2644 COMPAT_TYPE(int *, void *);
2645 COMPAT_TYPE(int *, const int *);
2646 COMPAT_TYPE(char *, unsigned char *);
2647 COMPAT_TYPE(char, unsigned char);
2648 /* space is needed because tcc preprocessor introduces a space between each token */
2649 COMPAT_TYPE(char **, void *);
2650 printf("res = %d\n", __builtin_constant_p(1));
2651 printf("res = %d\n", __builtin_constant_p(1 + 2));
2652 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2653 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2656 #ifndef _WIN32
2657 extern int __attribute__((weak)) weak_f1(void);
2658 extern int __attribute__((weak)) weak_f2(void);
2659 extern int weak_f3(void);
2660 extern int __attribute__((weak)) weak_v1;
2661 extern int __attribute__((weak)) weak_v2;
2662 extern int weak_v3;
2664 extern int (*weak_fpa)() __attribute__((weak));
2665 extern int __attribute__((weak)) (*weak_fpb)();
2666 extern __attribute__((weak)) int (*weak_fpc)();
2668 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
2669 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2670 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
2671 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
2672 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
2673 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
2675 static const size_t dummy = 0;
2676 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
2677 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
2678 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
2680 int some_lib_func(void);
2681 int dummy_impl_of_slf(void) { return 444; }
2682 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
2684 int weak_toolate() __attribute__((weak));
2685 int weak_toolate() { return 0; }
2687 void __attribute__((weak)) weak_test(void)
2689 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2690 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2691 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
2692 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2693 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2694 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
2696 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
2697 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
2698 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
2700 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
2701 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
2702 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
2703 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
2704 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
2705 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
2708 int __attribute__((weak)) weak_f2() { return 222; }
2709 int __attribute__((weak)) weak_f3() { return 333; }
2710 int __attribute__((weak)) weak_v2 = 222;
2711 int __attribute__((weak)) weak_v3 = 333;
2712 #endif
2714 void const_func(const int a)
2718 void const_warn_test(void)
2720 const_func(1);
2723 struct condstruct {
2724 int i;
2727 int getme (struct condstruct *s, int i)
2729 int i1 = (i == 0 ? 0 : s)->i;
2730 int i2 = (i == 0 ? s : 0)->i;
2731 int i3 = (i == 0 ? (void*)0 : s)->i;
2732 int i4 = (i == 0 ? s : (void*)0)->i;
2733 return i1 + i2 + i3 + i4;
2736 struct global_data
2738 int a[40];
2739 int *b[40];
2742 struct global_data global_data;
2744 int global_data_getstuff (int *, int);
2746 void global_data_callit (int i)
2748 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
2751 int global_data_getstuff (int *p, int i)
2753 return *p + i;
2756 void global_data_test (void)
2758 global_data.a[0] = 42;
2759 global_data.b[0] = &global_data.a[0];
2760 global_data_callit (0);
2761 printf ("%d\n", global_data.a[0]);
2764 struct cmpcmpS
2766 unsigned char fill : 3;
2767 unsigned char b1 : 1;
2768 unsigned char b2 : 1;
2769 unsigned char fill2 : 3;
2772 int glob1, glob2, glob3;
2774 void compare_comparisons (struct cmpcmpS *s)
2776 if (s->b1 != (glob1 == glob2)
2777 || (s->b2 != (glob1 == glob3)))
2778 printf ("comparing comparisons broken\n");
2781 void cmp_comparison_test(void)
2783 struct cmpcmpS s;
2784 s.b1 = 1;
2785 glob1 = 42; glob2 = 42;
2786 s.b2 = 0;
2787 glob3 = 43;
2788 compare_comparisons (&s);
2791 int fcompare (double a, double b, int code)
2793 switch (code) {
2794 case 0: return a == b;
2795 case 1: return a != b;
2796 case 2: return a < b;
2797 case 3: return a >= b;
2798 case 4: return a > b;
2799 case 5: return a <= b;
2803 void math_cmp_test(void)
2805 double nan = 0.0/0.0;
2806 double one = 1.0;
2807 double two = 2.0;
2808 int comp = 0;
2809 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
2811 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
2812 And it does this in various ways so that all code generation paths
2813 are checked (generating inverted tests, or non-inverted tests, or
2814 producing a 0/1 value without jumps (that's done in the fcompare
2815 function). */
2816 #define FCMP(a,b,op,iop,code) \
2817 if (fcompare (a,b,code)) \
2818 bug (a,b,op,iop,1); \
2819 if (a op b) \
2820 bug (a,b,op,iop,2); \
2821 if (a iop b) \
2823 else \
2824 bug (a,b,op,iop,3); \
2825 if ((a op b) || comp) \
2826 bug (a,b,op,iop,4); \
2827 if ((a iop b) || comp) \
2829 else \
2830 bug (a,b,op,iop,5);
2832 /* Equality tests. */
2833 FCMP(nan, nan, ==, !=, 0);
2834 FCMP(one, two, ==, !=, 0);
2835 FCMP(one, one, !=, ==, 1);
2836 /* Non-equality is a bit special. */
2837 if (!fcompare (nan, nan, 1))
2838 bug (nan, nan, !=, ==, 6);
2840 /* Relational tests on numbers. */
2841 FCMP(two, one, <, >=, 2);
2842 FCMP(one, two, >=, <, 3);
2843 FCMP(one, two, >, <=, 4);
2844 FCMP(two, one, <=, >, 5);
2846 /* Relational tests on NaNs. Note that the inverse op here is
2847 always !=, there's no operator in C that is equivalent to !(a < b),
2848 when NaNs are involved, same for the other relational ops. */
2849 FCMP(nan, nan, <, !=, 2);
2850 FCMP(nan, nan, >=, !=, 3);
2851 FCMP(nan, nan, >, !=, 4);
2852 FCMP(nan, nan, <=, !=, 5);
2855 double get100 () { return 100.0; }
2857 void callsave_test(void)
2859 #if defined __i386__ || defined __x86_64__ || defined __arm__
2860 int i, s; double *d; double t;
2861 s = sizeof (double);
2862 printf ("callsavetest: %d\n", s);
2863 d = alloca (sizeof(double));
2864 d[0] = 10.0;
2865 /* x86-64 had a bug were the next call to get100 would evict
2866 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
2867 in int type, not pointer type. When alloca returns a pointer
2868 with the high 32 bit set (which is likely on x86-64) the access
2869 generates a segfault. */
2870 i = d[0] > get100 ();
2871 printf ("%d\n", i);
2872 #endif
2876 void bfa3(ptrdiff_t str_offset)
2878 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
2880 void bfa2(ptrdiff_t str_offset)
2882 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
2883 bfa3(str_offset);
2885 void bfa1(ptrdiff_t str_offset)
2887 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
2888 bfa2(str_offset);
2891 void builtin_frame_address_test(void)
2893 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
2894 #ifndef __arm__
2895 char str[] = "__builtin_frame_address";
2896 char *fp0 = __builtin_frame_address(0);
2898 printf("str: %s\n", str);
2899 bfa1(str-fp0);
2900 #endif