fixed lots of warnings in testprograms
[libfirm] / ir / be / test / ack / t7.c
1 #
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  *
6  */
7
8 char rcs_id[] = "$Id$" ;
9
10
11 /*
12 #define TEST1 1
13 */
14
15
16 /* This program can be used to test C-compilers */
17 /* It is supposed the first test program (= "test1") */
18 /* is used to test the basic arithmetic */
19
20 /* The following are global counters */
21
22 int t,          /* the value indicates the number of the test procedure */
23     ect,        /* error counter                                        */
24     tct;        /* count the number of test procedures called           */
25
26 /************************************************************************/
27 /*                                                                      */
28 /* The following is tested:                                             */
29 /* FOR STATEMENTS in test1                                              */
30 /* WHILE STATEMENTS in test2                                            */
31 /* WHILE and FOR STATEMENTS in test3                                    */
32 /* DO STATEMENTS in test4                                               */
33 /* SWITCH STATEMENTS in test5\e                                          */
34 /*                                                                      */
35 /************************************************************************/
36
37
38
39 char *pp1 = "End of test program, ";
40 char *pp2 = " test(s) completed, ";
41 char *pp3 = " errors detected\n";
42 char *pp4 = "Error ";
43 char *pp5 = " in test";
44 char *pp6 = "\n";
45
46 itoa(p,ptr)
47 /* converts integer "p" to ascii string "ptr" */
48 int p;
49 char *ptr;
50 {
51     register int k,l;
52     register char *str;
53     int sign;
54
55     str=ptr;
56     k=p;
57     if ((sign=k)<0)
58         k = -k;
59     do
60     {
61         l = k % 10;
62         k /= 10;
63         *str++ = l + '0';
64     }
65     while(k);
66     if (sign<0)
67         *str++ = '-';
68     *str = '\0';
69     reverse(ptr);
70 }
71
72
73
74 reverse(s)
75 char s[];
76 {
77     register int c,i,j;
78
79     for (i=0, j=strlen(s)-1; i<j; i++, j--)
80     {
81         c=s[i];
82         s[i]=s[j];
83         s[j]=c;
84     }
85 }
86
87 strlen(str)
88 /* returns the length of string str */
89 char *str;
90 {
91     register char *s, *p;
92
93     p = s = str;
94     while (*p)
95         p++;
96     return(p-s);
97 }
98
99
100
101 main()
102 {
103     char chtct[10],chect[10];
104     tct = 0;
105     ect = 0;            /* No errors, so far so good                    */
106     test1();            /* testing FOR STATEMENTS                       */
107     test2();            /* testing WHILE STATEMENTS                     */
108     test3();            /* testing combined FOR and WHILE statements    */
109     test4();            /* testing DO statements                        */
110     test5();            /* testing SWITCH statements                    */
111     test6();            /* testing GOTO statements                      */
112     test7();
113     test8();
114     write(1,pp1,strlen(pp1));
115     itoa(tct,chtct);
116     write(1,chtct,strlen(chtct));
117     write(1,pp2,strlen(pp2));
118     itoa(ect,chect);
119     write(1,chect,strlen(chect));
120     write(1,pp3,strlen(pp3));
121     return(ect);
122 }
123
124
125
126 e(n)                    /* prints an error message                      */
127 int n;
128 {
129     char cht[10],chn[10];
130     ect++;              /* total number of errors increased by 1        */
131     write(1,pp4,strlen(pp4));
132     itoa(n,chn);
133     write(1,chn,strlen(chn));
134     write(1,pp5,strlen(pp5));
135     itoa(t,cht);
136     write(1,cht,strlen(cht));
137     write(1,pp6,strlen(pp6));
138         abort();
139 }
140
141
142
143 test1()         /* Testing the for statement */
144 {
145     int i, j;   /* variables, used as contolling integers in the        */
146                 /* for statements                                       */
147
148     t = 1;              /* This is test 1                               */
149     tct++;
150     for ( ; ; )
151     {
152         break;
153         e(1);
154         return;         /* If the break doesn't work, let's hope the    */
155                         /* return does !                                */
156     }
157     for ( ; ; )
158     {
159         for ( ; ; )
160         {
161             for ( ; ; )
162             {
163                 for ( ; ; )
164                 {
165                     for ( ; ; )
166                     {
167                         for ( ; ; )
168                         {
169                             break;
170                             e(2);
171                             return;
172                         }
173                         break;
174                         e(3);
175                         return;
176                     }
177                     break;
178                     e(4);
179                     return;
180                 }
181                 break;
182                 e(5);
183                 return;
184             }
185             break;
186             e(6);
187             return;
188         }
189         break;
190         e(7);
191         return;
192     }
193     i=0;
194     for ( ; ; i++)
195     {
196         break;
197         e(8);
198         return;
199     }
200     for (i=0 ; ; i++)
201     {
202         break;
203         e(9);
204         return;
205     }
206     for (i=0 ; i<3; i++)
207         ;
208     if (i != 3) e(10);
209     for (i=0; i<0; i++)
210         e(11);
211     if (i != 0) e(12);
212     for (i=0; i<1; i++)
213         for (i=i; i<i; i++)
214             for (i=i; i<(i+0); i++)
215                 for (i=i+0; i<(i-0); i++)
216                     for (i=i-0; i<i; i++)
217                         e(13);
218     if (i != 1) e(14);
219     for (i=0; i<3; )
220         i++;
221     if (i != 3) e(15);
222     i = 18;
223     j = 3;
224     for ( ; j<i; --i)
225         ;
226     if (i != j) e(16);
227     if (i != 3) e(17);
228     j = -30;
229     for ( ; ; )
230         if (++j)
231             continue;
232         else break;
233     if (j != 0) e(18);
234     i = 0;
235     for (i++, i++, i++, i++; ; )
236     {
237         if (i != 4) e(19);
238         break;
239     }
240     i = 1;
241     for (i=j=i=j=i=j=i; i && j && i; --i, --j)
242     {
243         if (i != 1) e(20);
244     }
245     j=0;
246     for (i=32700; i<32767; i++)
247         j++;
248     if (j != 67) e(21);
249     j=0;
250 #ifdef TEST1
251     printf("*** 1\n");
252     for (i=32000; i<=32767; i++)
253         j++;
254     if (j != 68) e(22);
255     printf("*** 2\n");
256 #endif
257     j=0;
258     for (i=32767; i>32700; i--)
259         j++;
260     if (j != 67) e(23);
261     j=0;
262     for (i= -32768; i<-32700; i++)
263         j++;
264     if (j != 68) e(24);
265 }
266
267
268
269
270 test2()         /* Testing the while statement */
271 {
272     int i, j;
273
274     t = 2;
275     tct++;
276     while(1)
277     {
278         break;
279         e(1);
280         return;
281     }
282     while(0)
283     {
284         e(2);
285         break;
286         e(3);
287         return;
288     }
289     while (1 || 0)
290     {
291         break;
292         e(4);
293         return;
294     }
295     while (1 && 0)
296     {
297         e(5);
298         break;
299         e(6);
300         return;
301     }
302     j = 10;
303     while (--j)
304         ;
305     if (j != 0) e(7);
306     while (j)
307     {
308         e(8);
309         break;
310     }
311     while ( i=j )
312     {
313         e(9);
314         break;
315     }
316     while ( (i==j) && (i!=j) )
317     {
318         e(10);
319         break;
320     }
321     j = 1;
322     while (j)
323         while(j)
324             while(j)
325                 while(j)
326                     while(j)
327                         while(--j)
328                             ;
329     if (j != 0) e(11);
330     if (j) e(12);
331     j = 30;
332     while (--j)
333     {
334         continue;
335         continue;
336         continue;
337         continue;
338         continue;
339         break;
340         e(13);
341     }
342 }
343
344
345
346
347 test3()         /* Combined FOR and WHILE statements */
348 {
349     int i,j;
350
351     t = 3;
352     tct++;
353     j = 0;
354     for (i=3; i; i++)
355     {
356         while (i--)
357             ;
358         if (++j > 1) e(1);
359     }
360 }
361
362
363
364
365 test4()         /* Do statement */
366 {
367     int i;
368
369     t = 4;
370     tct++;
371     i = 0;
372     do
373         if (i) e(1);
374     while (i);
375     do
376     {
377         do
378         {
379             do
380             {
381                 do
382                 {
383                     i++;
384                 }
385                 while (!i);
386                 i++;
387             }
388             while (!i);
389             i++;
390         }
391         while (!i);
392         i++;
393     }
394     while (!i);
395     if (i != 4) e(2);
396 }
397
398
399
400
401 test5()         /* SWITCH statement */
402 {
403     int i,j;
404
405     t = 5;
406     tct++;
407     for (i=0; i<10; i++)
408     {
409         switch (i)
410         {
411             case 0: if (i != 0) e(1);
412                     break;
413             case 1: if (i != 1) e(2);
414                     break;
415             case 2: if (i != 2) e(3);
416                     break;
417             case 3: if (i != 3) e(4);
418                     i++;
419             case 4: if (i != 4) e(5);
420             case 5:
421             case 6:
422             case 7:
423             case 8:
424             case 9:
425                     break;
426             default: e(6);
427         }
428     }
429     for (i=j= -18; i<10; i++, j++)
430     {
431         switch (i)
432         {
433             case -3:
434             case 7:
435             case 1: switch (j)
436                     {
437                         case -3:
438                         case 7:
439                         case 1:
440                                 break;
441                         default: e(7);
442                     }
443                     break;
444                     e(8);
445             case -4: switch (j)
446                      {
447                         case -4: if (i != -4) e(9);
448                                  break;
449                         default: e(10);
450                      }
451         }
452     }
453     i = 'a';
454     switch (i)
455     {
456         case 'a':
457             switch ( i )
458             {
459                 case 'a':
460                     switch ( i )
461                     {
462                         case 'a':
463                             break;
464                         default: e(11);
465                     }
466                     break;
467                 default: e(12);
468             }
469             break;
470         default: e(13);
471     }
472 }
473
474
475
476 test6()         /* goto statement */
477 {
478     int k;
479
480     t = 6;
481     tct++;
482     k = 0;
483     goto lab0;
484 xl1:
485     k = 1;
486     goto lab1;
487 xl2:
488     k = 2;
489     goto lab2;
490 xl3:
491     k = 3;
492     goto lab3;
493 xl4:
494     k = 4;
495     goto llab1;
496 llab2: goto llab3;
497 llab4: goto llab5;
498 llab6: goto llab7;
499 llab8: if ( k != 4 ) e(5);
500         return ;
501 llab1: goto llab2;
502 llab3: goto llab4;
503 llab5: goto llab6;
504 llab7: goto llab8;
505 lab0: if ( k!= 0 ) e(1);
506     goto xl1 ;
507 lab1: if ( k!= 1 ) e(2);
508     goto xl2 ;
509 lab2: if ( k!= 2 ) e(3);
510     goto xl3 ;
511 lab3: if ( k!= 3 ) e(4);
512     goto xl4 ;
513 }
514
515
516
517 test7()         /* Combinations of FOR, WHILE, DO and SWITCH statements */
518 {
519     int i,j,k;
520
521     t = 7;
522     tct++;
523     for ( i=j=k=0; i<6; i++, j++, k++ )
524     {
525         if ( i != j ) e(1);
526         if ( i != k ) e(2);
527         if ( j != k ) e(3);
528         while ( i > j )
529         {
530             e(4);
531             break;
532         }
533         while ( i > k )
534         {
535             e(5);
536             break;
537         }
538         while ( j != k )
539         {
540             e(6);
541             break;
542         }
543         switch(i)
544         {
545             case 0:
546                 switch(j)
547                 {
548                     case 0:
549                         switch(k)
550                         {
551                             case 0: if ( i+j+k != 0 ) e(7);
552                                     break;
553                                     e(8);
554                             default: if ( i+j+k != k ) e(9);
555                         }
556                         break;
557                     default: if ( j > 6 ) e(10);
558                              if ( k != j ) e(11);
559                 }
560                 break;
561             case 1:
562             case 2:
563             case 3:
564             case 4:
565             case 5: break;
566             default: e(12);
567         }
568     }
569     for ( i=j= -3; i<0; i++,j++)
570         if ( j == -3 )
571             do
572                 if ( i )
573                     switch ( i )
574                     {
575                         case -3: if ( j != i ) e(13);
576                         case -2: if ( j != i ) e(14);
577                         case -1: for ( k=i; k < 2*j-j; k++)
578                                  {
579                                      e(15);
580                                      break;
581                                  }
582                                  break;
583                         case 0: e(16);
584                                 break;
585                         default: e(17);
586                                  break;
587                     }
588                 else e(18);
589             while ( 0 );
590     if ( i != j ) e(19);
591 }
592
593
594
595
596 test8()
597 {
598     int *p1, *p2;
599     int i,j,k;
600     int a1[1], a2[2][2], a3[3][3][3];
601
602     t = 8;
603     tct++;
604     a1[0] = 0;
605     for ( i=0; i<2; i++ )
606         for ( j=0; j<2; j++ )
607             a2[i][j] = (i*j) ^ (i+j);
608     if ( a2[0][0] != 0 ) e(1);
609     if ( a2[0][1] != 1 ) e(2);
610     if ( a2[1][0] != a2[0][1] ) e(3);
611     for ( i=0; i<3; i++)
612         for (j=0; j<3; j++)
613             for (k=0; k<3; k++)
614                 a3[i][j][k] = i | j | k;
615     if ( a3[0][0][0] != 0 ) e(4);
616     if ( a3[0][1][2] != a3[2][0][1] ) e(5);
617     if ( a3[2][1][1] != (2 | 1 | 1) ) e(6);
618     p2 = &a3[0][1][2];
619     p1 = &a3[0][1][2];
620     for ( ; p1 == p2 ; p1++ )
621     {
622         switch ( *p1 )
623         {
624             case 3: break;
625             default: e(7);
626         }
627         if ( *p1 != *p2 ) e(8);
628     }
629 }