fix-mixed-struct (patch by Pip Cet)
[tinycc.git] / tests / boundtest.c
blob15bffb4ed7108f3e529fa54a2a2cbcf2c4b27481
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 /* XXX: currently: bug */
85 int test6(void)
87 int i, sum = 0;
88 int *tab4;
90 tab4 = malloc(20 * sizeof(int));
91 free(tab4);
92 for(i=0;i<21;i++) {
93 sum += tab4[i];
96 return sum;
99 /* error */
100 int test7(void)
102 int i, sum = 0;
103 int *p;
105 for(i=0;i<TAB_SIZE + 1;i++) {
106 p = &tab[i];
107 if (i == TAB_SIZE)
108 printf("i=%d %x\n", i, p);
109 sum += *p;
111 return sum;
114 /* ok */
115 int test8(void)
117 int i, sum = 0;
118 int tab[10];
120 for(i=0;i<10;i++) {
121 sum += tab[i];
123 return sum;
126 /* error */
127 int test9(void)
129 int i, sum = 0;
130 char tab[10];
132 for(i=0;i<11;i++) {
133 sum += tab[i];
135 return sum;
138 /* ok */
139 int test10(void)
141 char tab[10];
142 char tab1[10];
144 memset(tab, 0, 10);
145 memcpy(tab, tab1, 10);
146 memmove(tab, tab1, 10);
147 return 0;
150 /* error */
151 int test11(void)
153 char tab[10];
155 memset(tab, 0, 11);
156 return 0;
159 /* error */
160 int test12(void)
162 void *ptr;
163 ptr = malloc(10);
164 free(ptr);
165 free(ptr);
166 return 0;
169 /* error */
170 int test13(void)
172 char pad1 = 0;
173 char tab[10];
174 char pad2 = 0;
175 memset(tab, 'a', sizeof(tab));
176 return strlen(tab);
179 int test14(void)
181 char *p = alloca(TAB_SIZE);
182 memset(p, 'a', TAB_SIZE);
183 p[TAB_SIZE-1] = 0;
184 return strlen(p);
187 /* error */
188 int test15(void)
190 char *p = alloca(TAB_SIZE-1);
191 memset(p, 'a', TAB_SIZE);
192 p[TAB_SIZE-1] = 0;
193 return strlen(p);
196 /* ok */
197 int test16()
199 char *demo = "This is only a test.";
200 char *p;
202 fprintf(stderr, "%s start\n", __FUNCTION__);
204 p = alloca(16);
205 strcpy(p,"12345678901234");
206 printf("alloca: p is %s\n", p);
208 /* Test alloca embedded in a larger expression */
209 printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) );
211 fprintf(stderr, "%s end\n", __FUNCTION__);
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__);
232 int (*table_test[])(void) = {
233 test1,
234 test2,
235 test3,
236 test4,
237 test5,
238 test6,
239 test7,
240 test8,
241 test9,
242 test10,
243 test11,
244 test12,
245 test13,
246 test14,
247 test15,
248 test16,
249 test17,
252 int main(int argc, char **argv)
254 int index;
255 int (*ftest)(void);
256 int index_max = sizeof(table_test)/sizeof(table_test[0]);
258 if (argc < 2) {
259 printf(
260 "test TCC bound checking system\n"
261 "usage: boundtest N\n"
262 " 1 <= N <= %d\n", index_max);
263 exit(1);
266 index = 0;
267 if (argc >= 2)
268 index = atoi(argv[1]) - 1;
270 if ((index < 0) || (index >= index_max)) {
271 printf("N is outside of the valid range (%d)\n", index);
272 exit(2);
275 /* well, we also use bounds on this ! */
276 ftest = table_test[index];
277 ftest();
279 return 0;
283 * without bound 0.77 s
284 * with bounds 4.73