Fix compile warning with cross compilers
[tinycc.git] / tests / boundtest.c
bloba7ab800df946c83aa611a6e2216b1c9614a4f50f
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
5 #define NB_ITS 1000000
6 //#define NB_ITS 1
7 #define TAB_SIZE 100
9 int tab[TAB_SIZE];
10 int ret_sum;
11 char tab3[256];
13 int test1(void)
15 int i, sum = 0;
16 for(i=0;i<TAB_SIZE;i++) {
17 sum += tab[i];
19 return sum;
22 /* error */
23 int test2(void)
25 int i, sum = 0;
26 for(i=0;i<TAB_SIZE + 1;i++) {
27 sum += tab[i];
29 return sum;
32 /* actually, profiling test */
33 int test3(void)
35 int sum;
36 int i, it;
38 sum = 0;
39 for(it=0;it<NB_ITS;it++) {
40 for(i=0;i<TAB_SIZE;i++) {
41 sum += tab[i];
44 return sum;
47 /* ok */
48 int test4(void)
50 int i, sum = 0;
51 int *tab4;
53 fprintf(stderr, "%s start\n", __FUNCTION__);
55 tab4 = malloc(20 * sizeof(int));
56 for(i=0;i<20;i++) {
57 sum += tab4[i];
59 free(tab4);
61 fprintf(stderr, "%s end\n", __FUNCTION__);
62 return sum;
65 /* error */
66 int test5(void)
68 int i, sum = 0;
69 int *tab4;
71 fprintf(stderr, "%s start\n", __FUNCTION__);
73 tab4 = malloc(20 * sizeof(int));
74 for(i=0;i<21;i++) {
75 sum += tab4[i];
77 free(tab4);
79 fprintf(stderr, "%s end\n", __FUNCTION__);
80 return sum;
83 /* error */
84 int test6(void)
86 int i, sum = 0;
87 int *tab4;
89 tab4 = malloc(20 * sizeof(int));
90 free(tab4);
91 for(i=0;i<21;i++) {
92 sum += tab4[i];
95 return sum;
98 /* error */
99 int test7(void)
101 int i, sum = 0;
102 int *p;
104 for(i=0;i<TAB_SIZE + 1;i++) {
105 p = &tab[i];
106 if (i == TAB_SIZE)
107 printf("i=%d %x\n", i, p);
108 sum += *p;
110 return sum;
113 /* ok */
114 int test8(void)
116 int i, sum = 0;
117 int tab[10];
119 for(i=0;i<10;i++) {
120 sum += tab[i];
122 return sum;
125 /* error */
126 int test9(void)
128 int i, sum = 0;
129 char tab[10];
131 for(i=0;i<11;i++) {
132 sum += tab[i];
134 return sum;
137 /* ok */
138 int test10(void)
140 char tab[10];
141 char tab1[10];
143 memset(tab, 0, 10);
144 memcpy(tab, tab1, 10);
145 memmove(tab, tab1, 10);
146 return 0;
149 /* error */
150 int test11(void)
152 char tab[10];
154 memset(tab, 0, 11);
155 return 0;
158 /* error */
159 int test12(void)
161 void *ptr;
162 ptr = malloc(10);
163 free(ptr);
164 free(ptr);
165 return 0;
168 /* error */
169 int test13(void)
171 char pad1 = 0;
172 char tab[10];
173 char pad2 = 0;
174 memset(tab, 'a', sizeof(tab));
175 return strlen(tab);
178 int test14(void)
180 char *p = alloca(TAB_SIZE);
181 memset(p, 'a', TAB_SIZE);
182 p[TAB_SIZE-1] = 0;
183 return strlen(p);
186 /* error */
187 int test15(void)
189 char *p = alloca(TAB_SIZE-1);
190 memset(p, 'a', TAB_SIZE);
191 p[TAB_SIZE-1] = 0;
192 return strlen(p);
195 /* ok */
196 int test16()
198 char *demo = "This is only a test.";
199 char *p;
201 fprintf(stderr, "%s start\n", __FUNCTION__);
203 p = alloca(16);
204 strcpy(p,"12345678901234");
205 printf("alloca: p is %s\n", p);
207 /* Test alloca embedded in a larger expression */
208 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
210 fprintf(stderr, "%s end\n", __FUNCTION__);
211 return 0;
214 /* error */
215 int test17()
217 char *demo = "This is only a test.";
218 char *p;
220 fprintf(stderr, "%s start\n", __FUNCTION__);
222 p = alloca(16);
223 strcpy(p,"12345678901234");
224 printf("alloca: p is %s\n", p);
226 /* Test alloca embedded in a larger expression */
227 printf("alloca: %s\n", strcpy(alloca(strlen(demo)),demo) );
229 fprintf(stderr, "%s end\n", __FUNCTION__);
230 return 0;
233 int (*table_test[])(void) = {
234 test1,
235 test2,
236 test3,
237 test4,
238 test5,
239 test6,
240 test7,
241 test8,
242 test9,
243 test10,
244 test11,
245 test12,
246 test13,
247 test14,
248 test15,
249 test16,
250 test17,
253 int main(int argc, char **argv)
255 int i;
256 char *cp;
257 int index;
258 int (*ftest)(void);
259 int index_max = sizeof(table_test)/sizeof(table_test[0]);
261 /* check bounds checking main arg */
262 for (i = 0; i < argc; i++) {
263 cp = argv[i];
264 while (*cp) {
265 cp++;
269 if (argc < 2) {
270 printf(
271 "test TCC bound checking system\n"
272 "usage: boundtest N\n"
273 " 1 <= N <= %d\n", index_max);
274 exit(1);
277 index = 0;
278 if (argc >= 2)
279 index = atoi(argv[1]) - 1;
281 if ((index < 0) || (index >= index_max)) {
282 printf("N is outside of the valid range (%d)\n", index);
283 exit(2);
286 /* well, we also use bounds on this ! */
287 ftest = table_test[index];
288 ftest();
290 return 0;
294 * without bound 0.77 s
295 * with bounds 4.73