converted to texinfo - added developper's guide
[tinycc.git] / tcctest.c
blob333b65205426426cebb2c7b22ef0c412c5f11e1b
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();
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 #define AAA 3
66 #undef AAA
67 #define AAA 4
69 #if 1
70 #define B3 1
71 #elif 1
72 #define B3 2
73 #elif 0
74 #define B3 3
75 #else
76 #define B3 4
77 #endif
79 void macro_test()
81 printf("macro:\n");
82 pf("N=%d\n", N);
83 printf("aaa=%d\n", AAA);
85 printf("min=%d\n", min(1, min(2, -1)));
87 printf("s1=%s\n", glue(HIGH, LOW));
88 printf("s2=%s\n", xglue(HIGH, LOW));
89 printf("s3=%s\n", str("c"));
90 printf("s4=%s\n", str(a1));
91 printf("B3=%d\n", B3);
93 #ifdef A
94 printf("A defined\n");
95 #endif
96 #ifdef B
97 printf("B defined\n");
98 #endif
99 #ifdef A
100 printf("A defined\n");
101 #else
102 printf("A not defined\n");
103 #endif
104 #ifdef B
105 printf("B defined\n");
106 #else
107 printf("B not defined\n");
108 #endif
110 #ifdef A
111 printf("A defined\n");
112 #ifdef B
113 printf("B1 defined\n");
114 #else
115 printf("B1 not defined\n");
116 #endif
117 #else
118 printf("A not defined\n");
119 #ifdef B
120 printf("B2 defined\n");
121 #else
122 printf("B2 not defined\n");
123 #endif
124 #endif
126 #if 1+1
127 printf("test true1\n");
128 #endif
129 #if 0
130 printf("test true2\n");
131 #endif
132 #if 1-1
133 printf("test true3\n");
134 #endif
135 #if defined(A)
136 printf("test trueA\n");
137 #endif
138 #if defined(B)
139 printf("test trueB\n");
140 #endif
142 #if 0
143 printf("test 0\n");
144 #elif 0
145 printf("test 1\n");
146 #elif 2
147 printf("test 2\n");
148 #else
149 printf("test 3\n");
150 #endif
152 printf("__LINE__=%d __FILE__=%s\n",
153 __LINE__, __FILE__);
154 /* not strictly preprocessor, but we test it there */
155 #ifdef C99_MACROS
156 printf("__func__ = %s\n", __func__);
157 dprintf(1, "vaarg=%d\n", 1);
158 #endif
161 int op(a,b)
163 return a / b;
166 int ret(a)
168 if (a == 2)
169 return 1;
170 if (a == 3)
171 return 2;
172 return 0;
175 void ps(char *s)
177 int c;
178 while (1) {
179 c = *s;
180 if (c == 0)
181 break;
182 printf("%c", c);
183 s++;
187 void string_test()
189 int b;
190 printf("string:\n");
191 printf("\141\1423\143\n");/* dezdez test */
192 printf("\x41\x42\x43\x3a\n");
193 printf("c=%c\n", 'r');
194 printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c');
195 printf("wstring=%S\n", L"abc");
196 printf("wstring=%S\n", L"abc" L"def" "ghi");
197 ps("test");
198 b = 32;
199 while ((b = b + 1) < 96) {
200 printf("%c", b);
202 printf("\n");
203 printf("fib=%d\n", fib(33));
204 b = 262144;
205 while (b != 0x80000000) {
206 num(b);
207 b = b * 2;
211 void loop_test()
213 int i;
214 i = 0;
215 while (i < 10)
216 printf("%d", i++);
217 printf("\n");
218 for(i = 0; i < 10;i++)
219 printf("%d", i);
220 printf("\n");
221 i = 0;
222 do {
223 printf("%d", i++);
224 } while (i < 10);
225 printf("\n");
227 /* break/continue tests */
228 i = 0;
229 while (1) {
230 if (i == 6)
231 break;
232 i++;
233 if (i == 3)
234 continue;
235 printf("%d", i);
237 printf("\n");
239 /* break/continue tests */
240 i = 0;
241 do {
242 if (i == 6)
243 break;
244 i++;
245 if (i == 3)
246 continue;
247 printf("%d", i);
248 } while(1);
249 printf("\n");
251 for(i = 0;i < 10;i++) {
252 if (i == 3)
253 continue;
254 printf("%d", i);
256 printf("\n");
260 void goto_test()
262 int i;
264 printf("goto:\n");
265 i = 0;
266 s_loop:
267 if (i >= 10)
268 goto s_end;
269 printf("%d", i);
270 i++;
271 goto s_loop;
272 s_end:
273 printf("\n");
276 enum {
278 E1 = 2,
279 E2 = 4,
284 enum test {
285 E5 = 1000,
288 void enum_test()
290 enum test b1;
291 printf("enum:\n%d %d %d %d %d %d\n",
292 E0, E1, E2, E3, E4, E5);
293 b1 = 1;
294 printf("b1=%d\n", b1);
297 typedef int *my_ptr;
299 void typedef_test()
301 my_ptr a;
302 int b;
303 a = &b;
304 *a = 1234;
305 printf("typedef:\n");
306 printf("a=%d\n", *a);
309 void forward_test()
311 printf("forward:\n");
312 forward_ref();
313 forward_ref();
317 void forward_ref(void)
319 printf("forward ok\n");
322 typedef struct struct1 {
323 int f1;
324 int f2, f3;
325 union union1 {
326 int v1;
327 int v2;
328 } u;
329 char str[3];
330 } struct1;
332 struct struct2 {
333 int a;
334 char b;
337 union union2 {
338 int w1;
339 int w2;
342 struct struct1 st1, st2;
344 int main(int argc, char **argv)
346 string_test();
347 expr_test();
348 macro_test();
349 scope_test();
350 forward_test();
351 funcptr_test();
352 loop_test();
353 switch_test();
354 goto_test();
355 enum_test();
356 typedef_test();
357 struct_test();
358 array_test();
359 expr_ptr_test();
360 bool_test();
361 expr2_test();
362 constant_expr_test();
363 expr_cmp_test();
364 char_short_test();
365 init_test();
366 compound_literal_test();
367 kr_test();
368 struct_assign_test();
369 cast_test();
370 bitfield_test();
371 c99_bool_test();
372 float_test();
373 longlong_test();
374 return 0;
377 int tab[3];
378 int tab2[3][2];
380 int g;
382 void f1(g)
384 printf("g1=%d\n", g);
387 void scope_test()
389 printf("scope:\n");
390 g = 2;
391 f1(1);
392 printf("g2=%d\n", g);
394 int g;
395 g = 3;
396 printf("g3=%d\n", g);
398 int g;
399 g = 4;
400 printf("g4=%d\n", g);
403 printf("g5=%d\n", g);
406 void array_test(int a[4])
408 int i, j;
410 printf("array:\n");
411 printf("sizeof(a) = %d\n", sizeof(a));
412 printf("sizeof(int) = %d\n", sizeof(int));
413 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
414 printf("sizeof(short) = %d\n", sizeof(short));
415 printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));
416 printf("sizeof(char) = %d\n", sizeof(char));
417 printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char));
418 printf("sizeof tab %d\n", sizeof(tab));
419 printf("sizeof tab2 %d\n", sizeof tab2);
420 tab[0] = 1;
421 tab[1] = 2;
422 tab[2] = 3;
423 printf("%d %d %d\n", tab[0], tab[1], tab[2]);
424 for(i=0;i<3;i++)
425 for(j=0;j<2;j++)
426 tab2[i][j] = 10 * i + j;
427 for(i=0;i<3*2;i++) {
428 printf(" %3d", ((int *)tab2)[i]);
430 printf("\n");
433 void expr_test()
435 int a, b;
436 a = 0;
437 printf("%d\n", a += 1);
438 printf("%d\n", a -= 2);
439 printf("%d\n", a *= 31232132);
440 printf("%d\n", a /= 4);
441 printf("%d\n", a %= 20);
442 printf("%d\n", a &= 6);
443 printf("%d\n", a ^= 7);
444 printf("%d\n", a |= 8);
445 printf("%d\n", a >>= 3);
446 printf("%d\n", a <<= 4);
448 a = 22321;
449 b = -22321;
450 printf("%d\n", a + 1);
451 printf("%d\n", a - 2);
452 printf("%d\n", a * 312);
453 printf("%d\n", a / 4);
454 printf("%d\n", b / 4);
455 printf("%d\n", (unsigned)b / 4);
456 printf("%d\n", a % 20);
457 printf("%d\n", b % 20);
458 printf("%d\n", (unsigned)b % 20);
459 printf("%d\n", a & 6);
460 printf("%d\n", a ^ 7);
461 printf("%d\n", a | 8);
462 printf("%d\n", a >> 3);
463 printf("%d\n", b >> 3);
464 printf("%d\n", (unsigned)b >> 3);
465 printf("%d\n", a << 4);
466 printf("%d\n", ~a);
467 printf("%d\n", -a);
468 printf("%d\n", +a);
470 printf("%d\n", 12 + 1);
471 printf("%d\n", 12 - 2);
472 printf("%d\n", 12 * 312);
473 printf("%d\n", 12 / 4);
474 printf("%d\n", 12 % 20);
475 printf("%d\n", 12 & 6);
476 printf("%d\n", 12 ^ 7);
477 printf("%d\n", 12 | 8);
478 printf("%d\n", 12 >> 2);
479 printf("%d\n", 12 << 4);
480 printf("%d\n", ~12);
481 printf("%d\n", -12);
482 printf("%d\n", +12);
483 printf("%d %d %d %d\n",
484 isid('a'),
485 isid('g'),
486 isid('T'),
487 isid('('));
490 int isid(int c)
492 return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_';
495 /**********************/
497 int vstack[10], *vstack_ptr;
499 void vpush(int vt, int vc)
501 *vstack_ptr++ = vt;
502 *vstack_ptr++ = vc;
505 void vpop(int *ft, int *fc)
507 *fc = *--vstack_ptr;
508 *ft = *--vstack_ptr;
511 void expr2_test()
513 int a, b;
515 printf("expr2:\n");
516 vstack_ptr = vstack;
517 vpush(1432432, 2);
518 vstack_ptr[-2] &= ~0xffffff80;
519 vpop(&a, &b);
520 printf("res= %d %d\n", a, b);
523 void constant_expr_test()
525 int a;
526 printf("constant_expr:\n");
527 a = 3;
528 printf("%d\n", a * 16);
529 printf("%d\n", a * 1);
530 printf("%d\n", a + 0);
533 int tab4[10];
535 void expr_ptr_test()
537 int *p, *q;
539 printf("expr_ptr:\n");
540 p = tab4;
541 q = tab4 + 10;
542 printf("diff=%d\n", q - p);
543 p++;
544 printf("inc=%d\n", p - tab4);
545 p--;
546 printf("dec=%d\n", p - tab4);
547 ++p;
548 printf("inc=%d\n", p - tab4);
549 --p;
550 printf("dec=%d\n", p - tab4);
551 printf("add=%d\n", p + 3 - tab4);
552 printf("add=%d\n", 3 + p - tab4);
555 void expr_cmp_test()
557 int a, b;
558 printf("constant_expr:\n");
559 a = -1;
560 b = 1;
561 printf("%d\n", a == a);
562 printf("%d\n", a != a);
564 printf("%d\n", a < b);
565 printf("%d\n", a <= b);
566 printf("%d\n", a <= a);
567 printf("%d\n", b >= a);
568 printf("%d\n", a >= a);
569 printf("%d\n", b > a);
571 printf("%d\n", (unsigned)a < b);
572 printf("%d\n", (unsigned)a <= b);
573 printf("%d\n", (unsigned)a <= a);
574 printf("%d\n", (unsigned)b >= a);
575 printf("%d\n", (unsigned)a >= a);
576 printf("%d\n", (unsigned)b > a);
579 void struct_test()
581 struct1 *s;
582 union union2 u;
584 printf("struct:\n");
585 printf("sizes: %d %d %d %d\n",
586 sizeof(struct struct1),
587 sizeof(struct struct2),
588 sizeof(union union1),
589 sizeof(union union2));
590 st1.f1 = 1;
591 st1.f2 = 2;
592 st1.f3 = 3;
593 printf("st1: %d %d %d\n",
594 st1.f1, st1.f2, st1.f3);
595 st1.u.v1 = 1;
596 st1.u.v2 = 2;
597 printf("union1: %d\n", st1.u.v1);
598 u.w1 = 1;
599 u.w2 = 2;
600 printf("union2: %d\n", u.w1);
601 s = &st2;
602 s->f1 = 3;
603 s->f2 = 2;
604 s->f3 = 1;
605 printf("st2: %d %d %d\n",
606 s->f1, s->f2, s->f3);
607 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
610 /* XXX: depend on endianness */
611 void char_short_test()
613 int var1, var2;
615 printf("char_short:\n");
617 var1 = 0x01020304;
618 var2 = 0xfffefdfc;
619 printf("s8=%d %d\n",
620 *(char *)&var1, *(char *)&var2);
621 printf("u8=%d %d\n",
622 *(unsigned char *)&var1, *(unsigned char *)&var2);
623 printf("s16=%d %d\n",
624 *(short *)&var1, *(short *)&var2);
625 printf("u16=%d %d\n",
626 *(unsigned short *)&var1, *(unsigned short *)&var2);
627 printf("s32=%d %d\n",
628 *(int *)&var1, *(int *)&var2);
629 printf("u32=%d %d\n",
630 *(unsigned int *)&var1, *(unsigned int *)&var2);
631 *(char *)&var1 = 0x08;
632 printf("var1=%x\n", var1);
633 *(short *)&var1 = 0x0809;
634 printf("var1=%x\n", var1);
635 *(int *)&var1 = 0x08090a0b;
636 printf("var1=%x\n", var1);
639 /******************/
641 typedef struct Sym {
642 int v;
643 int t;
644 int c;
645 struct Sym *next;
646 struct Sym *prev;
647 } Sym;
649 void bool_test()
651 int *s, a;
653 a = 0;
654 s = (void*)0;
655 printf("!s=%d\n", !s);
657 if (!s || !s[0])
658 a = 1;
659 printf("a=%d\n", a);
661 printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1);
662 printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1);
663 printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0);
664 #if 1 && 1
665 printf("a1\n");
666 #endif
667 #if 1 || 0
668 printf("a2\n");
669 #endif
670 #if 1 ? 0 : 1
671 printf("a3\n");
672 #endif
673 #if 0 ? 0 : 1
674 printf("a4\n");
675 #endif
677 a = 4;
678 printf("b=%d\n", a + (0 ? 1 : a / 2));
682 int cinit1 = 0;
683 int *cinit2 = (int []){3, 2, 1};
685 void compound_literal_test(void)
687 int *p, i;
688 char *q, *q3;
690 printf("compound_test:\n");
692 p = (int []){1, 2, 3};
693 for(i=0;i<3;i++)
694 printf(" %d", p[i]);
695 printf("\n");
697 for(i=0;i<3;i++)
698 printf("%d", cinit2[i]);
699 printf("\n");
701 q = "tralala1";
702 printf("q1=%s\n", q);
704 q = (char *){ "tralala2" };
705 printf("q2=%s\n", q);
707 q3 = (char *){ q };
708 printf("q3=%s\n", q3);
710 q = (char []){ "tralala3" };
711 printf("q4=%s\n", q);
713 #ifdef ALL_ISOC99
714 p = (int []){1, 2, cinit1 + 3};
715 for(i=0;i<3;i++)
716 printf(" %d", p[i]);
717 printf("\n");
719 for(i=0;i<3;i++) {
720 p = (int []){1, 2, 4 + i};
721 printf("%d %d %d\n",
722 p[0],
723 p[1],
724 p[2]);
726 #endif
729 /* K & R protos */
731 kr_func1(a, b)
733 return a + b;
736 int kr_func2(a, b)
738 return a + b;
741 kr_test()
743 printf("kr_test:\n");
744 printf("func1=%d\n", kr_func1(3, 4));
745 printf("func2=%d\n", kr_func2(3, 4));
746 return 0;
749 void num(int n)
751 char *tab, *p;
752 tab = (char*)malloc(20);
753 p = tab;
754 while (1) {
755 *p = 48 + (n % 10);
756 p++;
757 n = n / 10;
758 if (n == 0)
759 break;
761 while (p != tab) {
762 p--;
763 printf("%c", *p);
765 printf("\n");
768 /* structure assignment tests */
769 struct structa1 {
770 int f1;
771 char f2;
774 struct structa1 ssta1;
776 void struct_assign_test1(struct structa1 s1, int t)
778 printf("%d %d %d\n", s1.f1, s1.f2, t);
781 struct structa1 struct_assign_test2(struct structa1 s1, int t)
783 s1.f1 += t;
784 s1.f2 -= t;
785 return s1;
788 void struct_assign_test()
790 struct structa1 lsta1, lsta2;
792 printf("struct_assign_test:\n");
794 lsta1.f1 = 1;
795 lsta1.f2 = 2;
796 printf("%d %d\n", lsta1.f1, lsta1.f2);
797 lsta2 = lsta1;
798 printf("%d %d\n", lsta2.f1, lsta2.f2);
800 struct_assign_test1(lsta2, 3);
802 printf("before call: %d %d\n", lsta2.f1, lsta2.f2);
803 lsta2 = struct_assign_test2(lsta2, 4);
804 printf("after call: %d %d\n", lsta2.f1, lsta2.f2);
807 /* casts to short/char */
809 void cast1(char a, short b, unsigned char c, unsigned short d)
811 printf("%d %d %d %d\n", a, b, c, d);
814 char bcast;
815 short scast;
817 void cast_test()
819 int a;
821 printf("cast_test:\n");
822 a = 0xfffff;
823 cast1(a, a, a, a);
824 a = 0xffffe;
825 printf("%d %d %d %d\n",
826 (char)(a + 1),
827 (short)(a + 1),
828 (unsigned char)(a + 1),
829 (unsigned short)(a + 1));
830 printf("%d %d %d %d\n",
831 (char)0xfffff,
832 (short)0xfffff,
833 (unsigned char)0xfffff,
834 (unsigned short)0xfffff);
836 a = (bcast = 128) + 1;
837 printf("%d\n", a);
838 a = (scast = 65536) + 1;
839 printf("%d\n", a);
842 /* initializers tests */
843 struct structinit1 {
844 int f1;
845 char f2;
846 short f3;
847 int farray[3];
850 int sinit1 = 2;
851 int sinit2 = { 3 };
852 int sinit3[3] = { 1, 2, {{3}}, };
853 int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
854 int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 };
855 int sinit6[] = { 1, 2, 3 };
856 int sinit7[] = { [2] = 3, [0] = 1, 2 };
857 char sinit8[] = "hello" "trala";
859 struct structinit1 sinit9 = { 1, 2, 3 };
860 struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 };
861 struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1,
862 #ifdef ALL_ISOC99
863 .farray[0] = 10,
864 .farray[1] = 11,
865 .farray[2] = 12,
866 #endif
869 char *sinit12 = "hello world";
870 char *sinit13[] = {
871 "test1",
872 "test2",
873 "test3",
875 char sinit14[10] = { "abc" };
877 void init_test(void)
879 int linit1 = 2;
880 int linit2 = { 3 };
881 int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} };
882 int linit6[] = { 1, 2, 3 };
883 int i, j;
884 char linit8[] = "hello" "trala";
885 int linit12[10] = { 1, 2 };
886 int linit13[10] = { 1, 2, [7] = 3, [3] = 4, };
887 char linit14[10] = "abc";
888 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
889 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
891 printf("init_test:\n");
893 printf("sinit1=%d\n", sinit1);
894 printf("sinit2=%d\n", sinit2);
895 printf("sinit3=%d %d %d %d\n",
896 sizeof(sinit3),
897 sinit3[0],
898 sinit3[1],
899 sinit3[2]
901 printf("sinit6=%d\n", sizeof(sinit6));
902 printf("sinit7=%d %d %d %d\n",
903 sizeof(sinit7),
904 sinit7[0],
905 sinit7[1],
906 sinit7[2]
908 printf("sinit8=%s\n", sinit8);
909 printf("sinit9=%d %d %d\n",
910 sinit9.f1,
911 sinit9.f2,
912 sinit9.f3
914 printf("sinit10=%d %d %d\n",
915 sinit10.f1,
916 sinit10.f2,
917 sinit10.f3
919 printf("sinit11=%d %d %d %d %d %d\n",
920 sinit11.f1,
921 sinit11.f2,
922 sinit11.f3,
923 sinit11.farray[0],
924 sinit11.farray[1],
925 sinit11.farray[2]
928 for(i=0;i<3;i++)
929 for(j=0;j<2;j++)
930 printf("[%d][%d] = %d %d %d\n",
931 i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]);
932 printf("linit1=%d\n", linit1);
933 printf("linit2=%d\n", linit2);
934 printf("linit6=%d\n", sizeof(linit6));
935 printf("linit8=%d %s\n", sizeof(linit8), linit8);
937 printf("sinit12=%s\n", sinit12);
938 printf("sinit13=%d %s %s %s\n",
939 sizeof(sinit13),
940 sinit13[0],
941 sinit13[1],
942 sinit13[2]);
943 printf("sinit14=%s\n", sinit14);
945 for(i=0;i<10;i++) printf(" %d", linit12[i]);
946 printf("\n");
947 for(i=0;i<10;i++) printf(" %d", linit13[i]);
948 printf("\n");
949 for(i=0;i<10;i++) printf(" %d", linit14[i]);
950 printf("\n");
951 for(i=0;i<10;i++) printf(" %d", linit15[i]);
952 printf("\n");
953 printf("%d %d %d %d\n",
954 linit16.a1,
955 linit16.a2,
956 linit16.a3,
957 linit16.a4);
960 void switch_test()
962 int i;
964 for(i=0;i<15;i++) {
965 switch(i) {
966 case 0:
967 case 1:
968 printf("a");
969 break;
970 default:
971 printf("%d", i);
972 break;
973 case 8 ... 12:
974 printf("c");
975 break;
976 case 3:
977 printf("b");
978 break;
981 printf("\n");
984 /* ISOC99 _Bool type */
985 void c99_bool_test(void)
987 #ifdef BOOL_ISOC99
988 int a;
989 _Bool b;
991 printf("bool_test:\n");
992 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
993 a = 3;
994 printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a);
995 b = 3;
996 printf("b = %d\n", b);
997 b++;
998 printf("b = %d\n", b);
999 #endif
1002 void bitfield_test(void)
1004 int a;
1005 struct sbf1 {
1006 int f1 : 3;
1007 int : 2;
1008 int f2 : 1;
1009 int : 0;
1010 int f3 : 5;
1011 int f4 : 7;
1012 unsigned int f5 : 7;
1013 } st1;
1014 printf("bitfield_test:");
1015 printf("sizeof(st1) = %d\n", sizeof(st1));
1017 st1.f1 = 3;
1018 st1.f2 = 1;
1019 st1.f3 = 15;
1020 a = 120;
1021 st1.f4 = a;
1022 st1.f5 = a;
1023 st1.f5++;
1024 printf("%d %d %d %d %d\n",
1025 st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
1028 #define FTEST(prefix, type, fmt)\
1029 void prefix ## cmp(type a, type b)\
1031 printf("%d %d %d %d %d %d\n",\
1032 a == b,\
1033 a != b,\
1034 a < b,\
1035 a > b,\
1036 a >= b,\
1037 a <= b);\
1038 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1041 a + b,\
1042 a - b,\
1043 a * b,\
1044 a / b,\
1045 -a);\
1046 printf(fmt "\n", ++a);\
1047 printf(fmt "\n", a++);\
1048 printf(fmt "\n", a);\
1050 void prefix ## fcast(type a)\
1052 float fa;\
1053 double da;\
1054 long double la;\
1055 int ia;\
1056 unsigned int ua;\
1057 type b;\
1058 fa = a;\
1059 da = a;\
1060 la = a;\
1061 printf("ftof: %f %f %Lf\n", fa, da, la);\
1062 ia = (int)a;\
1063 ua = (unsigned int)a;\
1064 printf("ftoi: %d %u\n", ia, ua);\
1065 ia = -1234;\
1066 ua = 0x81234500;\
1067 b = ia;\
1068 printf("itof: " fmt "\n", b);\
1069 b = ua;\
1070 printf("utof: " fmt "\n", b);\
1073 void prefix ## test(void)\
1075 printf("testing '%s'\n", #type);\
1076 prefix ## cmp(1, 2.5);\
1077 prefix ## cmp(2, 1.5);\
1078 prefix ## cmp(1, 1);\
1079 prefix ## fcast(234.6);\
1080 prefix ## fcast(-2334.6);\
1083 FTEST(f, float, "%f")
1084 FTEST(d, double, "%f")
1085 FTEST(ld, long double, "%Lf")
1087 double ftab1[3] = { 1.2, 3.4, -5.6 };
1090 void float_test(void)
1092 float fa, fb;
1093 double da, db;
1094 int a;
1095 unsigned int b;
1097 printf("float_test:\n");
1098 printf("sizeof(float) = %d\n", sizeof(float));
1099 printf("sizeof(double) = %d\n", sizeof(double));
1100 printf("sizeof(long double) = %d\n", sizeof(long double));
1101 ftest();
1102 dtest();
1103 ldtest();
1104 printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]);
1105 printf("%f %f %f\n", 2.12, .5, 2.3e10);
1106 // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10);
1107 da = 123;
1108 printf("da=%f\n", da);
1109 fa = 123;
1110 printf("fa=%f\n", fa);
1111 a = 4000000000;
1112 da = a;
1113 printf("da = %f\n", da);
1114 b = 4000000000;
1115 db = b;
1116 printf("db = %f\n", db);
1119 int fib(int n)
1121 if (n <= 2)
1122 return 1;
1123 else
1124 return fib(n-1) + fib(n-2);
1127 void funcptr_test()
1129 void (*func)(int);
1130 int a;
1131 struct {
1132 int dummy;
1133 void (*func)(int);
1134 } st1;
1136 printf("funcptr:\n");
1137 func = &num;
1138 (*func)(12345);
1139 func = num;
1140 a = 1;
1141 a = 1;
1142 func(12345);
1143 /* more complicated pointer computation */
1144 st1.func = num;
1145 st1.func(12346);
1146 printf("sizeof1 = %d\n", sizeof(funcptr_test));
1147 printf("sizeof2 = %d\n", sizeof funcptr_test);
1148 printf("sizeof3 = %d\n", sizeof(&funcptr_test));
1149 printf("sizeof4 = %d\n", sizeof &funcptr_test);
1152 void lloptest(long long a, long long b)
1154 unsigned long long ua, ub;
1156 ua = a;
1157 ub = b;
1158 /* arith */
1159 printf("arith: %Ld %Ld %Ld %Ld %Ld\n",
1160 a + b,
1161 a - b,
1162 a * b,
1163 a / b,
1164 a % b);
1166 /* binary */
1167 printf("bin: %Ld %Ld %Ld\n",
1168 a & b,
1169 a | b,
1170 a ^ b);
1172 /* tests */
1173 printf("test: %d %d %d %d %d %d\n",
1174 a == b,
1175 a != b,
1176 a < b,
1177 a > b,
1178 a >= b,
1179 a <= b);
1181 printf("utest: %d %d %d %d %d %d\n",
1182 ua == ub,
1183 ua != ub,
1184 ua < ub,
1185 ua > ub,
1186 ua >= ub,
1187 ua <= ub);
1189 /* arith2 */
1190 a++;
1191 b++;
1192 printf("arith2: %Ld %Ld\n", a, b);
1193 printf("arith2: %Ld %Ld\n", a++, b++);
1194 printf("arith2: %Ld %Ld\n", --a, --b);
1195 printf("arith2: %Ld %Ld\n", a, b);
1198 void llshift(long long a, int b)
1200 printf("shift: %Ld %Ld %Ld\n",
1201 (unsigned long long)a >> b,
1202 a >> b,
1203 a << b);
1204 printf("shiftc: %Ld %Ld %Ld\n",
1205 (unsigned long long)a >> 3,
1206 a >> 3,
1207 a << 3);
1208 printf("shiftc: %Ld %Ld %Ld\n",
1209 (unsigned long long)a >> 35,
1210 a >> 35,
1211 a << 35);
1214 void llfloat(void)
1216 float fa;
1217 double da;
1218 long double lda;
1219 long long la, lb, lc;
1220 unsigned long long ula, ulb, ulc;
1221 la = 0x12345678;
1222 ula = 0x72345678;
1223 la = (la << 20) | 0x12345;
1224 ula = ula << 33;
1225 printf("la=%Ld ula=%Lu\n", la, ula);
1227 fa = la;
1228 da = la;
1229 lda = la;
1230 printf("lltof: %f %f %Lf\n", fa, da, lda);
1232 la = fa;
1233 lb = da;
1234 lc = lda;
1235 printf("ftoll: %Ld %Ld %Ld\n", la, lb, lc);
1237 fa = ula;
1238 da = ula;
1239 lda = ula;
1240 printf("ulltof: %f %f %Lf\n", fa, da, lda);
1242 ula = fa;
1243 ulb = da;
1244 ulc = lda;
1245 printf("ftoull: %Lu %Lu %Lu\n", ula, ulb, ulc);
1248 long long llfunc1(int a)
1250 return a;
1253 /* currently not supported, except for typing */
1254 void longlong_test(void)
1256 long long a, b, c;
1257 int ia;
1258 unsigned int ua;
1259 printf("longlong_test:\n");
1260 printf("sizeof(long long) = %d\n", sizeof(long long));
1261 ia = -1;
1262 ua = -2;
1263 a = ia;
1264 b = ua;
1265 printf("%Ld %Ld\n", a, b);
1266 printf("%Ld %Ld %Ld %Lx\n",
1267 (long long)1,
1268 (long long)-2,
1269 1LL,
1270 0x1234567812345679);
1271 a = llfunc1(-3);
1272 printf("%Ld\n", a);
1274 lloptest(1000, 23);
1275 lloptest(0xff, 0x1234);
1276 b = 0x72345678 << 10;
1277 lloptest(-3, b);
1278 llshift(0x123, 5);
1279 llshift(-23, 5);
1280 b = 0x72345678 << 10;
1281 llshift(b, 47);
1283 llfloat();
1284 #if 1
1285 b = 0x12345678;
1286 a = -1;
1287 c = a + b;
1288 printf("%Lx\n", c);
1289 #endif