Indentation was missing after empty string check was added by wanjochan.
[tinycc.git] / tests / boundtest.c
blob0d833950d404415510011e71187f69239f846d08
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 tab4 = malloc(20 * sizeof(int));
54 for(i=0;i<20;i++) {
55 sum += tab4[i];
57 free(tab4);
59 return sum;
62 /* error */
63 int test5(void)
65 int i, sum = 0;
66 int *tab4;
68 tab4 = malloc(20 * sizeof(int));
69 for(i=0;i<21;i++) {
70 sum += tab4[i];
72 free(tab4);
74 return sum;
77 /* error */
78 int test6(void)
80 int i, sum = 0;
81 int *tab4;
83 tab4 = malloc(20 * sizeof(int));
84 free(tab4);
85 for(i=0;i<21;i++) {
86 sum += tab4[i];
89 return sum;
92 /* error */
93 int test7(void)
95 int i, sum = 0;
96 int *p;
98 for(i=0;i<TAB_SIZE + 1;i++) {
99 p = &tab[i];
100 if (i == TAB_SIZE)
101 printf("i=%d %x\n", i, p);
102 sum += *p;
104 return sum;
107 /* ok */
108 int test8(void)
110 int i, sum = 0;
111 int tab[10];
113 for(i=0;i<10;i++) {
114 sum += tab[i];
116 return sum;
119 /* error */
120 int test9(void)
122 int i, sum = 0;
123 char tab[10];
125 for(i=0;i<11;i++) {
126 sum += tab[i];
128 return sum;
131 /* ok */
132 int test10(void)
134 char tab[10];
135 char tab1[10];
137 memset(tab, 0, 10);
138 memcpy(tab, tab1, 10);
139 memmove(tab, tab1, 10);
140 return 0;
143 /* error */
144 int test11(void)
146 char tab[10];
148 memset(tab, 0, 11);
149 return 0;
152 /* error */
153 int test12(void)
155 void *ptr;
156 ptr = malloc(10);
157 free(ptr);
158 free(ptr);
159 return 0;
162 /* error */
163 int test13(void)
165 char pad1 = 0;
166 char tab[10];
167 char pad2 = 0;
168 memset(tab, 'a', sizeof(tab));
169 return strlen(tab);
172 #if defined __i386__ || defined __x86_64__
173 #define allocf(x)
174 #else
175 #define alloca(x) malloc(x)
176 #define allocf(x) free(x)
177 #endif
179 int test14(void)
181 char *p = alloca(TAB_SIZE);
182 size_t ret;
183 memset(p, 'a', TAB_SIZE);
184 p[TAB_SIZE-1] = 0;
185 ret = strlen(p);
186 allocf(p);
187 return ret;
190 /* error */
191 int test15(void)
193 char *p = alloca(TAB_SIZE-1);
194 size_t ret;
195 memset(p, 'a', TAB_SIZE);
196 p[TAB_SIZE-1] = 0;
197 ret = strlen(p);
198 allocf(p);
199 return ret;
202 /* ok */
203 int test16()
205 char *demo = "This is only a test.";
206 char *p;
208 p = alloca(16);
209 strcpy(p,"12345678901234");
210 allocf(p);
212 /* Test alloca embedded in a larger expression */
213 printf("alloca : %s : %s\n", p, strcpy(alloca(strlen(demo)+1),demo) );
215 return 0;
218 /* error */
219 int test17()
221 char *demo = "This is only a test.";
222 char *p;
224 p = alloca(16);
225 strcpy(p,"12345678901234");
226 allocf(p);
228 /* Test alloca embedded in a larger expression */
229 printf("alloca : %s : %s\n", p, strcpy(alloca(strlen(demo)),demo) );
231 return 0;
234 int test18(void)
236 int i, sum = 0, n = TAB_SIZE;
237 int tab[n];
238 for(i=0;i<TAB_SIZE+1;i++) {
239 sum += tab[i];
241 return sum;
244 int (*table_test[])(void) = {
245 test1,
246 test2,
247 test3,
248 test4,
249 test5,
250 test6,
251 test7,
252 test8,
253 test9,
254 test10,
255 test11,
256 test12,
257 test13,
258 test14,
259 test15,
260 test16,
261 test17,
262 test18
265 int main(int argc, char **argv)
267 int i;
268 char *cp;
269 int index;
270 int (*ftest)(void);
271 int index_max = sizeof(table_test)/sizeof(table_test[0]);
273 /* check bounds checking main arg */
274 for (i = 0; i < argc; i++) {
275 cp = argv[i];
276 while (*cp) {
277 cp++;
281 if (argc < 2) {
282 printf(
283 "test TCC bound checking system\n"
284 "usage: boundtest N\n"
285 " 1 <= N <= %d\n", index_max);
286 exit(1);
289 index = 0;
290 if (argc >= 2)
291 index = atoi(argv[1]) - 1;
293 if ((index < 0) || (index >= index_max)) {
294 printf("N is outside of the valid range (%d)\n", index);
295 exit(2);
298 /* well, we also use bounds on this ! */
299 ftest = table_test[index];
300 ftest();
302 return 0;
306 * without bound 0.77 s
307 * with bounds 4.73