fixup! riscv: Implement large addend for global address
[tinycc.git] / tests / boundtest.c
blobe4b2ab1c2a2ea940beaff0d6bb9c41100e8c1103
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 #undef alloca
176 #define alloca(x) malloc(x)
177 #define allocf(x) free(x)
178 #endif
180 int test14(void)
182 char *p = alloca(TAB_SIZE);
183 size_t ret;
184 memset(p, 'a', TAB_SIZE);
185 p[TAB_SIZE-1] = 0;
186 ret = strlen(p);
187 allocf(p);
188 return ret;
191 /* error */
192 int test15(void)
194 char *p = alloca(TAB_SIZE-1);
195 size_t ret;
196 memset(p, 'a', TAB_SIZE);
197 p[TAB_SIZE-1] = 0;
198 ret = strlen(p);
199 allocf(p);
200 return ret;
203 /* ok */
204 int test16()
206 char *demo = "This is only a test.";
207 char *p;
209 p = alloca(16);
210 strcpy(p,"12345678901234");
212 /* Test alloca embedded in a larger expression */
213 printf("alloca : %s : %s\n", p, strcpy(alloca(strlen(demo)+1),demo) );
214 allocf(p);
216 return 0;
219 /* error */
220 int test17()
222 char *demo = "This is only a test.";
223 char *p;
225 p = alloca(16);
226 strcpy(p,"12345678901234");
228 /* Test alloca embedded in a larger expression */
229 printf("alloca : %s : %s\n", p, strcpy(alloca(strlen(demo)),demo) );
230 allocf(p);
232 return 0;
235 int test18(void)
237 int i, sum = 0, n = TAB_SIZE;
238 int tab[n];
239 for(i=0;i<TAB_SIZE+1;i++) {
240 sum += tab[i];
242 return sum;
245 int (*table_test[])(void) = {
246 test1,
247 test2,
248 test3,
249 test4,
250 test5,
251 test6,
252 test7,
253 test8,
254 test9,
255 test10,
256 test11,
257 test12,
258 test13,
259 test14,
260 test15,
261 test16,
262 test17,
263 test18
266 int main(int argc, char **argv)
268 int i;
269 char *cp;
270 int index;
271 int (*ftest)(void);
272 int index_max = sizeof(table_test)/sizeof(table_test[0]);
274 /* check bounds checking main arg */
275 for (i = 0; i < argc; i++) {
276 cp = argv[i];
277 while (*cp) {
278 cp++;
282 if (argc < 2) {
283 printf(
284 "test TCC bound checking system\n"
285 "usage: boundtest N\n"
286 " 1 <= N <= %d\n", index_max);
287 exit(1);
290 index = 0;
291 if (argc >= 2)
292 index = atoi(argv[1]) - 1;
294 if ((index < 0) || (index >= index_max)) {
295 printf("N is outside of the valid range (%d)\n", index);
296 exit(2);
299 /* well, we also use bounds on this ! */
300 ftest = table_test[index];
301 ftest();
303 return 0;
307 * without bound 0.77 s
308 * with bounds 4.73