Fix parsing array typedefs of unknown size
[tinycc.git] / tests / tcctest.c
blob0b894aa95fdc0b83d8c86f2ad346bb355a2bed66
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 #include "tcctest.h"
64 void intdiv_test();
65 void string_test();
66 void expr_test();
67 void macro_test();
68 void recursive_macro_test();
69 void scope_test();
70 void forward_test();
71 void funcptr_test();
72 void loop_test();
73 void switch_test();
74 void goto_test();
75 void enum_test();
76 void typedef_test();
77 void struct_test();
78 void array_test();
79 void expr_ptr_test();
80 void bool_test();
81 void expr2_test();
82 void constant_expr_test();
83 void expr_cmp_test();
84 void char_short_test();
85 void init_test(void);
86 void compound_literal_test(void);
87 int kr_test();
88 void struct_assign_test(void);
89 void cast_test(void);
90 void bitfield_test(void);
91 void c99_bool_test(void);
92 void float_test(void);
93 void longlong_test(void);
94 void manyarg_test(void);
95 void stdarg_test(void);
96 void whitespace_test(void);
97 void relocation_test(void);
98 void old_style_function(void);
99 void alloca_test(void);
100 void c99_vla_test(int size1, int size2);
101 void sizeof_test(void);
102 void typeof_test(void);
103 void local_label_test(void);
104 void statement_expr_test(void);
105 void asm_test(void);
106 void builtin_test(void);
107 void weak_test(void);
108 void global_data_test(void);
109 void cmp_comparison_test(void);
110 void math_cmp_test(void);
111 void callsave_test(void);
112 void builtin_frame_address_test(void);
113 void attrib_test(void);
115 int fib(int n);
116 void num(int n);
117 void forward_ref(void);
118 int isid(int c);
120 /* Line joining happens before tokenization, so the following
121 must be parsed as ellipsis. */
122 void funny_line_continuation (int, ..\
123 . );
125 char via_volatile (char);
127 #define A 2
128 #define N 1234 + A
129 #define pf printf
130 #define M1(a, b) (a) + (b)
132 #define str\
133 (s) # s
134 #define glue(a, b) a ## b
135 #define xglue(a, b) glue(a, b)
136 #define HIGHLOW "hello"
137 #define LOW LOW ", world"
139 static int onetwothree = 123;
140 #define onetwothree4 onetwothree
141 #define onetwothree xglue(onetwothree,4)
143 #define min(a, b) ((a) < (b) ? (a) : (b))
145 #ifdef C99_MACROS
146 #define dprintf(level,...) printf(__VA_ARGS__)
147 #endif
149 /* gcc vararg macros */
150 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
152 #define MACRO_NOARGS()
154 #define AAA 3
155 #undef AAA
156 #define AAA 4
158 #if 1
159 #define B3 1
160 #elif 1
161 #define B3 2
162 #elif 0
163 #define B3 3
164 #else
165 #define B3 4
166 #endif
168 #define __INT64_C(c) c ## LL
169 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
171 int qq(int x)
173 return x + 40;
175 #define qq(x) x
177 #define spin_lock(lock) do { } while (0)
178 #define wq_spin_lock spin_lock
179 #define TEST2() wq_spin_lock(a)
181 #define UINT_MAX ((unsigned) -1)
183 void intdiv_test(void)
185 printf("18/21=%u\n", 18/21);
186 printf("18%%21=%u\n", 18%21);
187 printf("41/21=%u\n", 41/21);
188 printf("41%%21=%u\n", 41%21);
189 printf("42/21=%u\n", 42/21);
190 printf("42%%21=%u\n", 42%21);
191 printf("43/21=%u\n", 43/21);
192 printf("43%%21=%u\n", 43%21);
193 printf("126/21=%u\n", 126/21);
194 printf("126%%21=%u\n", 126%21);
195 printf("131/21=%u\n", 131/21);
196 printf("131%%21=%u\n", 131%21);
197 printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2);
198 printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2);
200 printf("18/-21=%u\n", 18/-21);
201 printf("18%%-21=%u\n", 18%-21);
202 printf("41/-21=%u\n", 41/-21);
203 printf("41%%-21=%u\n", 41%-21);
204 printf("42/-21=%u\n", 42/-21);
205 printf("42%%-21=%u\n", 42%-21);
206 printf("43/-21=%u\n", 43/-21);
207 printf("43%%-21=%u\n", 43%-21);
208 printf("126/-21=%u\n", 126/-21);
209 printf("126%%-21=%u\n", 126%-21);
210 printf("131/-21=%u\n", 131/-21);
211 printf("131%%-21=%u\n", 131%-21);
212 printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2);
213 printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2);
215 printf("-18/21=%u\n", -18/21);
216 printf("-18%%21=%u\n", -18%21);
217 printf("-41/21=%u\n", -41/21);
218 printf("-41%%21=%u\n", -41%21);
219 printf("-42/21=%u\n", -42/21);
220 printf("-42%%21=%u\n", -42%21);
221 printf("-43/21=%u\n", -43/21);
222 printf("-43%%21=%u\n", -43%21);
223 printf("-126/21=%u\n", -126/21);
224 printf("-126%%21=%u\n", -126%21);
225 printf("-131/21=%u\n", -131/21);
226 printf("-131%%21=%u\n", -131%21);
227 printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2);
228 printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2);
230 printf("-18/-21=%u\n", -18/-21);
231 printf("-18%%-21=%u\n", -18%-21);
232 printf("-41/-21=%u\n", -41/-21);
233 printf("-41%%-21=%u\n", -41%-21);
234 printf("-42/-21=%u\n", -42/-21);
235 printf("-42%%-21=%u\n", -42%-21);
236 printf("-43/-21=%u\n", -43/-21);
237 printf("-43%%-21=%u\n", -43%-21);
238 printf("-126/-21=%u\n", -126/-21);
239 printf("-126%%-21=%u\n", -126%-21);
240 printf("-131/-21=%u\n", -131/-21);
241 printf("-131%%-21=%u\n", -131%-21);
242 printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2);
243 printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2);
246 void macro_test(void)
248 printf("macro:\n");\f\v
249 pf("N=%d\n", N);
250 printf("aaa=%d\n", AAA);
252 printf("min=%d\n", min(1, min(2, -1)));
254 printf("s1=%s\n", glue(HIGH, LOW));
255 printf("s2=%s\n", xglue(HIGH, LOW));
256 printf("s3=%s\n", str("c"));
257 printf("s4=%s\n", str(a1));
258 printf("B3=%d\n", B3);
260 printf("onetwothree=%d\n", onetwothree);
262 #ifdef A
263 printf("A defined\n");
264 #endif
265 #ifdef B
266 printf("B defined\n");
267 #endif
268 #ifdef A
269 printf("A defined\n");
270 #else
271 printf("A not defined\n");
272 #endif
273 #ifdef B
274 printf("B defined\n");
275 #else
276 printf("B not defined\n");
277 #endif
279 #ifdef A
280 printf("A defined\n");
281 #ifdef B
282 printf("B1 defined\n");
283 #else
284 printf("B1 not defined\n");
285 #endif
286 #else
287 printf("A not defined\n");
288 #ifdef B
289 printf("B2 defined\n");
290 #else
291 printf("B2 not defined\n");
292 #endif
293 #endif
295 #if 1+1
296 printf("test true1\n");
297 #endif
298 #if 0
299 printf("test true2\n");
300 #endif
301 #if 1-1
302 printf("test true3\n");
303 #endif
304 #if defined(A)
305 printf("test trueA\n");
306 #endif
307 #if defined(B)
308 printf("test trueB\n");
309 #endif
311 #if 0
312 printf("test 0\n");
313 #elif 0
314 printf("test 1\n");
315 #elif 2
316 printf("test 2\n");
317 #else
318 printf("test 3\n");
319 #endif
321 MACRO_NOARGS();
323 #ifdef __LINE__
324 printf("__LINE__ defined\n");
325 #endif
327 printf("__LINE__=%d __FILE__=%s\n",
328 __LINE__, __FILE__);
329 #if 0
330 #line 200
331 printf("__LINE__=%d __FILE__=%s\n",
332 __LINE__, __FILE__);
333 #line 203 "test"
334 printf("__LINE__=%d __FILE__=%s\n",
335 __LINE__, __FILE__);
336 #line 227 "tcctest.c"
337 #endif
339 /* not strictly preprocessor, but we test it there */
340 #ifdef C99_MACROS
341 printf("__func__ = %s\n", __func__);
342 dprintf(1, "vaarg=%d\n", 1);
343 #endif
344 dprintf1(1, "vaarg1\n");
345 dprintf1(1, "vaarg1=%d\n", 2);
346 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
348 /* gcc extension */
349 printf("func='%s'\n", __FUNCTION__);
351 /* complicated macros in glibc */
352 printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN);
354 int a;
355 a = 1;
356 glue(a+, +);
357 printf("a=%d\n", a);
358 glue(a <, <= 2);
359 printf("a=%d\n", a);
362 /* macro function with argument outside the macro string */
363 #define MF_s MF_hello
364 #define MF_hello(msg) printf("%s\n",msg)
366 #define MF_t printf("tralala\n"); MF_hello
368 MF_s("hi");
369 MF_t("hi");
371 /* test macro substituion inside args (should not eat stream) */
372 printf("qq=%d\n", qq(qq)(2));
374 /* test zero argument case. NOTE: gcc 2.95.x does not accept a
375 null argument without a space. gcc 3.2 fixes that. */
377 #define qq1(x) 1
378 printf("qq1=%d\n", qq1( ));
380 /* comment with stray handling *\
382 /* this is a valid *\/ comment */
383 /* this is a valid comment *\*/
384 // this is a valid\
385 comment
387 /* test function macro substitution when the function name is
388 substituted */
389 TEST2();
391 /* And again when the name and parenthes are separated by a
392 comment. */
393 TEST2 /* the comment */ ();
395 printf("%s\n", get_basefile_from_header());
396 printf("%s\n", __BASE_FILE__);
397 printf("%s\n", get_file_from_header());
398 printf("%s\n", __FILE__);
402 static void print_num(char *fn, int line, int num) {
403 printf("fn %s, line %d, num %d\n", fn, line, num);
406 void recursive_macro_test(void)
409 #define ELF32_ST_TYPE(val) ((val) & 0xf)
410 #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
411 #define STB_WEAK 2 /* Weak symbol */
412 #define ELFW(type) ELF##32##_##type
413 printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123)));
415 #define WRAP(x) x
417 #define print_num(x) print_num(__FILE__,__LINE__,x)
418 print_num(123);
419 WRAP(print_num(123));
420 WRAP(WRAP(print_num(123)));
422 static struct recursive_macro { int rm_field; } G;
423 #define rm_field (G.rm_field)
424 printf("rm_field = %d\n", rm_field);
425 printf("rm_field = %d\n", WRAP(rm_field));
426 WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field))));
429 int op(a,b)
431 return a / b;
434 int ret(a)
436 if (a == 2)
437 return 1;
438 if (a == 3)
439 return 2;
440 return 0;
443 void ps(const char *s)
445 int c;
446 while (1) {
447 c = *s;
448 if (c == 0)
449 break;
450 printf("%c", c);
451 s++;
455 const char foo1_string[] = "\
456 bar\n\
457 test\14\
460 void string_test()
462 unsigned int b;
463 printf("string:\n");
464 printf("\141\1423\143\n");/* dezdez test */
465 printf("\x41\x42\x43\x3a\n");
466 printf("c=%c\n", 'r');
467 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
468 printf("foo1_string='%s'\n", foo1_string);
469 #if 0
470 printf("wstring=%S\n", L"abc");
471 printf("wstring=%S\n", L"abc" L"def" "ghi");
472 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
473 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
474 #endif
475 ps("test\n");
476 b = 32;
477 while ((b = b + 1) < 96) {
478 printf("%c", b);
480 printf("\n");
481 printf("fib=%d\n", fib(33));
482 b = 262144;
483 while (b != 0x80000000) {
484 num(b);
485 b = b * 2;
489 void loop_test()
491 int i;
492 i = 0;
493 while (i < 10)
494 printf("%d", i++);
495 printf("\n");
496 for(i = 0; i < 10;i++)
497 printf("%d", i);
498 printf("\n");
499 i = 0;
500 do {
501 printf("%d", i++);
502 } while (i < 10);
503 printf("\n");
505 char count = 123;
506 /* c99 for loop init test */
507 for (size_t count = 1; count < 3; count++)
508 printf("count=%d\n", count);
509 printf("count = %d\n", count);
511 /* break/continue tests */
512 i = 0;
513 while (1) {
514 if (i == 6)
515 break;
516 i++;
517 if (i == 3)
518 continue;
519 printf("%d", i);
521 printf("\n");
523 /* break/continue tests */
524 i = 0;
525 do {
526 if (i == 6)
527 break;
528 i++;
529 if (i == 3)
530 continue;
531 printf("%d", i);
532 } while(1);
533 printf("\n");
535 for(i = 0;i < 10;i++) {
536 if (i == 3)
537 continue;
538 printf("%d", i);
540 printf("\n");
543 typedef int typedef_and_label;
545 void goto_test()
547 int i;
548 static void *label_table[3] = { &&label1, &&label2, &&label3 };
550 printf("goto:\n");
551 i = 0;
552 /* This needs to parse as label, not as start of decl. */
553 typedef_and_label:
554 s_loop:
555 if (i >= 10)
556 goto s_end;
557 printf("%d", i);
558 i++;
559 goto s_loop;
560 s_end:
561 printf("\n");
563 /* we also test computed gotos (GCC extension) */
564 for(i=0;i<3;i++) {
565 goto *label_table[i];
566 label1:
567 printf("label1\n");
568 goto next;
569 label2:
570 printf("label2\n");
571 goto next;
572 label3:
573 printf("label3\n");
574 next: ;
578 enum {
580 E1 = 2,
581 E2 = 4,
586 enum test {
587 E5 = 1000,
590 void enum_test()
592 enum test b1;
593 printf("enum:\n%d %d %d %d %d %d\n",
594 E0, E1, E2, E3, E4, E5);
595 b1 = 1;
596 printf("b1=%d\n", b1);
599 typedef int *my_ptr;
601 typedef int mytype1;
602 typedef int mytype2;
604 void typedef_test()
606 my_ptr a;
607 mytype1 mytype2;
608 int b;
610 a = &b;
611 *a = 1234;
612 printf("typedef:\n");
613 printf("a=%d\n", *a);
614 mytype2 = 2;
615 printf("mytype2=%d\n", mytype2);
618 void forward_test()
620 printf("forward:\n");
621 forward_ref();
622 forward_ref();
626 void forward_ref(void)
628 printf("forward ok\n");
631 typedef struct struct1 {
632 int f1;
633 int f2, f3;
634 union union1 {
635 int v1;
636 int v2;
637 } u;
638 char str[3];
639 } struct1;
641 struct struct2 {
642 int a;
643 char b;
646 union union2 {
647 int w1;
648 int w2;
651 struct struct1 st1, st2;
653 struct empty_mem {
654 /* nothing */ ;
655 int x;
658 int main(int argc, char **argv)
660 string_test();
661 expr_test();
662 macro_test();
663 recursive_macro_test();
664 scope_test();
665 forward_test();
666 funcptr_test();
667 loop_test();
668 switch_test();
669 goto_test();
670 enum_test();
671 typedef_test();
672 struct_test();
673 array_test();
674 expr_ptr_test();
675 bool_test();
676 expr2_test();
677 constant_expr_test();
678 expr_cmp_test();
679 char_short_test();
680 init_test();
681 compound_literal_test();
682 kr_test();
683 struct_assign_test();
684 cast_test();
685 bitfield_test();
686 c99_bool_test();
687 float_test();
688 longlong_test();
689 manyarg_test();
690 stdarg_test();
691 whitespace_test();
692 relocation_test();
693 old_style_function();
694 alloca_test();
695 c99_vla_test(5, 2);
696 sizeof_test();
697 typeof_test();
698 statement_expr_test();
699 local_label_test();
700 asm_test();
701 builtin_test();
702 #ifndef _WIN32
703 weak_test();
704 #endif
705 global_data_test();
706 cmp_comparison_test();
707 math_cmp_test();
708 callsave_test();
709 builtin_frame_address_test();
710 intdiv_test();
711 if (via_volatile (42) != 42)
712 printf ("via_volatile broken\n");
713 attrib_test();
714 return 0;
717 int tab[3];
718 int tab2[3][2];
720 int g;
722 void f1(g)
724 printf("g1=%d\n", g);
727 void scope_test()
729 printf("scope:\n");
730 g = 2;
731 f1(1);
732 printf("g2=%d\n", g);
734 int g;
735 g = 3;
736 printf("g3=%d\n", g);
738 int g;
739 g = 4;
740 printf("g4=%d\n", g);
743 printf("g5=%d\n", g);
746 void array_test()
748 int i, j, a[4];
750 printf("array:\n");
751 printf("sizeof(a) = %d\n", sizeof(a));
752 printf("sizeof(\"a\") = %d\n", sizeof("a"));
753 #ifdef C99_MACROS
754 printf("sizeof(__func__) = %d\n", sizeof(__func__));
755 #endif
756 printf("sizeof tab %d\n", sizeof(tab));
757 printf("sizeof tab2 %d\n", sizeof tab2);
758 tab[0] = 1;
759 tab[1] = 2;
760 tab[2] = 3;
761 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
762 for(i=0;i<3;i++)
763 for(j=0;j<2;j++)
764 tab2[i][j] = 10 * i + j;
765 for(i=0;i<3*2;i++) {
766 printf(" %3d", ((int *)tab2)[i]);
768 printf("\n");
769 printf("sizeof(size_t)=%d\n", sizeof(size_t));
770 printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t));
773 void expr_test()
775 int a, b;
776 a = 0;
777 printf("%d\n", a += 1);
778 printf("%d\n", a -= 2);
779 printf("%d\n", a *= 31232132);
780 printf("%d\n", a /= 4);
781 printf("%d\n", a %= 20);
782 printf("%d\n", a &= 6);
783 printf("%d\n", a ^= 7);
784 printf("%d\n", a |= 8);
785 printf("%d\n", a >>= 3);
786 printf("%d\n", a <<= 4);
788 a = 22321;
789 b = -22321;
790 printf("%d\n", a + 1);
791 printf("%d\n", a - 2);
792 printf("%d\n", a * 312);
793 printf("%d\n", a / 4);
794 printf("%d\n", b / 4);
795 printf("%d\n", (unsigned)b / 4);
796 printf("%d\n", a % 20);
797 printf("%d\n", b % 20);
798 printf("%d\n", (unsigned)b % 20);
799 printf("%d\n", a & 6);
800 printf("%d\n", a ^ 7);
801 printf("%d\n", a | 8);
802 printf("%d\n", a >> 3);
803 printf("%d\n", b >> 3);
804 printf("%d\n", (unsigned)b >> 3);
805 printf("%d\n", a << 4);
806 printf("%d\n", ~a);
807 printf("%d\n", -a);
808 printf("%d\n", +a);
810 printf("%d\n", 12 + 1);
811 printf("%d\n", 12 - 2);
812 printf("%d\n", 12 * 312);
813 printf("%d\n", 12 / 4);
814 printf("%d\n", 12 % 20);
815 printf("%d\n", 12 & 6);
816 printf("%d\n", 12 ^ 7);
817 printf("%d\n", 12 | 8);
818 printf("%d\n", 12 >> 2);
819 printf("%d\n", 12 << 4);
820 printf("%d\n", ~12);
821 printf("%d\n", -12);
822 printf("%d\n", +12);
823 printf("%d %d %d %d\n",
824 isid('a'),
825 isid('g'),
826 isid('T'),
827 isid('('));
830 int isid(int c)
832 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
835 /**********************/
837 int vstack[10], *vstack_ptr;
839 void vpush(int vt, int vc)
841 *vstack_ptr++ = vt;
842 *vstack_ptr++ = vc;
845 void vpop(int *ft, int *fc)
847 *fc = *--vstack_ptr;
848 *ft = *--vstack_ptr;
851 void expr2_test()
853 int a, b;
855 printf("expr2:\n");
856 vstack_ptr = vstack;
857 vpush(1432432, 2);
858 vstack_ptr[-2] &= ~0xffffff80;
859 vpop(&a, &b);
860 printf("res= %d %d\n", a, b);
863 void constant_expr_test()
865 int a;
866 printf("constant_expr:\n");
867 a = 3;
868 printf("%d\n", a * 16);
869 printf("%d\n", a * 1);
870 printf("%d\n", a + 0);
873 int tab4[10];
875 void expr_ptr_test()
877 int *p, *q;
878 int i = -1;
880 printf("expr_ptr:\n");
881 p = tab4;
882 q = tab4 + 10;
883 printf("diff=%d\n", q - p);
884 p++;
885 printf("inc=%d\n", p - tab4);
886 p--;
887 printf("dec=%d\n", p - tab4);
888 ++p;
889 printf("inc=%d\n", p - tab4);
890 --p;
891 printf("dec=%d\n", p - tab4);
892 printf("add=%d\n", p + 3 - tab4);
893 printf("add=%d\n", 3 + p - tab4);
895 /* check if 64bit support is ok */
896 q = p = 0;
897 q += i;
898 printf("%p %p %ld\n", q, p, p-q);
899 printf("%d %d %d %d %d %d\n",
900 p == q, p != q, p < q, p <= q, p >= q, p > q);
901 i = 0xf0000000;
902 p += i;
903 printf("%p %p %ld\n", q, p, p-q);
904 printf("%d %d %d %d %d %d\n",
905 p == q, p != q, p < q, p <= q, p >= q, p > q);
906 p = (int *)((char *)p + 0xf0000000);
907 printf("%p %p %ld\n", q, p, p-q);
908 printf("%d %d %d %d %d %d\n",
909 p == q, p != q, p < q, p <= q, p >= q, p > q);
910 p += 0xf0000000;
911 printf("%p %p %ld\n", q, p, p-q);
912 printf("%d %d %d %d %d %d\n",
913 p == q, p != q, p < q, p <= q, p >= q, p > q);
915 struct size12 {
916 int i, j, k;
918 struct size12 s[2], *sp = s;
919 int i, j;
920 sp->i = 42;
921 sp++;
922 j = -1;
923 printf("%d\n", sp[j].i);
927 void expr_cmp_test()
929 int a, b;
930 printf("constant_expr:\n");
931 a = -1;
932 b = 1;
933 printf("%d\n", a == a);
934 printf("%d\n", a != a);
936 printf("%d\n", a < b);
937 printf("%d\n", a <= b);
938 printf("%d\n", a <= a);
939 printf("%d\n", b >= a);
940 printf("%d\n", a >= a);
941 printf("%d\n", b > a);
943 printf("%d\n", (unsigned)a < b);
944 printf("%d\n", (unsigned)a <= b);
945 printf("%d\n", (unsigned)a <= a);
946 printf("%d\n", (unsigned)b >= a);
947 printf("%d\n", (unsigned)a >= a);
948 printf("%d\n", (unsigned)b > a);
951 struct empty {
954 struct aligntest1 {
955 char a[10];
958 struct aligntest2 {
959 int a;
960 char b[10];
963 struct aligntest3 {
964 double a, b;
967 struct aligntest4 {
968 double a[0];
971 void struct_test()
973 struct1 *s;
974 union union2 u;
976 printf("struct:\n");
977 printf("sizes: %d %d %d %d\n",
978 sizeof(struct struct1),
979 sizeof(struct struct2),
980 sizeof(union union1),
981 sizeof(union union2));
982 st1.f1 = 1;
983 st1.f2 = 2;
984 st1.f3 = 3;
985 printf("st1: %d %d %d\n",
986 st1.f1, st1.f2, st1.f3);
987 st1.u.v1 = 1;
988 st1.u.v2 = 2;
989 printf("union1: %d\n", st1.u.v1);
990 u.w1 = 1;
991 u.w2 = 2;
992 printf("union2: %d\n", u.w1);
993 s = &st2;
994 s->f1 = 3;
995 s->f2 = 2;
996 s->f3 = 1;
997 printf("st2: %d %d %d\n",
998 s->f1, s->f2, s->f3);
999 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
1001 /* align / size tests */
1002 printf("aligntest1 sizeof=%d alignof=%d\n",
1003 sizeof(struct aligntest1), __alignof__(struct aligntest1));
1004 printf("aligntest2 sizeof=%d alignof=%d\n",
1005 sizeof(struct aligntest2), __alignof__(struct aligntest2));
1006 printf("aligntest3 sizeof=%d alignof=%d\n",
1007 sizeof(struct aligntest3), __alignof__(struct aligntest3));
1008 printf("aligntest4 sizeof=%d alignof=%d\n",
1009 sizeof(struct aligntest4), __alignof__(struct aligntest4));
1011 /* empty structures (GCC extension) */
1012 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
1013 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
1016 /* XXX: depend on endianness */
1017 void char_short_test()
1019 int var1, var2;
1021 printf("char_short:\n");
1023 var1 = 0x01020304;
1024 var2 = 0xfffefdfc;
1025 printf("s8=%d %d\n",
1026 *(char *)&var1, *(char *)&var2);
1027 printf("u8=%d %d\n",
1028 *(unsigned char *)&var1, *(unsigned char *)&var2);
1029 printf("s16=%d %d\n",
1030 *(short *)&var1, *(short *)&var2);
1031 printf("u16=%d %d\n",
1032 *(unsigned short *)&var1, *(unsigned short *)&var2);
1033 printf("s32=%d %d\n",
1034 *(int *)&var1, *(int *)&var2);
1035 printf("u32=%d %d\n",
1036 *(unsigned int *)&var1, *(unsigned int *)&var2);
1037 *(char *)&var1 = 0x08;
1038 printf("var1=%x\n", var1);
1039 *(short *)&var1 = 0x0809;
1040 printf("var1=%x\n", var1);
1041 *(int *)&var1 = 0x08090a0b;
1042 printf("var1=%x\n", var1);
1045 /******************/
1047 typedef struct Sym {
1048 int v;
1049 int t;
1050 int c;
1051 struct Sym *next;
1052 struct Sym *prev;
1053 } Sym;
1055 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
1056 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
1058 static int toupper1(int a)
1060 return TOUPPER(a);
1063 void bool_test()
1065 int *s, a, b, t, f, i;
1067 a = 0;
1068 s = (void*)0;
1069 printf("!s=%d\n", !s);
1071 if (!s || !s[0])
1072 a = 1;
1073 printf("a=%d\n", a);
1075 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
1076 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
1077 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
1078 #if 1 && 1
1079 printf("a1\n");
1080 #endif
1081 #if 1 || 0
1082 printf("a2\n");
1083 #endif
1084 #if 1 ? 0 : 1
1085 printf("a3\n");
1086 #endif
1087 #if 0 ? 0 : 1
1088 printf("a4\n");
1089 #endif
1091 a = 4;
1092 printf("b=%d\n", a + (0 ? 1 : a / 2));
1094 /* test register spilling */
1095 a = 10;
1096 b = 10;
1097 a = (a + b) * ((a < b) ?
1098 ((b - a) * (a - b)): a + b);
1099 printf("a=%d\n", a);
1101 /* test complex || or && expressions */
1102 t = 1;
1103 f = 0;
1104 a = 32;
1105 printf("exp=%d\n", f == (32 <= a && a <= 3));
1106 printf("r=%d\n", (t || f) + (t && f));
1108 /* test ? : cast */
1110 int aspect_on;
1111 int aspect_native = 65536;
1112 double bfu_aspect = 1.0;
1113 int aspect;
1114 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
1115 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
1116 printf("aspect=%d\n", aspect);
1120 /* test ? : GCC extension */
1122 static int v1 = 34 ? : -1; /* constant case */
1123 static int v2 = 0 ? : -1; /* constant case */
1124 int a = 30;
1126 printf("%d %d\n", v1, v2);
1127 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
1130 /* again complex expression */
1131 for(i=0;i<256;i++) {
1132 if (toupper1 (i) != TOUPPER (i))
1133 printf("error %d\n", i);
1137 /* GCC accepts that */
1138 static int tab_reinit[];
1139 static int tab_reinit[10];
1141 //int cinit1; /* a global variable can be defined several times without error ! */
1142 int cinit1;
1143 int cinit1;
1144 int cinit1 = 0;
1145 int *cinit2 = (int []){3, 2, 1};
1147 void compound_literal_test(void)
1149 int *p, i;
1150 char *q, *q3;
1152 printf("compound_test:\n");
1154 p = (int []){1, 2, 3};
1155 for(i=0;i<3;i++)
1156 printf(" %d", p[i]);
1157 printf("\n");
1159 for(i=0;i<3;i++)
1160 printf("%d", cinit2[i]);
1161 printf("\n");
1163 q = "tralala1";
1164 printf("q1=%s\n", q);
1166 q = (char *){ "tralala2" };
1167 printf("q2=%s\n", q);
1169 q3 = (char *){ q };
1170 printf("q3=%s\n", q3);
1172 q = (char []){ "tralala3" };
1173 printf("q4=%s\n", q);
1175 #ifdef ALL_ISOC99
1176 p = (int []){1, 2, cinit1 + 3};
1177 for(i=0;i<3;i++)
1178 printf(" %d", p[i]);
1179 printf("\n");
1181 for(i=0;i<3;i++) {
1182 p = (int []){1, 2, 4 + i};
1183 printf("%d %d %d\n",
1184 p[0],
1185 p[1],
1186 p[2]);
1188 #endif
1191 /* K & R protos */
1193 kr_func1(a, b)
1195 return a + b;
1198 int kr_func2(a, b)
1200 return a + b;
1203 kr_test()
1205 printf("kr_test:\n");
1206 printf("func1=%d\n", kr_func1(3, 4));
1207 printf("func2=%d\n", kr_func2(3, 4));
1208 return 0;
1211 void num(int n)
1213 char *tab, *p;
1214 tab = (char*)malloc(20);
1215 p = tab;
1216 while (1) {
1217 *p = 48 + (n % 10);
1218 p++;
1219 n = n / 10;
1220 if (n == 0)
1221 break;
1223 while (p != tab) {
1224 p--;
1225 printf("%c", *p);
1227 printf("\n");
1228 free(tab);
1231 /* structure assignment tests */
1232 struct structa1 {
1233 int f1;
1234 char f2;
1237 struct structa1 ssta1;
1239 void struct_assign_test1(struct structa1 s1, int t, float f)
1241 printf("%d %d %d %f\n", s1.f1, s1.f2, t, f);
1244 struct structa1 struct_assign_test2(struct structa1 s1, int t)
1246 s1.f1 += t;
1247 s1.f2 -= t;
1248 return s1;
1251 void struct_assign_test(void)
1253 struct S {
1254 struct structa1 lsta1, lsta2;
1255 int i;
1256 } s, *ps;
1258 ps = &s;
1259 ps->i = 4;
1260 #if 0
1261 printf("struct_assign_test:\n");
1263 s.lsta1.f1 = 1;
1264 s.lsta1.f2 = 2;
1265 printf("%d %d\n", s.lsta1.f1, s.lsta1.f2);
1266 s.lsta2 = s.lsta1;
1267 printf("%d %d\n", s.lsta2.f1, s.lsta2.f2);
1268 #else
1269 s.lsta2.f1 = 1;
1270 s.lsta2.f2 = 2;
1271 #endif
1272 struct_assign_test1(ps->lsta2, 3, 4.5);
1274 printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2);
1275 ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i);
1276 printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2);
1278 static struct {
1279 void (*elem)();
1280 } t[] = {
1281 /* XXX: we should allow this even without braces */
1282 { struct_assign_test }
1284 printf("%d\n", struct_assign_test == t[0].elem);
1287 /* casts to short/char */
1289 void cast1(char a, short b, unsigned char c, unsigned short d)
1291 printf("%d %d %d %d\n", a, b, c, d);
1294 char bcast;
1295 short scast;
1297 void cast_test()
1299 int a;
1300 char c;
1301 char tab[10];
1302 unsigned b,d;
1303 short s;
1304 char *p = NULL;
1305 p -= 0x700000000042;
1307 printf("cast_test:\n");
1308 a = 0xfffff;
1309 cast1(a, a, a, a);
1310 a = 0xffffe;
1311 printf("%d %d %d %d\n",
1312 (char)(a + 1),
1313 (short)(a + 1),
1314 (unsigned char)(a + 1),
1315 (unsigned short)(a + 1));
1316 printf("%d %d %d %d\n",
1317 (char)0xfffff,
1318 (short)0xfffff,
1319 (unsigned char)0xfffff,
1320 (unsigned short)0xfffff);
1322 a = (bcast = 128) + 1;
1323 printf("%d\n", a);
1324 a = (scast = 65536) + 1;
1325 printf("%d\n", a);
1327 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1329 /* test cast from unsigned to signed short to int */
1330 b = 0xf000;
1331 d = (short)b;
1332 printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d);
1333 b = 0xf0f0;
1334 d = (char)b;
1335 printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d);
1337 /* test implicit int casting for array accesses */
1338 c = 0;
1339 tab[1] = 2;
1340 tab[c] = 1;
1341 printf("%d %d\n", tab[0], tab[1]);
1343 /* test implicit casting on some operators */
1344 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1345 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1346 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1348 /* from pointer to integer types */
1349 printf("%d %d %ld %ld %lld %lld\n",
1350 (int)p, (unsigned int)p,
1351 (long)p, (unsigned long)p,
1352 (long long)p, (unsigned long long)p);
1354 /* from integers to pointers */
1355 printf("%p %p %p %p\n",
1356 (void *)a, (void *)b, (void *)c, (void *)d);
1359 /* initializers tests */
1360 struct structinit1 {
1361 int f1;
1362 char f2;
1363 short f3;
1364 int farray[3];
1367 int sinit1 = 2;
1368 int sinit2 = { 3 };
1369 int sinit3[3] = { 1, 2, {{3}}, };
1370 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1371 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1372 int sinit6[] = { 1, 2, 3 };
1373 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1374 char sinit8[] = "hello" "trala";
1376 struct structinit1 sinit9 = { 1, 2, 3 };
1377 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1378 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1379 #ifdef ALL_ISOC99
1380 .farray[0] = 10,
1381 .farray[1] = 11,
1382 .farray[2] = 12,
1383 #endif
1386 char *sinit12 = "hello world";
1387 char *sinit13[] = {
1388 "test1",
1389 "test2",
1390 "test3",
1392 char sinit14[10] = { "abc" };
1393 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1395 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1397 struct bar {
1398 char *s;
1399 int len;
1400 } sinit17[] = {
1401 "a1", 4,
1402 "a2", 1
1405 int sinit18[10] = {
1406 [2 ... 5] = 20,
1408 [8] = 10,
1411 struct complexinit0 {
1412 int a;
1413 int b;
1416 struct complexinit {
1417 int a;
1418 const struct complexinit0 *b;
1421 const static struct complexinit cix[] = {
1422 [0] = {
1423 .a = 2000,
1424 .b = (const struct complexinit0[]) {
1425 { 2001, 2002 },
1426 { 2003, 2003 },
1432 struct complexinit2 {
1433 int a;
1434 int b[];
1437 struct complexinit2 cix20;
1439 struct complexinit2 cix21 = {
1440 .a = 3000,
1441 .b = { 3001, 3002, 3003 }
1444 struct complexinit2 cix22 = {
1445 .a = 4000,
1446 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1449 typedef int arrtype1[];
1450 arrtype1 sinit19 = {1};
1451 arrtype1 sinit20 = {2,3};
1452 typedef int arrtype2[3];
1453 arrtype2 sinit21 = {4};
1454 arrtype2 sinit22 = {5,6,7};
1456 void init_test(void)
1458 int linit1 = 2;
1459 int linit2 = { 3 };
1460 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1461 int linit6[] = { 1, 2, 3 };
1462 int i, j;
1463 char linit8[] = "hello" "trala";
1464 int linit12[10] = { 1, 2 };
1465 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1466 char linit14[10] = "abc";
1467 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1468 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1469 int linit17 = sizeof(linit17);
1471 printf("init_test:\n");
1473 printf("sinit1=%d\n", sinit1);
1474 printf("sinit2=%d\n", sinit2);
1475 printf("sinit3=%d %d %d %d\n",
1476 sizeof(sinit3),
1477 sinit3[0],
1478 sinit3[1],
1479 sinit3[2]
1481 printf("sinit6=%d\n", sizeof(sinit6));
1482 printf("sinit7=%d %d %d %d\n",
1483 sizeof(sinit7),
1484 sinit7[0],
1485 sinit7[1],
1486 sinit7[2]
1488 printf("sinit8=%s\n", sinit8);
1489 printf("sinit9=%d %d %d\n",
1490 sinit9.f1,
1491 sinit9.f2,
1492 sinit9.f3
1494 printf("sinit10=%d %d %d\n",
1495 sinit10.f1,
1496 sinit10.f2,
1497 sinit10.f3
1499 printf("sinit11=%d %d %d %d %d %d\n",
1500 sinit11.f1,
1501 sinit11.f2,
1502 sinit11.f3,
1503 sinit11.farray[0],
1504 sinit11.farray[1],
1505 sinit11.farray[2]
1508 for(i=0;i<3;i++)
1509 for(j=0;j<2;j++)
1510 printf("[%d][%d] = %d %d %d\n",
1511 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1512 printf("linit1=%d\n", linit1);
1513 printf("linit2=%d\n", linit2);
1514 printf("linit6=%d\n", sizeof(linit6));
1515 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1517 printf("sinit12=%s\n", sinit12);
1518 printf("sinit13=%d %s %s %s\n",
1519 sizeof(sinit13),
1520 sinit13[0],
1521 sinit13[1],
1522 sinit13[2]);
1523 printf("sinit14=%s\n", sinit14);
1525 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1526 printf("\n");
1527 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1528 printf("\n");
1529 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1530 printf("\n");
1531 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1532 printf("\n");
1533 printf("%d %d %d %d\n",
1534 linit16.a1,
1535 linit16.a2,
1536 linit16.a3,
1537 linit16.a4);
1538 /* test that initialisation is done after variable declare */
1539 printf("linit17=%d\n", linit17);
1540 printf("sinit15=%d\n", sinit15[0]);
1541 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1542 printf("sinit17=%s %d %s %d\n",
1543 sinit17[0].s, sinit17[0].len,
1544 sinit17[1].s, sinit17[1].len);
1545 for(i=0;i<10;i++)
1546 printf("%x ", sinit18[i]);
1547 printf("\n");
1548 /* complex init check */
1549 printf("cix: %d %d %d %d %d %d %d\n",
1550 cix[0].a,
1551 cix[0].b[0].a, cix[0].b[0].b,
1552 cix[0].b[1].a, cix[0].b[1].b,
1553 cix[0].b[2].a, cix[0].b[2].b);
1554 printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
1555 printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22);
1557 printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]);
1558 printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20));
1559 printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]);
1560 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1561 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1562 printf("arrtype6: %d\n", sizeof(arrtype2));
1566 void switch_test()
1568 int i;
1570 for(i=0;i<15;i++) {
1571 switch(i) {
1572 case 0:
1573 case 1:
1574 printf("a");
1575 break;
1576 default:
1577 printf("%d", i);
1578 break;
1579 case 8 ... 12:
1580 printf("c");
1581 break;
1582 case 3:
1583 printf("b");
1584 break;
1585 case 0xc33c6b9fU:
1586 case 0x7c9eeeb9U:
1587 break;
1590 printf("\n");
1593 /* ISOC99 _Bool type */
1594 void c99_bool_test(void)
1596 #ifdef BOOL_ISOC99
1597 int a;
1598 _Bool b;
1600 printf("bool_test:\n");
1601 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1602 a = 3;
1603 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1604 b = 3;
1605 printf("b = %d\n", b);
1606 b++;
1607 printf("b = %d\n", b);
1608 #endif
1611 void bitfield_test(void)
1613 int a;
1614 short sa;
1615 unsigned char ca;
1616 struct sbf1 {
1617 int f1 : 3;
1618 int : 2;
1619 int f2 : 1;
1620 int : 0;
1621 int f3 : 5;
1622 int f4 : 7;
1623 unsigned int f5 : 7;
1624 } st1;
1625 printf("bitfield_test:");
1626 printf("sizeof(st1) = %d\n", sizeof(st1));
1628 st1.f1 = 3;
1629 st1.f2 = 1;
1630 st1.f3 = 15;
1631 a = 120;
1632 st1.f4 = a;
1633 st1.f5 = a;
1634 st1.f5++;
1635 printf("%d %d %d %d %d\n",
1636 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1637 sa = st1.f5;
1638 ca = st1.f5;
1639 printf("%d %d\n", sa, ca);
1641 st1.f1 = 7;
1642 if (st1.f1 == -1)
1643 printf("st1.f1 == -1\n");
1644 else
1645 printf("st1.f1 != -1\n");
1646 if (st1.f2 == -1)
1647 printf("st1.f2 == -1\n");
1648 else
1649 printf("st1.f2 != -1\n");
1651 /* bit sizes below must be bigger than 32 since GCC doesn't allow
1652 long-long bitfields whose size is not bigger than int */
1653 struct sbf2 {
1654 long long f1 : 45;
1655 long long : 2;
1656 long long f2 : 35;
1657 unsigned long long f3 : 38;
1658 } st2;
1659 st2.f1 = 0x123456789ULL;
1660 a = 120;
1661 st2.f2 = (long long)a << 25;
1662 st2.f3 = a;
1663 st2.f2++;
1664 printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3);
1667 #ifdef __x86_64__
1668 #define FLOAT_FMT "%f\n"
1669 #else
1670 /* x86's float isn't compatible with GCC */
1671 #define FLOAT_FMT "%.5f\n"
1672 #endif
1674 /* declare strto* functions as they are C99 */
1675 double strtod(const char *nptr, char **endptr);
1677 #if defined(_WIN32)
1678 float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);}
1679 LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);}
1680 #else
1681 float strtof(const char *nptr, char **endptr);
1682 LONG_DOUBLE strtold(const char *nptr, char **endptr);
1683 #endif
1685 #define FTEST(prefix, typename, type, fmt)\
1686 void prefix ## cmp(type a, type b)\
1688 printf("%d %d %d %d %d %d\n",\
1689 a == b,\
1690 a != b,\
1691 a < b,\
1692 a > b,\
1693 a >= b,\
1694 a <= b);\
1695 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1698 a + b,\
1699 a - b,\
1700 a * b,\
1701 a / b,\
1702 -a);\
1703 printf(fmt "\n", ++a);\
1704 printf(fmt "\n", a++);\
1705 printf(fmt "\n", a);\
1706 b = 0;\
1707 printf("%d %d\n", !a, !b);\
1709 void prefix ## fcast(type a)\
1711 float fa;\
1712 double da;\
1713 LONG_DOUBLE la;\
1714 int ia;\
1715 long long llia;\
1716 unsigned int ua;\
1717 unsigned long long llua;\
1718 type b;\
1719 fa = a;\
1720 da = a;\
1721 la = a;\
1722 printf("ftof: %f %f %Lf\n", fa, da, la);\
1723 ia = (int)a;\
1724 llia = (long long)a;\
1725 a = (a >= 0) ? a : -a;\
1726 ua = (unsigned int)a;\
1727 llua = (unsigned long long)a;\
1728 printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\
1729 ia = -1234;\
1730 ua = 0x81234500;\
1731 llia = -0x123456789012345LL;\
1732 llua = 0xf123456789012345LLU;\
1733 b = ia;\
1734 printf("itof: " fmt "\n", b);\
1735 b = ua;\
1736 printf("utof: " fmt "\n", b);\
1737 b = llia;\
1738 printf("lltof: " fmt "\n", b);\
1739 b = llua;\
1740 printf("ulltof: " fmt "\n", b);\
1743 float prefix ## retf(type a) { return a; }\
1744 double prefix ## retd(type a) { return a; }\
1745 LONG_DOUBLE prefix ## retld(type a) { return a; }\
1747 void prefix ## call(void)\
1749 printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\
1750 printf("double: %f\n", prefix ## retd(42.123456789));\
1751 printf("long double: %Lf\n", prefix ## retld(42.123456789));\
1752 printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\
1755 void prefix ## signed_zeros(void) \
1757 type x = 0.0, y = -0.0, n, p;\
1758 if (x == y)\
1759 printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\
1760 1.0 / x != 1.0 / y);\
1761 else\
1762 printf ("x != y; this is wrong!\n");\
1764 n = -x;\
1765 if (x == n)\
1766 printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\
1767 1.0 / x != 1.0 / n);\
1768 else\
1769 printf ("x != -x; this is wrong!\n");\
1771 p = +y;\
1772 if (x == p)\
1773 printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\
1774 1.0 / x != 1.0 / p);\
1775 else\
1776 printf ("x != +y; this is wrong!\n");\
1777 p = -y;\
1778 if (x == p)\
1779 printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\
1780 1.0 / x != 1.0 / p);\
1781 else\
1782 printf ("x != -y; this is wrong!\n");\
1784 void prefix ## test(void)\
1786 printf("testing '%s'\n", #typename);\
1787 prefix ## cmp(1, 2.5);\
1788 prefix ## cmp(2, 1.5);\
1789 prefix ## cmp(1, 1);\
1790 prefix ## fcast(234.6);\
1791 prefix ## fcast(-2334.6);\
1792 prefix ## call();\
1793 prefix ## signed_zeros();\
1796 FTEST(f, float, float, "%f")
1797 FTEST(d, double, double, "%f")
1798 FTEST(ld, long double, LONG_DOUBLE, "%Lf")
1800 double ftab1[3] = { 1.2, 3.4, -5.6 };
1803 void float_test(void)
1805 #if !defined(__arm__) || defined(__ARM_PCS_VFP)
1806 float fa, fb;
1807 double da, db;
1808 int a;
1809 unsigned int b;
1811 printf("float_test:\n");
1812 printf("sizeof(float) = %d\n", sizeof(float));
1813 printf("sizeof(double) = %d\n", sizeof(double));
1814 printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE));
1815 ftest();
1816 dtest();
1817 ldtest();
1818 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1819 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1820 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1821 da = 123;
1822 printf("da=%f\n", da);
1823 fa = 123;
1824 printf("fa=%f\n", fa);
1825 a = 4000000000;
1826 da = a;
1827 printf("da = %f\n", da);
1828 b = 4000000000;
1829 db = b;
1830 printf("db = %f\n", db);
1831 #endif
1834 int fib(int n)
1836 if (n <= 2)
1837 return 1;
1838 else
1839 return fib(n-1) + fib(n-2);
1842 void funcptr_test()
1844 void (*func)(int);
1845 int a;
1846 struct {
1847 int dummy;
1848 void (*func)(int);
1849 } st1;
1851 printf("funcptr:\n");
1852 func = &num;
1853 (*func)(12345);
1854 func = num;
1855 a = 1;
1856 a = 1;
1857 func(12345);
1858 /* more complicated pointer computation */
1859 st1.func = num;
1860 st1.func(12346);
1861 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1862 printf("sizeof2 = %d\n", sizeof funcptr_test);
1863 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1864 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1867 void lloptest(long long a, long long b)
1869 unsigned long long ua, ub;
1871 ua = a;
1872 ub = b;
1873 /* arith */
1874 printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1875 a + b,
1876 a - b,
1877 a * b);
1879 if (b != 0) {
1880 printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1881 a / b,
1882 a % b);
1885 /* binary */
1886 printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1887 a & b,
1888 a | b,
1889 a ^ b);
1891 /* tests */
1892 printf("test: %d %d %d %d %d %d\n",
1893 a == b,
1894 a != b,
1895 a < b,
1896 a > b,
1897 a >= b,
1898 a <= b);
1900 printf("utest: %d %d %d %d %d %d\n",
1901 ua == ub,
1902 ua != ub,
1903 ua < ub,
1904 ua > ub,
1905 ua >= ub,
1906 ua <= ub);
1908 /* arith2 */
1909 a++;
1910 b++;
1911 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1912 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++);
1913 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b);
1914 printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
1915 b = ub = 0;
1916 printf("not: %d %d %d %d\n", !a, !ua, !b, !ub);
1919 void llshift(long long a, int b)
1921 printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1922 (unsigned long long)a >> b,
1923 a >> b,
1924 a << b);
1925 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1926 (unsigned long long)a >> 3,
1927 a >> 3,
1928 a << 3);
1929 printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n",
1930 (unsigned long long)a >> 35,
1931 a >> 35,
1932 a << 35);
1935 void llfloat(void)
1937 float fa;
1938 double da;
1939 LONG_DOUBLE lda;
1940 long long la, lb, lc;
1941 unsigned long long ula, ulb, ulc;
1942 la = 0x12345678;
1943 ula = 0x72345678;
1944 la = (la << 20) | 0x12345;
1945 ula = ula << 33;
1946 printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula);
1948 fa = la;
1949 da = la;
1950 lda = la;
1951 printf("lltof: %f %f %Lf\n", fa, da, lda);
1953 la = fa;
1954 lb = da;
1955 lc = lda;
1956 printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc);
1958 fa = ula;
1959 da = ula;
1960 lda = ula;
1961 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1963 ula = fa;
1964 ulb = da;
1965 ulc = lda;
1966 printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc);
1969 long long llfunc1(int a)
1971 return a * 2;
1974 struct S {
1975 int id;
1976 char item;
1979 long long int value(struct S *v)
1981 return ((long long int)v->item);
1984 long long llfunc2(long long x, long long y, int z)
1986 return x * y * z;
1989 void longlong_test(void)
1991 long long a, b, c;
1992 int ia;
1993 unsigned int ua;
1994 printf("longlong_test:\n");
1995 printf("sizeof(long long) = %d\n", sizeof(long long));
1996 ia = -1;
1997 ua = -2;
1998 a = ia;
1999 b = ua;
2000 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b);
2001 printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n",
2002 (long long)1,
2003 (long long)-2,
2004 1LL,
2005 0x1234567812345679);
2006 a = llfunc1(-3);
2007 printf(LONG_LONG_FORMAT "\n", a);
2009 lloptest(1000, 23);
2010 lloptest(0xff, 0x1234);
2011 b = 0x72345678 << 10;
2012 lloptest(-3, b);
2013 llshift(0x123, 5);
2014 llshift(-23, 5);
2015 b = 0x72345678LL << 10;
2016 llshift(b, 47);
2018 llfloat();
2019 #if 1
2020 b = 0x12345678;
2021 a = -1;
2022 c = a + b;
2023 printf("%Lx\n", c);
2024 #endif
2026 /* long long reg spill test */
2028 struct S a;
2030 a.item = 3;
2031 printf("%lld\n", value(&a));
2033 lloptest(0x80000000, 0);
2036 long long *p, v, **pp;
2037 v = 1;
2038 p = &v;
2039 p[0]++;
2040 printf("another long long spill test : %lld\n", *p);
2041 pp = &p;
2043 v = llfunc2(**pp, **pp, ia);
2044 printf("a long long function (arm-)reg-args test : %lld\n", v);
2046 a = 68719476720LL;
2047 b = 4294967295LL;
2048 printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b);
2050 printf(LONG_LONG_FORMAT "\n", 0x123456789LLU);
2052 /* long long pointer deref in argument passing test */
2053 a = 0x123;
2054 long long *p = &a;
2055 llshift(*p, 5);
2058 void manyarg_test(void)
2060 LONG_DOUBLE ld = 1234567891234LL;
2061 printf("manyarg_test:\n");
2062 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2063 1, 2, 3, 4, 5, 6, 7, 8,
2064 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2065 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2066 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2067 1, 2, 3, 4, 5, 6, 7, 8,
2068 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2069 1234567891234LL, 987654321986LL,
2070 42.0, 43.0);
2071 printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2072 LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n",
2073 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2074 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2075 1234567891234LL, 987654321986LL,
2076 42.0, 43.0);
2077 printf("%d %d %d %d %d %d %d %d %Lf\n",
2078 1, 2, 3, 4, 5, 6, 7, 8, ld);
2079 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2080 LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n",
2081 1, 2, 3, 4, 5, 6, 7, 8,
2082 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2083 1234567891234LL, 987654321986LL,
2084 42.0, 43.0, ld);
2085 printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2086 "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n",
2087 1, 2, 3, 4, 5, 6, 7, 8,
2088 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2089 ld, 1234567891234LL, 987654321986LL,
2090 42.0, 43.0, ld);
2093 void vprintf1(const char *fmt, ...)
2095 va_list ap, aq;
2096 const char *p;
2097 int c, i;
2098 double d;
2099 long long ll;
2100 LONG_DOUBLE ld;
2102 va_start(aq, fmt);
2103 va_copy(ap, aq);
2105 p = fmt;
2106 for(;;) {
2107 c = *p;
2108 if (c == '\0')
2109 break;
2110 p++;
2111 if (c == '%') {
2112 c = *p;
2113 switch(c) {
2114 case '\0':
2115 goto the_end;
2116 case 'd':
2117 i = va_arg(ap, int);
2118 printf("%d", i);
2119 break;
2120 case 'f':
2121 d = va_arg(ap, double);
2122 printf("%f", d);
2123 break;
2124 case 'l':
2125 ll = va_arg(ap, long long);
2126 printf(LONG_LONG_FORMAT, ll);
2127 break;
2128 case 'F':
2129 ld = va_arg(ap, LONG_DOUBLE);
2130 printf("%Lf", ld);
2131 break;
2133 p++;
2134 } else {
2135 putchar(c);
2138 the_end:
2139 va_end(aq);
2140 va_end(ap);
2143 struct myspace {
2144 short int profile;
2147 void stdarg_for_struct(struct myspace bob, ...)
2149 struct myspace george, bill;
2150 va_list ap;
2151 short int validate;
2153 va_start(ap, bob);
2154 bill = va_arg(ap, struct myspace);
2155 george = va_arg(ap, struct myspace);
2156 validate = va_arg(ap, int);
2157 printf("stdarg_for_struct: %d %d %d %d\n",
2158 bob.profile, bill.profile, george.profile, validate);
2159 va_end(ap);
2162 void stdarg_for_libc(const char *fmt, ...)
2164 va_list args;
2165 va_start(args, fmt);
2166 vprintf(fmt, args);
2167 va_end(args);
2170 void stdarg_test(void)
2172 LONG_DOUBLE ld = 1234567891234LL;
2173 struct myspace bob;
2175 vprintf1("%d %d %d\n", 1, 2, 3);
2176 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
2177 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
2178 vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4));
2179 vprintf1("%d %f %l %F %d %f %l %F\n",
2180 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1));
2181 vprintf1("%d %d %d %d %d %d %d %d %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);
2184 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n",
2185 1, 2, 3, 4, 5, 6, 7, 8,
2186 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0);
2187 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2188 "%l %l %f %f\n",
2189 1, 2, 3, 4, 5, 6, 7, 8,
2190 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2191 1234567891234LL, 987654321986LL,
2192 42.0, 43.0);
2193 vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2194 "%l %l %f %f\n",
2195 ld, 1, 2, 3, 4, 5, 6, 7, 8,
2196 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2197 1234567891234LL, 987654321986LL,
2198 42.0, 43.0);
2199 vprintf1("%d %d %d %d %d %d %d %d %F\n",
2200 1, 2, 3, 4, 5, 6, 7, 8, ld);
2201 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2202 "%l %l %f %f %F\n",
2203 1, 2, 3, 4, 5, 6, 7, 8,
2204 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2205 1234567891234LL, 987654321986LL,
2206 42.0, 43.0, ld);
2207 vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f "
2208 "%F %l %l %f %f %F\n",
2209 1, 2, 3, 4, 5, 6, 7, 8,
2210 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0,
2211 ld, 1234567891234LL, 987654321986LL,
2212 42.0, 43.0, ld);
2214 bob.profile = 42;
2215 stdarg_for_struct(bob, bob, bob, bob.profile);
2216 stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456);
2219 void whitespace_test(void)
2221 char *str;
2223 \f\v #if 1
2224 pri\
2225 ntf("whitspace:\n");\f\v
2226 #endif
2227 pf("N=%d\n", 2);
2229 #ifdef CORRECT_CR_HANDLING
2230 pri\
2231 ntf("aaa=%d\n", 3);
2232 #endif
2234 pri\
2236 ntf("min=%d\n", 4);
2238 #ifdef ACCEPT_CR_IN_STRINGS
2239 printf("len1=%d\n", strlen("
2240 "));
2241 #ifdef CORRECT_CR_HANDLING
2242 str = "
2244 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2245 #endif
2246 printf("len1=%d\n", strlen(" a
2247 "));
2248 #endif /* ACCEPT_CR_IN_STRINGS */
2251 int reltab[3] = { 1, 2, 3 };
2253 int *rel1 = &reltab[1];
2254 int *rel2 = &reltab[2];
2256 void getmyaddress(void)
2258 printf("in getmyaddress\n");
2260 unsigned long theaddress = (unsigned long)getmyaddress;
2261 void relocation_test(void)
2263 void (*fptr)(void) = (void (*)(void))theaddress;
2264 printf("*rel1=%d\n", *rel1);
2265 printf("*rel2=%d\n", *rel2);
2266 fptr();
2269 void old_style_f(a,b,c)
2270 int a, b;
2271 double c;
2273 printf("a=%d b=%d b=%f\n", a, b, c);
2276 void decl_func1(int cmpfn())
2278 printf("cmpfn=%lx\n", (long)cmpfn);
2281 void decl_func2(cmpfn)
2282 int cmpfn();
2284 printf("cmpfn=%lx\n", (long)cmpfn);
2287 void old_style_function(void)
2289 old_style_f((void *)1, 2, 3.0);
2290 decl_func1(NULL);
2291 decl_func2(NULL);
2294 void alloca_test()
2296 #if defined __i386__ || defined __x86_64__ || defined __arm__
2297 char *p = alloca(16);
2298 strcpy(p,"123456789012345");
2299 printf("alloca: p is %s\n", p);
2300 char *demo = "This is only a test.\n";
2301 /* Test alloca embedded in a larger expression */
2302 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
2303 #endif
2306 void *bounds_checking_is_enabled()
2308 char ca[10], *cp = ca-1;
2309 return (ca != cp + 1) ? cp : NULL;
2312 typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1];
2314 void c99_vla_test(int size1, int size2)
2316 #if defined __i386__ || defined __x86_64__
2317 int size = size1 * size2;
2318 int tab1[size][2], tab2[10][2];
2319 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2321 /* "size" should have been 'captured' at tab1 declaration,
2322 so modifying it should have no effect on VLA behaviour. */
2323 size = size-1;
2325 printf("Test C99 VLA 1 (sizeof): ");
2326 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
2327 tab1_ptr = tab1;
2328 tab2_ptr = tab2;
2329 printf("Test C99 VLA 2 (ptrs subtract): ");
2330 printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED");
2331 printf("Test C99 VLA 3 (ptr add): ");
2332 printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED");
2333 printf("Test C99 VLA 4 (ptr access): ");
2334 tab1[size1][1] = 42;
2335 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2337 printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2338 if (bad_ptr = bounds_checking_is_enabled()) {
2339 int *t1 = &tab1[size1 * size2 - 1][3];
2340 int *t2 = &tab2[9][3];
2341 printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2342 printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2344 char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2345 char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2346 printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2347 printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2349 int *i1 = tab1[-1];
2350 int *i2 = tab2[-1];
2351 printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2352 printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2354 int *x1 = tab1[size1 * size2 + 1];
2355 int *x2 = tab2[10 + 1];
2356 printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2357 printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2358 } else {
2359 printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2361 printf("\n");
2362 #endif
2365 #ifndef __TINYC__
2366 typedef __SIZE_TYPE__ uintptr_t;
2367 #endif
2369 void sizeof_test(void)
2371 int a;
2372 int **ptr;
2374 printf("sizeof(int) = %d\n", sizeof(int));
2375 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
2376 printf("sizeof(long) = %d\n", sizeof(long));
2377 printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long));
2378 printf("sizeof(short) = %d\n", sizeof(short));
2379 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
2380 printf("sizeof(char) = %d\n", sizeof(char));
2381 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
2382 printf("sizeof(func) = %d\n", sizeof sizeof_test());
2383 a = 1;
2384 printf("sizeof(a++) = %d\n", sizeof a++);
2385 printf("a=%d\n", a);
2386 ptr = NULL;
2387 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
2389 /* The type of sizeof should be as large as a pointer, actually
2390 it should be size_t. */
2391 printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int)));
2392 uintptr_t t = 1;
2393 uintptr_t t2;
2394 /* Effectively <<32, but defined also on 32bit machines. */
2395 t <<= 16;
2396 t <<= 16;
2397 t++;
2398 /* This checks that sizeof really can be used to manipulate
2399 uintptr_t objects, without truncation. */
2400 t2 = t & -sizeof(uintptr_t);
2401 printf ("%lu %lu\n", t, t2);
2403 /* some alignof tests */
2404 printf("__alignof__(int) = %d\n", __alignof__(int));
2405 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
2406 printf("__alignof__(short) = %d\n", __alignof__(short));
2407 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
2408 printf("__alignof__(char) = %d\n", __alignof__(char));
2409 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
2410 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
2413 void typeof_test(void)
2415 double a;
2416 typeof(a) b;
2417 typeof(float) c;
2419 a = 1.5;
2420 b = 2.5;
2421 c = 3.5;
2422 printf("a=%f b=%f c=%f\n", a, b, c);
2425 void statement_expr_test(void)
2427 int a, i;
2429 a = 0;
2430 for(i=0;i<10;i++) {
2431 a += 1 +
2432 ( { int b, j;
2433 b = 0;
2434 for(j=0;j<5;j++)
2435 b += j; b;
2436 } );
2438 printf("a=%d\n", a);
2442 void local_label_test(void)
2444 int a;
2445 goto l1;
2447 a = 1 + ({
2448 __label__ l1, l2, l3, l4;
2449 goto l1;
2451 printf("aa1\n");
2452 goto l3;
2454 printf("aa3\n");
2455 goto l4;
2457 printf("aa2\n");
2458 goto l2;
2459 l3:;
2462 printf("a=%d\n", a);
2463 return;
2465 printf("bb1\n");
2466 goto l2;
2468 printf("bb2\n");
2469 goto l4;
2472 /* inline assembler test */
2473 #if defined(__i386__) || defined(__x86_64__)
2475 /* from linux kernel */
2476 static char * strncat1(char * dest,const char * src,size_t count)
2478 long d0, d1, d2, d3;
2479 __asm__ __volatile__(
2480 "repne\n\t"
2481 "scasb\n\t"
2482 "dec %1\n\t"
2483 "mov %8,%3\n"
2484 "1:\tdec %3\n\t"
2485 "js 2f\n\t"
2486 "lodsb\n\t"
2487 "stosb\n\t"
2488 "testb %%al,%%al\n\t"
2489 "jne 1b\n"
2490 "2:\txor %2,%2\n\t"
2491 "stosb"
2492 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2493 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2494 : "memory");
2495 return dest;
2498 static char * strncat2(char * dest,const char * src,size_t count)
2500 long d0, d1, d2, d3;
2501 __asm__ __volatile__(
2502 "repne scasb\n\t" /* one-line repne prefix + string op */
2503 "dec %1\n\t"
2504 "mov %8,%3\n"
2505 "1:\tdec %3\n\t"
2506 "js 2f\n\t"
2507 "lodsb\n\t"
2508 "stosb\n\t"
2509 "testb %%al,%%al\n\t"
2510 "jne 1b\n"
2511 "2:\txor %2,%2\n\t"
2512 "stosb"
2513 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
2514 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
2515 : "memory");
2516 return dest;
2519 static inline void * memcpy1(void * to, const void * from, size_t n)
2521 long d0, d1, d2;
2522 __asm__ __volatile__(
2523 "rep ; movsl\n\t"
2524 "testb $2,%b4\n\t"
2525 "je 1f\n\t"
2526 "movsw\n"
2527 "1:\ttestb $1,%b4\n\t"
2528 "je 2f\n\t"
2529 "movsb\n"
2530 "2:"
2531 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2532 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2533 : "memory");
2534 return (to);
2537 static inline void * memcpy2(void * to, const void * from, size_t n)
2539 long d0, d1, d2;
2540 __asm__ __volatile__(
2541 "rep movsl\n\t" /* one-line rep prefix + string op */
2542 "testb $2,%b4\n\t"
2543 "je 1f\n\t"
2544 "movsw\n"
2545 "1:\ttestb $1,%b4\n\t"
2546 "je 2f\n\t"
2547 "movsb\n"
2548 "2:"
2549 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
2550 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
2551 : "memory");
2552 return (to);
2555 static __inline__ void sigaddset1(unsigned int *set, int _sig)
2557 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2560 static __inline__ void sigdelset1(unsigned int *set, int _sig)
2562 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
2565 static __inline__ __const__ unsigned int swab32(unsigned int x)
2567 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
2568 "rorl $16,%0\n\t" /* swap words */
2569 "xchgb %b0,%h0" /* swap higher bytes */
2570 :"=" "q" (x)
2571 : "0" (x));
2572 return x;
2575 static __inline__ unsigned long long mul64(unsigned int a, unsigned int b)
2577 unsigned long long res;
2578 #ifdef __x86_64__
2579 /* Using the A constraint is wrong (it means rdx:rax, which is too large)
2580 but still test the 32bit->64bit mull. */
2581 unsigned int resh, resl;
2582 __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b));
2583 res = ((unsigned long long)resh << 32) | resl;
2584 #else
2585 __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b));
2586 #endif
2587 return res;
2590 static __inline__ unsigned long long inc64(unsigned long long a)
2592 unsigned long long res;
2593 #ifdef __x86_64__
2594 /* Using the A constraint is wrong, and increments are tested
2595 elsewere. */
2596 res = a + 1;
2597 #else
2598 __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a));
2599 #endif
2600 return res;
2603 struct struct123 {
2604 int a;
2605 int b;
2607 struct struct1231 {
2608 unsigned long addr;
2611 unsigned long mconstraint_test(struct struct1231 *r)
2613 unsigned long ret;
2614 unsigned int a[2];
2615 a[0] = 0;
2616 __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1"
2617 : "=&r" (ret), "=m" (a)
2618 : "m" (*(struct struct123 *)r->addr));
2619 return ret + a[0];
2622 #ifdef __x86_64__
2623 int fls64(unsigned long long x)
2625 int bitpos = -1;
2626 asm("bsrq %1,%q0"
2627 : "+r" (bitpos)
2628 : "rm" (x));
2629 return bitpos + 1;
2631 #endif
2633 void other_constraints_test(void)
2635 unsigned long ret;
2636 int var;
2637 __asm__ volatile ("movq %P1,%0" : "=r" (ret) : "p" (&var));
2638 printf ("oc1: %d\n", ret == (unsigned long)&var);
2641 unsigned int set;
2643 void asm_test(void)
2645 char buf[128];
2646 unsigned int val;
2647 struct struct123 s1;
2648 struct struct1231 s2 = { (unsigned long)&s1 };
2650 printf("inline asm:\n");
2652 // parse 0x1E-1 as 3 tokens in asm mode
2653 asm volatile ("mov $0x1E-1,%eax");
2655 /* test the no operand case */
2656 asm volatile ("xorl %eax, %eax");
2658 memcpy1(buf, "hello", 6);
2659 strncat1(buf, " worldXXXXX", 3);
2660 printf("%s\n", buf);
2662 memcpy2(buf, "hello", 6);
2663 strncat2(buf, " worldXXXXX", 3);
2664 printf("%s\n", buf);
2666 /* 'A' constraint test */
2667 printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234));
2668 printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff));
2670 s1.a = 42;
2671 s1.b = 43;
2672 printf("mconstraint: %d", mconstraint_test(&s2));
2673 printf(" %d %d\n", s1.a, s1.b);
2674 other_constraints_test();
2675 set = 0xff;
2676 sigdelset1(&set, 2);
2677 sigaddset1(&set, 16);
2678 /* NOTE: we test here if C labels are correctly restored after the
2679 asm statement */
2680 goto label1;
2681 label2:
2682 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
2683 printf("set=0x%x\n", set);
2684 val = 0x01020304;
2685 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
2686 return;
2687 label1:
2688 goto label2;
2691 #else
2693 void asm_test(void)
2697 #endif
2699 #define COMPAT_TYPE(type1, type2) \
2701 printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \
2702 __builtin_types_compatible_p (type1, type2));\
2705 int constant_p_var;
2707 void builtin_test(void)
2709 short s;
2710 int i;
2711 long long ll;
2712 #if GCC_MAJOR >= 3
2713 COMPAT_TYPE(int, int);
2714 COMPAT_TYPE(int, unsigned int);
2715 COMPAT_TYPE(int, char);
2716 COMPAT_TYPE(int, const int);
2717 COMPAT_TYPE(int, volatile int);
2718 COMPAT_TYPE(int *, int *);
2719 COMPAT_TYPE(int *, void *);
2720 COMPAT_TYPE(int *, const int *);
2721 COMPAT_TYPE(char *, unsigned char *);
2722 COMPAT_TYPE(char *, signed char *);
2723 COMPAT_TYPE(char *, char *);
2724 /* space is needed because tcc preprocessor introduces a space between each token */
2725 COMPAT_TYPE(char * *, void *);
2726 #endif
2727 printf("res = %d\n", __builtin_constant_p(1));
2728 printf("res = %d\n", __builtin_constant_p(1 + 2));
2729 printf("res = %d\n", __builtin_constant_p(&constant_p_var));
2730 printf("res = %d\n", __builtin_constant_p(constant_p_var));
2731 s = 1;
2732 ll = 2;
2733 i = __builtin_choose_expr (1 != 0, ll, s);
2734 printf("bce: %d\n", i);
2735 i = __builtin_choose_expr (1 != 1, ll, s);
2736 printf("bce: %d\n", i);
2737 i = sizeof (__builtin_choose_expr (1, ll, s));
2738 printf("bce: %d\n", i);
2739 i = sizeof (__builtin_choose_expr (0, ll, s));
2740 printf("bce: %d\n", i);
2743 #ifndef _WIN32
2744 extern int __attribute__((weak)) weak_f1(void);
2745 extern int __attribute__((weak)) weak_f2(void);
2746 extern int weak_f3(void);
2747 extern int __attribute__((weak)) weak_v1;
2748 extern int __attribute__((weak)) weak_v2;
2749 extern int weak_v3;
2751 extern int (*weak_fpa)() __attribute__((weak));
2752 extern int __attribute__((weak)) (*weak_fpb)();
2753 extern __attribute__((weak)) int (*weak_fpc)();
2755 extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak));
2756 extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ;
2757 extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak));
2758 extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak));
2759 extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ;
2760 extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak));
2762 static const size_t dummy = 0;
2763 extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy")));
2764 extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2;
2765 extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3;
2767 int some_lib_func(void);
2768 int dummy_impl_of_slf(void) { return 444; }
2769 int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf")));
2771 int weak_toolate() __attribute__((weak));
2772 int weak_toolate() { return 0; }
2774 void __attribute__((weak)) weak_test(void)
2776 printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123);
2777 printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123);
2778 printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123);
2779 printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123);
2780 printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123);
2781 printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123);
2783 printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123);
2784 printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123);
2785 printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123);
2787 printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL);
2788 printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL);
2789 printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL);
2790 printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL);
2791 printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL);
2792 printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL);
2795 int __attribute__((weak)) weak_f2() { return 222; }
2796 int __attribute__((weak)) weak_f3() { return 333; }
2797 int __attribute__((weak)) weak_v2 = 222;
2798 int __attribute__((weak)) weak_v3 = 333;
2799 #endif
2801 void const_func(const int a)
2805 void const_warn_test(void)
2807 const_func(1);
2810 struct condstruct {
2811 int i;
2814 int getme (struct condstruct *s, int i)
2816 int i1 = (i == 0 ? 0 : s)->i;
2817 int i2 = (i == 0 ? s : 0)->i;
2818 int i3 = (i == 0 ? (void*)0 : s)->i;
2819 int i4 = (i == 0 ? s : (void*)0)->i;
2820 return i1 + i2 + i3 + i4;
2823 struct global_data
2825 int a[40];
2826 int *b[40];
2829 struct global_data global_data;
2831 int global_data_getstuff (int *, int);
2833 void global_data_callit (int i)
2835 *global_data.b[i] = global_data_getstuff (global_data.b[i], 1);
2838 int global_data_getstuff (int *p, int i)
2840 return *p + i;
2843 void global_data_test (void)
2845 global_data.a[0] = 42;
2846 global_data.b[0] = &global_data.a[0];
2847 global_data_callit (0);
2848 printf ("%d\n", global_data.a[0]);
2851 struct cmpcmpS
2853 unsigned char fill : 3;
2854 unsigned char b1 : 1;
2855 unsigned char b2 : 1;
2856 unsigned char fill2 : 3;
2859 int glob1, glob2, glob3;
2861 void compare_comparisons (struct cmpcmpS *s)
2863 if (s->b1 != (glob1 == glob2)
2864 || (s->b2 != (glob1 == glob3)))
2865 printf ("comparing comparisons broken\n");
2868 void cmp_comparison_test(void)
2870 struct cmpcmpS s;
2871 s.b1 = 1;
2872 glob1 = 42; glob2 = 42;
2873 s.b2 = 0;
2874 glob3 = 43;
2875 compare_comparisons (&s);
2878 int fcompare (double a, double b, int code)
2880 switch (code) {
2881 case 0: return a == b;
2882 case 1: return a != b;
2883 case 2: return a < b;
2884 case 3: return a >= b;
2885 case 4: return a > b;
2886 case 5: return a <= b;
2890 void math_cmp_test(void)
2892 double nan = 0.0/0.0;
2893 double one = 1.0;
2894 double two = 2.0;
2895 int comp = 0;
2896 #define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part)
2898 /* This asserts that "a op b" is _not_ true, but "a iop b" is true.
2899 And it does this in various ways so that all code generation paths
2900 are checked (generating inverted tests, or non-inverted tests, or
2901 producing a 0/1 value without jumps (that's done in the fcompare
2902 function). */
2903 #define FCMP(a,b,op,iop,code) \
2904 if (fcompare (a,b,code)) \
2905 bug (a,b,op,iop,1); \
2906 if (a op b) \
2907 bug (a,b,op,iop,2); \
2908 if (a iop b) \
2910 else \
2911 bug (a,b,op,iop,3); \
2912 if ((a op b) || comp) \
2913 bug (a,b,op,iop,4); \
2914 if ((a iop b) || comp) \
2916 else \
2917 bug (a,b,op,iop,5);
2919 /* Equality tests. */
2920 FCMP(nan, nan, ==, !=, 0);
2921 FCMP(one, two, ==, !=, 0);
2922 FCMP(one, one, !=, ==, 1);
2923 /* Non-equality is a bit special. */
2924 if (!fcompare (nan, nan, 1))
2925 bug (nan, nan, !=, ==, 6);
2927 /* Relational tests on numbers. */
2928 FCMP(two, one, <, >=, 2);
2929 FCMP(one, two, >=, <, 3);
2930 FCMP(one, two, >, <=, 4);
2931 FCMP(two, one, <=, >, 5);
2933 /* Relational tests on NaNs. Note that the inverse op here is
2934 always !=, there's no operator in C that is equivalent to !(a < b),
2935 when NaNs are involved, same for the other relational ops. */
2936 FCMP(nan, nan, <, !=, 2);
2937 FCMP(nan, nan, >=, !=, 3);
2938 FCMP(nan, nan, >, !=, 4);
2939 FCMP(nan, nan, <=, !=, 5);
2942 double get100 () { return 100.0; }
2944 void callsave_test(void)
2946 #if defined __i386__ || defined __x86_64__ || defined __arm__
2947 int i, s; double *d; double t;
2948 s = sizeof (double);
2949 printf ("callsavetest: %d\n", s);
2950 d = alloca (sizeof(double));
2951 d[0] = 10.0;
2952 /* x86-64 had a bug were the next call to get100 would evict
2953 the lvalue &d[0] as VT_LLOCAL, and the reload would be done
2954 in int type, not pointer type. When alloca returns a pointer
2955 with the high 32 bit set (which is likely on x86-64) the access
2956 generates a segfault. */
2957 i = d[0] > get100 ();
2958 printf ("%d\n", i);
2959 #endif
2963 void bfa3(ptrdiff_t str_offset)
2965 printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset);
2967 void bfa2(ptrdiff_t str_offset)
2969 printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset);
2970 bfa3(str_offset);
2972 void bfa1(ptrdiff_t str_offset)
2974 printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset);
2975 bfa2(str_offset);
2978 void builtin_frame_address_test(void)
2980 /* builtin_frame_address fails on ARM with gcc which make test3 fail */
2981 #ifndef __arm__
2982 char str[] = "__builtin_frame_address";
2983 char *fp0 = __builtin_frame_address(0);
2985 printf("str: %s\n", str);
2986 bfa1(str-fp0);
2987 #endif
2990 char via_volatile (char i)
2992 char volatile vi;
2993 vi = i;
2994 return vi;
2997 struct __attribute__((__packed__)) Spacked {
2998 char a;
2999 short b;
3000 int c;
3002 struct Spacked spacked;
3003 typedef struct __attribute__((__packed__)) {
3004 char a;
3005 short b;
3006 int c;
3007 } Spacked2;
3008 Spacked2 spacked2;
3009 #ifdef BROKEN
3010 /* This doesn't work for now. Requires adjusting field offsets/sizes
3011 after parsing the struct members. */
3012 typedef struct Spacked3_s {
3013 char a;
3014 short b;
3015 int c;
3016 } __attribute__((__packed__)) Spacked3;
3017 Spacked3 spacked3;
3018 #endif
3019 void attrib_test(void)
3021 printf("attr: %d %d %d %d\n", sizeof(struct Spacked),
3022 sizeof(spacked), sizeof(Spacked2), sizeof(spacked2));
3023 #ifdef BROKEN
3024 printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3));
3025 #endif