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(void);
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__)
65 /* gcc vararg macros */
66 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
68 #define MACRO_NOARGS()
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
);
99 printf("A defined\n");
102 printf("B defined\n");
105 printf("A defined\n");
107 printf("A not defined\n");
110 printf("B defined\n");
112 printf("B not defined\n");
116 printf("A defined\n");
118 printf("B1 defined\n");
120 printf("B1 not defined\n");
123 printf("A not defined\n");
125 printf("B2 defined\n");
127 printf("B2 not defined\n");
132 printf("test true1\n");
135 printf("test true2\n");
138 printf("test true3\n");
141 printf("test trueA\n");
144 printf("test trueB\n");
159 printf("__LINE__=%d __FILE__=%s\n",
161 /* not strictly preprocessor, but we test it there */
163 printf("__func__ = %s\n", __func__
);
164 dprintf(1, "vaarg=%d\n", 1);
166 dprintf1(1, "vaarg1\n");
167 dprintf1(1, "vaarg1=%d\n", 2);
168 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
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");
209 while ((b
= b
+ 1) < 96) {
213 printf("fib=%d\n", fib(33));
215 while (b
!= 0x80000000) {
228 for(i
= 0; i
< 10;i
++)
237 /* break/continue tests */
249 /* break/continue tests */
261 for(i
= 0;i
< 10;i
++) {
301 printf("enum:\n%d %d %d %d %d %d\n",
302 E0
, E1
, E2
, E3
, E4
, E5
);
304 printf("b1=%d\n", b1
);
315 printf("typedef:\n");
316 printf("a=%d\n", *a
);
321 printf("forward:\n");
327 void forward_ref(void)
329 printf("forward ok\n");
332 typedef struct struct1
{
352 struct struct1 st1
, st2
;
354 int main(int argc
, char **argv
)
372 constant_expr_test();
376 compound_literal_test();
378 struct_assign_test();
394 printf("g1=%d\n", g
);
402 printf("g2=%d\n", g
);
406 printf("g3=%d\n", g
);
410 printf("g4=%d\n", g
);
413 printf("g5=%d\n", g
);
416 void array_test(int a
[4])
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
);
433 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
436 tab2
[i
][j
] = 10 * i
+ j
;
438 printf(" %3d", ((int *)tab2
)[i
]);
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);
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);
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);
493 printf("%d %d %d %d\n",
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
)
515 void vpop(int *ft
, int *fc
)
528 vstack_ptr
[-2] &= ~0xffffff80;
530 printf("res= %d %d\n", a
, b
);
533 void constant_expr_test()
536 printf("constant_expr:\n");
538 printf("%d\n", a
* 16);
539 printf("%d\n", a
* 1);
540 printf("%d\n", a
+ 0);
549 printf("expr_ptr:\n");
552 printf("diff=%d\n", q
- p
);
554 printf("inc=%d\n", p
- tab4
);
556 printf("dec=%d\n", p
- tab4
);
558 printf("inc=%d\n", p
- tab4
);
560 printf("dec=%d\n", p
- tab4
);
561 printf("add=%d\n", p
+ 3 - tab4
);
562 printf("add=%d\n", 3 + p
- tab4
);
568 printf("constant_expr:\n");
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
);
595 printf("sizes: %d %d %d %d\n",
596 sizeof(struct struct1
),
597 sizeof(struct struct2
),
598 sizeof(union union1
),
599 sizeof(union union2
));
603 printf("st1: %d %d %d\n",
604 st1
.f1
, st1
.f2
, st1
.f3
);
607 printf("union1: %d\n", st1
.u
.v1
);
610 printf("union2: %d\n", u
.w1
);
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()
625 printf("char_short:\n");
630 *(char *)&var1
, *(char *)&var2
);
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
);
665 printf("!s=%d\n", !s
);
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);
688 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
690 /* test register spilling */
693 a
= (a
+ b
) * ((a
< b
) ?
694 ((b
- a
) * (a
- b
)): a
+ b
);
700 int *cinit2
= (int []){3, 2, 1};
702 void compound_literal_test(void)
707 printf("compound_test:\n");
709 p
= (int []){1, 2, 3};
715 printf("%d", cinit2
[i
]);
719 printf("q1=%s\n", q
);
721 q
= (char *){ "tralala2" };
722 printf("q2=%s\n", q
);
725 printf("q3=%s\n", q3
);
727 q
= (char []){ "tralala3" };
728 printf("q4=%s\n", q
);
731 p
= (int []){1, 2, cinit1
+ 3};
737 p
= (int []){1, 2, 4 + i
};
760 printf("kr_test:\n");
761 printf("func1=%d\n", kr_func1(3, 4));
762 printf("func2=%d\n", kr_func2(3, 4));
769 tab
= (char*)malloc(20);
785 /* structure assignment tests */
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
)
805 void struct_assign_test(void)
807 struct structa1 lsta1
, lsta2
;
810 printf("struct_assign_test:\n");
814 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
816 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
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
);
844 printf("cast_test:\n");
848 printf("%d %d %d %d\n",
851 (unsigned char)(a
+ 1),
852 (unsigned short)(a
+ 1));
853 printf("%d %d %d %d\n",
856 (unsigned char)0xfffff,
857 (unsigned short)0xfffff);
859 a
= (bcast
= 128) + 1;
861 a
= (scast
= 65536) + 1;
864 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
866 /* test implicit int casting for array accesses */
870 printf("%d %d\n", tab
[0], tab
[1]);
873 /* initializers tests */
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,
900 char *sinit12
= "hello world";
906 char sinit14
[10] = { "abc" };
907 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
913 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
914 int linit6
[] = { 1, 2, 3 };
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",
934 printf("sinit6=%d\n", sizeof(sinit6
));
935 printf("sinit7=%d %d %d %d\n",
941 printf("sinit8=%s\n", sinit8
);
942 printf("sinit9=%d %d %d\n",
947 printf("sinit10=%d %d %d\n",
952 printf("sinit11=%d %d %d %d %d %d\n",
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",
976 printf("sinit14=%s\n", sinit14
);
978 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
980 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
982 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
984 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
986 printf("%d %d %d %d\n",
991 /* test that initialisation is done after variable declare */
992 printf("linit17=%d\n", linit17
);
993 printf("sinit15=%d\n", sinit15
[0]);
1020 /* ISOC99 _Bool type */
1021 void c99_bool_test(void)
1027 printf("bool_test:\n");
1028 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1030 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1032 printf("b = %d\n", b
);
1034 printf("b = %d\n", b
);
1038 void bitfield_test(void)
1048 unsigned int f5
: 7;
1050 printf("bitfield_test:");
1051 printf("sizeof(st1) = %d\n", sizeof(st1
));
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",\
1074 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1082 printf(fmt "\n", ++a);\
1083 printf(fmt "\n", a++);\
1084 printf(fmt "\n", a);\
1086 void prefix ## fcast(type a)\
1097 printf("ftof: %f %f %Lf\n", fa, da, la);\
1099 ua = (unsigned int)a;\
1100 printf("ftoi: %d %u\n", ia, ua);\
1104 printf("itof: " fmt "\n", b);\
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)
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));
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);
1144 printf("da=%f\n", da
);
1146 printf("fa=%f\n", fa
);
1149 printf("da = %f\n", da
);
1152 printf("db = %f\n", db
);
1160 return fib(n
-1) + fib(n
-2);
1172 printf("funcptr:\n");
1179 /* more complicated pointer computation */
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
;
1195 printf("arith: %Ld %Ld %Ld %Ld %Ld\n",
1203 printf("bin: %Ld %Ld %Ld\n",
1209 printf("test: %d %d %d %d %d %d\n",
1217 printf("utest: %d %d %d %d %d %d\n",
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
,
1240 printf("shiftc: %Ld %Ld %Ld\n",
1241 (unsigned long long)a
>> 3,
1244 printf("shiftc: %Ld %Ld %Ld\n",
1245 (unsigned long long)a
>> 35,
1255 long long la
, lb
, lc
;
1256 unsigned long long ula
, ulb
, ulc
;
1259 la
= (la
<< 20) | 0x12345;
1261 printf("la=%Ld ula=%Lu\n", la
, ula
);
1266 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1271 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1276 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1281 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1284 long long llfunc1(int a
)
1289 /* currently not supported, except for typing */
1290 void longlong_test(void)
1295 printf("longlong_test:\n");
1296 printf("sizeof(long long) = %d\n", sizeof(long long));
1301 printf("%Ld %Ld\n", a
, b
);
1302 printf("%Ld %Ld %Ld %Lx\n",
1306 0x1234567812345679);
1311 lloptest(0xff, 0x1234);
1312 b
= 0x72345678 << 10;
1316 b
= 0x72345678 << 10;