update
[tinycc.git] / tcctest.c
blob414df556567e249c483d468450f9ed70329dcce0
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);
64 int fib(int n);
65 void num(int n);
66 void forward_ref(void);
67 int isid(int c);
69 #define A 2
70 #define N 1234 + A
71 #define pf printf
72 #define M1(a, b) (a) + (b)
74 #define str(s) # s
75 #define glue(a, b) a ## b
76 #define xglue(a, b) glue(a, b)
77 #define HIGHLOW "hello"
78 #define LOW LOW ", world"
80 #define min(a, b) ((a) < (b) ? (a) : (b))
82 #ifdef C99_MACROS
83 #define dprintf(level,...) printf(__VA_ARGS__)
84 #endif
86 /* gcc vararg macros */
87 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
89 #define MACRO_NOARGS()
91 #define AAA 3
92 #undef AAA
93 #define AAA 4
95 #if 1
96 #define B3 1
97 #elif 1
98 #define B3 2
99 #elif 0
100 #define B3 3
101 #else
102 #define B3 4
103 #endif
105 void macro_test()
107 printf("macro:\n");\f\v
108 pf("N=%d\n", N);
109 printf("aaa=%d\n", AAA);
111 printf("min=%d\n", min(1, min(2, -1)));
113 printf("s1=%s\n", glue(HIGH, LOW));
114 printf("s2=%s\n", xglue(HIGH, LOW));
115 printf("s3=%s\n", str("c"));
116 printf("s4=%s\n", str(a1));
117 printf("B3=%d\n", B3);
119 #ifdef A
120 printf("A defined\n");
121 #endif
122 #ifdef B
123 printf("B defined\n");
124 #endif
125 #ifdef A
126 printf("A defined\n");
127 #else
128 printf("A not defined\n");
129 #endif
130 #ifdef B
131 printf("B defined\n");
132 #else
133 printf("B not defined\n");
134 #endif
136 #ifdef A
137 printf("A defined\n");
138 #ifdef B
139 printf("B1 defined\n");
140 #else
141 printf("B1 not defined\n");
142 #endif
143 #else
144 printf("A not defined\n");
145 #ifdef B
146 printf("B2 defined\n");
147 #else
148 printf("B2 not defined\n");
149 #endif
150 #endif
152 #if 1+1
153 printf("test true1\n");
154 #endif
155 #if 0
156 printf("test true2\n");
157 #endif
158 #if 1-1
159 printf("test true3\n");
160 #endif
161 #if defined(A)
162 printf("test trueA\n");
163 #endif
164 #if defined(B)
165 printf("test trueB\n");
166 #endif
168 #if 0
169 printf("test 0\n");
170 #elif 0
171 printf("test 1\n");
172 #elif 2
173 printf("test 2\n");
174 #else
175 printf("test 3\n");
176 #endif
178 MACRO_NOARGS();
180 #ifdef __LINE__
181 printf("__LINE__ defined\n");
182 #endif
184 printf("__LINE__=%d __FILE__=%s\n",
185 __LINE__, __FILE__);
186 #line 200
187 printf("__LINE__=%d __FILE__=%s\n",
188 __LINE__, __FILE__);
189 #line 203 "test"
190 printf("__LINE__=%d __FILE__=%s\n",
191 __LINE__, __FILE__);
192 #line 185 "tcctest.c"
194 /* not strictly preprocessor, but we test it there */
195 #ifdef C99_MACROS
196 printf("__func__ = %s\n", __func__);
197 dprintf(1, "vaarg=%d\n", 1);
198 #endif
199 dprintf1(1, "vaarg1\n");
200 dprintf1(1, "vaarg1=%d\n", 2);
201 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
203 /* gcc extension */
204 printf("func='%s'\n", __FUNCTION__);
207 int op(a,b)
209 return a / b;
212 int ret(a)
214 if (a == 2)
215 return 1;
216 if (a == 3)
217 return 2;
218 return 0;
221 void ps(char *s)
223 int c;
224 while (1) {
225 c = *s;
226 if (c == 0)
227 break;
228 printf("%c", c);
229 s++;
233 void string_test()
235 int b;
236 printf("string:\n");
237 printf("\141\1423\143\n");/* dezdez test */
238 printf("\x41\x42\x43\x3a\n");
239 printf("c=%c\n", 'r');
240 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
241 printf("wstring=%S\n", L"abc");
242 printf("wstring=%S\n", L"abc" L"def" "ghi");
243 printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff');
244 printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff');
245 ps("test\n");
246 b = 32;
247 while ((b = b + 1) < 96) {
248 printf("%c", b);
250 printf("\n");
251 printf("fib=%d\n", fib(33));
252 b = 262144;
253 while (b != 0x80000000) {
254 num(b);
255 b = b * 2;
259 void loop_test()
261 int i;
262 i = 0;
263 while (i < 10)
264 printf("%d", i++);
265 printf("\n");
266 for(i = 0; i < 10;i++)
267 printf("%d", i);
268 printf("\n");
269 i = 0;
270 do {
271 printf("%d", i++);
272 } while (i < 10);
273 printf("\n");
275 /* break/continue tests */
276 i = 0;
277 while (1) {
278 if (i == 6)
279 break;
280 i++;
281 if (i == 3)
282 continue;
283 printf("%d", i);
285 printf("\n");
287 /* break/continue tests */
288 i = 0;
289 do {
290 if (i == 6)
291 break;
292 i++;
293 if (i == 3)
294 continue;
295 printf("%d", i);
296 } while(1);
297 printf("\n");
299 for(i = 0;i < 10;i++) {
300 if (i == 3)
301 continue;
302 printf("%d", i);
304 printf("\n");
308 void goto_test()
310 int i;
312 printf("goto:\n");
313 i = 0;
314 s_loop:
315 if (i >= 10)
316 goto s_end;
317 printf("%d", i);
318 i++;
319 goto s_loop;
320 s_end:
321 printf("\n");
324 enum {
326 E1 = 2,
327 E2 = 4,
332 enum test {
333 E5 = 1000,
336 void enum_test()
338 enum test b1;
339 printf("enum:\n%d %d %d %d %d %d\n",
340 E0, E1, E2, E3, E4, E5);
341 b1 = 1;
342 printf("b1=%d\n", b1);
345 typedef int *my_ptr;
347 void typedef_test()
349 my_ptr a;
350 int b;
351 a = &b;
352 *a = 1234;
353 printf("typedef:\n");
354 printf("a=%d\n", *a);
357 void forward_test()
359 printf("forward:\n");
360 forward_ref();
361 forward_ref();
365 void forward_ref(void)
367 printf("forward ok\n");
370 typedef struct struct1 {
371 int f1;
372 int f2, f3;
373 union union1 {
374 int v1;
375 int v2;
376 } u;
377 char str[3];
378 } struct1;
380 struct struct2 {
381 int a;
382 char b;
385 union union2 {
386 int w1;
387 int w2;
390 struct struct1 st1, st2;
392 int main(int argc, char **argv)
394 string_test();
395 expr_test();
396 macro_test();
397 scope_test();
398 forward_test();
399 funcptr_test();
400 loop_test();
401 switch_test();
402 goto_test();
403 enum_test();
404 typedef_test();
405 struct_test();
406 array_test();
407 expr_ptr_test();
408 bool_test();
409 expr2_test();
410 constant_expr_test();
411 expr_cmp_test();
412 char_short_test();
413 init_test();
414 compound_literal_test();
415 kr_test();
416 struct_assign_test();
417 cast_test();
418 bitfield_test();
419 c99_bool_test();
420 float_test();
421 longlong_test();
422 stdarg_test();
423 whitespace_test();
424 relocation_test();
425 return 0;
428 int tab[3];
429 int tab2[3][2];
431 int g;
433 void f1(g)
435 printf("g1=%d\n", g);
438 void scope_test()
440 printf("scope:\n");
441 g = 2;
442 f1(1);
443 printf("g2=%d\n", g);
445 int g;
446 g = 3;
447 printf("g3=%d\n", g);
449 int g;
450 g = 4;
451 printf("g4=%d\n", g);
454 printf("g5=%d\n", g);
457 void array_test(int a[4])
459 int i, j;
461 printf("array:\n");
462 printf("sizeof(a) = %d\n", sizeof(a));
463 printf("sizeof(int) = %d\n", sizeof(int));
464 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
465 printf("sizeof(short) = %d\n", sizeof(short));
466 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
467 printf("sizeof(char) = %d\n", sizeof(char));
468 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
469 printf("sizeof(\"a\") = %d\n", sizeof("a"));
470 #ifdef C99_MACROS
471 printf("sizeof(__func__) = %d\n", sizeof(__func__));
472 #endif
473 printf("sizeof tab %d\n", sizeof(tab));
474 printf("sizeof tab2 %d\n", sizeof tab2);
475 tab[0] = 1;
476 tab[1] = 2;
477 tab[2] = 3;
478 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
479 for(i=0;i<3;i++)
480 for(j=0;j<2;j++)
481 tab2[i][j] = 10 * i + j;
482 for(i=0;i<3*2;i++) {
483 printf(" %3d", ((int *)tab2)[i]);
485 printf("\n");
488 void expr_test()
490 int a, b;
491 a = 0;
492 printf("%d\n", a += 1);
493 printf("%d\n", a -= 2);
494 printf("%d\n", a *= 31232132);
495 printf("%d\n", a /= 4);
496 printf("%d\n", a %= 20);
497 printf("%d\n", a &= 6);
498 printf("%d\n", a ^= 7);
499 printf("%d\n", a |= 8);
500 printf("%d\n", a >>= 3);
501 printf("%d\n", a <<= 4);
503 a = 22321;
504 b = -22321;
505 printf("%d\n", a + 1);
506 printf("%d\n", a - 2);
507 printf("%d\n", a * 312);
508 printf("%d\n", a / 4);
509 printf("%d\n", b / 4);
510 printf("%d\n", (unsigned)b / 4);
511 printf("%d\n", a % 20);
512 printf("%d\n", b % 20);
513 printf("%d\n", (unsigned)b % 20);
514 printf("%d\n", a & 6);
515 printf("%d\n", a ^ 7);
516 printf("%d\n", a | 8);
517 printf("%d\n", a >> 3);
518 printf("%d\n", b >> 3);
519 printf("%d\n", (unsigned)b >> 3);
520 printf("%d\n", a << 4);
521 printf("%d\n", ~a);
522 printf("%d\n", -a);
523 printf("%d\n", +a);
525 printf("%d\n", 12 + 1);
526 printf("%d\n", 12 - 2);
527 printf("%d\n", 12 * 312);
528 printf("%d\n", 12 / 4);
529 printf("%d\n", 12 % 20);
530 printf("%d\n", 12 & 6);
531 printf("%d\n", 12 ^ 7);
532 printf("%d\n", 12 | 8);
533 printf("%d\n", 12 >> 2);
534 printf("%d\n", 12 << 4);
535 printf("%d\n", ~12);
536 printf("%d\n", -12);
537 printf("%d\n", +12);
538 printf("%d %d %d %d\n",
539 isid('a'),
540 isid('g'),
541 isid('T'),
542 isid('('));
545 int isid(int c)
547 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
550 /**********************/
552 int vstack[10], *vstack_ptr;
554 void vpush(int vt, int vc)
556 *vstack_ptr++ = vt;
557 *vstack_ptr++ = vc;
560 void vpop(int *ft, int *fc)
562 *fc = *--vstack_ptr;
563 *ft = *--vstack_ptr;
566 void expr2_test()
568 int a, b;
570 printf("expr2:\n");
571 vstack_ptr = vstack;
572 vpush(1432432, 2);
573 vstack_ptr[-2] &= ~0xffffff80;
574 vpop(&a, &b);
575 printf("res= %d %d\n", a, b);
578 void constant_expr_test()
580 int a;
581 printf("constant_expr:\n");
582 a = 3;
583 printf("%d\n", a * 16);
584 printf("%d\n", a * 1);
585 printf("%d\n", a + 0);
588 int tab4[10];
590 void expr_ptr_test()
592 int *p, *q;
594 printf("expr_ptr:\n");
595 p = tab4;
596 q = tab4 + 10;
597 printf("diff=%d\n", q - p);
598 p++;
599 printf("inc=%d\n", p - tab4);
600 p--;
601 printf("dec=%d\n", p - tab4);
602 ++p;
603 printf("inc=%d\n", p - tab4);
604 --p;
605 printf("dec=%d\n", p - tab4);
606 printf("add=%d\n", p + 3 - tab4);
607 printf("add=%d\n", 3 + p - tab4);
610 void expr_cmp_test()
612 int a, b;
613 printf("constant_expr:\n");
614 a = -1;
615 b = 1;
616 printf("%d\n", a == a);
617 printf("%d\n", a != a);
619 printf("%d\n", a < b);
620 printf("%d\n", a <= b);
621 printf("%d\n", a <= a);
622 printf("%d\n", b >= a);
623 printf("%d\n", a >= a);
624 printf("%d\n", b > a);
626 printf("%d\n", (unsigned)a < b);
627 printf("%d\n", (unsigned)a <= b);
628 printf("%d\n", (unsigned)a <= a);
629 printf("%d\n", (unsigned)b >= a);
630 printf("%d\n", (unsigned)a >= a);
631 printf("%d\n", (unsigned)b > a);
634 void struct_test()
636 struct1 *s;
637 union union2 u;
639 printf("struct:\n");
640 printf("sizes: %d %d %d %d\n",
641 sizeof(struct struct1),
642 sizeof(struct struct2),
643 sizeof(union union1),
644 sizeof(union union2));
645 st1.f1 = 1;
646 st1.f2 = 2;
647 st1.f3 = 3;
648 printf("st1: %d %d %d\n",
649 st1.f1, st1.f2, st1.f3);
650 st1.u.v1 = 1;
651 st1.u.v2 = 2;
652 printf("union1: %d\n", st1.u.v1);
653 u.w1 = 1;
654 u.w2 = 2;
655 printf("union2: %d\n", u.w1);
656 s = &st2;
657 s->f1 = 3;
658 s->f2 = 2;
659 s->f3 = 1;
660 printf("st2: %d %d %d\n",
661 s->f1, s->f2, s->f3);
662 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
665 /* XXX: depend on endianness */
666 void char_short_test()
668 int var1, var2;
670 printf("char_short:\n");
672 var1 = 0x01020304;
673 var2 = 0xfffefdfc;
674 printf("s8=%d %d\n",
675 *(char *)&var1, *(char *)&var2);
676 printf("u8=%d %d\n",
677 *(unsigned char *)&var1, *(unsigned char *)&var2);
678 printf("s16=%d %d\n",
679 *(short *)&var1, *(short *)&var2);
680 printf("u16=%d %d\n",
681 *(unsigned short *)&var1, *(unsigned short *)&var2);
682 printf("s32=%d %d\n",
683 *(int *)&var1, *(int *)&var2);
684 printf("u32=%d %d\n",
685 *(unsigned int *)&var1, *(unsigned int *)&var2);
686 *(char *)&var1 = 0x08;
687 printf("var1=%x\n", var1);
688 *(short *)&var1 = 0x0809;
689 printf("var1=%x\n", var1);
690 *(int *)&var1 = 0x08090a0b;
691 printf("var1=%x\n", var1);
694 /******************/
696 typedef struct Sym {
697 int v;
698 int t;
699 int c;
700 struct Sym *next;
701 struct Sym *prev;
702 } Sym;
704 void bool_test()
706 int *s, a, b, t, f;
708 a = 0;
709 s = (void*)0;
710 printf("!s=%d\n", !s);
712 if (!s || !s[0])
713 a = 1;
714 printf("a=%d\n", a);
716 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
717 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
718 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
719 #if 1 && 1
720 printf("a1\n");
721 #endif
722 #if 1 || 0
723 printf("a2\n");
724 #endif
725 #if 1 ? 0 : 1
726 printf("a3\n");
727 #endif
728 #if 0 ? 0 : 1
729 printf("a4\n");
730 #endif
732 a = 4;
733 printf("b=%d\n", a + (0 ? 1 : a / 2));
735 /* test register spilling */
736 a = 10;
737 b = 10;
738 a = (a + b) * ((a < b) ?
739 ((b - a) * (a - b)): a + b);
740 printf("a=%d\n", a);
742 /* test complex || or && expressions */
743 t = 1;
744 f = 0;
745 a = 32;
746 printf("exp=%d\n", f == (32 <= a && a <= 3));
747 printf("r=%d\n", (t || f) + (t && f));
749 /* test ? : cast */
751 int aspect_on;
752 int aspect_native = 65536;
753 double bfu_aspect = 1.0;
754 int aspect;
755 for(aspect_on = 0; aspect_on < 2; aspect_on++) {
756 aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL;
757 printf("aspect=%d\n", aspect);
763 int cinit1 = 0;
764 int *cinit2 = (int []){3, 2, 1};
766 void compound_literal_test(void)
768 int *p, i;
769 char *q, *q3;
771 printf("compound_test:\n");
773 p = (int []){1, 2, 3};
774 for(i=0;i<3;i++)
775 printf(" %d", p[i]);
776 printf("\n");
778 for(i=0;i<3;i++)
779 printf("%d", cinit2[i]);
780 printf("\n");
782 q = "tralala1";
783 printf("q1=%s\n", q);
785 q = (char *){ "tralala2" };
786 printf("q2=%s\n", q);
788 q3 = (char *){ q };
789 printf("q3=%s\n", q3);
791 q = (char []){ "tralala3" };
792 printf("q4=%s\n", q);
794 #ifdef ALL_ISOC99
795 p = (int []){1, 2, cinit1 + 3};
796 for(i=0;i<3;i++)
797 printf(" %d", p[i]);
798 printf("\n");
800 for(i=0;i<3;i++) {
801 p = (int []){1, 2, 4 + i};
802 printf("%d %d %d\n",
803 p[0],
804 p[1],
805 p[2]);
807 #endif
810 /* K & R protos */
812 kr_func1(a, b)
814 return a + b;
817 int kr_func2(a, b)
819 return a + b;
822 kr_test()
824 printf("kr_test:\n");
825 printf("func1=%d\n", kr_func1(3, 4));
826 printf("func2=%d\n", kr_func2(3, 4));
827 return 0;
830 void num(int n)
832 char *tab, *p;
833 tab = (char*)malloc(20);
834 p = tab;
835 while (1) {
836 *p = 48 + (n % 10);
837 p++;
838 n = n / 10;
839 if (n == 0)
840 break;
842 while (p != tab) {
843 p--;
844 printf("%c", *p);
846 printf("\n");
849 /* structure assignment tests */
850 struct structa1 {
851 int f1;
852 char f2;
855 struct structa1 ssta1;
857 void struct_assign_test1(struct structa1 s1, int t)
859 printf("%d %d %d\n", s1.f1, s1.f2, t);
862 struct structa1 struct_assign_test2(struct structa1 s1, int t)
864 s1.f1 += t;
865 s1.f2 -= t;
866 return s1;
869 void struct_assign_test(void)
871 struct structa1 lsta1, lsta2;
873 #if 0
874 printf("struct_assign_test:\n");
876 lsta1.f1 = 1;
877 lsta1.f2 = 2;
878 printf("%d %d\n", lsta1.f1, lsta1.f2);
879 lsta2 = lsta1;
880 printf("%d %d\n", lsta2.f1, lsta2.f2);
881 #else
882 lsta2.f1 = 1;
883 lsta2.f2 = 2;
884 #endif
885 struct_assign_test1(lsta2, 3);
887 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
888 lsta2 = struct_assign_test2(lsta2, 4);
889 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
892 /* casts to short/char */
894 void cast1(char a, short b, unsigned char c, unsigned short d)
896 printf("%d %d %d %d\n", a, b, c, d);
899 char bcast;
900 short scast;
902 void cast_test()
904 int a;
905 char c;
906 char tab[10];
908 printf("cast_test:\n");
909 a = 0xfffff;
910 cast1(a, a, a, a);
911 a = 0xffffe;
912 printf("%d %d %d %d\n",
913 (char)(a + 1),
914 (short)(a + 1),
915 (unsigned char)(a + 1),
916 (unsigned short)(a + 1));
917 printf("%d %d %d %d\n",
918 (char)0xfffff,
919 (short)0xfffff,
920 (unsigned char)0xfffff,
921 (unsigned short)0xfffff);
923 a = (bcast = 128) + 1;
924 printf("%d\n", a);
925 a = (scast = 65536) + 1;
926 printf("%d\n", a);
928 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c));
930 /* test implicit int casting for array accesses */
931 c = 0;
932 tab[1] = 2;
933 tab[c] = 1;
934 printf("%d %d\n", tab[0], tab[1]);
936 /* test implicit casting on some operators */
937 printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a'));
938 printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a'));
939 printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a'));
942 /* initializers tests */
943 struct structinit1 {
944 int f1;
945 char f2;
946 short f3;
947 int farray[3];
950 int sinit1 = 2;
951 int sinit2 = { 3 };
952 int sinit3[3] = { 1, 2, {{3}}, };
953 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
954 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
955 int sinit6[] = { 1, 2, 3 };
956 int sinit7[] = { [2] = 3, [0] = 1, 2 };
957 char sinit8[] = "hello" "trala";
959 struct structinit1 sinit9 = { 1, 2, 3 };
960 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
961 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
962 #ifdef ALL_ISOC99
963 .farray[0] = 10,
964 .farray[1] = 11,
965 .farray[2] = 12,
966 #endif
969 char *sinit12 = "hello world";
970 char *sinit13[] = {
971 "test1",
972 "test2",
973 "test3",
975 char sinit14[10] = { "abc" };
976 int sinit15[3] = { sizeof(sinit15), 1, 2 };
978 void init_test(void)
980 int linit1 = 2;
981 int linit2 = { 3 };
982 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
983 int linit6[] = { 1, 2, 3 };
984 int i, j;
985 char linit8[] = "hello" "trala";
986 int linit12[10] = { 1, 2 };
987 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
988 char linit14[10] = "abc";
989 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
990 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
991 int linit17 = sizeof(linit17);
993 printf("init_test:\n");
995 printf("sinit1=%d\n", sinit1);
996 printf("sinit2=%d\n", sinit2);
997 printf("sinit3=%d %d %d %d\n",
998 sizeof(sinit3),
999 sinit3[0],
1000 sinit3[1],
1001 sinit3[2]
1003 printf("sinit6=%d\n", sizeof(sinit6));
1004 printf("sinit7=%d %d %d %d\n",
1005 sizeof(sinit7),
1006 sinit7[0],
1007 sinit7[1],
1008 sinit7[2]
1010 printf("sinit8=%s\n", sinit8);
1011 printf("sinit9=%d %d %d\n",
1012 sinit9.f1,
1013 sinit9.f2,
1014 sinit9.f3
1016 printf("sinit10=%d %d %d\n",
1017 sinit10.f1,
1018 sinit10.f2,
1019 sinit10.f3
1021 printf("sinit11=%d %d %d %d %d %d\n",
1022 sinit11.f1,
1023 sinit11.f2,
1024 sinit11.f3,
1025 sinit11.farray[0],
1026 sinit11.farray[1],
1027 sinit11.farray[2]
1030 for(i=0;i<3;i++)
1031 for(j=0;j<2;j++)
1032 printf("[%d][%d] = %d %d %d\n",
1033 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
1034 printf("linit1=%d\n", linit1);
1035 printf("linit2=%d\n", linit2);
1036 printf("linit6=%d\n", sizeof(linit6));
1037 printf("linit8=%d %s\n", sizeof(linit8), linit8);
1039 printf("sinit12=%s\n", sinit12);
1040 printf("sinit13=%d %s %s %s\n",
1041 sizeof(sinit13),
1042 sinit13[0],
1043 sinit13[1],
1044 sinit13[2]);
1045 printf("sinit14=%s\n", sinit14);
1047 for(i=0;i<10;i++) printf(" %d", linit12[i]);
1048 printf("\n");
1049 for(i=0;i<10;i++) printf(" %d", linit13[i]);
1050 printf("\n");
1051 for(i=0;i<10;i++) printf(" %d", linit14[i]);
1052 printf("\n");
1053 for(i=0;i<10;i++) printf(" %d", linit15[i]);
1054 printf("\n");
1055 printf("%d %d %d %d\n",
1056 linit16.a1,
1057 linit16.a2,
1058 linit16.a3,
1059 linit16.a4);
1060 /* test that initialisation is done after variable declare */
1061 printf("linit17=%d\n", linit17);
1062 printf("sinit15=%d\n", sinit15[0]);
1065 void switch_test()
1067 int i;
1069 for(i=0;i<15;i++) {
1070 switch(i) {
1071 case 0:
1072 case 1:
1073 printf("a");
1074 break;
1075 default:
1076 printf("%d", i);
1077 break;
1078 case 8 ... 12:
1079 printf("c");
1080 break;
1081 case 3:
1082 printf("b");
1083 break;
1086 printf("\n");
1089 /* ISOC99 _Bool type */
1090 void c99_bool_test(void)
1092 #ifdef BOOL_ISOC99
1093 int a;
1094 _Bool b;
1096 printf("bool_test:\n");
1097 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
1098 a = 3;
1099 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
1100 b = 3;
1101 printf("b = %d\n", b);
1102 b++;
1103 printf("b = %d\n", b);
1104 #endif
1107 void bitfield_test(void)
1109 int a;
1110 struct sbf1 {
1111 int f1 : 3;
1112 int : 2;
1113 int f2 : 1;
1114 int : 0;
1115 int f3 : 5;
1116 int f4 : 7;
1117 unsigned int f5 : 7;
1118 } st1;
1119 printf("bitfield_test:");
1120 printf("sizeof(st1) = %d\n", sizeof(st1));
1122 st1.f1 = 3;
1123 st1.f2 = 1;
1124 st1.f3 = 15;
1125 a = 120;
1126 st1.f4 = a;
1127 st1.f5 = a;
1128 st1.f5++;
1129 printf("%d %d %d %d %d\n",
1130 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1133 #define FTEST(prefix, type, fmt)\
1134 void prefix ## cmp(type a, type b)\
1136 printf("%d %d %d %d %d %d\n",\
1137 a == b,\
1138 a != b,\
1139 a < b,\
1140 a > b,\
1141 a >= b,\
1142 a <= b);\
1143 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1146 a + b,\
1147 a - b,\
1148 a * b,\
1149 a / b,\
1150 -a);\
1151 printf(fmt "\n", ++a);\
1152 printf(fmt "\n", a++);\
1153 printf(fmt "\n", a);\
1155 void prefix ## fcast(type a)\
1157 float fa;\
1158 double da;\
1159 long double la;\
1160 int ia;\
1161 unsigned int ua;\
1162 type b;\
1163 fa = a;\
1164 da = a;\
1165 la = a;\
1166 printf("ftof: %f %f %Lf\n", fa, da, la);\
1167 ia = (int)a;\
1168 ua = (unsigned int)a;\
1169 printf("ftoi: %d %u\n", ia, ua);\
1170 ia = -1234;\
1171 ua = 0x81234500;\
1172 b = ia;\
1173 printf("itof: " fmt "\n", b);\
1174 b = ua;\
1175 printf("utof: " fmt "\n", b);\
1178 void prefix ## test(void)\
1180 printf("testing '%s'\n", #type);\
1181 prefix ## cmp(1, 2.5);\
1182 prefix ## cmp(2, 1.5);\
1183 prefix ## cmp(1, 1);\
1184 prefix ## fcast(234.6);\
1185 prefix ## fcast(-2334.6);\
1188 FTEST(f, float, "%f")
1189 FTEST(d, double, "%f")
1190 FTEST(ld, long double, "%Lf")
1192 double ftab1[3] = { 1.2, 3.4, -5.6 };
1195 void float_test(void)
1197 float fa, fb;
1198 double da, db;
1199 int a;
1200 unsigned int b;
1202 printf("float_test:\n");
1203 printf("sizeof(float) = %d\n", sizeof(float));
1204 printf("sizeof(double) = %d\n", sizeof(double));
1205 printf("sizeof(long double) = %d\n", sizeof(long double));
1206 ftest();
1207 dtest();
1208 ldtest();
1209 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1210 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1211 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1212 da = 123;
1213 printf("da=%f\n", da);
1214 fa = 123;
1215 printf("fa=%f\n", fa);
1216 a = 4000000000;
1217 da = a;
1218 printf("da = %f\n", da);
1219 b = 4000000000;
1220 db = b;
1221 printf("db = %f\n", db);
1224 int fib(int n)
1226 if (n <= 2)
1227 return 1;
1228 else
1229 return fib(n-1) + fib(n-2);
1232 void funcptr_test()
1234 void (*func)(int);
1235 int a;
1236 struct {
1237 int dummy;
1238 void (*func)(int);
1239 } st1;
1241 printf("funcptr:\n");
1242 func = &num;
1243 (*func)(12345);
1244 func = num;
1245 a = 1;
1246 a = 1;
1247 func(12345);
1248 /* more complicated pointer computation */
1249 st1.func = num;
1250 st1.func(12346);
1251 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1252 printf("sizeof2 = %d\n", sizeof funcptr_test);
1253 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1254 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1257 void lloptest(long long a, long long b)
1259 unsigned long long ua, ub;
1261 ua = a;
1262 ub = b;
1263 /* arith */
1264 printf("arith: %Ld %Ld %Ld %Ld %Ld\n",
1265 a + b,
1266 a - b,
1267 a * b,
1268 a / b,
1269 a % b);
1271 /* binary */
1272 printf("bin: %Ld %Ld %Ld\n",
1273 a & b,
1274 a | b,
1275 a ^ b);
1277 /* tests */
1278 printf("test: %d %d %d %d %d %d\n",
1279 a == b,
1280 a != b,
1281 a < b,
1282 a > b,
1283 a >= b,
1284 a <= b);
1286 printf("utest: %d %d %d %d %d %d\n",
1287 ua == ub,
1288 ua != ub,
1289 ua < ub,
1290 ua > ub,
1291 ua >= ub,
1292 ua <= ub);
1294 /* arith2 */
1295 a++;
1296 b++;
1297 printf("arith2: %Ld %Ld\n", a, b);
1298 printf("arith2: %Ld %Ld\n", a++, b++);
1299 printf("arith2: %Ld %Ld\n", --a, --b);
1300 printf("arith2: %Ld %Ld\n", a, b);
1303 void llshift(long long a, int b)
1305 printf("shift: %Ld %Ld %Ld\n",
1306 (unsigned long long)a >> b,
1307 a >> b,
1308 a << b);
1309 printf("shiftc: %Ld %Ld %Ld\n",
1310 (unsigned long long)a >> 3,
1311 a >> 3,
1312 a << 3);
1313 printf("shiftc: %Ld %Ld %Ld\n",
1314 (unsigned long long)a >> 35,
1315 a >> 35,
1316 a << 35);
1319 void llfloat(void)
1321 float fa;
1322 double da;
1323 long double lda;
1324 long long la, lb, lc;
1325 unsigned long long ula, ulb, ulc;
1326 la = 0x12345678;
1327 ula = 0x72345678;
1328 la = (la << 20) | 0x12345;
1329 ula = ula << 33;
1330 printf("la=%Ld ula=%Lu\n", la, ula);
1332 fa = la;
1333 da = la;
1334 lda = la;
1335 printf("lltof: %f %f %Lf\n", fa, da, lda);
1337 la = fa;
1338 lb = da;
1339 lc = lda;
1340 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1342 fa = ula;
1343 da = ula;
1344 lda = ula;
1345 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1347 ula = fa;
1348 ulb = da;
1349 ulc = lda;
1350 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1353 long long llfunc1(int a)
1355 return a;
1358 /* currently not supported, except for typing */
1359 void longlong_test(void)
1361 long long a, b, c;
1362 int ia;
1363 unsigned int ua;
1364 printf("longlong_test:\n");
1365 printf("sizeof(long long) = %d\n", sizeof(long long));
1366 ia = -1;
1367 ua = -2;
1368 a = ia;
1369 b = ua;
1370 printf("%Ld %Ld\n", a, b);
1371 printf("%Ld %Ld %Ld %Lx\n",
1372 (long long)1,
1373 (long long)-2,
1374 1LL,
1375 0x1234567812345679);
1376 a = llfunc1(-3);
1377 printf("%Ld\n", a);
1379 lloptest(1000, 23);
1380 lloptest(0xff, 0x1234);
1381 b = 0x72345678 << 10;
1382 lloptest(-3, b);
1383 llshift(0x123, 5);
1384 llshift(-23, 5);
1385 b = 0x72345678 << 10;
1386 llshift(b, 47);
1388 llfloat();
1389 #if 1
1390 b = 0x12345678;
1391 a = -1;
1392 c = a + b;
1393 printf("%Lx\n", c);
1394 #endif
1397 void vprintf1(const char *fmt, ...)
1399 va_list ap;
1400 const char *p;
1401 int c, i;
1402 double d;
1403 long long ll;
1405 va_start(ap, fmt);
1407 p = fmt;
1408 for(;;) {
1409 c = *p;
1410 if (c == '\0')
1411 break;
1412 p++;
1413 if (c == '%') {
1414 c = *p;
1415 switch(c) {
1416 case '\0':
1417 goto the_end;
1418 case 'd':
1419 i = va_arg(ap, int);
1420 printf("%d", i);
1421 break;
1422 case 'f':
1423 d = va_arg(ap, double);
1424 printf("%f", d);
1425 break;
1426 case 'l':
1427 ll = va_arg(ap, long long);
1428 printf("%Ld", ll);
1429 break;
1431 p++;
1432 } else {
1433 putchar(c);
1436 the_end:
1437 va_end(ap);
1441 void stdarg_test(void)
1443 vprintf1("%d %d %d\n", 1, 2, 3);
1444 vprintf1("%f %d %f\n", 1.0, 2, 3.0);
1445 vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0);
1448 void whitespace_test(void)
1450 char *str;
1452 \f\v #if 1
1453 pri\
1454 ntf("whitspace:\n");\f\v
1455 #endif
1456 pf("N=%d\n", 2);
1457 pri\
1458 ntf("aaa=%d\n", 3);
1460 pri\
1462 ntf("min=%d\n", 4);
1463 printf("len1=%d\n", strlen("
1464 "));
1465 str = "
1467 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
1468 printf("len1=%d\n", strlen(" a
1469 "));
1472 int reltab[3] = { 1, 2, 3 };
1474 int *rel1 = &reltab[1];
1475 int *rel2 = &reltab[2];
1477 void relocation_test(void)
1479 printf("*rel1=%d\n", *rel1);
1480 printf("*rel2=%d\n", *rel2);