Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / ctest / ctest2 / 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: t7.c,v 2.3 1994/06/24 12:08:28 ceriel Exp $" ;
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 }
139
140
141
142 test1()         /* Testing the for statement */
143 {
144     int i, j;   /* variables, used as contolling integers in the        */
145                 /* for statements                                       */
146
147     t = 1;              /* This is test 1                               */
148     tct++;
149     for ( ; ; )
150     {
151         break;
152         e(1);
153         return;         /* If the break doesn't work, let's hope the    */
154                         /* return does !                                */
155     }
156     for ( ; ; )
157     {
158         for ( ; ; )
159         {
160             for ( ; ; )
161             {
162                 for ( ; ; )
163                 {
164                     for ( ; ; )
165                     {
166                         for ( ; ; )
167                         {
168                             break;
169                             e(2);
170                             return;
171                         }
172                         break;
173                         e(3);
174                         return;
175                     }
176                     break;
177                     e(4);
178                     return;
179                 }
180                 break;
181                 e(5);
182                 return;
183             }
184             break;
185             e(6);
186             return;
187         }
188         break;
189         e(7);
190         return;
191     }
192     i=0;
193     for ( ; ; i++)
194     {
195         break;
196         e(8);
197         return;
198     }
199     for (i=0 ; ; i++)
200     {
201         break;
202         e(9);
203         return;
204     }
205     for (i=0 ; i<3; i++)
206         ;
207     if (i != 3) e(10);
208     for (i=0; i<0; i++)
209         e(11);
210     if (i != 0) e(12);
211     for (i=0; i<1; i++)
212         for (i=i; i<i; i++)
213             for (i=i; i<(i+0); i++)
214                 for (i=i+0; i<(i-0); i++)
215                     for (i=i-0; i<i; i++)
216                         e(13);
217     if (i != 1) e(14);
218     for (i=0; i<3; )
219         i++;
220     if (i != 3) e(15);
221     i = 18;
222     j = 3;
223     for ( ; j<i; --i)
224         ;
225     if (i != j) e(16);
226     if (i != 3) e(17);
227     j = -30;
228     for ( ; ; )
229         if (++j)
230             continue;
231         else break;
232     if (j != 0) e(18);
233     i = 0;
234     for (i++, i++, i++, i++; ; )
235     {
236         if (i != 4) e(19);
237         break;
238     }
239     i = 1;
240     for (i=j=i=j=i=j=i; i && j && i; --i, --j)
241     {
242         if (i != 1) e(20);
243     }
244     j=0;
245     for (i=32700; i<32767; i++)
246         j++;
247     if (j != 67) e(21);
248     j=0;
249 #ifdef TEST1
250     printf("*** 1\n");
251     for (i=32000; i<=32767; i++)
252         j++;
253     if (j != 68) e(22);
254     printf("*** 2\n");
255 #endif
256     j=0;
257     for (i=32767; i>32700; i--)
258         j++;
259     if (j != 67) e(23);
260     j=0;
261     for (i= -32768; i<-32700; i++)
262         j++;
263     if (j != 68) e(24);
264 }
265
266
267
268
269 test2()         /* Testing the while statement */
270 {
271     int i, j;
272
273     t = 2;
274     tct++;
275     while(1)
276     {
277         break;
278         e(1);
279         return;
280     }
281     while(0)
282     {
283         e(2);
284         break;
285         e(3);
286         return;
287     }
288     while (1 || 0)
289     {
290         break;
291         e(4);
292         return;
293     }
294     while (1 && 0)
295     {
296         e(5);
297         break;
298         e(6);
299         return;
300     }
301     j = 10;
302     while (--j)
303         ;
304     if (j != 0) e(7);
305     while (j)
306     {
307         e(8);
308         break;
309     }
310     while ( i=j )
311     {
312         e(9);
313         break;
314     }
315     while ( (i==j) && (i!=j) )
316     {
317         e(10);
318         break;
319     }
320     j = 1;
321     while (j)
322         while(j)
323             while(j)
324                 while(j)
325                     while(j)
326                         while(--j)
327                             ;
328     if (j != 0) e(11);
329     if (j) e(12);
330     j = 30;
331     while (--j)
332     {
333         continue;
334         continue;
335         continue;
336         continue;
337         continue;
338         break;
339         e(13);
340     }
341 }
342
343
344
345
346 test3()         /* Combined FOR and WHILE statements */
347 {
348     int i,j;
349
350     t = 3;
351     tct++;
352     j = 0;
353     for (i=3; i; i++)
354     {
355         while (i--)
356             ;
357         if (++j > 1) e(1);
358     }
359 }
360
361
362
363
364 test4()         /* Do statement */
365 {
366     int i;
367
368     t = 4;
369     tct++;
370     i = 0;
371     do
372         if (i) e(1);
373     while (i);
374     do
375     {
376         do
377         {
378             do
379             {
380                 do
381                 {
382                     i++;
383                 }
384                 while (!i);
385                 i++;
386             }
387             while (!i);
388             i++;
389         }
390         while (!i);
391         i++;
392     }
393     while (!i);
394     if (i != 4) e(2);
395 }
396
397
398
399
400 test5()         /* SWITCH statement */
401 {
402     int i,j;
403
404     t = 5;
405     tct++;
406     for (i=0; i<10; i++)
407     {
408         switch (i)
409         {
410             case 0: if (i != 0) e(1);
411                     break;
412             case 1: if (i != 1) e(2);
413                     break;
414             case 2: if (i != 2) e(3);
415                     break;
416             case 3: if (i != 3) e(4);
417                     i++;
418             case 4: if (i != 4) e(5);
419             case 5:
420             case 6:
421             case 7:
422             case 8:
423             case 9:
424                     break;
425             default: e(6);
426         }
427     }
428     for (i=j= -18; i<10; i++, j++)
429     {
430         switch (i)
431         {
432             case -3:
433             case 7:
434             case 1: switch (j)
435                     {
436                         case -3:
437                         case 7:
438                         case 1:
439                                 break;
440                         default: e(7);
441                     }
442                     break;
443                     e(8);
444             case -4: switch (j)
445                      {
446                         case -4: if (i != -4) e(9);
447                                  break;
448                         default: e(10);
449                      }
450         }
451     }
452     i = 'a';
453     switch (i)
454     {
455         case 'a':
456             switch ( i )
457             {
458                 case 'a':
459                     switch ( i )
460                     {
461                         case 'a':
462                             break;
463                         default: e(11);
464                     }
465                     break;
466                 default: e(12);
467             }
468             break;
469         default: e(13);
470     }
471 }
472
473
474
475 test6()         /* goto statement */
476 {
477     int k;
478
479     t = 6;
480     tct++;
481     k = 0;
482     goto lab0;
483 xl1:
484     k = 1;
485     goto lab1;
486 xl2:
487     k = 2;
488     goto lab2;
489 xl3:
490     k = 3;
491     goto lab3;
492 xl4:
493     k = 4;
494     goto llab1;
495 llab2: goto llab3;
496 llab4: goto llab5;
497 llab6: goto llab7;
498 llab8: if ( k != 4 ) e(5);
499         return ;
500 llab1: goto llab2;
501 llab3: goto llab4;
502 llab5: goto llab6;
503 llab7: goto llab8;
504 lab0: if ( k!= 0 ) e(1);
505     goto xl1 ;
506 lab1: if ( k!= 1 ) e(2);
507     goto xl2 ;
508 lab2: if ( k!= 2 ) e(3);
509     goto xl3 ;
510 lab3: if ( k!= 3 ) e(4);
511     goto xl4 ;
512 }
513
514
515
516 test7()         /* Combinations of FOR, WHILE, DO and SWITCH statements */
517 {
518     int i,j,k;
519
520     t = 7;
521     tct++;
522     for ( i=j=k=0; i<6; i++, j++, k++ )
523     {
524         if ( i != j ) e(1);
525         if ( i != k ) e(2);
526         if ( j != k ) e(3);
527         while ( i > j )
528         {
529             e(4);
530             break;
531         }
532         while ( i > k )
533         {
534             e(5);
535             break;
536         }
537         while ( j != k )
538         {
539             e(6);
540             break;
541         }
542         switch(i)
543         {
544             case 0:
545                 switch(j)
546                 {
547                     case 0:
548                         switch(k)
549                         {
550                             case 0: if ( i+j+k != 0 ) e(7);
551                                     break;
552                                     e(8);
553                             default: if ( i+j+k != k ) e(9);
554                         }
555                         break;
556                     default: if ( j > 6 ) e(10);
557                              if ( k != j ) e(11);
558                 }
559                 break;
560             case 1:
561             case 2:
562             case 3:
563             case 4:
564             case 5: break;
565             default: e(12);
566         }
567     }
568     for ( i=j= -3; i<0; i++,j++)
569         if ( j == -3 )
570             do
571                 if ( i )
572                     switch ( i )
573                     {
574                         case -3: if ( j != i ) e(13);
575                         case -2: if ( j != i ) e(14);
576                         case -1: for ( k=i; k < 2*j-j; k++)
577                                  {
578                                      e(15);
579                                      break;
580                                  }
581                                  break;
582                         case 0: e(16);
583                                 break;
584                         default: e(17);
585                                  break;
586                     }
587                 else e(18);
588             while ( 0 );
589     if ( i != j ) e(19);
590 }
591
592
593
594
595 test8()
596 {
597     int *p1, *p2;
598     int i,j,k;
599     int a1[1], a2[2][2], a3[3][3][3];
600
601     t = 8;
602     tct++;
603     a1[0] = 0;
604     for ( i=0; i<2; i++ )
605         for ( j=0; j<2; j++ )
606             a2[i][j] = (i*j) ^ (i+j);
607     if ( a2[0][0] != 0 ) e(1);
608     if ( a2[0][1] != 1 ) e(2);
609     if ( a2[1][0] != a2[0][1] ) e(3);
610     for ( i=0; i<3; i++)
611         for (j=0; j<3; j++)
612             for (k=0; k<3; k++)
613                 a3[i][j][k] = i | j | k;
614     if ( a3[0][0][0] != 0 ) e(4);
615     if ( a3[0][1][2] != a3[2][0][1] ) e(5);
616     if ( a3[2][1][1] != (2 | 1 | 1) ) e(6);
617     p2 = &a3[0][1][2];
618     p1 = &a3[0][1][2];
619     for ( ; p1 == p2 ; p1++ )
620     {
621         switch ( *p1 )
622         {
623             case 3: break;
624             default: e(7);
625         }
626         if ( *p1 != *p2 ) e(8);
627     }
628 }