FreeBSD fixes - file type guessing - glibc2.2 fixes - ELF align issue
[tinycc.git] / tcctest.c
bloba893f609f12c9b7b1b34da1c0af06314c8003f74
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 /* test various include syntaxes */
15 #define TCCLIB_INC <tcclib.h>
16 #define TCCLIB_INC1 <tcclib
17 #define TCCLIB_INC2 h>
18 #define TCCLIB_INC3 "tcclib"
20 #include TCCLIB_INC
22 #include TCCLIB_INC1.TCCLIB_INC2
24 #include TCCLIB_INC1.h>
26 #include TCCLIB_INC3 ".h"
28 #include <tcclib.h>
30 #include "tcclib.h"
32 void string_test();
33 void expr_test();
34 void macro_test();
35 void scope_test();
36 void forward_test();
37 void funcptr_test();
38 void loop_test();
39 void switch_test();
40 void goto_test();
41 void enum_test();
42 void typedef_test();
43 void struct_test();
44 void array_test();
45 void expr_ptr_test();
46 void bool_test();
47 void expr2_test();
48 void constant_expr_test();
49 void expr_cmp_test();
50 void char_short_test();
51 void init_test(void);
52 void compound_literal_test(void);
53 int kr_test();
54 void struct_assign_test(void);
55 void cast_test(void);
56 void bitfield_test(void);
57 void c99_bool_test(void);
58 void float_test(void);
59 void longlong_test(void);
60 void stdarg_test(void);
61 void whitespace_test(void);
62 void relocation_test(void);
63 void old_style_function(void);
64 void sizeof_test(void);
65 void typeof_test(void);
67 int fib(int n);
68 void num(int n);
69 void forward_ref(void);
70 int isid(int c);
72 #define A 2
73 #define N 1234 + A
74 #define pf printf
75 #define M1(a, b) (a) + (b)
77 #define str\
78 (s) # s
79 #define glue(a, b) a ## b
80 #define xglue(a, b) glue(a, b)
81 #define HIGHLOW "hello"
82 #define LOW LOW ", world"
84 #define min(a, b) ((a) < (b) ? (a) : (b))
86 #ifdef C99_MACROS
87 #define dprintf(level,...) printf(__VA_ARGS__)
88 #endif
90 /* gcc vararg macros */
91 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
93 #define MACRO_NOARGS()
95 #define AAA 3
96 #undef AAA
97 #define AAA 4
99 #if 1
100 #define B3 1
101 #elif 1
102 #define B3 2
103 #elif 0
104 #define B3 3
105 #else
106 #define B3 4
107 #endif
109 #define __INT64_C(c) c ## LL
110 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
112 void macro_test(void)
114 printf("macro:\n");\f\v
115 pf("N=%d\n", N);
116 printf("aaa=%d\n", AAA);
118 printf("min=%d\n", min(1, min(2, -1)));
120 printf("s1=%s\n", glue(HIGH, LOW));
121 printf("s2=%s\n", xglue(HIGH, LOW));
122 printf("s3=%s\n", str("c"));
123 printf("s4=%s\n", str(a1));
124 printf("B3=%d\n", B3);
126 #ifdef A
127 printf("A defined\n");
128 #endif
129 #ifdef B
130 printf("B defined\n");
131 #endif
132 #ifdef A
133 printf("A defined\n");
134 #else
135 printf("A not defined\n");
136 #endif
137 #ifdef B
138 printf("B defined\n");
139 #else
140 printf("B not defined\n");
141 #endif
143 #ifdef A
144 printf("A defined\n");
145 #ifdef B
146 printf("B1 defined\n");
147 #else
148 printf("B1 not defined\n");
149 #endif
150 #else
151 printf("A not defined\n");
152 #ifdef B
153 printf("B2 defined\n");
154 #else
155 printf("B2 not defined\n");
156 #endif
157 #endif
159 #if 1+1
160 printf("test true1\n");
161 #endif
162 #if 0
163 printf("test true2\n");
164 #endif
165 #if 1-1
166 printf("test true3\n");
167 #endif
168 #if defined(A)
169 printf("test trueA\n");
170 #endif
171 #if defined(B)
172 printf("test trueB\n");
173 #endif
175 #if 0
176 printf("test 0\n");
177 #elif 0
178 printf("test 1\n");
179 #elif 2
180 printf("test 2\n");
181 #else
182 printf("test 3\n");
183 #endif
185 MACRO_NOARGS();
187 #ifdef __LINE__
188 printf("__LINE__ defined\n");
189 #endif
191 printf("__LINE__=%d __FILE__=%s\n",
192 __LINE__, __FILE__);
193 #line 200
194 printf("__LINE__=%d __FILE__=%s\n",
195 __LINE__, __FILE__);
196 #line 203 "test"
197 printf("__LINE__=%d __FILE__=%s\n",
198 __LINE__, __FILE__);
199 #line 200 "tcctest.c"
201 /* not strictly preprocessor, but we test it there */
202 #ifdef C99_MACROS
203 printf("__func__ = %s\n", __func__);
204 dprintf(1, "vaarg=%d\n", 1);
205 #endif
206 dprintf1(1, "vaarg1\n");
207 dprintf1(1, "vaarg1=%d\n", 2);
208 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
210 /* gcc extension */
211 printf("func='%s'\n", __FUNCTION__);
213 /* complicated macros in glibc */
214 printf("INT64_MIN=%Ld\n", INT64_MIN);
216 int a;
217 a = 1;
218 glue(a+, +);
219 printf("a=%d\n", a);
220 glue(a <, <= 2);
221 printf("a=%d\n", a);
224 /* comment with stray handling *\
228 int op(a,b)
230 return a / b;
233 int ret(a)
235 if (a == 2)
236 return 1;
237 if (a == 3)
238 return 2;
239 return 0;
242 void ps(char *s)
244 int c;
245 while (1) {
246 c = *s;
247 if (c == 0)
248 break;
249 printf("%c", c);
250 s++;
254 void string_test()
256 int b;
257 printf("string:\n");
258 printf("\141\1423\143\n");/* dezdez test */
259 printf("\x41\x42\x43\x3a\n");
260 printf("c=%c\n", 'r');
261 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
262 #if 0
263 printf("wstring=%S\n", L"abc");
264 printf("wstring=%S\n", L"abc" L"def" "ghi");
265 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
266 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
267 #endif
268 ps("test\n");
269 b = 32;
270 while ((b = b + 1) < 96) {
271 printf("%c", b);
273 printf("\n");
274 printf("fib=%d\n", fib(33));
275 b = 262144;
276 while (b != 0x80000000) {
277 num(b);
278 b = b * 2;
282 void loop_test()
284 int i;
285 i = 0;
286 while (i < 10)
287 printf("%d", i++);
288 printf("\n");
289 for(i = 0; i < 10;i++)
290 printf("%d", i);
291 printf("\n");
292 i = 0;
293 do {
294 printf("%d", i++);
295 } while (i < 10);
296 printf("\n");
298 /* break/continue tests */
299 i = 0;
300 while (1) {
301 if (i == 6)
302 break;
303 i++;
304 if (i == 3)
305 continue;
306 printf("%d", i);
308 printf("\n");
310 /* break/continue tests */
311 i = 0;
312 do {
313 if (i == 6)
314 break;
315 i++;
316 if (i == 3)
317 continue;
318 printf("%d", i);
319 } while(1);
320 printf("\n");
322 for(i = 0;i < 10;i++) {
323 if (i == 3)
324 continue;
325 printf("%d", i);
327 printf("\n");
331 void goto_test()
333 int i;
334 static void *label_table[3] = { &&label1, &&label2, &&label3 };
336 printf("goto:\n");
337 i = 0;
338 s_loop:
339 if (i >= 10)
340 goto s_end;
341 printf("%d", i);
342 i++;
343 goto s_loop;
344 s_end:
345 printf("\n");
347 /* we also test computed gotos (GCC extension) */
348 for(i=0;i<3;i++) {
349 goto *label_table[i];
350 label1:
351 printf("label1\n");
352 goto next;
353 label2:
354 printf("label2\n");
355 goto next;
356 label3:
357 printf("label3\n");
358 next: ;
362 enum {
364 E1 = 2,
365 E2 = 4,
370 enum test {
371 E5 = 1000,
374 void enum_test()
376 enum test b1;
377 printf("enum:\n%d %d %d %d %d %d\n",
378 E0, E1, E2, E3, E4, E5);
379 b1 = 1;
380 printf("b1=%d\n", b1);
383 typedef int *my_ptr;
385 void typedef_test()
387 my_ptr a;
388 int b;
389 a = &b;
390 *a = 1234;
391 printf("typedef:\n");
392 printf("a=%d\n", *a);
395 void forward_test()
397 printf("forward:\n");
398 forward_ref();
399 forward_ref();
403 void forward_ref(void)
405 printf("forward ok\n");
408 typedef struct struct1 {
409 int f1;
410 int f2, f3;
411 union union1 {
412 int v1;
413 int v2;
414 } u;
415 char str[3];
416 } struct1;
418 struct struct2 {
419 int a;
420 char b;
423 union union2 {
424 int w1;
425 int w2;
428 struct struct1 st1, st2;
430 int main(int argc, char **argv)
432 string_test();
433 expr_test();
434 macro_test();
435 scope_test();
436 forward_test();
437 funcptr_test();
438 loop_test();
439 switch_test();
440 goto_test();
441 enum_test();
442 typedef_test();
443 struct_test();
444 array_test();
445 expr_ptr_test();
446 bool_test();
447 expr2_test();
448 constant_expr_test();
449 expr_cmp_test();
450 char_short_test();
451 init_test();
452 compound_literal_test();
453 kr_test();
454 struct_assign_test();
455 cast_test();
456 bitfield_test();
457 c99_bool_test();
458 float_test();
459 longlong_test();
460 stdarg_test();
461 whitespace_test();
462 relocation_test();
463 old_style_function();
464 sizeof_test();
465 typeof_test();
466 return 0;
469 int tab[3];
470 int tab2[3][2];
472 int g;
474 void f1(g)
476 printf("g1=%d\n", g);
479 void scope_test()
481 printf("scope:\n");
482 g = 2;
483 f1(1);
484 printf("g2=%d\n", g);
486 int g;
487 g = 3;
488 printf("g3=%d\n", g);
490 int g;
491 g = 4;
492 printf("g4=%d\n", g);
495 printf("g5=%d\n", g);
498 void array_test(int a[4])
500 int i, j;
502 printf("array:\n");
503 printf("sizeof(a) = %d\n", sizeof(a));
504 printf("sizeof(\"a\") = %d\n", sizeof("a"));
505 #ifdef C99_MACROS
506 printf("sizeof(__func__) = %d\n", sizeof(__func__));
507 #endif
508 printf("sizeof tab %d\n", sizeof(tab));
509 printf("sizeof tab2 %d\n", sizeof tab2);
510 tab[0] = 1;
511 tab[1] = 2;
512 tab[2] = 3;
513 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
514 for(i=0;i<3;i++)
515 for(j=0;j<2;j++)
516 tab2[i][j] = 10 * i + j;
517 for(i=0;i<3*2;i++) {
518 printf(" %3d", ((int *)tab2)[i]);
520 printf("\n");
523 void expr_test()
525 int a, b;
526 a = 0;
527 printf("%d\n", a += 1);
528 printf("%d\n", a -= 2);
529 printf("%d\n", a *= 31232132);
530 printf("%d\n", a /= 4);
531 printf("%d\n", a %= 20);
532 printf("%d\n", a &= 6);
533 printf("%d\n", a ^= 7);
534 printf("%d\n", a |= 8);
535 printf("%d\n", a >>= 3);
536 printf("%d\n", a <<= 4);
538 a = 22321;
539 b = -22321;
540 printf("%d\n", a + 1);
541 printf("%d\n", a - 2);
542 printf("%d\n", a * 312);
543 printf("%d\n", a / 4);
544 printf("%d\n", b / 4);
545 printf("%d\n", (unsigned)b / 4);
546 printf("%d\n", a % 20);
547 printf("%d\n", b % 20);
548 printf("%d\n", (unsigned)b % 20);
549 printf("%d\n", a & 6);
550 printf("%d\n", a ^ 7);
551 printf("%d\n", a | 8);
552 printf("%d\n", a >> 3);
553 printf("%d\n", b >> 3);
554 printf("%d\n", (unsigned)b >> 3);
555 printf("%d\n", a << 4);
556 printf("%d\n", ~a);
557 printf("%d\n", -a);
558 printf("%d\n", +a);
560 printf("%d\n", 12 + 1);
561 printf("%d\n", 12 - 2);
562 printf("%d\n", 12 * 312);
563 printf("%d\n", 12 / 4);
564 printf("%d\n", 12 % 20);
565 printf("%d\n", 12 & 6);
566 printf("%d\n", 12 ^ 7);
567 printf("%d\n", 12 | 8);
568 printf("%d\n", 12 >> 2);
569 printf("%d\n", 12 << 4);
570 printf("%d\n", ~12);
571 printf("%d\n", -12);
572 printf("%d\n", +12);
573 printf("%d %d %d %d\n",
574 isid('a'),
575 isid('g'),
576 isid('T'),
577 isid('('));
580 int isid(int c)
582 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
585 /**********************/
587 int vstack[10], *vstack_ptr;
589 void vpush(int vt, int vc)
591 *vstack_ptr++ = vt;
592 *vstack_ptr++ = vc;
595 void vpop(int *ft, int *fc)
597 *fc = *--vstack_ptr;
598 *ft = *--vstack_ptr;
601 void expr2_test()
603 int a, b;
605 printf("expr2:\n");
606 vstack_ptr = vstack;
607 vpush(1432432, 2);
608 vstack_ptr[-2] &= ~0xffffff80;
609 vpop(&a, &b);
610 printf("res= %d %d\n", a, b);
613 void constant_expr_test()
615 int a;
616 printf("constant_expr:\n");
617 a = 3;
618 printf("%d\n", a * 16);
619 printf("%d\n", a * 1);
620 printf("%d\n", a + 0);
623 int tab4[10];
625 void expr_ptr_test()
627 int *p, *q;
629 printf("expr_ptr:\n");
630 p = tab4;
631 q = tab4 + 10;
632 printf("diff=%d\n", q - p);
633 p++;
634 printf("inc=%d\n", p - tab4);
635 p--;
636 printf("dec=%d\n", p - tab4);
637 ++p;
638 printf("inc=%d\n", p - tab4);
639 --p;
640 printf("dec=%d\n", p - tab4);
641 printf("add=%d\n", p + 3 - tab4);
642 printf("add=%d\n", 3 + p - tab4);
645 void expr_cmp_test()
647 int a, b;
648 printf("constant_expr:\n");
649 a = -1;
650 b = 1;
651 printf("%d\n", a == a);
652 printf("%d\n", a != a);
654 printf("%d\n", a < b);
655 printf("%d\n", a <= b);
656 printf("%d\n", a <= a);
657 printf("%d\n", b >= a);
658 printf("%d\n", a >= a);
659 printf("%d\n", b > a);
661 printf("%d\n", (unsigned)a < b);
662 printf("%d\n", (unsigned)a <= b);
663 printf("%d\n", (unsigned)a <= a);
664 printf("%d\n", (unsigned)b >= a);
665 printf("%d\n", (unsigned)a >= a);
666 printf("%d\n", (unsigned)b > a);
669 struct empty {
672 struct aligntest1 {
673 char a[10];
676 struct aligntest2 {
677 int a;
678 char b[10];
681 struct aligntest3 {
682 double a, b;
685 struct aligntest4 {
686 double a[0];
689 void struct_test()
691 struct1 *s;
692 union union2 u;
694 printf("struct:\n");
695 printf("sizes: %d %d %d %d\n",
696 sizeof(struct struct1),
697 sizeof(struct struct2),
698 sizeof(union union1),
699 sizeof(union union2));
700 st1.f1 = 1;
701 st1.f2 = 2;
702 st1.f3 = 3;
703 printf("st1: %d %d %d\n",
704 st1.f1, st1.f2, st1.f3);
705 st1.u.v1 = 1;
706 st1.u.v2 = 2;
707 printf("union1: %d\n", st1.u.v1);
708 u.w1 = 1;
709 u.w2 = 2;
710 printf("union2: %d\n", u.w1);
711 s = &st2;
712 s->f1 = 3;
713 s->f2 = 2;
714 s->f3 = 1;
715 printf("st2: %d %d %d\n",
716 s->f1, s->f2, s->f3);
717 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
719 /* align / size tests */
720 printf("aligntest1 sizeof=%d alignof=%d\n",
721 sizeof(struct aligntest1), __alignof__(struct aligntest1));
722 printf("aligntest2 sizeof=%d alignof=%d\n",
723 sizeof(struct aligntest2), __alignof__(struct aligntest2));
724 printf("aligntest3 sizeof=%d alignof=%d\n",
725 sizeof(struct aligntest3), __alignof__(struct aligntest3));
726 printf("aligntest4 sizeof=%d alignof=%d\n",
727 sizeof(struct aligntest4), __alignof__(struct aligntest4));
729 /* empty structures (GCC extension) */
730 printf("sizeof(struct empty) = %d\n", sizeof(struct empty));
731 printf("alignof(struct empty) = %d\n", __alignof__(struct empty));
734 /* XXX: depend on endianness */
735 void char_short_test()
737 int var1, var2;
739 printf("char_short:\n");
741 var1 = 0x01020304;
742 var2 = 0xfffefdfc;
743 printf("s8=%d %d\n",
744 *(char *)&var1, *(char *)&var2);
745 printf("u8=%d %d\n",
746 *(unsigned char *)&var1, *(unsigned char *)&var2);
747 printf("s16=%d %d\n",
748 *(short *)&var1, *(short *)&var2);
749 printf("u16=%d %d\n",
750 *(unsigned short *)&var1, *(unsigned short *)&var2);
751 printf("s32=%d %d\n",
752 *(int *)&var1, *(int *)&var2);
753 printf("u32=%d %d\n",
754 *(unsigned int *)&var1, *(unsigned int *)&var2);
755 *(char *)&var1 = 0x08;
756 printf("var1=%x\n", var1);
757 *(short *)&var1 = 0x0809;
758 printf("var1=%x\n", var1);
759 *(int *)&var1 = 0x08090a0b;
760 printf("var1=%x\n", var1);
763 /******************/
765 typedef struct Sym {
766 int v;
767 int t;
768 int c;
769 struct Sym *next;
770 struct Sym *prev;
771 } Sym;
773 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
774 #define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
776 static int toupper1(int a)
778 return TOUPPER(a);
781 void bool_test()
783 int *s, a, b, t, f, i;
785 a = 0;
786 s = (void*)0;
787 printf("!s=%d\n", !s);
789 if (!s || !s[0])
790 a = 1;
791 printf("a=%d\n", a);
793 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
794 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
795 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
796 #if 1 && 1
797 printf("a1\n");
798 #endif
799 #if 1 || 0
800 printf("a2\n");
801 #endif
802 #if 1 ? 0 : 1
803 printf("a3\n");
804 #endif
805 #if 0 ? 0 : 1
806 printf("a4\n");
807 #endif
809 a = 4;
810 printf("b=%d\n", a + (0 ? 1 : a / 2));
812 /* test register spilling */
813 a = 10;
814 b = 10;
815 a = (a + b) * ((a < b) ?
816 ((b - a) * (a - b)): a + b);
817 printf("a=%d\n", a);
819 /* test complex || or && expressions */
820 t = 1;
821 f = 0;
822 a = 32;
823 printf("exp=%d\n", f == (32 <= a && a <= 3));
824 printf("r=%d\n", (t || f) + (t && f));
826 /* test ? : cast */
828 int aspect_on;
829 int aspect_native = 65536;
830 double bfu_aspect = 1.0;
831 int aspect;
832 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
833 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
834 printf("aspect=%d\n", aspect);
838 /* again complex expression */
839 for(i=0;i<256;i++) {
840 if (toupper1 (i) != TOUPPER (i))
841 printf("error %d\n", i);
846 int cinit1 = 0;
847 int *cinit2 = (int []){3, 2, 1};
849 void compound_literal_test(void)
851 int *p, i;
852 char *q, *q3;
854 printf("compound_test:\n");
856 p = (int []){1, 2, 3};
857 for(i=0;i<3;i++)
858 printf(" %d", p[i]);
859 printf("\n");
861 for(i=0;i<3;i++)
862 printf("%d", cinit2[i]);
863 printf("\n");
865 q = "tralala1";
866 printf("q1=%s\n", q);
868 q = (char *){ "tralala2" };
869 printf("q2=%s\n", q);
871 q3 = (char *){ q };
872 printf("q3=%s\n", q3);
874 q = (char []){ "tralala3" };
875 printf("q4=%s\n", q);
877 #ifdef ALL_ISOC99
878 p = (int []){1, 2, cinit1 + 3};
879 for(i=0;i<3;i++)
880 printf(" %d", p[i]);
881 printf("\n");
883 for(i=0;i<3;i++) {
884 p = (int []){1, 2, 4 + i};
885 printf("%d %d %d\n",
886 p[0],
887 p[1],
888 p[2]);
890 #endif
893 /* K & R protos */
895 kr_func1(a, b)
897 return a + b;
900 int kr_func2(a, b)
902 return a + b;
905 kr_test()
907 printf("kr_test:\n");
908 printf("func1=%d\n", kr_func1(3, 4));
909 printf("func2=%d\n", kr_func2(3, 4));
910 return 0;
913 void num(int n)
915 char *tab, *p;
916 tab = (char*)malloc(20);
917 p = tab;
918 while (1) {
919 *p = 48 + (n % 10);
920 p++;
921 n = n / 10;
922 if (n == 0)
923 break;
925 while (p != tab) {
926 p--;
927 printf("%c", *p);
929 printf("\n");
932 /* structure assignment tests */
933 struct structa1 {
934 int f1;
935 char f2;
938 struct structa1 ssta1;
940 void struct_assign_test1(struct structa1 s1, int t)
942 printf("%d %d %d\n", s1.f1, s1.f2, t);
945 struct structa1 struct_assign_test2(struct structa1 s1, int t)
947 s1.f1 += t;
948 s1.f2 -= t;
949 return s1;
952 void struct_assign_test(void)
954 struct structa1 lsta1, lsta2;
956 #if 0
957 printf("struct_assign_test:\n");
959 lsta1.f1 = 1;
960 lsta1.f2 = 2;
961 printf("%d %d\n", lsta1.f1, lsta1.f2);
962 lsta2 = lsta1;
963 printf("%d %d\n", lsta2.f1, lsta2.f2);
964 #else
965 lsta2.f1 = 1;
966 lsta2.f2 = 2;
967 #endif
968 struct_assign_test1(lsta2, 3);
970 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
971 lsta2 = struct_assign_test2(lsta2, 4);
972 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
975 /* casts to short/char */
977 void cast1(char a, short b, unsigned char c, unsigned short d)
979 printf("%d %d %d %d\n", a, b, c, d);
982 char bcast;
983 short scast;
985 void cast_test()
987 int a;
988 char c;
989 char tab[10];
991 printf("cast_test:\n");
992 a = 0xfffff;
993 cast1(a, a, a, a);
994 a = 0xffffe;
995 printf("%d %d %d %d\n",
996 (char)(a + 1),
997 (short)(a + 1),
998 (unsigned char)(a + 1),
999 (unsigned short)(a + 1));
1000 printf("%d %d %d %d\n",
1001 (char)0xfffff,
1002 (short)0xfffff,
1003 (unsigned char)0xfffff,
1004 (unsigned short)0xfffff);
1006 a = (bcast = 128) + 1;
1007 printf("%d\n", a);
1008 a = (scast = 65536) + 1;
1009 printf("%d\n", a);
1011 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
1013 /* test implicit int casting for array accesses */
1014 c = 0;
1015 tab[1] = 2;
1016 tab[c] = 1;
1017 printf("%d %d\n", tab[0], tab[1]);
1019 /* test implicit casting on some operators */
1020 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
1021 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
1022 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
1025 /* initializers tests */
1026 struct structinit1 {
1027 int f1;
1028 char f2;
1029 short f3;
1030 int farray[3];
1033 int sinit1 = 2;
1034 int sinit2 = { 3 };
1035 int sinit3[3] = { 1, 2, {{3}}, };
1036 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1037 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
1038 int sinit6[] = { 1, 2, 3 };
1039 int sinit7[] = { [2] = 3, [0] = 1, 2 };
1040 char sinit8[] = "hello" "trala";
1042 struct structinit1 sinit9 = { 1, 2, 3 };
1043 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
1044 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
1045 #ifdef ALL_ISOC99
1046 .farray[0] = 10,
1047 .farray[1] = 11,
1048 .farray[2] = 12,
1049 #endif
1052 char *sinit12 = "hello world";
1053 char *sinit13[] = {
1054 "test1",
1055 "test2",
1056 "test3",
1058 char sinit14[10] = { "abc" };
1059 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1061 struct { int a[3], b; } sinit16[] = { { 1 }, 2 };
1063 struct bar {
1064 char *s;
1065 int len;
1066 } sinit17[] = {
1067 "a1", 4,
1068 "a2", 1
1071 void init_test(void)
1073 int linit1 = 2;
1074 int linit2 = { 3 };
1075 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1076 int linit6[] = { 1, 2, 3 };
1077 int i, j;
1078 char linit8[] = "hello" "trala";
1079 int linit12[10] = { 1, 2 };
1080 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1081 char linit14[10] = "abc";
1082 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1083 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1084 int linit17 = sizeof(linit17);
1086 printf("init_test:\n");
1088 printf("sinit1=%d\n", sinit1);
1089 printf("sinit2=%d\n", sinit2);
1090 printf("sinit3=%d %d %d %d\n",
1091 sizeof(sinit3),
1092 sinit3[0],
1093 sinit3[1],
1094 sinit3[2]
1096 printf("sinit6=%d\n", sizeof(sinit6));
1097 printf("sinit7=%d %d %d %d\n",
1098 sizeof(sinit7),
1099 sinit7[0],
1100 sinit7[1],
1101 sinit7[2]
1103 printf("sinit8=%s\n", sinit8);
1104 printf("sinit9=%d %d %d\n",
1105 sinit9.f1,
1106 sinit9.f2,
1107 sinit9.f3
1109 printf("sinit10=%d %d %d\n",
1110 sinit10.f1,
1111 sinit10.f2,
1112 sinit10.f3
1114 printf("sinit11=%d %d %d %d %d %d\n",
1115 sinit11.f1,
1116 sinit11.f2,
1117 sinit11.f3,
1118 sinit11.farray[0],
1119 sinit11.farray[1],
1120 sinit11.farray[2]
1123 for(i=0;i<3;i++)
1124 for(j=0;j<2;j++)
1125 printf("[%d][%d] = %d %d %d\n",
1126 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1127 printf("linit1=%d\n", linit1);
1128 printf("linit2=%d\n", linit2);
1129 printf("linit6=%d\n", sizeof(linit6));
1130 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1132 printf("sinit12=%s\n", sinit12);
1133 printf("sinit13=%d %s %s %s\n",
1134 sizeof(sinit13),
1135 sinit13[0],
1136 sinit13[1],
1137 sinit13[2]);
1138 printf("sinit14=%s\n", sinit14);
1140 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1141 printf("\n");
1142 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1143 printf("\n");
1144 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1145 printf("\n");
1146 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1147 printf("\n");
1148 printf("%d %d %d %d\n",
1149 linit16.a1,
1150 linit16.a2,
1151 linit16.a3,
1152 linit16.a4);
1153 /* test that initialisation is done after variable declare */
1154 printf("linit17=%d\n", linit17);
1155 printf("sinit15=%d\n", sinit15[0]);
1156 printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]);
1157 printf("sinit17=%s %d %s %d\n",
1158 sinit17[0].s, sinit17[0].len,
1159 sinit17[1].s, sinit17[1].len);
1162 void switch_test()
1164 int i;
1166 for(i=0;i<15;i++) {
1167 switch(i) {
1168 case 0:
1169 case 1:
1170 printf("a");
1171 break;
1172 default:
1173 printf("%d", i);
1174 break;
1175 case 8 ... 12:
1176 printf("c");
1177 break;
1178 case 3:
1179 printf("b");
1180 break;
1183 printf("\n");
1186 /* ISOC99 _Bool type */
1187 void c99_bool_test(void)
1189 #ifdef BOOL_ISOC99
1190 int a;
1191 _Bool b;
1193 printf("bool_test:\n");
1194 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1195 a = 3;
1196 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1197 b = 3;
1198 printf("b = %d\n", b);
1199 b++;
1200 printf("b = %d\n", b);
1201 #endif
1204 void bitfield_test(void)
1206 int a;
1207 struct sbf1 {
1208 int f1 : 3;
1209 int : 2;
1210 int f2 : 1;
1211 int : 0;
1212 int f3 : 5;
1213 int f4 : 7;
1214 unsigned int f5 : 7;
1215 } st1;
1216 printf("bitfield_test:");
1217 printf("sizeof(st1) = %d\n", sizeof(st1));
1219 st1.f1 = 3;
1220 st1.f2 = 1;
1221 st1.f3 = 15;
1222 a = 120;
1223 st1.f4 = a;
1224 st1.f5 = a;
1225 st1.f5++;
1226 printf("%d %d %d %d %d\n",
1227 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1230 #define FTEST(prefix, type, fmt)\
1231 void prefix ## cmp(type a, type b)\
1233 printf("%d %d %d %d %d %d\n",\
1234 a == b,\
1235 a != b,\
1236 a < b,\
1237 a > b,\
1238 a >= b,\
1239 a <= b);\
1240 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1243 a + b,\
1244 a - b,\
1245 a * b,\
1246 a / b,\
1247 -a);\
1248 printf(fmt "\n", ++a);\
1249 printf(fmt "\n", a++);\
1250 printf(fmt "\n", a);\
1252 void prefix ## fcast(type a)\
1254 float fa;\
1255 double da;\
1256 long double la;\
1257 int ia;\
1258 unsigned int ua;\
1259 type b;\
1260 fa = a;\
1261 da = a;\
1262 la = a;\
1263 printf("ftof: %f %f %Lf\n", fa, da, la);\
1264 ia = (int)a;\
1265 ua = (unsigned int)a;\
1266 printf("ftoi: %d %u\n", ia, ua);\
1267 ia = -1234;\
1268 ua = 0x81234500;\
1269 b = ia;\
1270 printf("itof: " fmt "\n", b);\
1271 b = ua;\
1272 printf("utof: " fmt "\n", b);\
1275 void prefix ## test(void)\
1277 printf("testing '%s'\n", #type);\
1278 prefix ## cmp(1, 2.5);\
1279 prefix ## cmp(2, 1.5);\
1280 prefix ## cmp(1, 1);\
1281 prefix ## fcast(234.6);\
1282 prefix ## fcast(-2334.6);\
1285 FTEST(f, float, "%f")
1286 FTEST(d, double, "%f")
1287 FTEST(ld, long double, "%Lf")
1289 double ftab1[3] = { 1.2, 3.4, -5.6 };
1292 void float_test(void)
1294 float fa, fb;
1295 double da, db;
1296 int a;
1297 unsigned int b;
1299 printf("float_test:\n");
1300 printf("sizeof(float) = %d\n", sizeof(float));
1301 printf("sizeof(double) = %d\n", sizeof(double));
1302 printf("sizeof(long double) = %d\n", sizeof(long double));
1303 ftest();
1304 dtest();
1305 ldtest();
1306 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1307 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1308 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1309 da = 123;
1310 printf("da=%f\n", da);
1311 fa = 123;
1312 printf("fa=%f\n", fa);
1313 a = 4000000000;
1314 da = a;
1315 printf("da = %f\n", da);
1316 b = 4000000000;
1317 db = b;
1318 printf("db = %f\n", db);
1321 int fib(int n)
1323 if (n <= 2)
1324 return 1;
1325 else
1326 return fib(n-1) + fib(n-2);
1329 void funcptr_test()
1331 void (*func)(int);
1332 int a;
1333 struct {
1334 int dummy;
1335 void (*func)(int);
1336 } st1;
1338 printf("funcptr:\n");
1339 func = &num;
1340 (*func)(12345);
1341 func = num;
1342 a = 1;
1343 a = 1;
1344 func(12345);
1345 /* more complicated pointer computation */
1346 st1.func = num;
1347 st1.func(12346);
1348 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1349 printf("sizeof2 = %d\n", sizeof funcptr_test);
1350 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1351 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1354 void lloptest(long long a, long long b)
1356 unsigned long long ua, ub;
1358 ua = a;
1359 ub = b;
1360 /* arith */
1361 printf("arith: %Ld %Ld %Ld\n",
1362 a + b,
1363 a - b,
1364 a * b);
1366 if (b != 0) {
1367 printf("arith1: %Ld %Ld\n",
1368 a / b,
1369 a % b);
1372 /* binary */
1373 printf("bin: %Ld %Ld %Ld\n",
1374 a & b,
1375 a | b,
1376 a ^ b);
1378 /* tests */
1379 printf("test: %d %d %d %d %d %d\n",
1380 a == b,
1381 a != b,
1382 a < b,
1383 a > b,
1384 a >= b,
1385 a <= b);
1387 printf("utest: %d %d %d %d %d %d\n",
1388 ua == ub,
1389 ua != ub,
1390 ua < ub,
1391 ua > ub,
1392 ua >= ub,
1393 ua <= ub);
1395 /* arith2 */
1396 a++;
1397 b++;
1398 printf("arith2: %Ld %Ld\n", a, b);
1399 printf("arith2: %Ld %Ld\n", a++, b++);
1400 printf("arith2: %Ld %Ld\n", --a, --b);
1401 printf("arith2: %Ld %Ld\n", a, b);
1404 void llshift(long long a, int b)
1406 printf("shift: %Ld %Ld %Ld\n",
1407 (unsigned long long)a >> b,
1408 a >> b,
1409 a << b);
1410 printf("shiftc: %Ld %Ld %Ld\n",
1411 (unsigned long long)a >> 3,
1412 a >> 3,
1413 a << 3);
1414 printf("shiftc: %Ld %Ld %Ld\n",
1415 (unsigned long long)a >> 35,
1416 a >> 35,
1417 a << 35);
1420 void llfloat(void)
1422 float fa;
1423 double da;
1424 long double lda;
1425 long long la, lb, lc;
1426 unsigned long long ula, ulb, ulc;
1427 la = 0x12345678;
1428 ula = 0x72345678;
1429 la = (la << 20) | 0x12345;
1430 ula = ula << 33;
1431 printf("la=%Ld ula=%Lu\n", la, ula);
1433 fa = la;
1434 da = la;
1435 lda = la;
1436 printf("lltof: %f %f %Lf\n", fa, da, lda);
1438 la = fa;
1439 lb = da;
1440 lc = lda;
1441 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1443 fa = ula;
1444 da = ula;
1445 lda = ula;
1446 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1448 ula = fa;
1449 ulb = da;
1450 ulc = lda;
1451 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1454 long long llfunc1(int a)
1456 return a * 2;
1459 struct S {
1460 int id;
1461 char item;
1464 long long int value(struct S *v)
1466 return ((long long int)v->item);
1469 void longlong_test(void)
1471 long long a, b, c;
1472 int ia;
1473 unsigned int ua;
1474 printf("longlong_test:\n");
1475 printf("sizeof(long long) = %d\n", sizeof(long long));
1476 ia = -1;
1477 ua = -2;
1478 a = ia;
1479 b = ua;
1480 printf("%Ld %Ld\n", a, b);
1481 printf("%Ld %Ld %Ld %Lx\n",
1482 (long long)1,
1483 (long long)-2,
1484 1LL,
1485 0x1234567812345679);
1486 a = llfunc1(-3);
1487 printf("%Ld\n", a);
1489 lloptest(1000, 23);
1490 lloptest(0xff, 0x1234);
1491 b = 0x72345678 << 10;
1492 lloptest(-3, b);
1493 llshift(0x123, 5);
1494 llshift(-23, 5);
1495 b = 0x72345678LL << 10;
1496 llshift(b, 47);
1498 llfloat();
1499 #if 1
1500 b = 0x12345678;
1501 a = -1;
1502 c = a + b;
1503 printf("%Lx\n", c);
1504 #endif
1506 /* long long reg spill test */
1508 struct S a;
1510 a.item = 3;
1511 printf("%lld\n", value(&a));
1513 lloptest(0x80000000, 0);
1516 void vprintf1(const char *fmt, ...)
1518 va_list ap;
1519 const char *p;
1520 int c, i;
1521 double d;
1522 long long ll;
1524 va_start(ap, fmt);
1526 p = fmt;
1527 for(;;) {
1528 c = *p;
1529 if (c == '\0')
1530 break;
1531 p++;
1532 if (c == '%') {
1533 c = *p;
1534 switch(c) {
1535 case '\0':
1536 goto the_end;
1537 case 'd':
1538 i = va_arg(ap, int);
1539 printf("%d", i);
1540 break;
1541 case 'f':
1542 d = va_arg(ap, double);
1543 printf("%f", d);
1544 break;
1545 case 'l':
1546 ll = va_arg(ap, long long);
1547 printf("%Ld", ll);
1548 break;
1550 p++;
1551 } else {
1552 putchar(c);
1555 the_end:
1556 va_end(ap);
1560 void stdarg_test(void)
1562 vprintf1("%d %d %d\n", 1, 2, 3);
1563 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1564 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1567 void whitespace_test(void)
1569 char *str;
1571 \f\v #if 1
1572 pri\
1573 ntf("whitspace:\n");\f\v
1574 #endif
1575 pf("N=%d\n", 2);
1576 pri\
1577 ntf("aaa=%d\n", 3);
1579 pri\
1581 ntf("min=%d\n", 4);
1582 printf("len1=%d\n", strlen("
1583 "));
1584 str = "
1586 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1587 printf("len1=%d\n", strlen(" a
1588 "));
1591 int reltab[3] = { 1, 2, 3 };
1593 int *rel1 = &reltab[1];
1594 int *rel2 = &reltab[2];
1596 void relocation_test(void)
1598 printf("*rel1=%d\n", *rel1);
1599 printf("*rel2=%d\n", *rel2);
1602 void old_style_f(a,b,c)
1603 int a, b;
1604 double c;
1606 printf("a=%d b=%d b=%f\n", a, b, c);
1609 void old_style_function(void)
1611 old_style_f((void *)1, 2, 3.0);
1614 void sizeof_test(void)
1616 int a;
1617 int **ptr;
1619 printf("sizeof(int) = %d\n", sizeof(int));
1620 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1621 printf("sizeof(short) = %d\n", sizeof(short));
1622 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1623 printf("sizeof(char) = %d\n", sizeof(char));
1624 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1625 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1626 a = 1;
1627 printf("sizeof(a++) = %d\n", sizeof a++);
1628 printf("a=%d\n", a);
1629 ptr = NULL;
1630 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1632 /* some alignof tests */
1633 printf("__alignof__(int) = %d\n", __alignof__(int));
1634 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1635 printf("__alignof__(short) = %d\n", __alignof__(short));
1636 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1637 printf("__alignof__(char) = %d\n", __alignof__(char));
1638 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1639 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1642 void typeof_test(void)
1644 double a;
1645 typeof(a) b;
1646 typeof(float) c;
1648 a = 1.5;
1649 b = 2.5;
1650 c = 3.5;
1651 printf("a=%f b=%f c=%f\n", a, b, c);