riscv: fix more sign/zero-extension problems
[tinycc.git] / tests / boundtest.c
blobe5c3ff410939051dfaed8884963bf5f46d338cf4
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__);
212 return 0;
215 /* error */
216 int test17()
218 char *demo = "This is only a test.";
219 char *p;
221 fprintf(stderr, "%s start\n", __FUNCTION__);
223 p = alloca(16);
224 strcpy(p,"12345678901234");
225 printf("alloca: p is %s\n", p);
227 /* Test alloca embedded in a larger expression */
228 printf("alloca: %s\n", strcpy(alloca(strlen(demo)),demo) );
230 fprintf(stderr, "%s end\n", __FUNCTION__);
231 return 0;
234 int (*table_test[])(void) = {
235 test1,
236 test2,
237 test3,
238 test4,
239 test5,
240 test6,
241 test7,
242 test8,
243 test9,
244 test10,
245 test11,
246 test12,
247 test13,
248 test14,
249 test15,
250 test16,
251 test17,
254 int main(int argc, char **argv)
256 int index;
257 int (*ftest)(void);
258 int index_max = sizeof(table_test)/sizeof(table_test[0]);
260 if (argc < 2) {
261 printf(
262 "test TCC bound checking system\n"
263 "usage: boundtest N\n"
264 " 1 <= N <= %d\n", index_max);
265 exit(1);
268 index = 0;
269 if (argc >= 2)
270 index = atoi(argv[1]) - 1;
272 if ((index < 0) || (index >= index_max)) {
273 printf("N is outside of the valid range (%d)\n", index);
274 exit(2);
277 /* well, we also use bounds on this ! */
278 ftest = table_test[index];
279 ftest();
281 return 0;
285 * without bound 0.77 s
286 * with bounds 4.73