suppressed ch1 and preparser
[tinycc.git] / tcctest.c
blob64258ad0ab27803afd4b1fe49ea535eff7486cb4
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(s) # s
78 #define glue(a, b) a ## b
79 #define xglue(a, b) glue(a, b)
80 #define HIGHLOW "hello"
81 #define LOW LOW ", world"
83 #define min(a, b) ((a) < (b) ? (a) : (b))
85 #ifdef C99_MACROS
86 #define dprintf(level,...) printf(__VA_ARGS__)
87 #endif
89 /* gcc vararg macros */
90 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
92 #define MACRO_NOARGS()
94 #define AAA 3
95 #undef AAA
96 #define AAA 4
98 #if 1
99 #define B3 1
100 #elif 1
101 #define B3 2
102 #elif 0
103 #define B3 3
104 #else
105 #define B3 4
106 #endif
108 #define __INT64_C(c) c ## LL
109 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
111 void macro_test(void)
113 printf("macro:\n");\f\v
114 pf("N=%d\n", N);
115 printf("aaa=%d\n", AAA);
117 printf("min=%d\n", min(1, min(2, -1)));
119 printf("s1=%s\n", glue(HIGH, LOW));
120 printf("s2=%s\n", xglue(HIGH, LOW));
121 printf("s3=%s\n", str("c"));
122 printf("s4=%s\n", str(a1));
123 printf("B3=%d\n", B3);
125 #ifdef A
126 printf("A defined\n");
127 #endif
128 #ifdef B
129 printf("B defined\n");
130 #endif
131 #ifdef A
132 printf("A defined\n");
133 #else
134 printf("A not defined\n");
135 #endif
136 #ifdef B
137 printf("B defined\n");
138 #else
139 printf("B not defined\n");
140 #endif
142 #ifdef A
143 printf("A defined\n");
144 #ifdef B
145 printf("B1 defined\n");
146 #else
147 printf("B1 not defined\n");
148 #endif
149 #else
150 printf("A not defined\n");
151 #ifdef B
152 printf("B2 defined\n");
153 #else
154 printf("B2 not defined\n");
155 #endif
156 #endif
158 #if 1+1
159 printf("test true1\n");
160 #endif
161 #if 0
162 printf("test true2\n");
163 #endif
164 #if 1-1
165 printf("test true3\n");
166 #endif
167 #if defined(A)
168 printf("test trueA\n");
169 #endif
170 #if defined(B)
171 printf("test trueB\n");
172 #endif
174 #if 0
175 printf("test 0\n");
176 #elif 0
177 printf("test 1\n");
178 #elif 2
179 printf("test 2\n");
180 #else
181 printf("test 3\n");
182 #endif
184 MACRO_NOARGS();
186 #ifdef __LINE__
187 printf("__LINE__ defined\n");
188 #endif
190 printf("__LINE__=%d __FILE__=%s\n",
191 __LINE__, __FILE__);
192 #line 200
193 printf("__LINE__=%d __FILE__=%s\n",
194 __LINE__, __FILE__);
195 #line 203 "test"
196 printf("__LINE__=%d __FILE__=%s\n",
197 __LINE__, __FILE__);
198 #line 198 "tcctest.c"
200 /* not strictly preprocessor, but we test it there */
201 #ifdef C99_MACROS
202 printf("__func__ = %s\n", __func__);
203 dprintf(1, "vaarg=%d\n", 1);
204 #endif
205 dprintf1(1, "vaarg1\n");
206 dprintf1(1, "vaarg1=%d\n", 2);
207 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
209 /* gcc extension */
210 printf("func='%s'\n", __FUNCTION__);
212 /* complicated macros in glibc */
213 printf("INT64_MIN=%Ld\n", INT64_MIN);
215 int a;
216 a = 1;
217 glue(a+, +);
218 printf("a=%d\n", a);
219 glue(a <, <= 2);
220 printf("a=%d\n", a);
224 int op(a,b)
226 return a / b;
229 int ret(a)
231 if (a == 2)
232 return 1;
233 if (a == 3)
234 return 2;
235 return 0;
238 void ps(char *s)
240 int c;
241 while (1) {
242 c = *s;
243 if (c == 0)
244 break;
245 printf("%c", c);
246 s++;
250 void string_test()
252 int b;
253 printf("string:\n");
254 printf("\141\1423\143\n");/* dezdez test */
255 printf("\x41\x42\x43\x3a\n");
256 printf("c=%c\n", 'r');
257 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
258 printf("wstring=%S\n", L"abc");
259 printf("wstring=%S\n", L"abc" L"def" "ghi");
260 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
261 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
262 ps("test\n");
263 b = 32;
264 while ((b = b + 1) < 96) {
265 printf("%c", b);
267 printf("\n");
268 printf("fib=%d\n", fib(33));
269 b = 262144;
270 while (b != 0x80000000) {
271 num(b);
272 b = b * 2;
276 void loop_test()
278 int i;
279 i = 0;
280 while (i < 10)
281 printf("%d", i++);
282 printf("\n");
283 for(i = 0; i < 10;i++)
284 printf("%d", i);
285 printf("\n");
286 i = 0;
287 do {
288 printf("%d", i++);
289 } while (i < 10);
290 printf("\n");
292 /* break/continue tests */
293 i = 0;
294 while (1) {
295 if (i == 6)
296 break;
297 i++;
298 if (i == 3)
299 continue;
300 printf("%d", i);
302 printf("\n");
304 /* break/continue tests */
305 i = 0;
306 do {
307 if (i == 6)
308 break;
309 i++;
310 if (i == 3)
311 continue;
312 printf("%d", i);
313 } while(1);
314 printf("\n");
316 for(i = 0;i < 10;i++) {
317 if (i == 3)
318 continue;
319 printf("%d", i);
321 printf("\n");
325 void goto_test()
327 int i;
328 static void *label_table[3] = { &&label1, &&label2, &&label3 };
330 printf("goto:\n");
331 i = 0;
332 s_loop:
333 if (i >= 10)
334 goto s_end;
335 printf("%d", i);
336 i++;
337 goto s_loop;
338 s_end:
339 printf("\n");
341 /* we also test computed gotos (GCC extension) */
342 for(i=0;i<3;i++) {
343 goto *label_table[i];
344 label1:
345 printf("label1\n");
346 goto next;
347 label2:
348 printf("label2\n");
349 goto next;
350 label3:
351 printf("label3\n");
352 next: ;
356 enum {
358 E1 = 2,
359 E2 = 4,
364 enum test {
365 E5 = 1000,
368 void enum_test()
370 enum test b1;
371 printf("enum:\n%d %d %d %d %d %d\n",
372 E0, E1, E2, E3, E4, E5);
373 b1 = 1;
374 printf("b1=%d\n", b1);
377 typedef int *my_ptr;
379 void typedef_test()
381 my_ptr a;
382 int b;
383 a = &b;
384 *a = 1234;
385 printf("typedef:\n");
386 printf("a=%d\n", *a);
389 void forward_test()
391 printf("forward:\n");
392 forward_ref();
393 forward_ref();
397 void forward_ref(void)
399 printf("forward ok\n");
402 typedef struct struct1 {
403 int f1;
404 int f2, f3;
405 union union1 {
406 int v1;
407 int v2;
408 } u;
409 char str[3];
410 } struct1;
412 struct struct2 {
413 int a;
414 char b;
417 union union2 {
418 int w1;
419 int w2;
422 struct struct1 st1, st2;
424 int main(int argc, char **argv)
426 string_test();
427 expr_test();
428 macro_test();
429 scope_test();
430 forward_test();
431 funcptr_test();
432 loop_test();
433 switch_test();
434 goto_test();
435 enum_test();
436 typedef_test();
437 struct_test();
438 array_test();
439 expr_ptr_test();
440 bool_test();
441 expr2_test();
442 constant_expr_test();
443 expr_cmp_test();
444 char_short_test();
445 init_test();
446 compound_literal_test();
447 kr_test();
448 struct_assign_test();
449 cast_test();
450 bitfield_test();
451 c99_bool_test();
452 float_test();
453 longlong_test();
454 stdarg_test();
455 whitespace_test();
456 relocation_test();
457 old_style_function();
458 sizeof_test();
459 typeof_test();
460 return 0;
463 int tab[3];
464 int tab2[3][2];
466 int g;
468 void f1(g)
470 printf("g1=%d\n", g);
473 void scope_test()
475 printf("scope:\n");
476 g = 2;
477 f1(1);
478 printf("g2=%d\n", g);
480 int g;
481 g = 3;
482 printf("g3=%d\n", g);
484 int g;
485 g = 4;
486 printf("g4=%d\n", g);
489 printf("g5=%d\n", g);
492 void array_test(int a[4])
494 int i, j;
496 printf("array:\n");
497 printf("sizeof(a) = %d\n", sizeof(a));
498 printf("sizeof(\"a\") = %d\n", sizeof("a"));
499 #ifdef C99_MACROS
500 printf("sizeof(__func__) = %d\n", sizeof(__func__));
501 #endif
502 printf("sizeof tab %d\n", sizeof(tab));
503 printf("sizeof tab2 %d\n", sizeof tab2);
504 tab[0] = 1;
505 tab[1] = 2;
506 tab[2] = 3;
507 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
508 for(i=0;i<3;i++)
509 for(j=0;j<2;j++)
510 tab2[i][j] = 10 * i + j;
511 for(i=0;i<3*2;i++) {
512 printf(" %3d", ((int *)tab2)[i]);
514 printf("\n");
517 void expr_test()
519 int a, b;
520 a = 0;
521 printf("%d\n", a += 1);
522 printf("%d\n", a -= 2);
523 printf("%d\n", a *= 31232132);
524 printf("%d\n", a /= 4);
525 printf("%d\n", a %= 20);
526 printf("%d\n", a &= 6);
527 printf("%d\n", a ^= 7);
528 printf("%d\n", a |= 8);
529 printf("%d\n", a >>= 3);
530 printf("%d\n", a <<= 4);
532 a = 22321;
533 b = -22321;
534 printf("%d\n", a + 1);
535 printf("%d\n", a - 2);
536 printf("%d\n", a * 312);
537 printf("%d\n", a / 4);
538 printf("%d\n", b / 4);
539 printf("%d\n", (unsigned)b / 4);
540 printf("%d\n", a % 20);
541 printf("%d\n", b % 20);
542 printf("%d\n", (unsigned)b % 20);
543 printf("%d\n", a & 6);
544 printf("%d\n", a ^ 7);
545 printf("%d\n", a | 8);
546 printf("%d\n", a >> 3);
547 printf("%d\n", b >> 3);
548 printf("%d\n", (unsigned)b >> 3);
549 printf("%d\n", a << 4);
550 printf("%d\n", ~a);
551 printf("%d\n", -a);
552 printf("%d\n", +a);
554 printf("%d\n", 12 + 1);
555 printf("%d\n", 12 - 2);
556 printf("%d\n", 12 * 312);
557 printf("%d\n", 12 / 4);
558 printf("%d\n", 12 % 20);
559 printf("%d\n", 12 & 6);
560 printf("%d\n", 12 ^ 7);
561 printf("%d\n", 12 | 8);
562 printf("%d\n", 12 >> 2);
563 printf("%d\n", 12 << 4);
564 printf("%d\n", ~12);
565 printf("%d\n", -12);
566 printf("%d\n", +12);
567 printf("%d %d %d %d\n",
568 isid('a'),
569 isid('g'),
570 isid('T'),
571 isid('('));
574 int isid(int c)
576 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
579 /**********************/
581 int vstack[10], *vstack_ptr;
583 void vpush(int vt, int vc)
585 *vstack_ptr++ = vt;
586 *vstack_ptr++ = vc;
589 void vpop(int *ft, int *fc)
591 *fc = *--vstack_ptr;
592 *ft = *--vstack_ptr;
595 void expr2_test()
597 int a, b;
599 printf("expr2:\n");
600 vstack_ptr = vstack;
601 vpush(1432432, 2);
602 vstack_ptr[-2] &= ~0xffffff80;
603 vpop(&a, &b);
604 printf("res= %d %d\n", a, b);
607 void constant_expr_test()
609 int a;
610 printf("constant_expr:\n");
611 a = 3;
612 printf("%d\n", a * 16);
613 printf("%d\n", a * 1);
614 printf("%d\n", a + 0);
617 int tab4[10];
619 void expr_ptr_test()
621 int *p, *q;
623 printf("expr_ptr:\n");
624 p = tab4;
625 q = tab4 + 10;
626 printf("diff=%d\n", q - p);
627 p++;
628 printf("inc=%d\n", p - tab4);
629 p--;
630 printf("dec=%d\n", p - tab4);
631 ++p;
632 printf("inc=%d\n", p - tab4);
633 --p;
634 printf("dec=%d\n", p - tab4);
635 printf("add=%d\n", p + 3 - tab4);
636 printf("add=%d\n", 3 + p - tab4);
639 void expr_cmp_test()
641 int a, b;
642 printf("constant_expr:\n");
643 a = -1;
644 b = 1;
645 printf("%d\n", a == a);
646 printf("%d\n", a != a);
648 printf("%d\n", a < b);
649 printf("%d\n", a <= b);
650 printf("%d\n", a <= a);
651 printf("%d\n", b >= a);
652 printf("%d\n", a >= a);
653 printf("%d\n", b > a);
655 printf("%d\n", (unsigned)a < b);
656 printf("%d\n", (unsigned)a <= b);
657 printf("%d\n", (unsigned)a <= a);
658 printf("%d\n", (unsigned)b >= a);
659 printf("%d\n", (unsigned)a >= a);
660 printf("%d\n", (unsigned)b > a);
663 void struct_test()
665 struct1 *s;
666 union union2 u;
668 printf("struct:\n");
669 printf("sizes: %d %d %d %d\n",
670 sizeof(struct struct1),
671 sizeof(struct struct2),
672 sizeof(union union1),
673 sizeof(union union2));
674 st1.f1 = 1;
675 st1.f2 = 2;
676 st1.f3 = 3;
677 printf("st1: %d %d %d\n",
678 st1.f1, st1.f2, st1.f3);
679 st1.u.v1 = 1;
680 st1.u.v2 = 2;
681 printf("union1: %d\n", st1.u.v1);
682 u.w1 = 1;
683 u.w2 = 2;
684 printf("union2: %d\n", u.w1);
685 s = &st2;
686 s->f1 = 3;
687 s->f2 = 2;
688 s->f3 = 1;
689 printf("st2: %d %d %d\n",
690 s->f1, s->f2, s->f3);
691 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
694 /* XXX: depend on endianness */
695 void char_short_test()
697 int var1, var2;
699 printf("char_short:\n");
701 var1 = 0x01020304;
702 var2 = 0xfffefdfc;
703 printf("s8=%d %d\n",
704 *(char *)&var1, *(char *)&var2);
705 printf("u8=%d %d\n",
706 *(unsigned char *)&var1, *(unsigned char *)&var2);
707 printf("s16=%d %d\n",
708 *(short *)&var1, *(short *)&var2);
709 printf("u16=%d %d\n",
710 *(unsigned short *)&var1, *(unsigned short *)&var2);
711 printf("s32=%d %d\n",
712 *(int *)&var1, *(int *)&var2);
713 printf("u32=%d %d\n",
714 *(unsigned int *)&var1, *(unsigned int *)&var2);
715 *(char *)&var1 = 0x08;
716 printf("var1=%x\n", var1);
717 *(short *)&var1 = 0x0809;
718 printf("var1=%x\n", var1);
719 *(int *)&var1 = 0x08090a0b;
720 printf("var1=%x\n", var1);
723 /******************/
725 typedef struct Sym {
726 int v;
727 int t;
728 int c;
729 struct Sym *next;
730 struct Sym *prev;
731 } Sym;
733 void bool_test()
735 int *s, a, b, t, f;
737 a = 0;
738 s = (void*)0;
739 printf("!s=%d\n", !s);
741 if (!s || !s[0])
742 a = 1;
743 printf("a=%d\n", a);
745 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
746 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
747 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
748 #if 1 && 1
749 printf("a1\n");
750 #endif
751 #if 1 || 0
752 printf("a2\n");
753 #endif
754 #if 1 ? 0 : 1
755 printf("a3\n");
756 #endif
757 #if 0 ? 0 : 1
758 printf("a4\n");
759 #endif
761 a = 4;
762 printf("b=%d\n", a + (0 ? 1 : a / 2));
764 /* test register spilling */
765 a = 10;
766 b = 10;
767 a = (a + b) * ((a < b) ?
768 ((b - a) * (a - b)): a + b);
769 printf("a=%d\n", a);
771 /* test complex || or && expressions */
772 t = 1;
773 f = 0;
774 a = 32;
775 printf("exp=%d\n", f == (32 <= a && a <= 3));
776 printf("r=%d\n", (t || f) + (t && f));
778 /* test ? : cast */
780 int aspect_on;
781 int aspect_native = 65536;
782 double bfu_aspect = 1.0;
783 int aspect;
784 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
785 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
786 printf("aspect=%d\n", aspect);
792 int cinit1 = 0;
793 int *cinit2 = (int []){3, 2, 1};
795 void compound_literal_test(void)
797 int *p, i;
798 char *q, *q3;
800 printf("compound_test:\n");
802 p = (int []){1, 2, 3};
803 for(i=0;i<3;i++)
804 printf(" %d", p[i]);
805 printf("\n");
807 for(i=0;i<3;i++)
808 printf("%d", cinit2[i]);
809 printf("\n");
811 q = "tralala1";
812 printf("q1=%s\n", q);
814 q = (char *){ "tralala2" };
815 printf("q2=%s\n", q);
817 q3 = (char *){ q };
818 printf("q3=%s\n", q3);
820 q = (char []){ "tralala3" };
821 printf("q4=%s\n", q);
823 #ifdef ALL_ISOC99
824 p = (int []){1, 2, cinit1 + 3};
825 for(i=0;i<3;i++)
826 printf(" %d", p[i]);
827 printf("\n");
829 for(i=0;i<3;i++) {
830 p = (int []){1, 2, 4 + i};
831 printf("%d %d %d\n",
832 p[0],
833 p[1],
834 p[2]);
836 #endif
839 /* K & R protos */
841 kr_func1(a, b)
843 return a + b;
846 int kr_func2(a, b)
848 return a + b;
851 kr_test()
853 printf("kr_test:\n");
854 printf("func1=%d\n", kr_func1(3, 4));
855 printf("func2=%d\n", kr_func2(3, 4));
856 return 0;
859 void num(int n)
861 char *tab, *p;
862 tab = (char*)malloc(20);
863 p = tab;
864 while (1) {
865 *p = 48 + (n % 10);
866 p++;
867 n = n / 10;
868 if (n == 0)
869 break;
871 while (p != tab) {
872 p--;
873 printf("%c", *p);
875 printf("\n");
878 /* structure assignment tests */
879 struct structa1 {
880 int f1;
881 char f2;
884 struct structa1 ssta1;
886 void struct_assign_test1(struct structa1 s1, int t)
888 printf("%d %d %d\n", s1.f1, s1.f2, t);
891 struct structa1 struct_assign_test2(struct structa1 s1, int t)
893 s1.f1 += t;
894 s1.f2 -= t;
895 return s1;
898 void struct_assign_test(void)
900 struct structa1 lsta1, lsta2;
902 #if 0
903 printf("struct_assign_test:\n");
905 lsta1.f1 = 1;
906 lsta1.f2 = 2;
907 printf("%d %d\n", lsta1.f1, lsta1.f2);
908 lsta2 = lsta1;
909 printf("%d %d\n", lsta2.f1, lsta2.f2);
910 #else
911 lsta2.f1 = 1;
912 lsta2.f2 = 2;
913 #endif
914 struct_assign_test1(lsta2, 3);
916 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
917 lsta2 = struct_assign_test2(lsta2, 4);
918 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
921 /* casts to short/char */
923 void cast1(char a, short b, unsigned char c, unsigned short d)
925 printf("%d %d %d %d\n", a, b, c, d);
928 char bcast;
929 short scast;
931 void cast_test()
933 int a;
934 char c;
935 char tab[10];
937 printf("cast_test:\n");
938 a = 0xfffff;
939 cast1(a, a, a, a);
940 a = 0xffffe;
941 printf("%d %d %d %d\n",
942 (char)(a + 1),
943 (short)(a + 1),
944 (unsigned char)(a + 1),
945 (unsigned short)(a + 1));
946 printf("%d %d %d %d\n",
947 (char)0xfffff,
948 (short)0xfffff,
949 (unsigned char)0xfffff,
950 (unsigned short)0xfffff);
952 a = (bcast = 128) + 1;
953 printf("%d\n", a);
954 a = (scast = 65536) + 1;
955 printf("%d\n", a);
957 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
959 /* test implicit int casting for array accesses */
960 c = 0;
961 tab[1] = 2;
962 tab[c] = 1;
963 printf("%d %d\n", tab[0], tab[1]);
965 /* test implicit casting on some operators */
966 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
967 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
968 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
971 /* initializers tests */
972 struct structinit1 {
973 int f1;
974 char f2;
975 short f3;
976 int farray[3];
979 int sinit1 = 2;
980 int sinit2 = { 3 };
981 int sinit3[3] = { 1, 2, {{3}}, };
982 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
983 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
984 int sinit6[] = { 1, 2, 3 };
985 int sinit7[] = { [2] = 3, [0] = 1, 2 };
986 char sinit8[] = "hello" "trala";
988 struct structinit1 sinit9 = { 1, 2, 3 };
989 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
990 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
991 #ifdef ALL_ISOC99
992 .farray[0] = 10,
993 .farray[1] = 11,
994 .farray[2] = 12,
995 #endif
998 char *sinit12 = "hello world";
999 char *sinit13[] = {
1000 "test1",
1001 "test2",
1002 "test3",
1004 char sinit14[10] = { "abc" };
1005 int sinit15[3] = { sizeof(sinit15), 1, 2 };
1007 void init_test(void)
1009 int linit1 = 2;
1010 int linit2 = { 3 };
1011 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
1012 int linit6[] = { 1, 2, 3 };
1013 int i, j;
1014 char linit8[] = "hello" "trala";
1015 int linit12[10] = { 1, 2 };
1016 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
1017 char linit14[10] = "abc";
1018 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1019 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1020 int linit17 = sizeof(linit17);
1022 printf("init_test:\n");
1024 printf("sinit1=%d\n", sinit1);
1025 printf("sinit2=%d\n", sinit2);
1026 printf("sinit3=%d %d %d %d\n",
1027 sizeof(sinit3),
1028 sinit3[0],
1029 sinit3[1],
1030 sinit3[2]
1032 printf("sinit6=%d\n", sizeof(sinit6));
1033 printf("sinit7=%d %d %d %d\n",
1034 sizeof(sinit7),
1035 sinit7[0],
1036 sinit7[1],
1037 sinit7[2]
1039 printf("sinit8=%s\n", sinit8);
1040 printf("sinit9=%d %d %d\n",
1041 sinit9.f1,
1042 sinit9.f2,
1043 sinit9.f3
1045 printf("sinit10=%d %d %d\n",
1046 sinit10.f1,
1047 sinit10.f2,
1048 sinit10.f3
1050 printf("sinit11=%d %d %d %d %d %d\n",
1051 sinit11.f1,
1052 sinit11.f2,
1053 sinit11.f3,
1054 sinit11.farray[0],
1055 sinit11.farray[1],
1056 sinit11.farray[2]
1059 for(i=0;i<3;i++)
1060 for(j=0;j<2;j++)
1061 printf("[%d][%d] = %d %d %d\n",
1062 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1063 printf("linit1=%d\n", linit1);
1064 printf("linit2=%d\n", linit2);
1065 printf("linit6=%d\n", sizeof(linit6));
1066 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1068 printf("sinit12=%s\n", sinit12);
1069 printf("sinit13=%d %s %s %s\n",
1070 sizeof(sinit13),
1071 sinit13[0],
1072 sinit13[1],
1073 sinit13[2]);
1074 printf("sinit14=%s\n", sinit14);
1076 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1077 printf("\n");
1078 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1079 printf("\n");
1080 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1081 printf("\n");
1082 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1083 printf("\n");
1084 printf("%d %d %d %d\n",
1085 linit16.a1,
1086 linit16.a2,
1087 linit16.a3,
1088 linit16.a4);
1089 /* test that initialisation is done after variable declare */
1090 printf("linit17=%d\n", linit17);
1091 printf("sinit15=%d\n", sinit15[0]);
1094 void switch_test()
1096 int i;
1098 for(i=0;i<15;i++) {
1099 switch(i) {
1100 case 0:
1101 case 1:
1102 printf("a");
1103 break;
1104 default:
1105 printf("%d", i);
1106 break;
1107 case 8 ... 12:
1108 printf("c");
1109 break;
1110 case 3:
1111 printf("b");
1112 break;
1115 printf("\n");
1118 /* ISOC99 _Bool type */
1119 void c99_bool_test(void)
1121 #ifdef BOOL_ISOC99
1122 int a;
1123 _Bool b;
1125 printf("bool_test:\n");
1126 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1127 a = 3;
1128 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1129 b = 3;
1130 printf("b = %d\n", b);
1131 b++;
1132 printf("b = %d\n", b);
1133 #endif
1136 void bitfield_test(void)
1138 int a;
1139 struct sbf1 {
1140 int f1 : 3;
1141 int : 2;
1142 int f2 : 1;
1143 int : 0;
1144 int f3 : 5;
1145 int f4 : 7;
1146 unsigned int f5 : 7;
1147 } st1;
1148 printf("bitfield_test:");
1149 printf("sizeof(st1) = %d\n", sizeof(st1));
1151 st1.f1 = 3;
1152 st1.f2 = 1;
1153 st1.f3 = 15;
1154 a = 120;
1155 st1.f4 = a;
1156 st1.f5 = a;
1157 st1.f5++;
1158 printf("%d %d %d %d %d\n",
1159 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1162 #define FTEST(prefix, type, fmt)\
1163 void prefix ## cmp(type a, type b)\
1165 printf("%d %d %d %d %d %d\n",\
1166 a == b,\
1167 a != b,\
1168 a < b,\
1169 a > b,\
1170 a >= b,\
1171 a <= b);\
1172 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1175 a + b,\
1176 a - b,\
1177 a * b,\
1178 a / b,\
1179 -a);\
1180 printf(fmt "\n", ++a);\
1181 printf(fmt "\n", a++);\
1182 printf(fmt "\n", a);\
1184 void prefix ## fcast(type a)\
1186 float fa;\
1187 double da;\
1188 long double la;\
1189 int ia;\
1190 unsigned int ua;\
1191 type b;\
1192 fa = a;\
1193 da = a;\
1194 la = a;\
1195 printf("ftof: %f %f %Lf\n", fa, da, la);\
1196 ia = (int)a;\
1197 ua = (unsigned int)a;\
1198 printf("ftoi: %d %u\n", ia, ua);\
1199 ia = -1234;\
1200 ua = 0x81234500;\
1201 b = ia;\
1202 printf("itof: " fmt "\n", b);\
1203 b = ua;\
1204 printf("utof: " fmt "\n", b);\
1207 void prefix ## test(void)\
1209 printf("testing '%s'\n", #type);\
1210 prefix ## cmp(1, 2.5);\
1211 prefix ## cmp(2, 1.5);\
1212 prefix ## cmp(1, 1);\
1213 prefix ## fcast(234.6);\
1214 prefix ## fcast(-2334.6);\
1217 FTEST(f, float, "%f")
1218 FTEST(d, double, "%f")
1219 FTEST(ld, long double, "%Lf")
1221 double ftab1[3] = { 1.2, 3.4, -5.6 };
1224 void float_test(void)
1226 float fa, fb;
1227 double da, db;
1228 int a;
1229 unsigned int b;
1231 printf("float_test:\n");
1232 printf("sizeof(float) = %d\n", sizeof(float));
1233 printf("sizeof(double) = %d\n", sizeof(double));
1234 printf("sizeof(long double) = %d\n", sizeof(long double));
1235 ftest();
1236 dtest();
1237 ldtest();
1238 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1239 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1240 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1241 da = 123;
1242 printf("da=%f\n", da);
1243 fa = 123;
1244 printf("fa=%f\n", fa);
1245 a = 4000000000;
1246 da = a;
1247 printf("da = %f\n", da);
1248 b = 4000000000;
1249 db = b;
1250 printf("db = %f\n", db);
1253 int fib(int n)
1255 if (n <= 2)
1256 return 1;
1257 else
1258 return fib(n-1) + fib(n-2);
1261 void funcptr_test()
1263 void (*func)(int);
1264 int a;
1265 struct {
1266 int dummy;
1267 void (*func)(int);
1268 } st1;
1270 printf("funcptr:\n");
1271 func = &num;
1272 (*func)(12345);
1273 func = num;
1274 a = 1;
1275 a = 1;
1276 func(12345);
1277 /* more complicated pointer computation */
1278 st1.func = num;
1279 st1.func(12346);
1280 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1281 printf("sizeof2 = %d\n", sizeof funcptr_test);
1282 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1283 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1286 void lloptest(long long a, long long b)
1288 unsigned long long ua, ub;
1290 ua = a;
1291 ub = b;
1292 /* arith */
1293 printf("arith: %Ld %Ld %Ld %Ld %Ld\n",
1294 a + b,
1295 a - b,
1296 a * b,
1297 a / b,
1298 a % b);
1300 /* binary */
1301 printf("bin: %Ld %Ld %Ld\n",
1302 a & b,
1303 a | b,
1304 a ^ b);
1306 /* tests */
1307 printf("test: %d %d %d %d %d %d\n",
1308 a == b,
1309 a != b,
1310 a < b,
1311 a > b,
1312 a >= b,
1313 a <= b);
1315 printf("utest: %d %d %d %d %d %d\n",
1316 ua == ub,
1317 ua != ub,
1318 ua < ub,
1319 ua > ub,
1320 ua >= ub,
1321 ua <= ub);
1323 /* arith2 */
1324 a++;
1325 b++;
1326 printf("arith2: %Ld %Ld\n", a, b);
1327 printf("arith2: %Ld %Ld\n", a++, b++);
1328 printf("arith2: %Ld %Ld\n", --a, --b);
1329 printf("arith2: %Ld %Ld\n", a, b);
1332 void llshift(long long a, int b)
1334 printf("shift: %Ld %Ld %Ld\n",
1335 (unsigned long long)a >> b,
1336 a >> b,
1337 a << b);
1338 printf("shiftc: %Ld %Ld %Ld\n",
1339 (unsigned long long)a >> 3,
1340 a >> 3,
1341 a << 3);
1342 printf("shiftc: %Ld %Ld %Ld\n",
1343 (unsigned long long)a >> 35,
1344 a >> 35,
1345 a << 35);
1348 void llfloat(void)
1350 float fa;
1351 double da;
1352 long double lda;
1353 long long la, lb, lc;
1354 unsigned long long ula, ulb, ulc;
1355 la = 0x12345678;
1356 ula = 0x72345678;
1357 la = (la << 20) | 0x12345;
1358 ula = ula << 33;
1359 printf("la=%Ld ula=%Lu\n", la, ula);
1361 fa = la;
1362 da = la;
1363 lda = la;
1364 printf("lltof: %f %f %Lf\n", fa, da, lda);
1366 la = fa;
1367 lb = da;
1368 lc = lda;
1369 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1371 fa = ula;
1372 da = ula;
1373 lda = ula;
1374 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1376 ula = fa;
1377 ulb = da;
1378 ulc = lda;
1379 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1382 long long llfunc1(int a)
1384 return a * 2;
1387 struct S {
1388 int id;
1389 char item;
1392 long long int value(struct S *v)
1394 return ((long long int)v->item);
1397 void longlong_test(void)
1399 long long a, b, c;
1400 int ia;
1401 unsigned int ua;
1402 printf("longlong_test:\n");
1403 printf("sizeof(long long) = %d\n", sizeof(long long));
1404 ia = -1;
1405 ua = -2;
1406 a = ia;
1407 b = ua;
1408 printf("%Ld %Ld\n", a, b);
1409 printf("%Ld %Ld %Ld %Lx\n",
1410 (long long)1,
1411 (long long)-2,
1412 1LL,
1413 0x1234567812345679);
1414 a = llfunc1(-3);
1415 printf("%Ld\n", a);
1417 lloptest(1000, 23);
1418 lloptest(0xff, 0x1234);
1419 b = 0x72345678 << 10;
1420 lloptest(-3, b);
1421 llshift(0x123, 5);
1422 llshift(-23, 5);
1423 b = 0x72345678LL << 10;
1424 llshift(b, 47);
1426 llfloat();
1427 #if 1
1428 b = 0x12345678;
1429 a = -1;
1430 c = a + b;
1431 printf("%Lx\n", c);
1432 #endif
1434 /* long long reg spill test */
1436 struct S a;
1438 a.item = 3;
1439 printf("%lld\n", value(&a));
1443 void vprintf1(const char *fmt, ...)
1445 va_list ap;
1446 const char *p;
1447 int c, i;
1448 double d;
1449 long long ll;
1451 va_start(ap, fmt);
1453 p = fmt;
1454 for(;;) {
1455 c = *p;
1456 if (c == '\0')
1457 break;
1458 p++;
1459 if (c == '%') {
1460 c = *p;
1461 switch(c) {
1462 case '\0':
1463 goto the_end;
1464 case 'd':
1465 i = va_arg(ap, int);
1466 printf("%d", i);
1467 break;
1468 case 'f':
1469 d = va_arg(ap, double);
1470 printf("%f", d);
1471 break;
1472 case 'l':
1473 ll = va_arg(ap, long long);
1474 printf("%Ld", ll);
1475 break;
1477 p++;
1478 } else {
1479 putchar(c);
1482 the_end:
1483 va_end(ap);
1487 void stdarg_test(void)
1489 vprintf1("%d %d %d\n", 1, 2, 3);
1490 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1491 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1494 void whitespace_test(void)
1496 char *str;
1498 \f\v #if 1
1499 pri\
1500 ntf("whitspace:\n");\f\v
1501 #endif
1502 pf("N=%d\n", 2);
1503 pri\
1504 ntf("aaa=%d\n", 3);
1506 pri\
1508 ntf("min=%d\n", 4);
1509 printf("len1=%d\n", strlen("
1510 "));
1511 str = "
1513 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1514 printf("len1=%d\n", strlen(" a
1515 "));
1518 int reltab[3] = { 1, 2, 3 };
1520 int *rel1 = &reltab[1];
1521 int *rel2 = &reltab[2];
1523 void relocation_test(void)
1525 printf("*rel1=%d\n", *rel1);
1526 printf("*rel2=%d\n", *rel2);
1529 void old_style_f(a,b,c)
1530 int a, b;
1531 double c;
1533 printf("a=%d b=%d b=%f\n", a, b, c);
1536 void old_style_function(void)
1538 old_style_f((void *)1, 2, 3.0);
1541 void sizeof_test(void)
1543 int a;
1544 int **ptr;
1546 printf("sizeof(int) = %d\n", sizeof(int));
1547 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
1548 printf("sizeof(short) = %d\n", sizeof(short));
1549 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
1550 printf("sizeof(char) = %d\n", sizeof(char));
1551 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
1552 printf("sizeof(func) = %d\n", sizeof sizeof_test());
1553 a = 1;
1554 printf("sizeof(a++) = %d\n", sizeof a++);
1555 printf("a=%d\n", a);
1556 ptr = NULL;
1557 printf("sizeof(**ptr) = %d\n", sizeof (**ptr));
1559 /* some alignof tests */
1560 printf("__alignof__(int) = %d\n", __alignof__(int));
1561 printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int));
1562 printf("__alignof__(short) = %d\n", __alignof__(short));
1563 printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short));
1564 printf("__alignof__(char) = %d\n", __alignof__(char));
1565 printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char));
1566 printf("__alignof__(func) = %d\n", __alignof__ sizeof_test());
1569 void typeof_test(void)
1571 double a;
1572 typeof(a) b;
1573 typeof(float) c;
1575 a = 1.5;
1576 b = 2.5;
1577 c = 3.5;
1578 printf("a=%f b=%f c=%f\n", a, b, c);