update options
[tinycc.git] / tcctest.c
blob72d72fb2856b1693262adc6dff2bf469fc500fb6
1 /*
2 * TCC auto test program
3 */
4 #include <tcclib.h>
6 /* Unfortunately, gcc version < 3 does not handle that! */
7 //#define ALL_ISOC99
9 /* only gcc 3 handles _Bool correctly */
10 //#define BOOL_ISOC99
12 #define C99_MACROS
14 void string_test();
15 void expr_test();
16 void macro_test();
17 void scope_test();
18 void forward_test();
19 void funcptr_test();
20 void loop_test();
21 void switch_test();
22 void goto_test();
23 void enum_test();
24 void typedef_test();
25 void struct_test();
26 void array_test();
27 void expr_ptr_test();
28 void bool_test();
29 void expr2_test();
30 void constant_expr_test();
31 void expr_cmp_test();
32 void char_short_test();
33 void init_test(void);
34 void compound_literal_test(void);
35 int kr_test();
36 void struct_assign_test(void);
37 void cast_test(void);
38 void bitfield_test(void);
39 void c99_bool_test(void);
40 void float_test(void);
41 void longlong_test(void);
43 int fib(int n);
44 void num(int n);
45 void forward_ref(void);
46 int isid(int c);
48 #define A 2
49 #define N 1234 + A
50 #define pf printf
51 #define M1(a, b) (a) + (b)
53 #define str(s) # s
54 #define glue(a, b) a ## b
55 #define xglue(a, b) glue(a, b)
56 #define HIGHLOW "hello"
57 #define LOW LOW ", world"
59 #define min(a, b) ((a) < (b) ? (a) : (b))
61 #ifdef C99_MACROS
62 #define dprintf(level,...) printf(__VA_ARGS__)
63 #endif
65 /* gcc vararg macros */
66 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
68 #define MACRO_NOARGS()
70 #define AAA 3
71 #undef AAA
72 #define AAA 4
74 #if 1
75 #define B3 1
76 #elif 1
77 #define B3 2
78 #elif 0
79 #define B3 3
80 #else
81 #define B3 4
82 #endif
84 void macro_test()
86 printf("macro:\n");
87 pf("N=%d\n", N);
88 printf("aaa=%d\n", AAA);
90 printf("min=%d\n", min(1, min(2, -1)));
92 printf("s1=%s\n", glue(HIGH, LOW));
93 printf("s2=%s\n", xglue(HIGH, LOW));
94 printf("s3=%s\n", str("c"));
95 printf("s4=%s\n", str(a1));
96 printf("B3=%d\n", B3);
98 #ifdef A
99 printf("A defined\n");
100 #endif
101 #ifdef B
102 printf("B defined\n");
103 #endif
104 #ifdef A
105 printf("A defined\n");
106 #else
107 printf("A not defined\n");
108 #endif
109 #ifdef B
110 printf("B defined\n");
111 #else
112 printf("B not defined\n");
113 #endif
115 #ifdef A
116 printf("A defined\n");
117 #ifdef B
118 printf("B1 defined\n");
119 #else
120 printf("B1 not defined\n");
121 #endif
122 #else
123 printf("A not defined\n");
124 #ifdef B
125 printf("B2 defined\n");
126 #else
127 printf("B2 not defined\n");
128 #endif
129 #endif
131 #if 1+1
132 printf("test true1\n");
133 #endif
134 #if 0
135 printf("test true2\n");
136 #endif
137 #if 1-1
138 printf("test true3\n");
139 #endif
140 #if defined(A)
141 printf("test trueA\n");
142 #endif
143 #if defined(B)
144 printf("test trueB\n");
145 #endif
147 #if 0
148 printf("test 0\n");
149 #elif 0
150 printf("test 1\n");
151 #elif 2
152 printf("test 2\n");
153 #else
154 printf("test 3\n");
155 #endif
157 MACRO_NOARGS();
159 printf("__LINE__=%d __FILE__=%s\n",
160 __LINE__, __FILE__);
161 /* not strictly preprocessor, but we test it there */
162 #ifdef C99_MACROS
163 printf("__func__ = %s\n", __func__);
164 dprintf(1, "vaarg=%d\n", 1);
165 #endif
166 dprintf1(1, "vaarg1\n");
167 dprintf1(1, "vaarg1=%d\n", 2);
168 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
171 int op(a,b)
173 return a / b;
176 int ret(a)
178 if (a == 2)
179 return 1;
180 if (a == 3)
181 return 2;
182 return 0;
185 void ps(char *s)
187 int c;
188 while (1) {
189 c = *s;
190 if (c == 0)
191 break;
192 printf("%c", c);
193 s++;
197 void string_test()
199 int b;
200 printf("string:\n");
201 printf("\141\1423\143\n");/* dezdez test */
202 printf("\x41\x42\x43\x3a\n");
203 printf("c=%c\n", 'r');
204 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
205 printf("wstring=%S\n", L"abc");
206 printf("wstring=%S\n", L"abc" L"def" "ghi");
207 ps("test\n");
208 b = 32;
209 while ((b = b + 1) < 96) {
210 printf("%c", b);
212 printf("\n");
213 printf("fib=%d\n", fib(33));
214 b = 262144;
215 while (b != 0x80000000) {
216 num(b);
217 b = b * 2;
221 void loop_test()
223 int i;
224 i = 0;
225 while (i < 10)
226 printf("%d", i++);
227 printf("\n");
228 for(i = 0; i < 10;i++)
229 printf("%d", i);
230 printf("\n");
231 i = 0;
232 do {
233 printf("%d", i++);
234 } while (i < 10);
235 printf("\n");
237 /* break/continue tests */
238 i = 0;
239 while (1) {
240 if (i == 6)
241 break;
242 i++;
243 if (i == 3)
244 continue;
245 printf("%d", i);
247 printf("\n");
249 /* break/continue tests */
250 i = 0;
251 do {
252 if (i == 6)
253 break;
254 i++;
255 if (i == 3)
256 continue;
257 printf("%d", i);
258 } while(1);
259 printf("\n");
261 for(i = 0;i < 10;i++) {
262 if (i == 3)
263 continue;
264 printf("%d", i);
266 printf("\n");
270 void goto_test()
272 int i;
274 printf("goto:\n");
275 i = 0;
276 s_loop:
277 if (i >= 10)
278 goto s_end;
279 printf("%d", i);
280 i++;
281 goto s_loop;
282 s_end:
283 printf("\n");
286 enum {
288 E1 = 2,
289 E2 = 4,
294 enum test {
295 E5 = 1000,
298 void enum_test()
300 enum test b1;
301 printf("enum:\n%d %d %d %d %d %d\n",
302 E0, E1, E2, E3, E4, E5);
303 b1 = 1;
304 printf("b1=%d\n", b1);
307 typedef int *my_ptr;
309 void typedef_test()
311 my_ptr a;
312 int b;
313 a = &b;
314 *a = 1234;
315 printf("typedef:\n");
316 printf("a=%d\n", *a);
319 void forward_test()
321 printf("forward:\n");
322 forward_ref();
323 forward_ref();
327 void forward_ref(void)
329 printf("forward ok\n");
332 typedef struct struct1 {
333 int f1;
334 int f2, f3;
335 union union1 {
336 int v1;
337 int v2;
338 } u;
339 char str[3];
340 } struct1;
342 struct struct2 {
343 int a;
344 char b;
347 union union2 {
348 int w1;
349 int w2;
352 struct struct1 st1, st2;
354 int main(int argc, char **argv)
356 string_test();
357 expr_test();
358 macro_test();
359 scope_test();
360 forward_test();
361 funcptr_test();
362 loop_test();
363 switch_test();
364 goto_test();
365 enum_test();
366 typedef_test();
367 struct_test();
368 array_test();
369 expr_ptr_test();
370 bool_test();
371 expr2_test();
372 constant_expr_test();
373 expr_cmp_test();
374 char_short_test();
375 init_test();
376 compound_literal_test();
377 kr_test();
378 struct_assign_test();
379 cast_test();
380 bitfield_test();
381 c99_bool_test();
382 float_test();
383 longlong_test();
384 return 0;
387 int tab[3];
388 int tab2[3][2];
390 int g;
392 void f1(g)
394 printf("g1=%d\n", g);
397 void scope_test()
399 printf("scope:\n");
400 g = 2;
401 f1(1);
402 printf("g2=%d\n", g);
404 int g;
405 g = 3;
406 printf("g3=%d\n", g);
408 int g;
409 g = 4;
410 printf("g4=%d\n", g);
413 printf("g5=%d\n", g);
416 void array_test(int a[4])
418 int i, j;
420 printf("array:\n");
421 printf("sizeof(a) = %d\n", sizeof(a));
422 printf("sizeof(int) = %d\n", sizeof(int));
423 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
424 printf("sizeof(short) = %d\n", sizeof(short));
425 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
426 printf("sizeof(char) = %d\n", sizeof(char));
427 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
428 printf("sizeof tab %d\n", sizeof(tab));
429 printf("sizeof tab2 %d\n", sizeof tab2);
430 tab[0] = 1;
431 tab[1] = 2;
432 tab[2] = 3;
433 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
434 for(i=0;i<3;i++)
435 for(j=0;j<2;j++)
436 tab2[i][j] = 10 * i + j;
437 for(i=0;i<3*2;i++) {
438 printf(" %3d", ((int *)tab2)[i]);
440 printf("\n");
443 void expr_test()
445 int a, b;
446 a = 0;
447 printf("%d\n", a += 1);
448 printf("%d\n", a -= 2);
449 printf("%d\n", a *= 31232132);
450 printf("%d\n", a /= 4);
451 printf("%d\n", a %= 20);
452 printf("%d\n", a &= 6);
453 printf("%d\n", a ^= 7);
454 printf("%d\n", a |= 8);
455 printf("%d\n", a >>= 3);
456 printf("%d\n", a <<= 4);
458 a = 22321;
459 b = -22321;
460 printf("%d\n", a + 1);
461 printf("%d\n", a - 2);
462 printf("%d\n", a * 312);
463 printf("%d\n", a / 4);
464 printf("%d\n", b / 4);
465 printf("%d\n", (unsigned)b / 4);
466 printf("%d\n", a % 20);
467 printf("%d\n", b % 20);
468 printf("%d\n", (unsigned)b % 20);
469 printf("%d\n", a & 6);
470 printf("%d\n", a ^ 7);
471 printf("%d\n", a | 8);
472 printf("%d\n", a >> 3);
473 printf("%d\n", b >> 3);
474 printf("%d\n", (unsigned)b >> 3);
475 printf("%d\n", a << 4);
476 printf("%d\n", ~a);
477 printf("%d\n", -a);
478 printf("%d\n", +a);
480 printf("%d\n", 12 + 1);
481 printf("%d\n", 12 - 2);
482 printf("%d\n", 12 * 312);
483 printf("%d\n", 12 / 4);
484 printf("%d\n", 12 % 20);
485 printf("%d\n", 12 & 6);
486 printf("%d\n", 12 ^ 7);
487 printf("%d\n", 12 | 8);
488 printf("%d\n", 12 >> 2);
489 printf("%d\n", 12 << 4);
490 printf("%d\n", ~12);
491 printf("%d\n", -12);
492 printf("%d\n", +12);
493 printf("%d %d %d %d\n",
494 isid('a'),
495 isid('g'),
496 isid('T'),
497 isid('('));
500 int isid(int c)
502 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
505 /**********************/
507 int vstack[10], *vstack_ptr;
509 void vpush(int vt, int vc)
511 *vstack_ptr++ = vt;
512 *vstack_ptr++ = vc;
515 void vpop(int *ft, int *fc)
517 *fc = *--vstack_ptr;
518 *ft = *--vstack_ptr;
521 void expr2_test()
523 int a, b;
525 printf("expr2:\n");
526 vstack_ptr = vstack;
527 vpush(1432432, 2);
528 vstack_ptr[-2] &= ~0xffffff80;
529 vpop(&a, &b);
530 printf("res= %d %d\n", a, b);
533 void constant_expr_test()
535 int a;
536 printf("constant_expr:\n");
537 a = 3;
538 printf("%d\n", a * 16);
539 printf("%d\n", a * 1);
540 printf("%d\n", a + 0);
543 int tab4[10];
545 void expr_ptr_test()
547 int *p, *q;
549 printf("expr_ptr:\n");
550 p = tab4;
551 q = tab4 + 10;
552 printf("diff=%d\n", q - p);
553 p++;
554 printf("inc=%d\n", p - tab4);
555 p--;
556 printf("dec=%d\n", p - tab4);
557 ++p;
558 printf("inc=%d\n", p - tab4);
559 --p;
560 printf("dec=%d\n", p - tab4);
561 printf("add=%d\n", p + 3 - tab4);
562 printf("add=%d\n", 3 + p - tab4);
565 void expr_cmp_test()
567 int a, b;
568 printf("constant_expr:\n");
569 a = -1;
570 b = 1;
571 printf("%d\n", a == a);
572 printf("%d\n", a != a);
574 printf("%d\n", a < b);
575 printf("%d\n", a <= b);
576 printf("%d\n", a <= a);
577 printf("%d\n", b >= a);
578 printf("%d\n", a >= a);
579 printf("%d\n", b > a);
581 printf("%d\n", (unsigned)a < b);
582 printf("%d\n", (unsigned)a <= b);
583 printf("%d\n", (unsigned)a <= a);
584 printf("%d\n", (unsigned)b >= a);
585 printf("%d\n", (unsigned)a >= a);
586 printf("%d\n", (unsigned)b > a);
589 void struct_test()
591 struct1 *s;
592 union union2 u;
594 printf("struct:\n");
595 printf("sizes: %d %d %d %d\n",
596 sizeof(struct struct1),
597 sizeof(struct struct2),
598 sizeof(union union1),
599 sizeof(union union2));
600 st1.f1 = 1;
601 st1.f2 = 2;
602 st1.f3 = 3;
603 printf("st1: %d %d %d\n",
604 st1.f1, st1.f2, st1.f3);
605 st1.u.v1 = 1;
606 st1.u.v2 = 2;
607 printf("union1: %d\n", st1.u.v1);
608 u.w1 = 1;
609 u.w2 = 2;
610 printf("union2: %d\n", u.w1);
611 s = &st2;
612 s->f1 = 3;
613 s->f2 = 2;
614 s->f3 = 1;
615 printf("st2: %d %d %d\n",
616 s->f1, s->f2, s->f3);
617 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
620 /* XXX: depend on endianness */
621 void char_short_test()
623 int var1, var2;
625 printf("char_short:\n");
627 var1 = 0x01020304;
628 var2 = 0xfffefdfc;
629 printf("s8=%d %d\n",
630 *(char *)&var1, *(char *)&var2);
631 printf("u8=%d %d\n",
632 *(unsigned char *)&var1, *(unsigned char *)&var2);
633 printf("s16=%d %d\n",
634 *(short *)&var1, *(short *)&var2);
635 printf("u16=%d %d\n",
636 *(unsigned short *)&var1, *(unsigned short *)&var2);
637 printf("s32=%d %d\n",
638 *(int *)&var1, *(int *)&var2);
639 printf("u32=%d %d\n",
640 *(unsigned int *)&var1, *(unsigned int *)&var2);
641 *(char *)&var1 = 0x08;
642 printf("var1=%x\n", var1);
643 *(short *)&var1 = 0x0809;
644 printf("var1=%x\n", var1);
645 *(int *)&var1 = 0x08090a0b;
646 printf("var1=%x\n", var1);
649 /******************/
651 typedef struct Sym {
652 int v;
653 int t;
654 int c;
655 struct Sym *next;
656 struct Sym *prev;
657 } Sym;
659 void bool_test()
661 int *s, a, b;
663 a = 0;
664 s = (void*)0;
665 printf("!s=%d\n", !s);
667 if (!s || !s[0])
668 a = 1;
669 printf("a=%d\n", a);
671 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
672 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
673 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
674 #if 1 && 1
675 printf("a1\n");
676 #endif
677 #if 1 || 0
678 printf("a2\n");
679 #endif
680 #if 1 ? 0 : 1
681 printf("a3\n");
682 #endif
683 #if 0 ? 0 : 1
684 printf("a4\n");
685 #endif
687 a = 4;
688 printf("b=%d\n", a + (0 ? 1 : a / 2));
690 /* test register spilling */
691 a = 10;
692 b = 10;
693 a = (a + b) * ((a < b) ?
694 ((b - a) * (a - b)): a + b);
695 printf("a=%d\n", a);
699 int cinit1 = 0;
700 int *cinit2 = (int []){3, 2, 1};
702 void compound_literal_test(void)
704 int *p, i;
705 char *q, *q3;
707 printf("compound_test:\n");
709 p = (int []){1, 2, 3};
710 for(i=0;i<3;i++)
711 printf(" %d", p[i]);
712 printf("\n");
714 for(i=0;i<3;i++)
715 printf("%d", cinit2[i]);
716 printf("\n");
718 q = "tralala1";
719 printf("q1=%s\n", q);
721 q = (char *){ "tralala2" };
722 printf("q2=%s\n", q);
724 q3 = (char *){ q };
725 printf("q3=%s\n", q3);
727 q = (char []){ "tralala3" };
728 printf("q4=%s\n", q);
730 #ifdef ALL_ISOC99
731 p = (int []){1, 2, cinit1 + 3};
732 for(i=0;i<3;i++)
733 printf(" %d", p[i]);
734 printf("\n");
736 for(i=0;i<3;i++) {
737 p = (int []){1, 2, 4 + i};
738 printf("%d %d %d\n",
739 p[0],
740 p[1],
741 p[2]);
743 #endif
746 /* K & R protos */
748 kr_func1(a, b)
750 return a + b;
753 int kr_func2(a, b)
755 return a + b;
758 kr_test()
760 printf("kr_test:\n");
761 printf("func1=%d\n", kr_func1(3, 4));
762 printf("func2=%d\n", kr_func2(3, 4));
763 return 0;
766 void num(int n)
768 char *tab, *p;
769 tab = (char*)malloc(20);
770 p = tab;
771 while (1) {
772 *p = 48 + (n % 10);
773 p++;
774 n = n / 10;
775 if (n == 0)
776 break;
778 while (p != tab) {
779 p--;
780 printf("%c", *p);
782 printf("\n");
785 /* structure assignment tests */
786 struct structa1 {
787 int f1;
788 char f2;
791 struct structa1 ssta1;
793 void struct_assign_test1(struct structa1 s1, int t)
795 printf("%d %d %d\n", s1.f1, s1.f2, t);
798 struct structa1 struct_assign_test2(struct structa1 s1, int t)
800 s1.f1 += t;
801 s1.f2 -= t;
802 return s1;
805 void struct_assign_test(void)
807 struct structa1 lsta1, lsta2;
809 #if 0
810 printf("struct_assign_test:\n");
812 lsta1.f1 = 1;
813 lsta1.f2 = 2;
814 printf("%d %d\n", lsta1.f1, lsta1.f2);
815 lsta2 = lsta1;
816 printf("%d %d\n", lsta2.f1, lsta2.f2);
817 #else
818 lsta2.f1 = 1;
819 lsta2.f2 = 2;
820 #endif
821 struct_assign_test1(lsta2, 3);
823 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
824 lsta2 = struct_assign_test2(lsta2, 4);
825 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
828 /* casts to short/char */
830 void cast1(char a, short b, unsigned char c, unsigned short d)
832 printf("%d %d %d %d\n", a, b, c, d);
835 char bcast;
836 short scast;
838 void cast_test()
840 int a;
841 char c;
842 char tab[10];
844 printf("cast_test:\n");
845 a = 0xfffff;
846 cast1(a, a, a, a);
847 a = 0xffffe;
848 printf("%d %d %d %d\n",
849 (char)(a + 1),
850 (short)(a + 1),
851 (unsigned char)(a + 1),
852 (unsigned short)(a + 1));
853 printf("%d %d %d %d\n",
854 (char)0xfffff,
855 (short)0xfffff,
856 (unsigned char)0xfffff,
857 (unsigned short)0xfffff);
859 a = (bcast = 128) + 1;
860 printf("%d\n", a);
861 a = (scast = 65536) + 1;
862 printf("%d\n", a);
864 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
866 /* test implicit int casting for array accesses */
867 c = 0;
868 tab[1] = 2;
869 tab[c] = 1;
870 printf("%d %d\n", tab[0], tab[1]);
873 /* initializers tests */
874 struct structinit1 {
875 int f1;
876 char f2;
877 short f3;
878 int farray[3];
881 int sinit1 = 2;
882 int sinit2 = { 3 };
883 int sinit3[3] = { 1, 2, {{3}}, };
884 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
885 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
886 int sinit6[] = { 1, 2, 3 };
887 int sinit7[] = { [2] = 3, [0] = 1, 2 };
888 char sinit8[] = "hello" "trala";
890 struct structinit1 sinit9 = { 1, 2, 3 };
891 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
892 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
893 #ifdef ALL_ISOC99
894 .farray[0] = 10,
895 .farray[1] = 11,
896 .farray[2] = 12,
897 #endif
900 char *sinit12 = "hello world";
901 char *sinit13[] = {
902 "test1",
903 "test2",
904 "test3",
906 char sinit14[10] = { "abc" };
907 int sinit15[3] = { sizeof(sinit15), 1, 2 };
909 void init_test(void)
911 int linit1 = 2;
912 int linit2 = { 3 };
913 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
914 int linit6[] = { 1, 2, 3 };
915 int i, j;
916 char linit8[] = "hello" "trala";
917 int linit12[10] = { 1, 2 };
918 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
919 char linit14[10] = "abc";
920 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
921 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
922 int linit17 = sizeof(linit17);
924 printf("init_test:\n");
926 printf("sinit1=%d\n", sinit1);
927 printf("sinit2=%d\n", sinit2);
928 printf("sinit3=%d %d %d %d\n",
929 sizeof(sinit3),
930 sinit3[0],
931 sinit3[1],
932 sinit3[2]
934 printf("sinit6=%d\n", sizeof(sinit6));
935 printf("sinit7=%d %d %d %d\n",
936 sizeof(sinit7),
937 sinit7[0],
938 sinit7[1],
939 sinit7[2]
941 printf("sinit8=%s\n", sinit8);
942 printf("sinit9=%d %d %d\n",
943 sinit9.f1,
944 sinit9.f2,
945 sinit9.f3
947 printf("sinit10=%d %d %d\n",
948 sinit10.f1,
949 sinit10.f2,
950 sinit10.f3
952 printf("sinit11=%d %d %d %d %d %d\n",
953 sinit11.f1,
954 sinit11.f2,
955 sinit11.f3,
956 sinit11.farray[0],
957 sinit11.farray[1],
958 sinit11.farray[2]
961 for(i=0;i<3;i++)
962 for(j=0;j<2;j++)
963 printf("[%d][%d] = %d %d %d\n",
964 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
965 printf("linit1=%d\n", linit1);
966 printf("linit2=%d\n", linit2);
967 printf("linit6=%d\n", sizeof(linit6));
968 printf("linit8=%d %s\n", sizeof(linit8), linit8);
970 printf("sinit12=%s\n", sinit12);
971 printf("sinit13=%d %s %s %s\n",
972 sizeof(sinit13),
973 sinit13[0],
974 sinit13[1],
975 sinit13[2]);
976 printf("sinit14=%s\n", sinit14);
978 for(i=0;i<10;i++) printf(" %d", linit12[i]);
979 printf("\n");
980 for(i=0;i<10;i++) printf(" %d", linit13[i]);
981 printf("\n");
982 for(i=0;i<10;i++) printf(" %d", linit14[i]);
983 printf("\n");
984 for(i=0;i<10;i++) printf(" %d", linit15[i]);
985 printf("\n");
986 printf("%d %d %d %d\n",
987 linit16.a1,
988 linit16.a2,
989 linit16.a3,
990 linit16.a4);
991 /* test that initialisation is done after variable declare */
992 printf("linit17=%d\n", linit17);
993 printf("sinit15=%d\n", sinit15[0]);
996 void switch_test()
998 int i;
1000 for(i=0;i<15;i++) {
1001 switch(i) {
1002 case 0:
1003 case 1:
1004 printf("a");
1005 break;
1006 default:
1007 printf("%d", i);
1008 break;
1009 case 8 ... 12:
1010 printf("c");
1011 break;
1012 case 3:
1013 printf("b");
1014 break;
1017 printf("\n");
1020 /* ISOC99 _Bool type */
1021 void c99_bool_test(void)
1023 #ifdef BOOL_ISOC99
1024 int a;
1025 _Bool b;
1027 printf("bool_test:\n");
1028 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1029 a = 3;
1030 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1031 b = 3;
1032 printf("b = %d\n", b);
1033 b++;
1034 printf("b = %d\n", b);
1035 #endif
1038 void bitfield_test(void)
1040 int a;
1041 struct sbf1 {
1042 int f1 : 3;
1043 int : 2;
1044 int f2 : 1;
1045 int : 0;
1046 int f3 : 5;
1047 int f4 : 7;
1048 unsigned int f5 : 7;
1049 } st1;
1050 printf("bitfield_test:");
1051 printf("sizeof(st1) = %d\n", sizeof(st1));
1053 st1.f1 = 3;
1054 st1.f2 = 1;
1055 st1.f3 = 15;
1056 a = 120;
1057 st1.f4 = a;
1058 st1.f5 = a;
1059 st1.f5++;
1060 printf("%d %d %d %d %d\n",
1061 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1064 #define FTEST(prefix, type, fmt)\
1065 void prefix ## cmp(type a, type b)\
1067 printf("%d %d %d %d %d %d\n",\
1068 a == b,\
1069 a != b,\
1070 a < b,\
1071 a > b,\
1072 a >= b,\
1073 a <= b);\
1074 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1077 a + b,\
1078 a - b,\
1079 a * b,\
1080 a / b,\
1081 -a);\
1082 printf(fmt "\n", ++a);\
1083 printf(fmt "\n", a++);\
1084 printf(fmt "\n", a);\
1086 void prefix ## fcast(type a)\
1088 float fa;\
1089 double da;\
1090 long double la;\
1091 int ia;\
1092 unsigned int ua;\
1093 type b;\
1094 fa = a;\
1095 da = a;\
1096 la = a;\
1097 printf("ftof: %f %f %Lf\n", fa, da, la);\
1098 ia = (int)a;\
1099 ua = (unsigned int)a;\
1100 printf("ftoi: %d %u\n", ia, ua);\
1101 ia = -1234;\
1102 ua = 0x81234500;\
1103 b = ia;\
1104 printf("itof: " fmt "\n", b);\
1105 b = ua;\
1106 printf("utof: " fmt "\n", b);\
1109 void prefix ## test(void)\
1111 printf("testing '%s'\n", #type);\
1112 prefix ## cmp(1, 2.5);\
1113 prefix ## cmp(2, 1.5);\
1114 prefix ## cmp(1, 1);\
1115 prefix ## fcast(234.6);\
1116 prefix ## fcast(-2334.6);\
1119 FTEST(f, float, "%f")
1120 FTEST(d, double, "%f")
1121 FTEST(ld, long double, "%Lf")
1123 double ftab1[3] = { 1.2, 3.4, -5.6 };
1126 void float_test(void)
1128 float fa, fb;
1129 double da, db;
1130 int a;
1131 unsigned int b;
1133 printf("float_test:\n");
1134 printf("sizeof(float) = %d\n", sizeof(float));
1135 printf("sizeof(double) = %d\n", sizeof(double));
1136 printf("sizeof(long double) = %d\n", sizeof(long double));
1137 ftest();
1138 dtest();
1139 ldtest();
1140 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1141 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1142 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1143 da = 123;
1144 printf("da=%f\n", da);
1145 fa = 123;
1146 printf("fa=%f\n", fa);
1147 a = 4000000000;
1148 da = a;
1149 printf("da = %f\n", da);
1150 b = 4000000000;
1151 db = b;
1152 printf("db = %f\n", db);
1155 int fib(int n)
1157 if (n <= 2)
1158 return 1;
1159 else
1160 return fib(n-1) + fib(n-2);
1163 void funcptr_test()
1165 void (*func)(int);
1166 int a;
1167 struct {
1168 int dummy;
1169 void (*func)(int);
1170 } st1;
1172 printf("funcptr:\n");
1173 func = &num;
1174 (*func)(12345);
1175 func = num;
1176 a = 1;
1177 a = 1;
1178 func(12345);
1179 /* more complicated pointer computation */
1180 st1.func = num;
1181 st1.func(12346);
1182 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1183 printf("sizeof2 = %d\n", sizeof funcptr_test);
1184 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1185 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1188 void lloptest(long long a, long long b)
1190 unsigned long long ua, ub;
1192 ua = a;
1193 ub = b;
1194 /* arith */
1195 printf("arith: %Ld %Ld %Ld %Ld %Ld\n",
1196 a + b,
1197 a - b,
1198 a * b,
1199 a / b,
1200 a % b);
1202 /* binary */
1203 printf("bin: %Ld %Ld %Ld\n",
1204 a & b,
1205 a | b,
1206 a ^ b);
1208 /* tests */
1209 printf("test: %d %d %d %d %d %d\n",
1210 a == b,
1211 a != b,
1212 a < b,
1213 a > b,
1214 a >= b,
1215 a <= b);
1217 printf("utest: %d %d %d %d %d %d\n",
1218 ua == ub,
1219 ua != ub,
1220 ua < ub,
1221 ua > ub,
1222 ua >= ub,
1223 ua <= ub);
1225 /* arith2 */
1226 a++;
1227 b++;
1228 printf("arith2: %Ld %Ld\n", a, b);
1229 printf("arith2: %Ld %Ld\n", a++, b++);
1230 printf("arith2: %Ld %Ld\n", --a, --b);
1231 printf("arith2: %Ld %Ld\n", a, b);
1234 void llshift(long long a, int b)
1236 printf("shift: %Ld %Ld %Ld\n",
1237 (unsigned long long)a >> b,
1238 a >> b,
1239 a << b);
1240 printf("shiftc: %Ld %Ld %Ld\n",
1241 (unsigned long long)a >> 3,
1242 a >> 3,
1243 a << 3);
1244 printf("shiftc: %Ld %Ld %Ld\n",
1245 (unsigned long long)a >> 35,
1246 a >> 35,
1247 a << 35);
1250 void llfloat(void)
1252 float fa;
1253 double da;
1254 long double lda;
1255 long long la, lb, lc;
1256 unsigned long long ula, ulb, ulc;
1257 la = 0x12345678;
1258 ula = 0x72345678;
1259 la = (la << 20) | 0x12345;
1260 ula = ula << 33;
1261 printf("la=%Ld ula=%Lu\n", la, ula);
1263 fa = la;
1264 da = la;
1265 lda = la;
1266 printf("lltof: %f %f %Lf\n", fa, da, lda);
1268 la = fa;
1269 lb = da;
1270 lc = lda;
1271 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1273 fa = ula;
1274 da = ula;
1275 lda = ula;
1276 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1278 ula = fa;
1279 ulb = da;
1280 ulc = lda;
1281 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1284 long long llfunc1(int a)
1286 return a;
1289 /* currently not supported, except for typing */
1290 void longlong_test(void)
1292 long long a, b, c;
1293 int ia;
1294 unsigned int ua;
1295 printf("longlong_test:\n");
1296 printf("sizeof(long long) = %d\n", sizeof(long long));
1297 ia = -1;
1298 ua = -2;
1299 a = ia;
1300 b = ua;
1301 printf("%Ld %Ld\n", a, b);
1302 printf("%Ld %Ld %Ld %Lx\n",
1303 (long long)1,
1304 (long long)-2,
1305 1LL,
1306 0x1234567812345679);
1307 a = llfunc1(-3);
1308 printf("%Ld\n", a);
1310 lloptest(1000, 23);
1311 lloptest(0xff, 0x1234);
1312 b = 0x72345678 << 10;
1313 lloptest(-3, b);
1314 llshift(0x123, 5);
1315 llshift(-23, 5);
1316 b = 0x72345678 << 10;
1317 llshift(b, 47);
1319 llfloat();
1320 #if 1
1321 b = 0x12345678;
1322 a = -1;
1323 c = a + b;
1324 printf("%Lx\n", c);
1325 #endif