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