2 * TCC auto test program
5 /* Unfortunately, gcc version < 3 does not handle that! */
8 /* only gcc 3 handles _Bool correctly */
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"
22 #include TCCLIB_INC1.TCCLIB_INC2
24 #include TCCLIB_INC1.h>
26 #include TCCLIB_INC3 ".h"
48 void constant_expr_test();
50 void char_short_test();
52 void compound_literal_test(void);
54 void struct_assign_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);
66 void forward_ref(void);
72 #define M1(a, b) (a) + (b)
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))
83 #define dprintf(level,...) printf(__VA_ARGS__)
86 /* gcc vararg macros */
87 #define dprintf1(level, fmt, args...) printf(fmt, ## args)
89 #define MACRO_NOARGS()
107 printf("macro:\n");\f\v
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
);
120 printf("A defined\n");
123 printf("B defined\n");
126 printf("A defined\n");
128 printf("A not defined\n");
131 printf("B defined\n");
133 printf("B not defined\n");
137 printf("A defined\n");
139 printf("B1 defined\n");
141 printf("B1 not defined\n");
144 printf("A not defined\n");
146 printf("B2 defined\n");
148 printf("B2 not defined\n");
153 printf("test true1\n");
156 printf("test true2\n");
159 printf("test true3\n");
162 printf("test trueA\n");
165 printf("test trueB\n");
181 printf("__LINE__ defined\n");
184 printf("__LINE__=%d __FILE__=%s\n",
187 printf("__LINE__=%d __FILE__=%s\n",
190 printf("__LINE__=%d __FILE__=%s\n",
192 #line 185 "tcctest.c"
194 /* not strictly preprocessor, but we test it there */
196 printf("__func__ = %s\n", __func__
);
197 dprintf(1, "vaarg=%d\n", 1);
199 dprintf1(1, "vaarg1\n");
200 dprintf1(1, "vaarg1=%d\n", 2);
201 dprintf1(1, "vaarg1=%d %d\n", 1, 2);
204 printf("func='%s'\n", __FUNCTION__
);
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');
247 while ((b
= b
+ 1) < 96) {
251 printf("fib=%d\n", fib(33));
253 while (b
!= 0x80000000) {
266 for(i
= 0; i
< 10;i
++)
275 /* break/continue tests */
287 /* break/continue tests */
299 for(i
= 0;i
< 10;i
++) {
339 printf("enum:\n%d %d %d %d %d %d\n",
340 E0
, E1
, E2
, E3
, E4
, E5
);
342 printf("b1=%d\n", b1
);
353 printf("typedef:\n");
354 printf("a=%d\n", *a
);
359 printf("forward:\n");
365 void forward_ref(void)
367 printf("forward ok\n");
370 typedef struct struct1
{
390 struct struct1 st1
, st2
;
392 int main(int argc
, char **argv
)
410 constant_expr_test();
414 compound_literal_test();
416 struct_assign_test();
435 printf("g1=%d\n", g
);
443 printf("g2=%d\n", g
);
447 printf("g3=%d\n", g
);
451 printf("g4=%d\n", g
);
454 printf("g5=%d\n", g
);
457 void array_test(int a
[4])
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"));
471 printf("sizeof(__func__) = %d\n", sizeof(__func__
));
473 printf("sizeof tab %d\n", sizeof(tab
));
474 printf("sizeof tab2 %d\n", sizeof tab2
);
478 printf("%d %d %d\n", tab
[0], tab
[1], tab
[2]);
481 tab2
[i
][j
] = 10 * i
+ j
;
483 printf(" %3d", ((int *)tab2
)[i
]);
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);
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);
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);
538 printf("%d %d %d %d\n",
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
)
560 void vpop(int *ft
, int *fc
)
573 vstack_ptr
[-2] &= ~0xffffff80;
575 printf("res= %d %d\n", a
, b
);
578 void constant_expr_test()
581 printf("constant_expr:\n");
583 printf("%d\n", a
* 16);
584 printf("%d\n", a
* 1);
585 printf("%d\n", a
+ 0);
594 printf("expr_ptr:\n");
597 printf("diff=%d\n", q
- p
);
599 printf("inc=%d\n", p
- tab4
);
601 printf("dec=%d\n", p
- tab4
);
603 printf("inc=%d\n", p
- tab4
);
605 printf("dec=%d\n", p
- tab4
);
606 printf("add=%d\n", p
+ 3 - tab4
);
607 printf("add=%d\n", 3 + p
- tab4
);
613 printf("constant_expr:\n");
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
);
640 printf("sizes: %d %d %d %d\n",
641 sizeof(struct struct1
),
642 sizeof(struct struct2
),
643 sizeof(union union1
),
644 sizeof(union union2
));
648 printf("st1: %d %d %d\n",
649 st1
.f1
, st1
.f2
, st1
.f3
);
652 printf("union1: %d\n", st1
.u
.v1
);
655 printf("union2: %d\n", u
.w1
);
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()
670 printf("char_short:\n");
675 *(char *)&var1
, *(char *)&var2
);
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
);
710 printf("!s=%d\n", !s
);
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);
733 printf("b=%d\n", a
+ (0 ? 1 : a
/ 2));
735 /* test register spilling */
738 a
= (a
+ b
) * ((a
< b
) ?
739 ((b
- a
) * (a
- b
)): a
+ b
);
742 /* test complex || or && expressions */
746 printf("exp=%d\n", f
== (32 <= a
&& a
<= 3));
747 printf("r=%d\n", (t
|| f
) + (t
&& f
));
752 int aspect_native
= 65536;
753 double bfu_aspect
= 1.0;
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
);
764 int *cinit2
= (int []){3, 2, 1};
766 void compound_literal_test(void)
771 printf("compound_test:\n");
773 p
= (int []){1, 2, 3};
779 printf("%d", cinit2
[i
]);
783 printf("q1=%s\n", q
);
785 q
= (char *){ "tralala2" };
786 printf("q2=%s\n", q
);
789 printf("q3=%s\n", q3
);
791 q
= (char []){ "tralala3" };
792 printf("q4=%s\n", q
);
795 p
= (int []){1, 2, cinit1
+ 3};
801 p
= (int []){1, 2, 4 + i
};
824 printf("kr_test:\n");
825 printf("func1=%d\n", kr_func1(3, 4));
826 printf("func2=%d\n", kr_func2(3, 4));
833 tab
= (char*)malloc(20);
849 /* structure assignment tests */
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
)
869 void struct_assign_test(void)
871 struct structa1 lsta1
, lsta2
;
874 printf("struct_assign_test:\n");
878 printf("%d %d\n", lsta1
.f1
, lsta1
.f2
);
880 printf("%d %d\n", lsta2
.f1
, lsta2
.f2
);
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
);
908 printf("cast_test:\n");
912 printf("%d %d %d %d\n",
915 (unsigned char)(a
+ 1),
916 (unsigned short)(a
+ 1));
917 printf("%d %d %d %d\n",
920 (unsigned char)0xfffff,
921 (unsigned short)0xfffff);
923 a
= (bcast
= 128) + 1;
925 a
= (scast
= 65536) + 1;
928 printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c
), sizeof((int)c
));
930 /* test implicit int casting for array accesses */
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 */
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,
969 char *sinit12
= "hello world";
975 char sinit14
[10] = { "abc" };
976 int sinit15
[3] = { sizeof(sinit15
), 1, 2 };
982 int linit4
[3][2] = { {1, 2}, {3, 4}, {5, 6} };
983 int linit6
[] = { 1, 2, 3 };
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",
1003 printf("sinit6=%d\n", sizeof(sinit6
));
1004 printf("sinit7=%d %d %d %d\n",
1010 printf("sinit8=%s\n", sinit8
);
1011 printf("sinit9=%d %d %d\n",
1016 printf("sinit10=%d %d %d\n",
1021 printf("sinit11=%d %d %d %d %d %d\n",
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",
1045 printf("sinit14=%s\n", sinit14
);
1047 for(i
=0;i
<10;i
++) printf(" %d", linit12
[i
]);
1049 for(i
=0;i
<10;i
++) printf(" %d", linit13
[i
]);
1051 for(i
=0;i
<10;i
++) printf(" %d", linit14
[i
]);
1053 for(i
=0;i
<10;i
++) printf(" %d", linit15
[i
]);
1055 printf("%d %d %d %d\n",
1060 /* test that initialisation is done after variable declare */
1061 printf("linit17=%d\n", linit17
);
1062 printf("sinit15=%d\n", sinit15
[0]);
1089 /* ISOC99 _Bool type */
1090 void c99_bool_test(void)
1096 printf("bool_test:\n");
1097 printf("sizeof(_Bool) = %d\n", sizeof(_Bool
));
1099 printf("cast: %d %d %d\n", (_Bool
)10, (_Bool
)0, (_Bool
)a
);
1101 printf("b = %d\n", b
);
1103 printf("b = %d\n", b
);
1107 void bitfield_test(void)
1117 unsigned int f5
: 7;
1119 printf("bitfield_test:");
1120 printf("sizeof(st1) = %d\n", sizeof(st1
));
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",\
1143 printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\
1151 printf(fmt "\n", ++a);\
1152 printf(fmt "\n", a++);\
1153 printf(fmt "\n", a);\
1155 void prefix ## fcast(type a)\
1166 printf("ftof: %f %f %Lf\n", fa, da, la);\
1168 ua = (unsigned int)a;\
1169 printf("ftoi: %d %u\n", ia, ua);\
1173 printf("itof: " fmt "\n", b);\
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)
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));
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);
1213 printf("da=%f\n", da
);
1215 printf("fa=%f\n", fa
);
1218 printf("da = %f\n", da
);
1221 printf("db = %f\n", db
);
1229 return fib(n
-1) + fib(n
-2);
1241 printf("funcptr:\n");
1248 /* more complicated pointer computation */
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
;
1264 printf("arith: %Ld %Ld %Ld %Ld %Ld\n",
1272 printf("bin: %Ld %Ld %Ld\n",
1278 printf("test: %d %d %d %d %d %d\n",
1286 printf("utest: %d %d %d %d %d %d\n",
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
,
1309 printf("shiftc: %Ld %Ld %Ld\n",
1310 (unsigned long long)a
>> 3,
1313 printf("shiftc: %Ld %Ld %Ld\n",
1314 (unsigned long long)a
>> 35,
1324 long long la
, lb
, lc
;
1325 unsigned long long ula
, ulb
, ulc
;
1328 la
= (la
<< 20) | 0x12345;
1330 printf("la=%Ld ula=%Lu\n", la
, ula
);
1335 printf("lltof: %f %f %Lf\n", fa
, da
, lda
);
1340 printf("ftoll: %Ld %Ld %Ld\n", la
, lb
, lc
);
1345 printf("ulltof: %f %f %Lf\n", fa
, da
, lda
);
1350 printf("ftoull: %Lu %Lu %Lu\n", ula
, ulb
, ulc
);
1353 long long llfunc1(int a
)
1358 /* currently not supported, except for typing */
1359 void longlong_test(void)
1364 printf("longlong_test:\n");
1365 printf("sizeof(long long) = %d\n", sizeof(long long));
1370 printf("%Ld %Ld\n", a
, b
);
1371 printf("%Ld %Ld %Ld %Lx\n",
1375 0x1234567812345679);
1380 lloptest(0xff, 0x1234);
1381 b
= 0x72345678 << 10;
1385 b
= 0x72345678 << 10;
1397 void vprintf1(const char *fmt
, ...)
1419 i
= va_arg(ap
, int);
1423 d
= va_arg(ap
, double);
1427 ll
= va_arg(ap
, long long);
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)
1454 ntf("whitspace:\n");\f\v
1463 printf("len1=%d\n", strlen("
1467 printf("len1=%d str[0]=%d\n", strlen(str
), str
[0]);
1468 printf("len1=%d\n", strlen("
a
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
);