fix installation amd bcheck for Windows
[tinycc.git] / tests / boundtest.c
blobeb95a9635d0b39f1427be32feff798e8ba494c05
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 /* XXX: currently: bug */
79 int test6(void)
81 int i, sum = 0;
82 int *tab4;
84 tab4 = malloc(20 * sizeof(int));
85 free(tab4);
86 for(i=0;i<21;i++) {
87 sum += tab4[i];
90 return sum;
93 /* error */
94 int test7(void)
96 int i, sum = 0;
97 int *p;
99 for(i=0;i<TAB_SIZE + 1;i++) {
100 p = &tab[i];
101 if (i == TAB_SIZE)
102 printf("i=%d %x\n", i, p);
103 sum += *p;
105 return sum;
108 /* ok */
109 int test8(void)
111 int i, sum = 0;
112 int tab[10];
114 for(i=0;i<10;i++) {
115 sum += tab[i];
117 return sum;
120 /* error */
121 int test9(void)
123 int i, sum = 0;
124 char tab[10];
126 for(i=0;i<11;i++) {
127 sum += tab[i];
129 return sum;
132 /* ok */
133 int test10(void)
135 char tab[10];
136 char tab1[10];
138 memset(tab, 0, 10);
139 memcpy(tab, tab1, 10);
140 memmove(tab, tab1, 10);
141 return 0;
144 /* error */
145 int test11(void)
147 char tab[10];
149 memset(tab, 0, 11);
150 return 0;
153 /* error */
154 int test12(void)
156 void *ptr;
157 ptr = malloc(10);
158 free(ptr);
159 free(ptr);
160 return 0;
163 /* error */
164 int test13(void)
166 char pad1 = 0;
167 char tab[10];
168 char pad2 = 0;
169 memset(tab, 'a', sizeof(tab));
170 return strlen(tab);
173 int test14(void)
175 char *p = alloca(TAB_SIZE);
176 memset(p, 'a', TAB_SIZE);
177 p[TAB_SIZE-1] = 0;
178 return strlen(p);
181 /* error */
182 int test15(void)
184 char *p = alloca(TAB_SIZE-1);
185 memset(p, 'a', TAB_SIZE);
186 p[TAB_SIZE-1] = 0;
187 return strlen(p);
190 int (*table_test[])(void) = {
191 test1,
192 test1,
193 test2,
194 test3,
195 test4,
196 test5,
197 test6,
198 test7,
199 test8,
200 test9,
201 test10,
202 test11,
203 test12,
204 test13,
205 test14,
206 test15,
209 int main(int argc, char **argv)
211 int index;
212 int (*ftest)(void);
214 if (argc < 2) {
215 printf("usage: boundtest n\n"
216 "test TCC bound checking system\n"
218 exit(1);
221 index = 0;
222 if (argc >= 2)
223 index = atoi(argv[1]);
224 /* well, we also use bounds on this ! */
225 ftest = table_test[index];
226 ftest();
228 return 0;
232 * without bound 0.77 s
233 * with bounds 4.73