Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / ctest / ctest5 / test1.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: test1.c,v 2.5 1994/06/24 12:08:53 ceriel Exp $" ;
8
9 /* This program can be used to test C-compilers     */
10
11 int i,j,k,l,m,ect,pct,t,*p1;
12 int a1[20];
13 #ifndef NOFLOAT
14 float a2[20],xf,yf,zf;
15 double a3[20],xd,yd,zd;
16 #endif
17
18 char alstr[3000] ;
19 char *alptr = alstr ;
20
21 struct tp2
22 { char c1;
23   int i,j;
24 #ifndef NOFLOAT
25   float aaa;
26   double bbb;
27 #endif
28 } r1,r2,*p3;
29
30 struct node
31 { int val;
32  struct node *next;
33 } *head,*tail,*p2;
34
35 main()
36 { ect = 0; pct = 0;
37   test1();test2();test3();
38   test4();test5();
39   test6();test7();test8();
40   test9();test10();
41 #ifndef NOFLOAT
42   test11();
43 #endif
44   printf("program test1\n");
45   printf("%d tests completed. Number of errors = %d\n",pct,ect);
46         return 0 ;
47 }
48
49 char *alloc(size) {
50         register char *retval ;
51
52         retval=alptr ;
53         alptr += size ;
54         if ( alptr-alstr>sizeof alstr ) {
55                 printf("allocation overflow\n") ;
56                 exit(8) ;
57         }
58         return(retval) ;
59 }
60
61 int abs(a) int a ; { return ( a<0 ? -a : a) ; }
62 #ifndef NOFLOAT
63 double fabs(a) double a ; { return( a<0 ? -a : a) ; }
64 #endif
65
66 e(n)
67 { ect++; printf("error %d in test %d \n",n,t);
68 }
69
70 inc(n)
71 { return(++n);}
72
73 /***********************************************************************/
74
75 test1()
76 /*arithmetic on constants */
77 { t = 1; pct++;
78   if (1+1 != 2) e(1);
79   if (3333 + 258 != 3591) e(2);
80   if (3*4 != 12) e(3);
81   if (111*111 != 12321) e(4);
82   if (50 / 5 != 10) e(5);
83   if (7498 / 75 != 99) e(6);
84   if (456 - 345 != 111) e(7);
85   if (1+(-2) != -1) e(8);
86   if (-3 * -4 != 12) e(9);
87   if (-2 / 2 != -1) e(10);
88   if (-5 / 1 != -5 ) e(11);
89   if (-4 - -5 != 1) e(12);
90   if ( 03 + 02 != 05) e(13);
91   if ( 03456 + 88 != 03606 ) e(14);
92   if ( 0100 * 23 != 02700 ) e(15);
93   if ( 045 / 020 != 2) e(16);
94   if (0472 - 0377 != 073 ) e(17);
95   if ('a' + 3 != 100) e(18);
96   if ('a' + 'c' != 'b' + 'b') e(19);
97   if ( 'z' * 'z' != 14884 ) e(20);
98   if ( -'z' / 01 != -'z' ) e(21);
99   if ( 077777 >> 3 != 07777 ) e(22);
100   if ( 077777 >> 15 ) e(23);
101   if ( 234 << 6 != 234 << 6 ) e(24);
102   if ( 0124 & 07765 != 0124 ) e(25);
103   if ( 34 & 31 != 2 ) e(26);
104   if ( ( -4 | 3 ) != -1 ) e(27);
105   if ( ( 5 | 013 | 020 ) != 31 ) e(28);
106   if ( ( -7 ^ 3 ) != -6 ) e(29);
107   if ( ( 07373 ^ 4632 ) != 016343 ) e(30);
108   if ( (1+2+3)*(2+3+4)*(3+5+5) / 2 != ((3*((5+3+2)*10)+51)*6)/6 ) e(31);
109   if ( (1000*2+5*7+13)/ 8 != 2*2*2*2*4*4 ) e(32);
110   if ((1*2*3*4*5*6*7 / 5040 !=
111                  5040 / 7 / 6 / 5 / 4 / 3 / 2 / 1 )) e(33);
112   if ( -(-(-(-(-(-(1)))))) != 1 ) e(34);
113   if (-                    1     !=  -((((((((((1)))))))))) ) e(35);
114   if ( -1-1-1-1-1-1       !=       -6-3+3 ) e(36);
115   if (  -4 * -5 != 20 ) e(37);  
116   if ( 2<1 ) e(38);
117   if ( 2<= 1 ) e(39);
118   if ( 2==3 ) e(40);
119   if ( 2 != 2 ) e(41);
120   if ( 2 >= 3) e(42);
121   if ( 2 > 3 ) e(43);
122   if (2 + 0 != 2 ) e(44);
123   if (2 - 0 != 2 ) e(45);
124   if (2 * 0 != 0 ) e(46);
125   if ( 0 / 1 != 0 ) e(47);
126   if ( -0 != 0 ) e(48);
127   if ( 0 * 0 != 0 ) e(49);
128   if ( 32767 > 32767 ) e(50);
129   if ( 0456 < 0400 ) e(51);
130   if ( 0456 != ( 0400 | 050 | 06 ) ) e(52);
131   if ( 2*2<<2*2/4 != 010 ) e(53);
132   if ( 0 || 0 ) e(54);
133   if ( 1 && 0 ) e(55);
134   if ( ( 123 ? 123 * 4 :345) != 492 ) e(56);
135   if ( ( 0 ? 345 : 280) != 280 ) e(57);
136   if ( ( (2*2/2<<2)|(2/2) ) != 9 ) e(58); 
137   if ( !( 111 || 23 && 0 ) ) e(59);
138   if ( !1 ) e(60);
139   if ( !0 == 0 ) e(61);
140   if ( !!!!!!!!0 ) e(62);
141 }
142
143 /***********************************************************************/
144
145 test2()
146 /*arithmetic on global integer variables*/
147 { t = 2;  pct++;
148   i = 1; j = 2; k = 3; l = 4; m = 10;
149   if ( i+j != k ) e(1);
150   if ( i+k != l ) e(2);
151   if ( j-k != -i ) e(3);
152   if ( j*(j + k) != m ) e(4);
153   if ( -m != -(k+k+l) ) e(5);
154   if ( i / i != 1 ) e(6);
155   if ( m*m / m != m ) e(7);
156   if ( 10 * m != 100 ) e(8);
157   if ( m * (-10) != -100 ) e(9);
158   if ( j / k != 0 ) e(10);
159   if ( 100 / k != 33 ) e(11);
160   if ( i+j*k+l+m / j + 50 / k != 32 ) e(12);
161   if ( j*k*m / 6 != 10 ) e(13);
162   if ( (k>4) || (k>=4) || (k==4) ) e(14);
163   if ( (m<j) || (m<=j) || (m==j) ) e(15);
164   if ( i+j-k ) e(16);
165   if ( j<i ) e(17);
166   if ( j != j ) e(18);
167   if ( i > j ) e(19);
168   if ( (i > j ? k : k*j ) != 6 ) e(20);
169   if ( (i < j ? k : k*j ) != 3 ) e(21);
170   if ( j<<i != l ) e(22);
171   if ( j>> i != i ) e(25);
172   if ( i++ != 1 ) e(26);
173   if ( --i != 1 ) e(27);
174   if ( i-- != 1 ) e(28);
175   if ( ( i+j ) && ( i<0 ) || (m-10) && (064) ) e(29);
176   if ( ( i+j ) &&  !(i>=0) || (m-10) && !( 0 ) ) e(30);
177 }
178
179 /***********************************************************************/
180
181 test3()
182 /*arithmetic on local integer variables*/
183 { int a,b,c,d,f;
184   t = 3;  pct++;
185   a = 1; b = 2; c = 3; d = 4; f = 10;
186   if ( a+b != c ) e(1);
187   if ( a+c != d ) e(2);
188   if ( b-c != -a ) e(3);
189   if ( b*(b + c) != f ) e(4);
190   if ( -f != -(c+c+d) ) e(5);
191   if ( a / a != 1 ) e(6);
192   if ( f*f / f != f ) e(7);
193   if ( 10 * f != 100 ) e(8);
194   if ( f * (-10) != -100 ) e(9);
195   if ( b / c != 0 ) e(10);
196   if ( 100 / c != 33 ) e(11);
197   if ( a+b*c+d+f / b + 50 / c != 32 ) e(12);
198   if ( b*c*f / 6 != 10 ) e(13);
199   if ( (c>4) || (c>=4) || (c==4) ) e(14);
200   if ( (f<b) || (f<=b) || (f==b) ) e(15);
201   if ( c != a+b ) e(16);
202   if ( b<a ) e(17);
203   if ( b != b ) e(18);
204   if ( a > b ) e(19);
205   if ( (a > b ? c : c*b ) != 6 ) e(20);
206   if ( (a < b ? c : c*b ) != 3 ) e(21);
207   if ( b<<a != d ) e(22);
208   if ( b>> a != a ) e(25);
209   if ( a++ != 1 ) e(26);
210   if ( --a != 1 ) e(27);
211   if ( a-- != 1 ) e(28);
212   if ( ( a+b ) && ( a<0 ) || (f-10) && (064) ) e(29);
213   if ( ( a+b ) &&  !(a>=0) || (f-10) && !( 0 ) ) e(30);
214 }
215
216 /***********************************************************************/
217
218 test4()
219 /* global arrays */
220 {
221 #ifndef NOFLOAT
222   float epsf;
223   double epsd;
224 #endif
225   t=4; pct++;
226 #ifndef NOFLOAT
227   epsf = 1e-7; epsd = 1e-14;
228 #endif
229   for ( i=0; i<20 ; i++ ) a1[i] = i*i;
230   if ( a1[9] != 81 || a1[17] != 289 || a1[0] != 0 ) e(1);
231   if ( a1[1] + a1[2] + a1[3]  !=  14 ) e(2);
232   if ( ! a1[15] ) e(3);
233   if ( a1[8] / a1[4] != 4 ) e(4);
234 #ifndef NOFLOAT
235   for ( i=0; i<20; i++ ) a2[i] = 10.0e-1 + i/54.324e-1;
236   if ( fabs(a2[4]*a2[4]-a2[4]*(10.0e-1 + 4/54.324e-1 ) ) > epsf ) e(5);
237   if ( fabs(a2[8]/a2[8]*a2[9]/a2[9]-a2[10]+a2[10]-1.0 ) > epsf ) e(6);
238   if ( fabs(a2[5]-a2[4]-1/54.324e-1 ) > epsf ) e(7);
239   for ( i=0; i<20; i++ ) a3[i]= 10.0e-1 + i/54.324e-1;
240   if ( fabs(a3[4]*a3[4]-a3[4]*(1.0e0+4/54.324e-1 )) > epsd ) e(8);
241   if ( fabs( a3[8]*a3[9]/a3[8]/a3[9]-a3[10]+a3[10]-1000e-3) > epsd ) e(9);
242   if ( fabs(a3[8]+a3[6]-2*a3[7]) > epsd ) e(10);
243 #endif
244 }
245
246 /****************************************************************/
247
248 test5()
249 /* local arrays */
250 { int b1[20];
251 #ifndef NOFLOAT
252   float epsf, b2[20]; double b3[20],epsd;
253   epsf = 1e-7; epsd = 1e-14;
254 #endif
255   t = 5; pct++;
256   for ( i=0; i<20 ; i++ ) b1[i] = i*i;
257   if ( b1[9]-b1[8] != 17 ) e(1);
258   if ( b1[3] + b1[4] != b1[5] ) e(2);
259   if ( b1[1] != 1||b1[3] != 9 || b1[5] != 25 || b1[7] != 49 ) e(3);
260   if ( b1[12] / b1[6] != 4   ) e(4);
261 #ifndef NOFLOAT
262   for ( i=0; i<20; i += 1) b2[i] = 10.0e-1+i/54.324e-1;
263   if (fabs(b2[4]*b2[4]-b2[4]*(10.0e-1+4/54.324e-1)) > epsf ) e(5);
264   if (fabs(b2[8]/b2[8]*b2[9]/b2[9]-b2[10]+b2[10]-1.0) > epsf ) e(6);
265   if ( fabs(b2[5]-b2[4]-1/5.4324 ) > epsf ) e(7);
266   for ( i=0; i<20 ; i += 1 ) b3[i] = 10.0e-1+i/54.324e-1;
267   if (fabs(b3[4]*b3[4]-b3[4]*(10.0e-1+4/54.324e-1)) > epsd ) e(8);
268   if (fabs(b3[8]*b3[9]/b3[8]/b3[9]+b3[10]-b3[10]-1.0) > epsd ) e(9);
269   if (fabs(b3[10]+b3[18]-2*b3[14]) > epsd ) e(10);
270 #endif
271 }
272
273
274 /****************************************************************/
275
276
277
278 test6()
279 /* mixed local and global */
280 { int li,b1[20];
281 #ifndef NOFLOAT
282   double b3[10],xxd,epsd;
283 #endif
284   t = 6;  pct++;
285 #ifndef NOFLOAT
286   epsd = 1e-14;
287 #endif
288   li = 6; i = li ;
289   if ( i != 6 ) e(1);
290   i = 6; li = i;
291   if ( i != li ) e(2);
292   if ( i % li ) e(3);
293   i=li=i=li=i=li=i=i=i=li=j;
294   if ( i != li || i != j ) e(4);
295   for ( i=li=0; i<20 ; i=li ) { b1[li]= (li+1)*(i+1) ; li++; }
296   if ( b1[9] != a1[10] ) e(5);
297   if ( b1[7]/a1[4] != a1[2] ) e(6);
298   li = i = 121;
299   if ( b1[10] != i && a1[11]!= li ) e(7);
300 #ifndef NOFLOAT
301   for ( li=0 ; li<10; li++ ) b3[li]= 1.0e0 + li/54.324e-1;
302   if ( fabs(b3[9]-a3[9]) > epsd ) e(8);
303   if ( fabs(8/54.324e-1 - b3[9]+a3[1] ) > epsd ) e(9);
304 #endif
305 }
306
307 /***************************************************************/
308
309
310 test7()
311 /*global records */
312 { t=7; pct++;
313   r1.c1= 'x';r1.i=40;r1.j=50;
314 #ifndef NOFLOAT
315   r1.aaa=3.0;r1.bbb=4.0;
316 #endif
317   r2.c1=r1.c1;
318   r2.i= 50;
319   r2.j=40;
320 #ifndef NOFLOAT
321   r2.aaa=4.0;r2.bbb=5.0;
322 #endif
323   if (r1.c1 != 'x' || r1.i != 40 ) e(1);
324 #ifndef NOFLOAT
325   if ( r1.aaa != 3.0 ) e(1);
326 #endif
327   i = 25;j=75;
328   if (r1.i != 40 || r2.i != 50 ) e(2);
329   if ( r2.j != 40 || r1.j != 50 ) e(3);
330   if ( (r1.c1 + r2.c1)/2 != 'x' ) e(4);
331 #ifndef NOFLOAT
332   if ( r1.aaa*r1.aaa+r2.aaa*r2.aaa != r2.bbb*r2.bbb) e(5);
333 #endif
334   r1.i = 34; if ( i!=25 ) e(6);
335 }
336
337
338 /****************************************************************/
339
340
341 test8()
342 /*local records */
343 { struct tp2  s1,s2;
344   t=8; pct++;
345   s1.c1= 'x';s1.i=40;s1.j=50;
346 #ifndef NOFLOAT
347   s1.aaa=3.0;s1.bbb=4.0;
348 #endif
349   s2.c1=s1.c1;
350   s2.i= 50;
351   s2.j=40;
352 #ifndef NOFLOAT
353   s2.aaa=4.0;s2.bbb=5.0;
354 #endif
355   if (s1.c1 != 'x' || s1.i != 40 ) e(1);
356 #ifndef NOFLOAT
357   if ( s1.aaa != 3.0 ) e(1);
358 #endif
359   i = 25;j=75;
360   if (s1.i != 40 || s2.i != 50 ) e(2);
361   if ( s2.j != 40 || s1.j != 50 ) e(3);
362   if ( (s1.c1 + s2.c1)/2 != 'x' ) e(4);
363 #ifndef NOFLOAT
364   if ( s1.aaa*s1.aaa+s2.aaa*s2.aaa != s2.bbb*s2.bbb) e(5);
365 #endif
366   s1.i = 34; if ( i!=25 ) e(6);
367 }
368
369
370
371 /***********************************************************************/
372 test9()
373 /*global pointers */
374 { t=9; pct++;
375   p1=alloc( sizeof *p1 );
376   p2=alloc( sizeof *p2);
377   p3=alloc(sizeof *p3);
378   *p1 = 1066;
379   if ( *p1 != 1066 ) e(1);
380   p3->i = 1215;
381   if ( p3->i != 1215 ) e(2);
382   p2->val = 1566;
383   if ( p2->val != 1566 || p2->next ) e(3);
384   if ( a1 != &a1[0] ) e(4);
385   p1 = a1;
386   if ( ++p1 != &a1[1] ) e(5);
387   head = 0;
388   for (i=0;i<=100;i += 1)
389   { tail = alloc(sizeof *p2);
390     tail->val = 100+i;tail->next = head;
391     head = tail;
392   }
393   if ( tail->val != 200 || tail->next->val != 199 ) e(6);
394   if ( tail->next->next->next->next->next->val != 195) e(7);
395   tail->next->next->next->next->next->val = 1;
396   if ( tail->next->next->next->next->next->val != 1) e(8);
397   i = 27;
398   if ( *&i != 27 ) e(9);
399   if ( &*&*&*&i != &i ) e(10);
400   p1 = &i;i++;
401   if ( p1 != &i ) e(11);
402 }
403
404 /*****************************************************************/
405 test10()
406 /*local pointers */
407 { struct tp2 *pp3;
408   struct node *pp2,*ingang,*uitgang;
409   int *pp1;
410   int b1[20];
411   t=10; pct++;
412   pp1=alloc( sizeof *pp1 );
413   pp2=alloc( sizeof *p2);
414   pp3=alloc(sizeof *pp3);
415   *pp1 = 1066;
416   if ( *pp1 != 1066 ) e(1);
417   pp3->i = 1215;
418   if ( pp3->i != 1215 ) e(2);
419   pp2->val = 1566;
420   if ( pp2->val != 1566 || p2->next ) e(3);
421   if ( b1 != &b1[0] ) e(4);
422   pp1 = b1;
423   if ( ++pp1 != &b1[1] ) e(5);
424   ingang = 0;
425   for (i=0;i<=100;i += 1)
426   { uitgang = alloc(sizeof *pp2);
427     uitgang->val = 100+i;uitgang->next = ingang;
428     ingang = uitgang;
429   }
430   if ( uitgang->val != 200 || uitgang->next->val != 199 ) e(6);
431   if ( uitgang->next->next->next->next->next->val != 195 ) e(7);
432   uitgang->next->next->next->next->next->val = 1;
433   if ( uitgang->next->next->next->next->next->val != 1) e(8);
434 }
435
436 /***************************************************************/
437
438 #ifndef NOFLOAT
439 test11()
440 /* real arithmetic  */
441 {
442   double epsd; float epsf;
443   t = 11; pct++; epsf = 1e-7; epsd = 1e-16;
444   xf = 1.50 ; yf = 3.00 ; zf = 0.10;
445   xd = 1.50 ; yd = 3.00 ; zd = 0.10;
446   if ( fabs(1.0 + 1.0 - 2.0 ) > epsd ) e(1);
447   if ( fabs( 1e10-1e10 ) > epsd ) e(2);
448   if ( fabs( 1.0e+5*1.0e+5-100e+8 ) > epsd ) e(3);
449   if ( fabs( 10.0/3.0*3.0/10.0-100e-2 ) > epsd ) e(4);
450   if ( 0.0e0 != 0 ) e(5);
451   if ( fabs( 32767.0 - 32767 ) > epsd ) e(6);
452   if ( fabs( 1.0+2+5+3.0e0+7.5e+1+140e-1-100.0 ) > epsd ) e(7);
453   if ( fabs(-1+(-1)+(-1.0)+(-1.0e0)+(-1.0e-0)+(-1e0)+6 ) > epsd ) e(8);
454   if ( fabs(5.0*yf*zf-xf) > epsf ) e(9);
455   if ( fabs(5.0*yd*zd-xd) > epsd ) e(10);
456   if ( fabs(yd*yd - (2.0*xd)*(2.0*xd) ) > epsd ) e(11);
457   if ( fabs(yf*yf - (2.0*xf)*(2.0*xf) ) > epsf ) e(12);
458   if ( fabs( yd*yd+zd*zd+2.0*yd*zd-(yd+zd)*(zd+yd) ) > epsd ) e(13);
459   if ( fabs( yf*yf+zf*zf+2.0*yf*zf-(yf+zf)*(zf+yf) ) > epsf ) e(14);
460   xf=1.10;yf=1.20;
461   if ( yd<xd ) e(15);
462   if ( yd<=xd ) e(16);
463   if ( yd==xd ) e(17);
464   if ( xd>=yd ) e(18);
465   if ( yd<xd ) e(19);
466   if ( fabs(yd-xd-1.5) > epsd ) e(20);
467 }
468 #endif
469
470
471 /*****************************************************************/