glibc compat fix
[tinycc.git] / tcctest.c
blob4e2a3046f446457bcb3263e7aba6b168643bceec
1 /*
2 * TCC auto test program
3 */
5 /* Unfortunately, gcc version < 3 does not handle that! */
6 //#define ALL_ISOC99
8 /* only gcc 3 handles _Bool correctly */
9 //#define BOOL_ISOC99
11 #define C99_MACROS
13 /* gcc 2.95.3 does not handle correctly CR in strings or after strays */
14 //#define CORRECT_CR_HANDLING
16 /* test various include syntaxes */
18 #define TCCLIB_INC <tcclib.h>
19 #define TCCLIB_INC1 <tcclib
20 #define TCCLIB_INC2 h>
21 #define TCCLIB_INC3 "tcclib"
23 #include TCCLIB_INC
25 #include TCCLIB_INC1.TCCLIB_INC2
27 #include TCCLIB_INC1.h>
29 /* gcc 3.2 does not accept that (bug ?) */
30 //#include TCCLIB_INC3 ".h"
32 #include <tcclib.h>
34 #include "tcclib.h"
36 void string_test();
37 void expr_test();
38 void macro_test();
39 void scope_test();
40 void forward_test();
41 void funcptr_test();
42 void loop_test();
43 void switch_test();
44 void goto_test();
45 void enum_test();
46 void typedef_test();
47 void struct_test();
48 void array_test();
49 void expr_ptr_test();
50 void bool_test();
51 void expr2_test();
52 void constant_expr_test();
53 void expr_cmp_test();
54 void char_short_test();
55 void init_test(void);
56 void compound_literal_test(void);
57 int kr_test();
58 void struct_assign_test(void);
59 void cast_test(void);
60 void bitfield_test(void);
61 void c99_bool_test(void);
62 void float_test(void);
63 void longlong_test(void);
64 void stdarg_test(void);
65 void whitespace_test(void);
66 void relocation_test(void);
67 void old_style_function(void);
68 void sizeof_test(void);
69 void typeof_test(void);
70 void local_label_test(void);
71 void statement_expr_test(void);
72 void asm_test(void);
74 int fib(int n);
75 void num(int n);
76 void forward_ref(void);
77 int isid(int c);
79 #define A 2
80 #define N 1234 + A
81 #define pf printf
82 #define M1(a, b) (a) + (b)
84 #define str\
85 (s) # s
86 #define glue(a, b) a ## b
87 #define xglue(a, b) glue(a, b)
88 #define HIGHLOW "hello"
89 #define LOW LOW ", world"
91 #define min(a, b) ((a) < (b) ? (a) : (b))
93 #ifdef C99_MACROS
94 #define dprintf(level,...) printf(__VA_ARGS__)
95 #endif
97 /* gcc vararg macros */
98 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
100 #define MACRO_NOARGS()
102 #define AAA 3
103 #undef AAA
104 #define AAA 4
106 #if 1
107 #define B3 1
108 #elif 1
109 #define B3 2
110 #elif 0
111 #define B3 3
112 #else
113 #define B3 4
114 #endif
116 #define __INT64_C(c) c ## LL
117 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
119 void macro_test(void)
121 printf("macro:\n");\f\v
122 pf("N=%d\n", N);
123 printf("aaa=%d\n", AAA);
125 printf("min=%d\n", min(1, min(2, -1)));
127 printf("s1=%s\n", glue(HIGH, LOW));
128 printf("s2=%s\n", xglue(HIGH, LOW));
129 printf("s3=%s\n", str("c"));
130 printf("s4=%s\n", str(a1));
131 printf("B3=%d\n", B3);
133 #ifdef A
134 printf("A defined\n");
135 #endif
136 #ifdef B
137 printf("B defined\n");
138 #endif
139 #ifdef A
140 printf("A defined\n");
141 #else
142 printf("A not defined\n");
143 #endif
144 #ifdef B
145 printf("B defined\n");
146 #else
147 printf("B not defined\n");
148 #endif
150 #ifdef A
151 printf("A defined\n");
152 #ifdef B
153 printf("B1 defined\n");
154 #else
155 printf("B1 not defined\n");
156 #endif
157 #else
158 printf("A not defined\n");
159 #ifdef B
160 printf("B2 defined\n");
161 #else
162 printf("B2 not defined\n");
163 #endif
164 #endif
166 #if 1+1
167 printf("test true1\n");
168 #endif
169 #if 0
170 printf("test true2\n");
171 #endif
172 #if 1-1
173 printf("test true3\n");
174 #endif
175 #if defined(A)
176 printf("test trueA\n");
177 #endif
178 #if defined(B)
179 printf("test trueB\n");
180 #endif
182 #if 0
183 printf("test 0\n");
184 #elif 0
185 printf("test 1\n");
186 #elif 2
187 printf("test 2\n");
188 #else
189 printf("test 3\n");
190 #endif
192 MACRO_NOARGS();
194 #ifdef __LINE__
195 printf("__LINE__ defined\n");
196 #endif
198 printf("__LINE__=%d __FILE__=%s\n",
199 __LINE__, __FILE__);
200 #line 200
201 printf("__LINE__=%d __FILE__=%s\n",
202 __LINE__, __FILE__);
203 #line 203 "test"
204 printf("__LINE__=%d __FILE__=%s\n",
205 __LINE__, __FILE__);
206 #line 206 "tcctest.c"
208 /* not strictly preprocessor, but we test it there */
209 #ifdef C99_MACROS
210 printf("__func__ = %s\n", __func__);
211 dprintf(1, "vaarg=%d\n", 1);
212 #endif
213 dprintf1(1, "vaarg1\n");
214 dprintf1(1, "vaarg1=%d\n", 2);
215 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
217 /* gcc extension */
218 printf("func='%s'\n", __FUNCTION__);
220 /* complicated macros in glibc */
221 printf("INT64_MIN=%Ld\n", INT64_MIN);
223 int a;
224 a = 1;
225 glue(a+, +);
226 printf("a=%d\n", a);
227 glue(a <, <= 2);
228 printf("a=%d\n", a);
231 /* comment with stray handling *\
235 int op(a,b)
237 return a / b;
240 int ret(a)
242 if (a == 2)
243 return 1;
244 if (a == 3)
245 return 2;
246 return 0;
249 void ps(char *s)
251 int c;
252 while (1) {
253 c = *s;
254 if (c == 0)
255 break;
256 printf("%c", c);
257 s++;
261 void string_test()
263 int b;
264 printf("string:\n");
265 printf("\141\1423\143\n");/* dezdez test */
266 printf("\x41\x42\x43\x3a\n");
267 printf("c=%c\n", 'r');
268 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
269 #if 0
270 printf("wstring=%S\n", L"abc");
271 printf("wstring=%S\n", L"abc" L"def" "ghi");
272 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
273 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
274 #endif
275 ps("test\n");
276 b = 32;
277 while ((b = b + 1) < 96) {
278 printf("%c", b);
280 printf("\n");
281 printf("fib=%d\n", fib(33));
282 b = 262144;
283 while (b != 0x80000000) {
284 num(b);
285 b = b * 2;
289 void loop_test()
291 int i;
292 i = 0;
293 while (i < 10)
294 printf("%d", i++);
295 printf("\n");
296 for(i = 0; i < 10;i++)
297 printf("%d", i);
298 printf("\n");
299 i = 0;
300 do {
301 printf("%d", i++);
302 } while (i < 10);
303 printf("\n");
305 /* break/continue tests */
306 i = 0;
307 while (1) {
308 if (i == 6)
309 break;
310 i++;
311 if (i == 3)
312 continue;
313 printf("%d", i);
315 printf("\n");
317 /* break/continue tests */
318 i = 0;
319 do {
320 if (i == 6)
321 break;
322 i++;
323 if (i == 3)
324 continue;
325 printf("%d", i);
326 } while(1);
327 printf("\n");
329 for(i = 0;i < 10;i++) {
330 if (i == 3)
331 continue;
332 printf("%d", i);
334 printf("\n");
338 void goto_test()
340 int i;
341 static void *label_table[3] = { &&label1, &&label2, &&label3 };
343 printf("goto:\n");
344 i = 0;
345 s_loop:
346 if (i >= 10)
347 goto s_end;
348 printf("%d", i);
349 i++;
350 goto s_loop;
351 s_end:
352 printf("\n");
354 /* we also test computed gotos (GCC extension) */
355 for(i=0;i<3;i++) {
356 goto *label_table[i];
357 label1:
358 printf("label1\n");
359 goto next;
360 label2:
361 printf("label2\n");
362 goto next;
363 label3:
364 printf("label3\n");
365 next: ;
369 enum {
371 E1 = 2,
372 E2 = 4,
377 enum test {
378 E5 = 1000,
381 void enum_test()
383 enum test b1;
384 printf("enum:\n%d %d %d %d %d %d\n",
385 E0, E1, E2, E3, E4, E5);
386 b1 = 1;
387 printf("b1=%d\n", b1);
390 typedef int *my_ptr;
392 void typedef_test()
394 my_ptr a;
395 int b;
396 a = &b;
397 *a = 1234;
398 printf("typedef:\n");
399 printf("a=%d\n", *a);
402 void forward_test()
404 printf("forward:\n");
405 forward_ref();
406 forward_ref();
410 void forward_ref(void)
412 printf("forward ok\n");
415 typedef struct struct1 {
416 int f1;
417 int f2, f3;
418 union union1 {
419 int v1;
420 int v2;
421 } u;
422 char str[3];
423 } struct1;
425 struct struct2 {
426 int a;
427 char b;
430 union union2 {
431 int w1;
432 int w2;
435 struct struct1 st1, st2;
437 int main(int argc, char **argv)
439 string_test();
440 expr_test();
441 macro_test();
442 scope_test();
443 forward_test();
444 funcptr_test();
445 loop_test();
446 switch_test();
447 goto_test();
448 enum_test();
449 typedef_test();
450 struct_test();
451 array_test();
452 expr_ptr_test();
453 bool_test();
454 expr2_test();
455 constant_expr_test();
456 expr_cmp_test();
457 char_short_test();
458 init_test();
459 compound_literal_test();
460 kr_test();
461 struct_assign_test();
462 cast_test();
463 bitfield_test();
464 c99_bool_test();
465 float_test();
466 longlong_test();
467 stdarg_test();
468 whitespace_test();
469 relocation_test();
470 old_style_function();
471 sizeof_test();
472 typeof_test();
473 statement_expr_test();
474 local_label_test();
475 asm_test();
476 return 0;
479 int tab[3];
480 int tab2[3][2];
482 int g;
484 void f1(g)
486 printf("g1=%d\n", g);
489 void scope_test()
491 printf("scope:\n");
492 g = 2;
493 f1(1);
494 printf("g2=%d\n", g);
496 int g;
497 g = 3;
498 printf("g3=%d\n", g);
500 int g;
501 g = 4;
502 printf("g4=%d\n", g);
505 printf("g5=%d\n", g);
508 void array_test(int a[4])
510 int i, j;
512 printf("array:\n");
513 printf("sizeof(a) = %d\n", sizeof(a));
514 printf("sizeof(\"a\") = %d\n", sizeof("a"));
515 #ifdef C99_MACROS
516 printf("sizeof(__func__) = %d\n", sizeof(__func__));
517 #endif
518 printf("sizeof tab %d\n", sizeof(tab));
519 printf("sizeof tab2 %d\n", sizeof tab2);
520 tab[0] = 1;
521 tab[1] = 2;
522 tab[2] = 3;
523 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
524 for(i=0;i<3;i++)
525 for(j=0;j<2;j++)
526 tab2[i][j] = 10 * i + j;
527 for(i=0;i<3*2;i++) {
528 printf(" %3d", ((int *)tab2)[i]);
530 printf("\n");
533 void expr_test()
535 int a, b;
536 a = 0;
537 printf("%d\n", a += 1);
538 printf("%d\n", a -= 2);
539 printf("%d\n", a *= 31232132);
540 printf("%d\n", a /= 4);
541 printf("%d\n", a %= 20);
542 printf("%d\n", a &= 6);
543 printf("%d\n", a ^= 7);
544 printf("%d\n", a |= 8);
545 printf("%d\n", a >>= 3);
546 printf("%d\n", a <<= 4);
548 a = 22321;
549 b = -22321;
550 printf("%d\n", a + 1);
551 printf("%d\n", a - 2);
552 printf("%d\n", a * 312);
553 printf("%d\n", a / 4);
554 printf("%d\n", b / 4);
555 printf("%d\n", (unsigned)b / 4);
556 printf("%d\n", a % 20);
557 printf("%d\n", b % 20);
558 printf("%d\n", (unsigned)b % 20);
559 printf("%d\n", a & 6);
560 printf("%d\n", a ^ 7);
561 printf("%d\n", a | 8);
562 printf("%d\n", a >> 3);
563 printf("%d\n", b >> 3);
564 printf("%d\n", (unsigned)b >> 3);
565 printf("%d\n", a << 4);
566 printf("%d\n", ~a);
567 printf("%d\n", -a);
568 printf("%d\n", +a);
570 printf("%d\n", 12 + 1);
571 printf("%d\n", 12 - 2);
572 printf("%d\n", 12 * 312);
573 printf("%d\n", 12 / 4);
574 printf("%d\n", 12 % 20);
575 printf("%d\n", 12 & 6);
576 printf("%d\n", 12 ^ 7);
577 printf("%d\n", 12 | 8);
578 printf("%d\n", 12 >> 2);
579 printf("%d\n", 12 << 4);
580 printf("%d\n", ~12);
581 printf("%d\n", -12);
582 printf("%d\n", +12);
583 printf("%d %d %d %d\n",
584 isid('a'),
585 isid('g'),
586 isid('T'),
587 isid('('));
590 int isid(int c)
592 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
595 /**********************/
597 int vstack[10], *vstack_ptr;
599 void vpush(int vt, int vc)
601 *vstack_ptr++ = vt;
602 *vstack_ptr++ = vc;
605 void vpop(int *ft, int *fc)
607 *fc = *--vstack_ptr;
608 *ft = *--vstack_ptr;
611 void expr2_test()
613 int a, b;
615 printf("expr2:\n");
616 vstack_ptr = vstack;
617 vpush(1432432, 2);
618 vstack_ptr[-2] &= ~0xffffff80;
619 vpop(&a, &b);
620 printf("res= %d %d\n", a, b);
623 void constant_expr_test()
625 int a;
626 printf("constant_expr:\n");
627 a = 3;
628 printf("%d\n", a * 16);
629 printf("%d\n", a * 1);
630 printf("%d\n", a + 0);
633 int tab4[10];
635 void expr_ptr_test()
637 int *p, *q;
639 printf("expr_ptr:\n");
640 p = tab4;
641 q = tab4 + 10;
642 printf("diff=%d\n", q - p);
643 p++;
644 printf("inc=%d\n", p - tab4);
645 p--;
646 printf("dec=%d\n", p - tab4);
647 ++p;
648 printf("inc=%d\n", p - tab4);
649 --p;
650 printf("dec=%d\n", p - tab4);
651 printf("add=%d\n", p + 3 - tab4);
652 printf("add=%d\n", 3 + p - tab4);
655 void expr_cmp_test()
657 int a, b;
658 printf("constant_expr:\n");
659 a = -1;
660 b = 1;
661 printf("%d\n", a == a);
662 printf("%d\n", a != a);
664 printf("%d\n", a < b);
665 printf("%d\n", a <= b);
666 printf("%d\n", a <= a);
667 printf("%d\n", b >= a);
668 printf("%d\n", a >= a);
669 printf("%d\n", b > a);
671 printf("%d\n", (unsigned)a < b);
672 printf("%d\n", (unsigned)a <= b);
673 printf("%d\n", (unsigned)a <= a);
674 printf("%d\n", (unsigned)b >= a);
675 printf("%d\n", (unsigned)a >= a);
676 printf("%d\n", (unsigned)b > a);
679 struct empty {
682 struct aligntest1 {
683 char a[10];
686 struct aligntest2 {
687 int a;
688 char b[10];
691 struct aligntest3 {
692 double a, b;
695 struct aligntest4 {
696 double a[0];
699 void struct_test()
701 struct1 *s;
702 union union2 u;
704 printf("struct:\n");
705 printf("sizes: %d %d %d %d\n",
706 sizeof(struct struct1),
707 sizeof(struct struct2),
708 sizeof(union union1),
709 sizeof(union union2));
710 st1.f1 = 1;
711 st1.f2 = 2;
712 st1.f3 = 3;
713 printf("st1: %d %d %d\n",
714 st1.f1, st1.f2, st1.f3);
715 st1.u.v1 = 1;
716 st1.u.v2 = 2;
717 printf("union1: %d\n", st1.u.v1);
718 u.w1 = 1;
719 u.w2 = 2;
720 printf("union2: %d\n", u.w1);
721 s = &st2;
722 s->f1 = 3;
723 s->f2 = 2;
724 s->f3 = 1;
725 printf("st2: %d %d %d\n",
726 s->f1, s->f2, s->f3);
727 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
729 /* align / size tests */
730 printf("aligntest1 sizeof=%d alignof=%d\n",
731 sizeof(struct aligntest1), __alignof__(struct aligntest1));
732 printf("aligntest2 sizeof=%d alignof=%d\n",
733 sizeof(struct aligntest2), __alignof__(struct aligntest2));
734 printf("aligntest3 sizeof=%d alignof=%d\n",
735 sizeof(struct aligntest3), __alignof__(struct aligntest3));
736 printf("aligntest4 sizeof=%d alignof=%d\n",
737 sizeof(struct aligntest4), __alignof__(struct aligntest4));
739 /* empty structures (GCC extension) */
740 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
741 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
744 /* XXX: depend on endianness */
745 void char_short_test()
747 int var1, var2;
749 printf("char_short:\n");
751 var1 = 0x01020304;
752 var2 = 0xfffefdfc;
753 printf("s8=%d %d\n",
754 *(char *)&var1, *(char *)&var2);
755 printf("u8=%d %d\n",
756 *(unsigned char *)&var1, *(unsigned char *)&var2);
757 printf("s16=%d %d\n",
758 *(short *)&var1, *(short *)&var2);
759 printf("u16=%d %d\n",
760 *(unsigned short *)&var1, *(unsigned short *)&var2);
761 printf("s32=%d %d\n",
762 *(int *)&var1, *(int *)&var2);
763 printf("u32=%d %d\n",
764 *(unsigned int *)&var1, *(unsigned int *)&var2);
765 *(char *)&var1 = 0x08;
766 printf("var1=%x\n", var1);
767 *(short *)&var1 = 0x0809;
768 printf("var1=%x\n", var1);
769 *(int *)&var1 = 0x08090a0b;
770 printf("var1=%x\n", var1);
773 /******************/
775 typedef struct Sym {
776 int v;
777 int t;
778 int c;
779 struct Sym *next;
780 struct Sym *prev;
781 } Sym;
783 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
784 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
786 static int toupper1(int a)
788 return TOUPPER(a);
791 void bool_test()
793 int *s, a, b, t, f, i;
795 a = 0;
796 s = (void*)0;
797 printf("!s=%d\n", !s);
799 if (!s || !s[0])
800 a = 1;
801 printf("a=%d\n", a);
803 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
804 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
805 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
806 #if 1 && 1
807 printf("a1\n");
808 #endif
809 #if 1 || 0
810 printf("a2\n");
811 #endif
812 #if 1 ? 0 : 1
813 printf("a3\n");
814 #endif
815 #if 0 ? 0 : 1
816 printf("a4\n");
817 #endif
819 a = 4;
820 printf("b=%d\n", a + (0 ? 1 : a / 2));
822 /* test register spilling */
823 a = 10;
824 b = 10;
825 a = (a + b) * ((a < b) ?
826 ((b - a) * (a - b)): a + b);
827 printf("a=%d\n", a);
829 /* test complex || or && expressions */
830 t = 1;
831 f = 0;
832 a = 32;
833 printf("exp=%d\n", f == (32 <= a && a <= 3));
834 printf("r=%d\n", (t || f) + (t && f));
836 /* test ? : cast */
838 int aspect_on;
839 int aspect_native = 65536;
840 double bfu_aspect = 1.0;
841 int aspect;
842 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
843 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
844 printf("aspect=%d\n", aspect);
848 /* test ? : GCC extension */
850 static int v1 = 34 ? : -1; /* constant case */
851 static int v2 = 0 ? : -1; /* constant case */
852 int a = 30;
854 printf("%d %d\n", v1, v2);
855 printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2);
858 /* again complex expression */
859 for(i=0;i<256;i++) {
860 if (toupper1 (i) != TOUPPER (i))
861 printf("error %d\n", i);
865 /* GCC accepts that */
866 static int tab_reinit[];
867 static int tab_reinit[10];
869 //int cinit1; /* a global variable can be defined several times without error ! */
870 int cinit1;
871 int cinit1;
872 int cinit1 = 0;
873 int *cinit2 = (int []){3, 2, 1};
875 void compound_literal_test(void)
877 int *p, i;
878 char *q, *q3;
880 printf("compound_test:\n");
882 p = (int []){1, 2, 3};
883 for(i=0;i<3;i++)
884 printf(" %d", p[i]);
885 printf("\n");
887 for(i=0;i<3;i++)
888 printf("%d", cinit2[i]);
889 printf("\n");
891 q = "tralala1";
892 printf("q1=%s\n", q);
894 q = (char *){ "tralala2" };
895 printf("q2=%s\n", q);
897 q3 = (char *){ q };
898 printf("q3=%s\n", q3);
900 q = (char []){ "tralala3" };
901 printf("q4=%s\n", q);
903 #ifdef ALL_ISOC99
904 p = (int []){1, 2, cinit1 + 3};
905 for(i=0;i<3;i++)
906 printf(" %d", p[i]);
907 printf("\n");
909 for(i=0;i<3;i++) {
910 p = (int []){1, 2, 4 + i};
911 printf("%d %d %d\n",
912 p[0],
913 p[1],
914 p[2]);
916 #endif
919 /* K & R protos */
921 kr_func1(a, b)
923 return a + b;
926 int kr_func2(a, b)
928 return a + b;
931 kr_test()
933 printf("kr_test:\n");
934 printf("func1=%d\n", kr_func1(3, 4));
935 printf("func2=%d\n", kr_func2(3, 4));
936 return 0;
939 void num(int n)
941 char *tab, *p;
942 tab = (char*)malloc(20);
943 p = tab;
944 while (1) {
945 *p = 48 + (n % 10);
946 p++;
947 n = n / 10;
948 if (n == 0)
949 break;
951 while (p != tab) {
952 p--;
953 printf("%c", *p);
955 printf("\n");
958 /* structure assignment tests */
959 struct structa1 {
960 int f1;
961 char f2;
964 struct structa1 ssta1;
966 void struct_assign_test1(struct structa1 s1, int t)
968 printf("%d %d %d\n", s1.f1, s1.f2, t);
971 struct structa1 struct_assign_test2(struct structa1 s1, int t)
973 s1.f1 += t;
974 s1.f2 -= t;
975 return s1;
978 void struct_assign_test(void)
980 struct structa1 lsta1, lsta2;
982 #if 0
983 printf("struct_assign_test:\n");
985 lsta1.f1 = 1;
986 lsta1.f2 = 2;
987 printf("%d %d\n", lsta1.f1, lsta1.f2);
988 lsta2 = lsta1;
989 printf("%d %d\n", lsta2.f1, lsta2.f2);
990 #else
991 lsta2.f1 = 1;
992 lsta2.f2 = 2;
993 #endif
994 struct_assign_test1(lsta2, 3);
996 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
997 lsta2 = struct_assign_test2(lsta2, 4);
998 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
1001 /* casts to short/char */
1003 void cast1(char a, short b, unsigned char c, unsigned short d)
1005 printf("%d %d %d %d\n", a, b, c, d);
1008 char bcast;
1009 short scast;
1011 void cast_test()
1013 int a;
1014 char c;
1015 char tab[10];
1017 printf("cast_test:\n");
1018 a = 0xfffff;
1019 cast1(a, a, a, a);
1020 a = 0xffffe;
1021 printf("%d %d %d %d\n",
1022 (char)(a + 1),
1023 (short)(a + 1),
1024 (unsigned char)(a + 1),
1025 (unsigned short)(a + 1));
1026 printf("%d %d %d %d\n",
1027 (char)0xfffff,
1028 (short)0xfffff,
1029 (unsigned char)0xfffff,
1030 (unsigned short)0xfffff);
1032 a = (bcast = 128) + 1;
1033 printf("%d\n", a);
1034 a = (scast = 65536) + 1;
1035 printf("%d\n", a);
1037 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1039 /* test implicit int casting for array accesses */
1040 c = 0;
1041 tab[1] = 2;
1042 tab[c] = 1;
1043 printf("%d %d\n", tab[0], tab[1]);
1045 /* test implicit casting on some operators */
1046 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1047 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1048 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1051 /* initializers tests */
1052 struct structinit1 {
1053 int f1;
1054 char f2;
1055 short f3;
1056 int farray[3];
1059 int sinit1 = 2;
1060 int sinit2 = { 3 };
1061 int sinit3[3] = { 1, 2, {{3}}, };
1062 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1063 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1064 int sinit6[] = { 1, 2, 3 };
1065 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1066 char sinit8[] = "hello" "trala";
1068 struct structinit1 sinit9 = { 1, 2, 3 };
1069 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1070 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1071 #ifdef ALL_ISOC99
1072 .farray[0] = 10,
1073 .farray[1] = 11,
1074 .farray[2] = 12,
1075 #endif
1078 char *sinit12 = "hello world";
1079 char *sinit13[] = {
1080 "test1",
1081 "test2",
1082 "test3",
1084 char sinit14[10] = { "abc" };
1085 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1087 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1089 struct bar {
1090 char *s;
1091 int len;
1092 } sinit17[] = {
1093 "a1", 4,
1094 "a2", 1
1097 int sinit18[10] = {
1098 [2 ... 5] = 20,
1100 [8] = 10,
1103 void init_test(void)
1105 int linit1 = 2;
1106 int linit2 = { 3 };
1107 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1108 int linit6[] = { 1, 2, 3 };
1109 int i, j;
1110 char linit8[] = "hello" "trala";
1111 int linit12[10] = { 1, 2 };
1112 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1113 char linit14[10] = "abc";
1114 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1115 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1116 int linit17 = sizeof(linit17);
1118 printf("init_test:\n");
1120 printf("sinit1=%d\n", sinit1);
1121 printf("sinit2=%d\n", sinit2);
1122 printf("sinit3=%d %d %d %d\n",
1123 sizeof(sinit3),
1124 sinit3[0],
1125 sinit3[1],
1126 sinit3[2]
1128 printf("sinit6=%d\n", sizeof(sinit6));
1129 printf("sinit7=%d %d %d %d\n",
1130 sizeof(sinit7),
1131 sinit7[0],
1132 sinit7[1],
1133 sinit7[2]
1135 printf("sinit8=%s\n", sinit8);
1136 printf("sinit9=%d %d %d\n",
1137 sinit9.f1,
1138 sinit9.f2,
1139 sinit9.f3
1141 printf("sinit10=%d %d %d\n",
1142 sinit10.f1,
1143 sinit10.f2,
1144 sinit10.f3
1146 printf("sinit11=%d %d %d %d %d %d\n",
1147 sinit11.f1,
1148 sinit11.f2,
1149 sinit11.f3,
1150 sinit11.farray[0],
1151 sinit11.farray[1],
1152 sinit11.farray[2]
1155 for(i=0;i<3;i++)
1156 for(j=0;j<2;j++)
1157 printf("[%d][%d] = %d %d %d\n",
1158 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1159 printf("linit1=%d\n", linit1);
1160 printf("linit2=%d\n", linit2);
1161 printf("linit6=%d\n", sizeof(linit6));
1162 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1164 printf("sinit12=%s\n", sinit12);
1165 printf("sinit13=%d %s %s %s\n",
1166 sizeof(sinit13),
1167 sinit13[0],
1168 sinit13[1],
1169 sinit13[2]);
1170 printf("sinit14=%s\n", sinit14);
1172 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1173 printf("\n");
1174 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1175 printf("\n");
1176 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1177 printf("\n");
1178 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1179 printf("\n");
1180 printf("%d %d %d %d\n",
1181 linit16.a1,
1182 linit16.a2,
1183 linit16.a3,
1184 linit16.a4);
1185 /* test that initialisation is done after variable declare */
1186 printf("linit17=%d\n", linit17);
1187 printf("sinit15=%d\n", sinit15[0]);
1188 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1189 printf("sinit17=%s %d %s %d\n",
1190 sinit17[0].s, sinit17[0].len,
1191 sinit17[1].s, sinit17[1].len);
1192 for(i=0;i<10;i++)
1193 printf("%x ", sinit18[i]);
1194 printf("\n");
1198 void switch_test()
1200 int i;
1202 for(i=0;i<15;i++) {
1203 switch(i) {
1204 case 0:
1205 case 1:
1206 printf("a");
1207 break;
1208 default:
1209 printf("%d", i);
1210 break;
1211 case 8 ... 12:
1212 printf("c");
1213 break;
1214 case 3:
1215 printf("b");
1216 break;
1219 printf("\n");
1222 /* ISOC99 _Bool type */
1223 void c99_bool_test(void)
1225 #ifdef BOOL_ISOC99
1226 int a;
1227 _Bool b;
1229 printf("bool_test:\n");
1230 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1231 a = 3;
1232 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1233 b = 3;
1234 printf("b = %d\n", b);
1235 b++;
1236 printf("b = %d\n", b);
1237 #endif
1240 void bitfield_test(void)
1242 int a;
1243 struct sbf1 {
1244 int f1 : 3;
1245 int : 2;
1246 int f2 : 1;
1247 int : 0;
1248 int f3 : 5;
1249 int f4 : 7;
1250 unsigned int f5 : 7;
1251 } st1;
1252 printf("bitfield_test:");
1253 printf("sizeof(st1) = %d\n", sizeof(st1));
1255 st1.f1 = 3;
1256 st1.f2 = 1;
1257 st1.f3 = 15;
1258 a = 120;
1259 st1.f4 = a;
1260 st1.f5 = a;
1261 st1.f5++;
1262 printf("%d %d %d %d %d\n",
1263 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1266 #define FTEST(prefix, type, fmt)\
1267 void prefix ## cmp(type a, type b)\
1269 printf("%d %d %d %d %d %d\n",\
1270 a == b,\
1271 a != b,\
1272 a < b,\
1273 a > b,\
1274 a >= b,\
1275 a <= b);\
1276 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1279 a + b,\
1280 a - b,\
1281 a * b,\
1282 a / b,\
1283 -a);\
1284 printf(fmt "\n", ++a);\
1285 printf(fmt "\n", a++);\
1286 printf(fmt "\n", a);\
1288 void prefix ## fcast(type a)\
1290 float fa;\
1291 double da;\
1292 long double la;\
1293 int ia;\
1294 unsigned int ua;\
1295 type b;\
1296 fa = a;\
1297 da = a;\
1298 la = a;\
1299 printf("ftof: %f %f %Lf\n", fa, da, la);\
1300 ia = (int)a;\
1301 ua = (unsigned int)a;\
1302 printf("ftoi: %d %u\n", ia, ua);\
1303 ia = -1234;\
1304 ua = 0x81234500;\
1305 b = ia;\
1306 printf("itof: " fmt "\n", b);\
1307 b = ua;\
1308 printf("utof: " fmt "\n", b);\
1311 void prefix ## test(void)\
1313 printf("testing '%s'\n", #type);\
1314 prefix ## cmp(1, 2.5);\
1315 prefix ## cmp(2, 1.5);\
1316 prefix ## cmp(1, 1);\
1317 prefix ## fcast(234.6);\
1318 prefix ## fcast(-2334.6);\
1321 FTEST(f, float, "%f")
1322 FTEST(d, double, "%f")
1323 FTEST(ld, long double, "%Lf")
1325 double ftab1[3] = { 1.2, 3.4, -5.6 };
1328 void float_test(void)
1330 float fa, fb;
1331 double da, db;
1332 int a;
1333 unsigned int b;
1335 printf("float_test:\n");
1336 printf("sizeof(float) = %d\n", sizeof(float));
1337 printf("sizeof(double) = %d\n", sizeof(double));
1338 printf("sizeof(long double) = %d\n", sizeof(long double));
1339 ftest();
1340 dtest();
1341 ldtest();
1342 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1343 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1344 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1345 da = 123;
1346 printf("da=%f\n", da);
1347 fa = 123;
1348 printf("fa=%f\n", fa);
1349 a = 4000000000;
1350 da = a;
1351 printf("da = %f\n", da);
1352 b = 4000000000;
1353 db = b;
1354 printf("db = %f\n", db);
1357 int fib(int n)
1359 if (n <= 2)
1360 return 1;
1361 else
1362 return fib(n-1) + fib(n-2);
1365 void funcptr_test()
1367 void (*func)(int);
1368 int a;
1369 struct {
1370 int dummy;
1371 void (*func)(int);
1372 } st1;
1374 printf("funcptr:\n");
1375 func = &num;
1376 (*func)(12345);
1377 func = num;
1378 a = 1;
1379 a = 1;
1380 func(12345);
1381 /* more complicated pointer computation */
1382 st1.func = num;
1383 st1.func(12346);
1384 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1385 printf("sizeof2 = %d\n", sizeof funcptr_test);
1386 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1387 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1390 void lloptest(long long a, long long b)
1392 unsigned long long ua, ub;
1394 ua = a;
1395 ub = b;
1396 /* arith */
1397 printf("arith: %Ld %Ld %Ld\n",
1398 a + b,
1399 a - b,
1400 a * b);
1402 if (b != 0) {
1403 printf("arith1: %Ld %Ld\n",
1404 a / b,
1405 a % b);
1408 /* binary */
1409 printf("bin: %Ld %Ld %Ld\n",
1410 a & b,
1411 a | b,
1412 a ^ b);
1414 /* tests */
1415 printf("test: %d %d %d %d %d %d\n",
1416 a == b,
1417 a != b,
1418 a < b,
1419 a > b,
1420 a >= b,
1421 a <= b);
1423 printf("utest: %d %d %d %d %d %d\n",
1424 ua == ub,
1425 ua != ub,
1426 ua < ub,
1427 ua > ub,
1428 ua >= ub,
1429 ua <= ub);
1431 /* arith2 */
1432 a++;
1433 b++;
1434 printf("arith2: %Ld %Ld\n", a, b);
1435 printf("arith2: %Ld %Ld\n", a++, b++);
1436 printf("arith2: %Ld %Ld\n", --a, --b);
1437 printf("arith2: %Ld %Ld\n", a, b);
1440 void llshift(long long a, int b)
1442 printf("shift: %Ld %Ld %Ld\n",
1443 (unsigned long long)a >> b,
1444 a >> b,
1445 a << b);
1446 printf("shiftc: %Ld %Ld %Ld\n",
1447 (unsigned long long)a >> 3,
1448 a >> 3,
1449 a << 3);
1450 printf("shiftc: %Ld %Ld %Ld\n",
1451 (unsigned long long)a >> 35,
1452 a >> 35,
1453 a << 35);
1456 void llfloat(void)
1458 float fa;
1459 double da;
1460 long double lda;
1461 long long la, lb, lc;
1462 unsigned long long ula, ulb, ulc;
1463 la = 0x12345678;
1464 ula = 0x72345678;
1465 la = (la << 20) | 0x12345;
1466 ula = ula << 33;
1467 printf("la=%Ld ula=%Lu\n", la, ula);
1469 fa = la;
1470 da = la;
1471 lda = la;
1472 printf("lltof: %f %f %Lf\n", fa, da, lda);
1474 la = fa;
1475 lb = da;
1476 lc = lda;
1477 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1479 fa = ula;
1480 da = ula;
1481 lda = ula;
1482 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1484 ula = fa;
1485 ulb = da;
1486 ulc = lda;
1487 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1490 long long llfunc1(int a)
1492 return a * 2;
1495 struct S {
1496 int id;
1497 char item;
1500 long long int value(struct S *v)
1502 return ((long long int)v->item);
1505 void longlong_test(void)
1507 long long a, b, c;
1508 int ia;
1509 unsigned int ua;
1510 printf("longlong_test:\n");
1511 printf("sizeof(long long) = %d\n", sizeof(long long));
1512 ia = -1;
1513 ua = -2;
1514 a = ia;
1515 b = ua;
1516 printf("%Ld %Ld\n", a, b);
1517 printf("%Ld %Ld %Ld %Lx\n",
1518 (long long)1,
1519 (long long)-2,
1520 1LL,
1521 0x1234567812345679);
1522 a = llfunc1(-3);
1523 printf("%Ld\n", a);
1525 lloptest(1000, 23);
1526 lloptest(0xff, 0x1234);
1527 b = 0x72345678 << 10;
1528 lloptest(-3, b);
1529 llshift(0x123, 5);
1530 llshift(-23, 5);
1531 b = 0x72345678LL << 10;
1532 llshift(b, 47);
1534 llfloat();
1535 #if 1
1536 b = 0x12345678;
1537 a = -1;
1538 c = a + b;
1539 printf("%Lx\n", c);
1540 #endif
1542 /* long long reg spill test */
1544 struct S a;
1546 a.item = 3;
1547 printf("%lld\n", value(&a));
1549 lloptest(0x80000000, 0);
1552 void vprintf1(const char *fmt, ...)
1554 va_list ap;
1555 const char *p;
1556 int c, i;
1557 double d;
1558 long long ll;
1560 va_start(ap, fmt);
1562 p = fmt;
1563 for(;;) {
1564 c = *p;
1565 if (c == '\0')
1566 break;
1567 p++;
1568 if (c == '%') {
1569 c = *p;
1570 switch(c) {
1571 case '\0':
1572 goto the_end;
1573 case 'd':
1574 i = va_arg(ap, int);
1575 printf("%d", i);
1576 break;
1577 case 'f':
1578 d = va_arg(ap, double);
1579 printf("%f", d);
1580 break;
1581 case 'l':
1582 ll = va_arg(ap, long long);
1583 printf("%Ld", ll);
1584 break;
1586 p++;
1587 } else {
1588 putchar(c);
1591 the_end:
1592 va_end(ap);
1596 void stdarg_test(void)
1598 vprintf1("%d %d %d\n", 1, 2, 3);
1599 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1600 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1603 void whitespace_test(void)
1605 char *str;
1607 \f\v #if 1
1608 pri\
1609 ntf("whitspace:\n");\f\v
1610 #endif
1611 pf("N=%d\n", 2);
1613 #ifdef CORRECT_CR_HANDLING
1614 pri\
1615 ntf("aaa=%d\n", 3);
1616 #endif
1618 pri\
1620 ntf("min=%d\n", 4);
1621 printf("len1=%d\n", strlen("
1622 "));
1623 #ifdef CORRECT_CR_HANDLING
1624 str = "
1626 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1627 #endif
1628 printf("len1=%d\n", strlen(" a
1629 "));
1632 int reltab[3] = { 1, 2, 3 };
1634 int *rel1 = &reltab[1];
1635 int *rel2 = &reltab[2];
1637 void relocation_test(void)
1639 printf("*rel1=%d\n", *rel1);
1640 printf("*rel2=%d\n", *rel2);
1643 void old_style_f(a,b,c)
1644 int a, b;
1645 double c;
1647 printf("a=%d b=%d b=%f\n", a, b, c);
1650 void old_style_function(void)
1652 old_style_f((void *)1, 2, 3.0);
1655 void sizeof_test(void)
1657 int a;
1658 int **ptr;
1660 printf("sizeof(int) = %d\n", sizeof(int));
1661 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1662 printf("sizeof(short) = %d\n", sizeof(short));
1663 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1664 printf("sizeof(char) = %d\n", sizeof(char));
1665 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1666 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1667 a = 1;
1668 printf("sizeof(a++) = %d\n", sizeof a++);
1669 printf("a=%d\n", a);
1670 ptr = NULL;
1671 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1673 /* some alignof tests */
1674 printf("__alignof__(int) = %d\n", __alignof__(int));
1675 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1676 printf("__alignof__(short) = %d\n", __alignof__(short));
1677 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1678 printf("__alignof__(char) = %d\n", __alignof__(char));
1679 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1680 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1683 void typeof_test(void)
1685 double a;
1686 typeof(a) b;
1687 typeof(float) c;
1689 a = 1.5;
1690 b = 2.5;
1691 c = 3.5;
1692 printf("a=%f b=%f c=%f\n", a, b, c);
1695 void statement_expr_test(void)
1697 int a, i;
1699 a = 0;
1700 for(i=0;i<10;i++) {
1701 a += 1 +
1702 ( { int b, j;
1703 b = 0;
1704 for(j=0;j<5;j++)
1705 b += j; b;
1706 } );
1708 printf("a=%d\n", a);
1712 void local_label_test(void)
1714 int a;
1715 goto l1;
1717 a = 1 + ({
1718 __label__ l1, l2, l3;
1719 goto l4;
1721 printf("aa1\n");
1722 goto l1;
1724 printf("aa3\n");
1725 goto l3;
1727 printf("aa2\n");
1728 goto l2;
1729 l3:;
1732 printf("a=%d\n", a);
1733 return;
1735 printf("bb1\n");
1736 goto l2;
1738 printf("bb2\n");
1739 goto l5;
1742 /* inline assembler test */
1743 #ifdef __i386__
1745 /* from linux kernel */
1746 static char * strncat1(char * dest,const char * src,size_t count)
1748 int d0, d1, d2, d3;
1749 __asm__ __volatile__(
1750 "repne\n\t"
1751 "scasb\n\t"
1752 "decl %1\n\t"
1753 "movl %8,%3\n"
1754 "1:\tdecl %3\n\t"
1755 "js 2f\n\t"
1756 "lodsb\n\t"
1757 "stosb\n\t"
1758 "testb %%al,%%al\n\t"
1759 "jne 1b\n"
1760 "2:\txorl %2,%2\n\t"
1761 "stosb"
1762 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
1763 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
1764 : "memory");
1765 return dest;
1768 static inline void * memcpy1(void * to, const void * from, size_t n)
1770 int d0, d1, d2;
1771 __asm__ __volatile__(
1772 "rep ; movsl\n\t"
1773 "testb $2,%b4\n\t"
1774 "je 1f\n\t"
1775 "movsw\n"
1776 "1:\ttestb $1,%b4\n\t"
1777 "je 2f\n\t"
1778 "movsb\n"
1779 "2:"
1780 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
1781 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
1782 : "memory");
1783 return (to);
1786 static __inline__ void sigaddset1(unsigned int *set, int _sig)
1788 __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1791 static __inline__ void sigdelset1(unsigned int *set, int _sig)
1793 asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc");
1796 static __inline__ __const__ unsigned int swab32(unsigned int x)
1798 __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
1799 "rorl $16,%0\n\t" /* swap words */
1800 "xchgb %b0,%h0" /* swap higher bytes */
1801 :"=q" (x)
1802 : "0" (x));
1803 return x;
1806 unsigned int set;
1808 void asm_test(void)
1810 char buf[128];
1811 unsigned int val;
1813 printf("inline asm:\n");
1814 memcpy1(buf, "hello", 6);
1815 strncat1(buf, " worldXXXXX", 3);
1816 printf("%s\n", buf);
1818 set = 0xff;
1819 sigdelset1(&set, 2);
1820 sigaddset1(&set, 16);
1821 /* NOTE: we test here if C labels are correctly restored after the
1822 asm statement */
1823 goto label1;
1824 label2:
1825 __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");
1826 printf("set=0x%x\n", set);
1827 val = 0x01020304;
1828 printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val));
1829 return;
1830 label1:
1831 goto label2;
1834 #else
1836 void asm_test(void)
1840 #endif