Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / ctest / ctest3 / test2.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  */
6
7 char rcs_id[] = "$Id: test2.c,v 2.3 1994/06/24 12:08:40 ceriel Exp $" ;
8
9 /* This program can be used to test C-compilers */
10
11
12 int t, ect, tct;
13
14
15 /**********************************************************************/
16 /*
17  * Testing basic function calls
18  *
19  */
20
21
22
23 main()
24 {
25     tct = 0;
26     ect = 0;
27     test1();
28     test2();
29     test3();
30     test4();
31     test5();
32     test6();
33     printf("End of test program, %d tests completed, %d errors detected\n",
34         tct,ect);
35         return 0 ;
36 }
37
38
39
40
41 e(n)
42 int n;
43 {
44     ect++;
45     printf("Error %d in test%d \n",n,t);
46 }
47
48
49
50
51 one()
52 {
53     return(1);
54 }
55
56
57
58
59 two()
60 {
61     return(2);
62 }
63
64
65
66
67 three()
68 {
69     return(3);
70 }
71
72
73
74
75 four()
76 {
77     return(4);
78 }
79
80
81
82
83 five()
84 {
85     return(5);
86 }
87
88
89
90
91 plus()
92 {
93     return ( one() + two() + three() + four() + five() );
94 }
95
96
97
98
99 multipl()
100 {
101     return( one() * two() * three() * four() * five() );
102 }
103
104
105
106
107 subtr()
108 {
109     return( - one() - two() - three() - four() - five() );
110 }
111
112
113
114
115 test1()
116 {
117     int i;
118     int count;
119
120     t = 1;
121     tct++;
122     if ( one() != 1 ) e(1);
123     if ( two() != 2 ) e(2);
124     if ( three() != 3 ) e(3);
125     if ( four() != 4 ) e(4);
126     if ( five() != 5 ) e(5);
127     if ( (one() + two()) != 3 ) e(6);
128     if ( ((((((one() + two())))))) != 3) e(7);
129     if ( (one() * three()) != 3) e(8);
130     if (( (four() + three()) * two()) != 14) e(9);
131     if ( (four() + four()) != (two() * four()) ) e(10);
132     if ( (four() - four()) / three() ) e(11);
133     if (( four() + 3 * 12 - ( one() * two() * 2 ) ) != 36 ) e(12);
134     if ( one() & two() & four() & three() ) e(13);
135     if ( !( three() && two() ) ) e(14);
136     for (i=0; i<8; i++)
137     {
138        count = one() + two() + three() + four();
139        count = count * one();
140        count = count * two() - one() - two() - three() - four();
141     }
142     if (count != 10) e(15);
143     if ( !one() ) e(16);
144     if ( plus() != 15 ) e(17);
145     if ( multipl() != 120 ) e(18);
146     if ( subtr() != -15 ) e(19);
147     if ( -subtr() != plus() ) e(18);
148     if ( -subtr() != plus() ) e(21);
149 }
150
151
152
153
154 echo(a)
155 int a;
156 {
157     return ( a );
158 }
159
160
161
162
163 min(a,b)
164 int a,b;
165 {
166     if ( a < b )
167         return(a);
168     return(b);
169 }
170
171
172
173
174 max1(a,b)
175 int a,b;
176 {
177     if ( a < b )
178         return(b);
179     return(a);
180 }
181
182
183
184
185 max2(a,b)
186 int a,b;
187 {
188     return ( ( a < b ? b : a ) );
189 }
190
191
192
193
194 test2()
195 {
196     int i,j;
197     int a,b;
198
199     t = 2;
200     tct++;
201     if ( echo(1) != 1 ) e(1);
202     if ( echo(3) != 3 ) e(2);
203     if ( echo(0) ) e(3);
204     if ( echo(2) + echo(3) != echo(5) ) e(4);
205     if ( echo( 2 + 3 ) != 5 ) e(5);
206     if ( echo ( 1 + 2 + 3 + 4 + 5 ) != 10 + echo(5) ) e(6);
207     if (( echo( 2<<1 ) ) != 4 ) e(7);
208     if ( echo( 2 >> 1 ) != 1 ) e(8);
209     if ( echo( 1 << 4 ) != echo( 2 << 3 ) ) e(9);
210     if ( echo( echo(4) ) != echo(4) ) e(10);
211     if (( echo ( echo ( echo( echo ( echo ( 3 ) ) ) ) ) ) != 3 ) e(11);
212     if ( echo( echo( echo(2+3-4+echo(4)*echo(2))) ) != 9 ) e(12);
213     if ( min(1,2) != 1) e(13);
214     if (min(0,45) != 0) e(14);
215     if (min(45,0) != 0) e(15);
216     if (min(-72,-100) != -100) e(16);
217     if (min(-100,-72) != -100) e(17);
218     if (min(1<<3,2<<3) != (1<<3) ) e(18);
219     if ( min( echo(3), echo(3) ) != echo (echo(3)) ) e(19);
220     if ( max1('a','b') != 'b' ) e(20);
221     if ( max1('b','a') != 'b' ) e(21);
222     if ( max1(-3,54+2) != ( -3 < 54+2 ? 54+2 : -3 ) ) e(22);
223     if (max1('a'+'b'+34,'a'*2) != max2('a'*2,'a'+'b'+34)) e(23);
224     if (max1(345/23,4) != max1( echo(345/23), 4) ) e(24);
225     if ( max1( max1(2,3), max1(2,3) ) != max1(2,3) ) e(25);
226     for (i=3; i<5; i++)
227         if ((max1(i,-i)) != i) e(26);
228     for (j=min('a',34); j<max2('a',34); j++)
229     {
230         if ( j<min(max1('a',34),min('a',34)) ) e(27);
231         if ( j>max1(min(34,'a'),max2(34,'a')) ) e(28);
232     }
233     a=b= -32768;
234     if ( min(echo(a),a) != a) e(29);
235     if ( max1(echo(b),max1(b,b)) != b) e(30);
236 }
237
238
239
240
241 sum(k)
242 int k;
243 {
244     if (k<=0)
245         return(0);
246     return(k+sum(k-1));
247 }
248
249
250
251
252 formula(k)
253 int k;
254 {
255     if (k<=0)
256         return(0);
257     return ( ((((( (k*(k+1))/2 ))))) );
258 }
259
260
261
262
263 test3()
264 {
265     int k;
266     int count;
267
268     t = 3;
269     tct++;
270     count=0;
271     if ( sum(-4) != 0 ) e(1);
272     if ( sum(0) != 0 ) e(2);
273     if ( sum(2) != 3 ) e(3);
274     if ( sum(10) != 55 ) e(4);
275     if ( sum(34) != formula(34) ) e(5);
276     if ( sum(101) != formula(101) ) e(6);
277     if ( sum( sum(11) ) != formula( formula(11) ) ) e(7);
278     if ( sum( sum(11) ) != formula( sum(11) ) ) e(8);
279     if ( sum( sum( sum(4) )) != sum ( formula ( sum( 4) )) ) e(9);
280     for (k = sum(-45); k<sum('a'); k += sum('b') )
281         if (count++) e(10);
282     if ( echo( sum ( formula( five() ))) != formula ( sum(5) ) ) e(11);
283 }
284
285
286
287
288 test4()
289 {
290     int i,j,k,l,m;
291     int a[50];
292     int b[1][2][3][4][5];
293
294     t = 4;
295     tct++;
296     b[0][1][2][3][4] = one();
297     if ( b[0][1][2][3][4] != 1) e(1);
298     if ( b[0][one()][two()][three()][four()] != 1) e(2);
299     if ( b[0][one()][five()-3][one()+2][four()] != b[0][1][2][2*2-1][4]) e(3);
300     for (i=0; i<50; i++)
301         a[i] = i+1;
302     if (a[one()-1] != one()) e(4);
303     if (echo(a[4]) != 5) e(5);
304     if ( echo( a[ echo(6) ] ) != 7 ) e(6);
305     if ( a[a[a[0]]] != 3 ) e(7);
306     for (i=11; i<22; i++)
307         if ( echo( a[i+echo('b'-'a'-1)] ) != i+1) e(8);
308     for (i=0; i<1; i++)
309         for (j=0; j<2; j++)
310             for (k=0; k<3; k++)
311                 for (l=0; l<4; l++)
312                     for (m=0; m<5; m++)
313                         b[echo(i)][echo(j)][echo(k)][l][echo(m)] = j*k*l*m;
314     for (i=3; i; --i)
315         for (j=4; j; --j)
316             if (b[0][max1( max1(0,0), 1)][2][i][j] != 1*2*echo(i)*j ) e(9);
317 }
318
319
320
321
322 /* More testing */
323
324
325 func(sw,i)
326 int sw;         /* switch selector */
327 int i;          /* value to be returned */
328 {
329     int a[10];
330
331     switch(sw)
332     {
333         case 0: return(-i);
334                 break;
335         case 1: return ( echo(i*i) - (i-1)*i );
336                 break;
337         case 2: return ( 2*func(0,i) );
338                 break;
339         case 3: a[0] = 3;
340                 a[3] = 1;
341                 a[1] = 8;
342                 a[8] = i;
343                 return( 3*a[a[a[a[0]]]] );
344     }
345     return(-32768);
346 }
347
348
349
350
351 /* testing function arguments */
352
353
354
355
356 f1(i,j)
357 int i,j;
358 {
359     return( i!=j );
360 }
361
362
363
364 f2(ptr)
365 int *ptr;
366 {
367     int *locptr;
368
369     locptr = ptr;
370     return ( *ptr | *locptr );
371 }
372
373
374
375 swap(a,b)
376 int *a,*b;
377 {
378     int temp;
379
380     temp = *a;
381     *a = *b;
382     *b = temp;
383 }
384
385
386
387 test5()
388 {
389     int k,l;
390     int *ptr;
391
392     t = 5;
393     tct++;
394     for (k = -6353; k < -6003; k++)
395         if (f1(k,k+3-echo(3))) e(1);
396     k = 'a' - 723;
397     ptr = &k;
398     if ( f2(ptr) != f2(&k) ) e(3);
399     if ( f2(ptr) + f2(&k) != 2*k ) e(4);
400     if ( f1(*ptr,*ptr) ) e(5);
401     if ( f1(*ptr,k) ) e(6);
402     if ( f1( f2(&k), *ptr ) ) e(7);
403     k = l=28;
404     swap(&k,&l);
405     if ( k != l ) e(8);
406     *ptr = k;
407     swap(&k,ptr);
408     if ( k != *ptr ) e(9);
409     l = -5;
410     k = 'h';
411     ptr = &k;
412     swap(ptr,&l);
413     if ( l != 'h' ) e(10);
414     if ( k != -5 ) e(11);
415     if ( *ptr != -5 ) e(12);
416     if ( ptr != &k ) e(13);
417 }
418
419
420
421 icount(i)
422 int i;
423 {
424     return(i+1);
425 }
426
427
428
429 test6()
430 {
431     int i;
432
433     tct++;
434     t=6;
435     for (i=0; i<10; i++)
436         if (func(0,i) != -i) e(i);
437     for (i=10; i<20; i++)
438         if ( -func(2,i) != 2*i) e(i);
439     if (func(func(0,0),0) != 0) e(20);
440     if (func(-func(0,1),32) != 32) e(21);
441     if (max1(32767,10) != max2(32767,10)) e(22);
442     if (func(3,10) != 30) e(22);
443     if (icount(1)!=2) e(23);
444     if (icount(2)!=3) e(24);
445     if (icount(32766) != 32767) e(25);
446     if (icount((int)-32768) != -32767) e(26);
447     if (icount(icount(1)) != 3) e(27);
448     if ( icount( (int)&i ) != (int)&i + 1 ) e(28) ;
449     if (icount(icount(icount(icount(icount(icount(icount(icount(0))))))))!=8) e(29);
450 }