1 /*
2  *     Copyright 1988, University Corporation for Atmospheric Research
3  *        Not for Resale. All copies to include this notice.
4  */
5 
6 #include "h4config.h"
7 
8 /*
9  * This program tests only the xdr library functionality required by netCDF.
10  *  'xdr_vector' is not used by the netCDF, it is used here for convenience.
11  */
12 
13 #ifdef H4_HAVE_NETINET_IN_H
14 #  include <netinet/in.h>     /* for htonl() */
15 #else
16 #  ifdef H4_HAVE_WINSOCK2_H
17 #    include <winsock2.h>
18 #  endif
19 #endif
20 
21 # define xdr_assert(ex)    {if (!(ex)){(void)fprintf(stderr,"Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);}}
22 
23 #ifndef    H4_NO_SYS_XDR_INC
24 #include <types.h>
25 #include <xdr.h>
26 #else
27 #include "types.h"
28 #include "xdr.h"
29 #endif
30 
31 #define TESTFILE "test.xdr"
32 /* if this is NOT defined, then the program just reads the file */
33 #define CREATE
34 
35 #ifdef __FreeBSD__
36 #define EPSILON .005
37 #else /* __FreeBSD__ */
38 #define EPSILON .0005
39 #endif /* __FreeBSD__ */
40 
main(ac,av)41 int main(ac,av)
42 int ac ;
43 char *av[] ;
44 {
45     int ii ;
46     const char *fname ;
47     FILE *F ;
48     XDR xdrs[1] ;
49     size_t count ;
50     u_int szof ;
51 
52     /* some random numbers, divisible by 4 and less than 32k */
53     static u_int seeks[] =
54     {
55         16828 ,
56         8448 ,
57         20632 ,
58         8124 ,
59         16764 ,
60         17232 ,
61         3476 ,
62         28168 , /* +(5 * sizeof(double) + (5 * sizeof(enum) */
63         /* */
64         28108 ,
65         16796 ,
66         10968 ,
67         24104 ,
68         30560 ,
69         16880 ,
70         17260 ,
71         12556 ,
72     } ;
73 
74     u_int poses[9] ;
75     int jj = 0 ;
76 
77     static char text[32] ={ "Hiya sailor. New in town?" };
78     char got_s[32] ;
79 
80     static unsigned char bytes[8] = { 254, 255, 0, 1, 2} ;
81     unsigned char *bp , got_ab[8] ;
82 
83     static int ints[5] = { 5, 6, 7, 8, 9} ;
84     int *ip , got_ip[5] ;
85 
86     static unsigned int u_ints[5] = {
87         ((unsigned)65535),
88         ((unsigned)65534),
89         ((unsigned)0),
90         ((unsigned)1),
91         ((unsigned)2)
92     } ;
93     unsigned int *uip , got_uip[5] ;
94 
95     long lnum = 82555 ;
96 
97     static long longs[5] = { -4, -3, -2, -1, 0} ;
98     long *lp , got_al[5] ;
99 
100     static unsigned long u_longs[5] = {
101         ((unsigned)65535),
102         ((unsigned)65534),
103         ((unsigned)0),
104         ((unsigned)1),
105         ((unsigned)2)
106     } ;
107     unsigned long *ulp , got_aul[5] ;
108 
109     static float floats[5] = { 100.125, 100.25, 100.375, 100.5, 100.625 } ;
110     float *fp , got_af[5] ;
111 
112     /* These all require 25 bits: 2^(25-i)+1/2^i, i = 2, 3, ..., 6 */
113     static double doubles[5] = { 8388608.25, 4194304.125, 2097152.0625,
114                     1048576.03125, 524288.015625 } ;
115 
116     double *dp , got_ad[5] ;
117 
118     typedef enum {ZERO_E, ONE_E, TWO_E, THREE_E, FOUR_E} encount ;
119     static encount encounts[5] = {ZERO_E, ONE_E, TWO_E, THREE_E, FOUR_E} ;
120     encount *ep , got_ep[5] ;
121 
122 
123 #ifdef MDEBUG
124     malloc_debug(2) ;
125 #endif
126     fname = TESTFILE ;
127     if(ac > 1)
128     {
129         fname = av[1] ;
130     }
131 #ifdef CREATE
132 /* Create */
133 
134 #ifdef __STDC__
135     F = fopen(fname,"wb") ;
136 #else
137     F = fopen(fname,"w") ;
138 #endif
139     if( F == NULL)
140     {
141         perror("fopen failed") ;
142         exit(-1) ;
143     }
144 
145     /* fill the file so seeks will work even on non-virtual machines */
146     for(ii=0 ; ii< 28227 ; ii++)
147     {
148         xdr_assert( putc(0, F) != EOF ) ;
149     }
150 
151     xdrstdio_create(xdrs, F, XDR_ENCODE) ;
152 
153     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
154     xdr_assert( xdr_opaque(xdrs, text, sizeof(text))) ;
155     poses[jj++] = xdr_getpos(xdrs) ;
156 
157     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
158     xdr_assert( xdr_opaque(xdrs, (caddr_t)bytes, sizeof(bytes))) ;
159 
160     /* no setpos, just for variety */
161     szof = sizeof(int) ;
162     count = sizeof(ints)/sizeof(int) ;
163     xdr_assert( xdr_vector(xdrs, (char *)ints, count, szof, xdr_int)) ;
164     poses[jj++] = xdr_getpos(xdrs) ;
165 
166     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
167     szof = sizeof(unsigned int) ;
168     count = sizeof(u_ints)/sizeof(unsigned int) ;
169     xdr_assert( xdr_vector(xdrs, (char *)u_ints, count, szof, xdr_u_int)) ;
170     poses[jj++] = xdr_getpos(xdrs) ;
171 
172     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
173     xdr_assert( xdr_long(xdrs, &lnum)) ;
174     poses[jj++] = xdr_getpos(xdrs) ;
175 
176     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
177     szof = sizeof(long) ;
178     count = sizeof(longs)/sizeof(long) ;
179     xdr_assert( xdr_vector(xdrs, (char *)longs, count, szof, xdr_long)) ;
180     poses[jj++] = xdr_getpos(xdrs) ;
181 
182     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
183     szof = sizeof(unsigned long) ;
184     count = sizeof(u_longs)/sizeof(unsigned long) ;
185     xdr_assert( xdr_vector(xdrs, (char *)u_longs, count, szof, xdr_u_long)) ;
186     poses[jj++] = xdr_getpos(xdrs) ;
187 
188     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
189     szof = sizeof(float) ;
190     count = sizeof(floats)/sizeof(float) ;
191     xdr_assert( xdr_vector(xdrs, (char *)floats, count, szof, xdr_float)) ;
192     poses[jj++] = xdr_getpos(xdrs) ;
193 
194     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
195     szof = sizeof(double) ;
196     count = sizeof(doubles)/sizeof(double) ;
197     xdr_assert( xdr_vector(xdrs, (char *)doubles, count, szof, xdr_double)) ;
198     poses[jj++] = xdr_getpos(xdrs) ;
199 
200     /* again no setpos  */
201     szof = sizeof(encount) ;
202     count = sizeof(encounts)/sizeof(encount) ;
203     xdr_assert( xdr_vector(xdrs, (char *)encounts, count, szof, xdr_enum)) ;
204     poses[jj++] = xdr_getpos(xdrs) ;
205 
206 #if 0
207     /* stdio performance check */
208     count = 8192 ;
209     for( lnum = 0 ; lnum < count ; lnum++)
210     {
211         xdr_assert( xdr_long(xdrs, &lnum) ) ;
212     }
213 #endif
214 
215 /* flush, rewind  and reopen */
216 
217     xdr_assert(fflush((FILE *)xdrs->x_private) != EOF) ; /* xdr_destroy(xdrs) */
218 
219     xdr_assert(fclose(F) != EOF) ;
220 #endif /* CREATE */
221 #ifdef __STDC__
222     F = fopen(fname,"rb") ;
223 #else
224     F = fopen(fname,"r") ;
225 #endif
226     if( F == NULL)
227     {
228         perror("fopen failed") ;
229         exit(-1) ;
230     }
231     xdrstdio_create(xdrs, F, XDR_DECODE) ;
232 
233     jj = 0 ;
234 
235 /* check contents */
236 
237     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
238     xdr_assert( xdr_opaque(xdrs, got_s, sizeof(text))) ;
239     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
240     printf("string: %s\n", got_s) ;
241 
242     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
243     xdr_assert( xdr_opaque(xdrs, (caddr_t)got_ab, sizeof(bytes))) ;
244     printf("unsigned bytes: ");
245     for(ii = 0, bp = got_ab ;
246             ii < (int)sizeof(bytes) ; ii++, bp++)
247     {
248         printf("%u ", *bp) ;
249         xdr_assert( *bp == bytes[ii] ) ;
250     }
251     putchar('\n') ;
252 
253     szof = sizeof(int) ;
254     count = sizeof(ints)/sizeof(int) ;
255     xdr_assert( xdr_vector(xdrs, (char *)got_ip, count, szof, xdr_int)) ;
256     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
257     printf("ints: ");
258     for(ii = 0, ip = got_ip ;
259             ii < (int)count ; ii++, ip++)
260     {
261         printf("%d ", *ip) ;
262         xdr_assert( *ip == ints[ii] ) ;
263     }
264     putchar('\n') ;
265 
266     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
267     szof = sizeof(unsigned int) ;
268     count = sizeof(u_ints)/sizeof(unsigned int) ;
269     xdr_assert( xdr_vector(xdrs, (char *)got_uip, count, szof, xdr_u_int)) ;
270     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
271     printf("unsigned ints: ");
272     for(ii = 0, uip = got_uip ;
273             ii < (int)count ; ii++, uip++)
274     {
275         printf("%u ", *uip) ;
276         xdr_assert( *uip == u_ints[ii] ) ;
277     }
278     putchar('\n') ;
279 
280     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
281     xdr_assert( xdr_long(xdrs, got_al)) ;
282     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
283     printf("LONG: %ld\n", *got_al) ;
284 
285     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
286     szof = sizeof(long) ;
287     count = sizeof(longs)/sizeof(long) ;
288     xdr_assert( xdr_vector(xdrs, (char *)got_al, count, szof, xdr_long)) ;
289     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
290     printf("longs: ");
291     for(ii = 0, lp = got_al ;
292             ii < (int)count ; ii++, lp++)
293     {
294         printf("%d ", (int)*lp) ;
295         xdr_assert( (int)*lp == (int)longs[ii] ) ;
296     }
297     putchar('\n') ;
298 
299     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
300     szof = sizeof(unsigned long) ;
301     count = sizeof(u_longs)/sizeof(unsigned long) ;
302     xdr_assert( xdr_vector(xdrs, (char *)got_aul, count, szof, xdr_u_long)) ;
303     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
304     printf("unsigned longs: ");
305     for(ii = 0, ulp = got_aul ;
306             ii < (int)count ; ii++, ulp++)
307     {
308         printf("%lu ", *ulp) ;
309         xdr_assert( *ulp == u_longs[ii] ) ;
310     }
311     putchar('\n') ;
312 
313     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
314     szof = sizeof(float) ;
315     count = sizeof(floats)/sizeof(float) ;
316     xdr_assert( xdr_vector(xdrs, (char *)got_af, count, szof, xdr_float)) ;
317     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
318     printf("floats:\n");
319     for(ii = 0, fp = got_af ;
320             ii < (int)count ; ii++, fp++)
321     {
322         printf("\t% .6e\n", *fp) ;
323         xdr_assert( *fp < floats[ii] + EPSILON &&  *fp > floats[ii] - EPSILON ) ;
324     }
325     putchar('\n') ;
326 
327     xdr_assert( xdr_setpos(xdrs, seeks[jj])) ;
328     szof = sizeof(double) ;
329     count = sizeof(doubles)/sizeof(double) ;
330     xdr_assert( xdr_vector(xdrs, (char *)got_ad, count, szof, xdr_double)) ;
331     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
332     printf("doubles:\n");
333     for(ii = 0, dp = got_ad ;
334             ii < (int)count ; ii++, dp++)
335     {
336         printf("\t% .12e\n", *dp) ;
337         xdr_assert( (*dp < (doubles[ii] + (double)EPSILON)) && (*dp > (doubles[ii] - (double)EPSILON) )) ;
338     }
339     putchar('\n') ;
340 
341     szof = sizeof(encount) ;
342     count = sizeof(encounts)/sizeof(encount) ;
343     xdr_assert( xdr_vector(xdrs, (char *)got_ep, count, szof, xdr_enum)) ;
344     printf("enums: ");
345     for(ii = 0, ep = got_ep ;
346             ii < (int)count ; ii++, ep++)
347     {
348         printf("%d ", (int)*ep) ;
349         xdr_assert( *ep == encounts[ii] ) ;
350     }
351     putchar('\n') ;
352     xdr_assert( poses[jj++] = xdr_getpos(xdrs) ) ;
353 
354 #if 0
355     /* stdio performance check */
356     count = 8192 ;
357     for( ii = 0 ; ii < count ; ii++)
358     {
359         xdr_assert( xdr_long(xdrs, got_al) ) ;
360         xdr_assert( *got_al == ii ) ;
361     }
362 #endif
363     exit(0) ;
364 }
365