2 * TCC auto test program
6 /* Unfortunately, gcc version < 3 does not handle that! */
9 /* only gcc 3 handles _Bool correctly */
30 void constant_expr_test();
32 void char_short_test();
34 void compound_literal_test(void);
36 void struct_assign_test();
38 void bitfield_test(void);
39 void c99_bool_test(void);
40 void float_test(void);
41 void longlong_test(void);
45 void forward_ref(void);
51 #define M1(a, b) (a) + (b)
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))
62 #define dprintf(level,...) printf(__VA_ARGS__)
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
);
94 printf("A defined\n");
97 printf("B defined\n");
100 printf("A defined\n");
102 printf("A not defined\n");
105 printf("B defined\n");
107 printf("B not defined\n");
111 printf("A defined\n");
113 printf("B1 defined\n");
115 printf("B1 not defined\n");
118 printf("A not defined\n");
120 printf("B2 defined\n");
122 printf("B2 not defined\n");
127 printf("test true1\n");
130 printf("test true2\n");
133 printf("test true3\n");
136 printf("test trueA\n");
139 printf("test trueB\n");
152 printf("__LINE__=%d __FILE__=%s\n",
154 /* not strictly preprocessor, but we test it there */
156 printf("__func__ = %s\n", __func__
);
157 dprintf(1, "vaarg=%d\n", 1);
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");
199 while ((b
= b
+ 1) < 96) {
203 printf("fib=%d\n", fib(33));
205 while (b
!= 0x80000000) {
218 for(i
= 0; i
< 10;i
++)
227 /* break/continue tests */
239 /* break/continue tests */
251 for(i
= 0;i
< 10;i
++) {
291 printf("enum:\n%d %d %d %d %d %d\n",
292 E0
, E1
, E2
, E3
, E4
, E5
);
294 printf("b1=%d\n", b1
);
305 printf("typedef:\n");
306 printf("a=%d\n", *a
);
311 printf("forward:\n");
317 void forward_ref(void)
319 printf("forward ok\n");
322 typedef struct struct1
{
342 struct struct1 st1
, st2
;
344 int main(int argc
, char **argv
)
362 constant_expr_test();
366 compound_literal_test();
368 struct_assign_test();
384 printf("g1=%d\n", g
);
392 printf("g2=%d\n", g
);
396 printf("g3=%d\n", g
);
400 printf("g4=%d\n", g
);
403 printf("g5=%d\n", g
);
406 void array_test(int a
[4])
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
);
423 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
426 tab2
[i
][j
] = 10 * i
+ j
;
428 printf(" %3d", ((int *)tab2
)[i
]);
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);
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);
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);
483 printf("%d %d %d %d\n",
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
)
505 void vpop(int *ft
, int *fc
)
518 vstack_ptr
[-2] &= ~0xffffff80;
520 printf("res= %d %d\n", a
, b
);
523 void constant_expr_test()
526 printf("constant_expr:\n");
528 printf("%d\n", a
* 16);
529 printf("%d\n", a
* 1);
530 printf("%d\n", a
+ 0);
539 printf("expr_ptr:\n");
542 printf("diff=%d\n", q
- p
);
544 printf("inc=%d\n", p
- tab4
);
546 printf("dec=%d\n", p
- tab4
);
548 printf("inc=%d\n", p
- tab4
);
550 printf("dec=%d\n", p
- tab4
);
551 printf("add=%d\n", p
+ 3 - tab4
);
552 printf("add=%d\n", 3 + p
- tab4
);
558 printf("constant_expr:\n");
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
);
585 printf("sizes: %d %d %d %d\n",
586 sizeof(struct struct1
),
587 sizeof(struct struct2
),
588 sizeof(union union1
),
589 sizeof(union union2
));
593 printf("st1: %d %d %d\n",
594 st1
.f1
, st1
.f2
, st1
.f3
);
597 printf("union1: %d\n", st1
.u
.v1
);
600 printf("union2: %d\n", u
.w1
);
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()
615 printf("char_short:\n");
620 *(char *)&var1
, *(char *)&var2
);
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
);
655 printf("!s=%d\n", !s
);
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);
678 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
683 int *cinit2
= (int []){3, 2, 1};
685 void compound_literal_test(void)
690 printf("compound_test:\n");
692 p
= (int []){1, 2, 3};
698 printf("%d", cinit2
[i
]);
702 printf("q1=%s\n", q
);
704 q
= (char *){ "tralala2" };
705 printf("q2=%s\n", q
);
708 printf("q3=%s\n", q3
);
710 q
= (char []){ "tralala3" };
711 printf("q4=%s\n", q
);
714 p
= (int []){1, 2, cinit1
+ 3};
720 p
= (int []){1, 2, 4 + i
};
743 printf("kr_test:\n");
744 printf("func1=%d\n", kr_func1(3, 4));
745 printf("func2=%d\n", kr_func2(3, 4));
752 tab
= (char*)malloc(20);
768 /* structure assignment tests */
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
)
788 void struct_assign_test()
790 struct structa1 lsta1
, lsta2
;
792 printf("struct_assign_test:\n");
796 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
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
);
821 printf("cast_test:\n");
825 printf("%d %d %d %d\n",
828 (unsigned char)(a
+ 1),
829 (unsigned short)(a
+ 1));
830 printf("%d %d %d %d\n",
833 (unsigned char)0xfffff,
834 (unsigned short)0xfffff);
836 a
= (bcast
= 128) + 1;
838 a
= (scast
= 65536) + 1;
842 /* initializers tests */
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,
869 char *sinit12
= "hello world";
875 char sinit14
[10] = { "abc" };
881 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
882 int linit6
[] = { 1, 2, 3 };
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",
901 printf("sinit6=%d\n", sizeof(sinit6
));
902 printf("sinit7=%d %d %d %d\n",
908 printf("sinit8=%s\n", sinit8
);
909 printf("sinit9=%d %d %d\n",
914 printf("sinit10=%d %d %d\n",
919 printf("sinit11=%d %d %d %d %d %d\n",
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",
943 printf("sinit14=%s\n", sinit14
);
945 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
947 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
949 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
951 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
953 printf("%d %d %d %d\n",
984 /* ISOC99 _Bool type */
985 void c99_bool_test(void)
991 printf("bool_test:\n");
992 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
994 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
996 printf("b = %d\n", b
);
998 printf("b = %d\n", b
);
1002 void bitfield_test(void)
1012 unsigned int f5
: 7;
1014 printf("bitfield_test:");
1015 printf("sizeof(st1) = %d\n", sizeof(st1
));
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",\
1038 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1046 printf(fmt "\n", ++a);\
1047 printf(fmt "\n", a++);\
1048 printf(fmt "\n", a);\
1050 void prefix ## fcast(type a)\
1061 printf("ftof: %f %f %Lf\n", fa, da, la);\
1063 ua = (unsigned int)a;\
1064 printf("ftoi: %d %u\n", ia, ua);\
1068 printf("itof: " fmt "\n", b);\
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)
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));
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);
1108 printf("da=%f\n", da
);
1110 printf("fa=%f\n", fa
);
1113 printf("da = %f\n", da
);
1116 printf("db = %f\n", db
);
1124 return fib(n
-1) + fib(n
-2);
1136 printf("funcptr:\n");
1143 /* more complicated pointer computation */
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
;
1159 printf("arith: %Ld %Ld %Ld %Ld %Ld\n",
1167 printf("bin: %Ld %Ld %Ld\n",
1173 printf("test: %d %d %d %d %d %d\n",
1181 printf("utest: %d %d %d %d %d %d\n",
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
,
1204 printf("shiftc: %Ld %Ld %Ld\n",
1205 (unsigned long long)a
>> 3,
1208 printf("shiftc: %Ld %Ld %Ld\n",
1209 (unsigned long long)a
>> 35,
1219 long long la
, lb
, lc
;
1220 unsigned long long ula
, ulb
, ulc
;
1223 la
= (la
<< 20) | 0x12345;
1225 printf("la=%Ld ula=%Lu\n", la
, ula
);
1230 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1235 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1240 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1245 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1248 long long llfunc1(int a
)
1253 /* currently not supported, except for typing */
1254 void longlong_test(void)
1259 printf("longlong_test:\n");
1260 printf("sizeof(long long) = %d\n", sizeof(long long));
1265 printf("%Ld %Ld\n", a
, b
);
1266 printf("%Ld %Ld %Ld %Lx\n",
1270 0x1234567812345679);
1275 lloptest(0xff, 0x1234);
1276 b
= 0x72345678 << 10;
1280 b
= 0x72345678 << 10;