PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/other/netcdf_write_matrix/src/libsrc/t_ncx.c

http://github.com/jbeezley/wrf-fire
C | 1250 lines | 969 code | 169 blank | 112 comment | 278 complexity | 3003d0f60710fd9d4d8d63976195095b MD5 | raw file
Possible License(s): AGPL-1.0
  1. /*
  2. * Copyright 1996, University Corporation for Atmospheric Research
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. */
  5. /* $Id: t_ncx.c,v 1.44 1998/03/06 22:11:18 davis Exp $ */
  6. #include <stdio.h>
  7. #include <limits.h>
  8. /* alias poorly named limits.h macros */
  9. #define SHORT_MAX SHRT_MAX
  10. #define SHORT_MIN SHRT_MIN
  11. #define USHORT_MAX USHRT_MAX
  12. #include <rpc/types.h>
  13. #include <rpc/xdr.h>
  14. #include <string.h>
  15. #include "ncx.h"
  16. #define NO_UNSIGNED
  17. #define NO_UNSIGNED_LONG
  18. /*
  19. * This program tests the xdr_mem implementation and
  20. * the ncx_ implementation, and compares the two.
  21. * Link like this:
  22. * cc t_ncx.c ncx.o [-lsome_xdr_lib] -o t_nxc
  23. * Successful output is:
  24. xdr_encode ends at byte 640
  25. xdr_check ends at byte 640
  26. ncx_encode ends at byte 640
  27. ncx_check ends at byte 640
  28. xdr_check ends at byte 640
  29. ncx_check ends at byte 640
  30. * with exit status 0;
  31. */
  32. #define XBSZ 1024
  33. char xdrb[XBSZ];
  34. char ncxb[XBSZ];
  35. #define ArraySize(thang) (sizeof(thang)/sizeof(thang[0]))
  36. #define uiArraySize(thang) ((u_int)ArraySize(thang))
  37. #define eSizeOf(thang) ((u_int)(sizeof(thang[0])))
  38. /*
  39. * Some test data
  40. */
  41. static char text[] = { "Hiya sailor. New in town?" };
  42. /*
  43. * Some test data
  44. * The ideas is that ncx_putn_type_type(...., types)
  45. * should not return NC_ERANGE.
  46. */
  47. #if SCHAR_MAX == X_SCHAR_MAX && SCHAR_MIN == X_SCHAR_MIN
  48. static schar schars[] = {
  49. SCHAR_MIN, SCHAR_MIN +1,
  50. -1, 0, 1,
  51. SCHAR_MAX - 1, SCHAR_MAX
  52. };
  53. #else
  54. /* The implementation and this test assume 8 bit bytes. */
  55. #error "Not 8 bit bytes ??"
  56. #endif
  57. static short shorts[] = {
  58. #if SHORT_MAX <= X_SHORT_MAX
  59. SHORT_MIN, SHORT_MIN + 1,
  60. # if SCHAR_MAX < X_SHORT_MAX
  61. SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
  62. # endif
  63. -1, 0, 1,
  64. # if SCHAR_MAX < X_SHORT_MAX
  65. SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
  66. # endif
  67. SHORT_MAX - 1, SHORT_MAX
  68. #else
  69. X_SHORT_MIN, X_SHORT_MIN + 1,
  70. # if SCHAR_MAX < X_SHORT_MAX
  71. SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
  72. # endif
  73. -1, 0, 1,
  74. # if SCHAR_MAX < X_SHORT_MAX
  75. SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
  76. # endif
  77. X_SHORT_MAX - 1, X_SHORT_MAX
  78. #endif
  79. };
  80. static int ints[] = {
  81. #if INT_MAX <= X_INT_MAX
  82. INT_MIN, INT_MIN +1,
  83. # if SHORT_MAX < X_INT_MAX
  84. SHORT_MIN -1, SHORT_MIN, SHORT_MIN + 1,
  85. # endif
  86. # if SCHAR_MAX < X_INT_MAX
  87. SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
  88. # endif
  89. -1, 0, 1,
  90. # if SCHAR_MAX < X_INT_MAX
  91. SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
  92. # endif
  93. # if SHORT_MAX < X_INT_MAX
  94. SHORT_MAX - 1, SHORT_MAX, SHORT_MAX +1,
  95. # endif
  96. INT_MAX - 1, INT_MAX
  97. #else
  98. X_INT_MIN, X_INT_MIN +1,
  99. # if SHORT_MAX < X_INT_MAX
  100. SHORT_MIN -1, SHORT_MIN, SHORT_MIN + 1,
  101. # endif
  102. # if SCHAR_MAX < X_INT_MAX
  103. SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
  104. # endif
  105. -1, 0, 1,
  106. # if SCHAR_MAX < X_INT_MAX
  107. SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
  108. # endif
  109. # if SHORT_MAX < X_INT_MAX
  110. SHORT_MAX - 1, SHORT_MAX, SHORT_MAX +1,
  111. # endif
  112. X_INT_MAX - 1, X_INT_MAX
  113. #endif /* INT */
  114. };
  115. /* N.B. only testing longs over X_INT range for now */
  116. static long longs[] = {
  117. #if LONG_MAX <= X_INT_MAX
  118. LONG_MIN, LONG_MIN +1,
  119. # if INT_MAX < X_INT_MAX
  120. INT_MIN -1, INT_MIN, INT_MIN + 1,
  121. # endif
  122. # if SHORT_MAX < X_INT_MAX
  123. SHORT_MIN -1, SHORT_MIN, SHORT_MIN + 1,
  124. # endif
  125. # if SCHAR_MAX < X_INT_MAX
  126. SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
  127. # endif
  128. -1, 0, 1,
  129. # if SCHAR_MAX < X_INT_MAX
  130. SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
  131. # endif
  132. # if SHORT_MAX < X_INT_MAX
  133. SHORT_MAX - 1, SHORT_MAX, SHORT_MAX +1,
  134. # endif
  135. # if INT_MAX < X_INT_MAX
  136. INT_MAX -1, INT_MAX, INT_MAX + 1,
  137. # endif
  138. LONG_MAX - 1, LONG_MAX
  139. #else
  140. X_INT_MIN, X_INT_MIN +1,
  141. # if SHORT_MAX < X_INT_MAX
  142. SHORT_MIN -1, SHORT_MIN, SHORT_MIN + 1,
  143. # endif
  144. # if SCHAR_MAX < X_INT_MAX
  145. SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
  146. # endif
  147. -1, 0, 1,
  148. # if SCHAR_MAX < X_INT_MAX
  149. SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
  150. # endif
  151. # if SHORT_MAX < X_INT_MAX
  152. SHORT_MAX - 1, SHORT_MAX, SHORT_MAX +1,
  153. # endif
  154. X_INT_MAX - 1, X_INT_MAX
  155. #endif
  156. };
  157. static float floats[] = {
  158. -100.625, -100.5, -100.375, -100.25, -100.125,
  159. -1.0, -.125, 0., .125, 1.,
  160. 100.125, 100.25, 100.375, 100.5, 100.625
  161. };
  162. /* The big numbers require 25 bits: 2^(25-i)+1/2^i, i = 2, 3, ..., 6 */
  163. static double doubles[] = {
  164. -8388608.25, -4194304.125, -2097152.0625, -1048576.03125, -524288.015625
  165. -100.625, -100.5, -100.375, -100.25, -100.125,
  166. -1.0, -.125, 0., .125, 1.,
  167. 100.125, 100.25, 100.375, 100.5, 100.625,
  168. 524288.015625, 1048576.03125, 2097152.0625, 4194304.125, 8388608.25
  169. };
  170. /* End of test data */
  171. /*
  172. * Copyright 1993, University Corporation for Atmospheric Research
  173. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  174. */
  175. /* $Id: t_ncx.c,v 1.44 1998/03/06 22:11:18 davis Exp $ */
  176. /* putget.c */
  177. /*
  178. * xdr 1 - 3 bytes, leaving adjoining bytes within the word ok.
  179. * (minimum unit of io is 4 bytes)
  180. */
  181. static bool_t
  182. xdr_NCvbyte(XDR *xdrs, unsigned rem, unsigned count, char *value)
  183. {
  184. char buf[4] ;
  185. u_int origin ;
  186. enum xdr_op x_op = xdrs->x_op ; /* save state */
  187. if(x_op == XDR_ENCODE)
  188. {
  189. /*
  190. * Since we only read/write multiples of four bytes,
  191. * We will read in the word to change one byte in it.
  192. */
  193. origin = xdr_getpos( xdrs ) ;
  194. /* next op is a get */
  195. xdrs->x_op = XDR_DECODE ;
  196. }
  197. if(!xdr_opaque(xdrs, buf, 4))
  198. {
  199. /* get failed, assume we are trying to read off the end */
  200. (void)memset(buf, 0, sizeof(buf)) ;
  201. }
  202. if(x_op == XDR_ENCODE) /* back to encode */
  203. xdrs->x_op = x_op ;
  204. while(count-- != 0)
  205. {
  206. if(x_op == XDR_ENCODE)
  207. buf[rem] = *value ;
  208. else
  209. *value = buf[rem] ;
  210. rem++ ;
  211. value++ ;
  212. }
  213. if(x_op == XDR_ENCODE)
  214. {
  215. if( !xdr_setpos(xdrs, origin) )
  216. return(FALSE) ;
  217. if( !xdr_opaque(xdrs, buf, 4))
  218. return(FALSE) ;
  219. }
  220. return(TRUE) ;
  221. }
  222. /* xdrshorts.c */
  223. /* you may wish to tune this: big on a cray, small on a PC? */
  224. #define NC_SHRT_BUFSIZ 8192
  225. #define NC_NSHRTS_PER (NC_SHRT_BUFSIZ/2) /* number of netshorts the buffer holds */
  226. /*
  227. * xdr a short leaving adjoining short within the word ok.
  228. * (minimum unit of io is 4 bytes)
  229. */
  230. static bool_t
  231. xdr_NCvshort(XDR *xdrs, unsigned which, short *value)
  232. {
  233. unsigned char buf[4] ; /* unsigned is important here */
  234. u_int origin ;
  235. enum xdr_op x_op = xdrs->x_op ; /* save state */
  236. if(x_op == XDR_ENCODE)
  237. {
  238. origin = xdr_getpos( xdrs ) ;
  239. /* next op is a get */
  240. xdrs->x_op = XDR_DECODE ;
  241. }
  242. if(!xdr_opaque(xdrs, (caddr_t)buf, 4))
  243. {
  244. /* get failed, assume we are trying to read off the end */
  245. (void)memset(buf, 0, sizeof(buf)) ;
  246. }
  247. if(x_op == XDR_ENCODE) /* back to encode */
  248. xdrs->x_op = x_op ;
  249. if(which != 0) which = 2 ;
  250. if(xdrs->x_op == XDR_ENCODE)
  251. {
  252. buf[which +1] = *value % 256 ;
  253. buf[which] = (*value >> 8) ;
  254. if( !xdr_setpos(xdrs, origin) )
  255. return(FALSE) ;
  256. if( !xdr_opaque(xdrs, (caddr_t)buf, 4))
  257. return(FALSE) ;
  258. }
  259. else
  260. {
  261. *value = (((unsigned)buf[which] & 0x7f) << 8) +
  262. (unsigned)buf[which + 1] ;
  263. if((unsigned)buf[which] & 0x80)
  264. {
  265. /* extern is neg */
  266. *value -= 0x8000 ;
  267. }
  268. }
  269. return(TRUE) ;
  270. }
  271. /*
  272. * internal function, bulk xdr of an even number of shorts, less than NC_NSHRTS_PER
  273. */
  274. static
  275. bool_t
  276. NCxdr_shortsb(XDR *xdrs, short *sp, u_int nshorts)
  277. {
  278. unsigned char buf[NC_SHRT_BUFSIZ] ;
  279. unsigned char *cp ;
  280. unsigned int nbytes = nshorts * 2;
  281. /* assert(nshorts <= NC_NSHRTS_PER) ; */
  282. /* assert(nshorts > 0) ; */
  283. if(xdrs->x_op == XDR_ENCODE)
  284. {
  285. for(cp = buf ; cp < &buf[nbytes] ; sp++, cp += 2 )
  286. {
  287. *(cp +1) = *sp % 256 ;
  288. *cp = (*sp >> 8) ;
  289. }
  290. }
  291. if(!xdr_opaque(xdrs, (caddr_t)buf, nbytes))
  292. return FALSE ;
  293. if(xdrs->x_op == XDR_DECODE)
  294. {
  295. for(cp = buf ; cp < &buf[nbytes] ; sp++, cp += 2 )
  296. {
  297. *sp = (((unsigned)*cp & 0x7f) << 8) +
  298. (unsigned)*(cp +1) ;
  299. if((unsigned)*cp & 0x80)
  300. {
  301. /* extern is neg */
  302. *sp -= 0x8000 ;
  303. }
  304. }
  305. }
  306. return TRUE ;
  307. }
  308. /*
  309. * Translate an array of cnt short integers at sp.
  310. */
  311. bool_t
  312. xdr_shorts(XDR *xdrs, short *sp, u_int cnt)
  313. {
  314. int odd ; /* 1 if cnt is odd, 0 otherwise */
  315. if(cnt == 0)
  316. return TRUE ; /* ? */
  317. odd = cnt % 2 ;
  318. if(odd)
  319. cnt-- ;
  320. /* cnt is even, odd is set if apropos */
  321. while(cnt > NC_NSHRTS_PER)
  322. {
  323. if(!NCxdr_shortsb(xdrs, sp, NC_NSHRTS_PER))
  324. return FALSE ;
  325. /* else */
  326. sp += NC_NSHRTS_PER ;
  327. cnt -= NC_NSHRTS_PER ;
  328. }
  329. /* we know cnt <= NC_NSHRTS_PER at this point */
  330. if(cnt != 0)
  331. {
  332. if(!NCxdr_shortsb(xdrs, sp, cnt))
  333. return FALSE ;
  334. /* else */
  335. sp += cnt ;
  336. cnt = 0 ;
  337. }
  338. if(odd)
  339. if(!xdr_NCvshort(xdrs, 0, sp))
  340. return FALSE ;
  341. return TRUE ;
  342. }
  343. /*
  344. * Use standard xdr interface (plus the netcdf xdr_shorts())
  345. * to encode data to 'buf'
  346. * Returns 0 on success.
  347. */
  348. static int
  349. xdr_encode(char *buf, u_int sz)
  350. {
  351. XDR xdrs[1];
  352. u_int pos;
  353. int ii;
  354. xdrmem_create(xdrs, buf, sz, XDR_ENCODE);
  355. if(!xdr_opaque(xdrs, (caddr_t)text, (u_int)sizeof(text)))
  356. return 1;
  357. if(!xdr_opaque(xdrs, (caddr_t)schars, (u_int)sizeof(schars)))
  358. return 2;
  359. if(!xdr_shorts(xdrs, shorts, uiArraySize(shorts)))
  360. return 3;
  361. if(!xdr_vector(xdrs, (char *)ints,
  362. uiArraySize(ints), eSizeOf(ints),
  363. (xdrproc_t)xdr_int))
  364. return 4;
  365. /* double the ints to check both ncx_ interfaces */
  366. if(!xdr_vector(xdrs, (char *)ints,
  367. uiArraySize(ints), eSizeOf(ints),
  368. (xdrproc_t)xdr_int))
  369. return 5;
  370. #ifndef NO_UNSIGNED
  371. if(!xdr_vector(xdrs, (char *)u_ints,
  372. uiArraySize(u_ints), eSizeOf(u_ints),
  373. (xdrproc_t)xdr_u_int))
  374. return 6;
  375. #endif
  376. if(!xdr_vector(xdrs, (char *)longs,
  377. uiArraySize(longs), eSizeOf(longs),
  378. (xdrproc_t)xdr_long))
  379. return 7;
  380. #ifndef NO_UNSIGNED_LONG
  381. if(!xdr_vector(xdrs, (char *)u_longs,
  382. uiArraySize(u_longs), eSizeOf(u_longs),
  383. (xdrproc_t)xdr_u_long))
  384. return 9;
  385. #endif
  386. if(!xdr_vector(xdrs, (char *)floats,
  387. uiArraySize(floats), eSizeOf(floats),
  388. (xdrproc_t)xdr_float))
  389. return 10;
  390. if(!xdr_vector(xdrs, (char *)doubles,
  391. uiArraySize(doubles), eSizeOf(doubles),
  392. (xdrproc_t)xdr_double))
  393. return 11;
  394. /* mix it up */
  395. for(ii = 1; ii < 5; ii++)
  396. {
  397. if(
  398. !xdr_opaque(xdrs, (caddr_t)text, ii)
  399. || !xdr_shorts(xdrs, shorts, ii)
  400. || !xdr_opaque(xdrs, (caddr_t)schars, ii)
  401. )
  402. return (11 + ii);
  403. }
  404. /*
  405. * Test non-aligned unit ops used by netcdf.
  406. */
  407. for(ii = 1; ii < 5; ii++)
  408. {
  409. pos = xdr_getpos(xdrs);
  410. if(!xdr_NCvbyte(xdrs, ii, BYTES_PER_XDR_UNIT -ii, &text[ii]))
  411. return (15 + ii);
  412. if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
  413. return (15 + ii);
  414. }
  415. for(ii = 1; ii < 5; ii++)
  416. {
  417. pos = xdr_getpos(xdrs);
  418. if(!xdr_NCvbyte(xdrs, ii, BYTES_PER_XDR_UNIT -ii,
  419. (char *)&schars[ii]))
  420. return (19 + ii);
  421. if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
  422. return (18 + ii);
  423. }
  424. for(ii = 1; ii < 3; ii++)
  425. {
  426. pos = xdr_getpos(xdrs);
  427. if(!xdr_NCvshort(xdrs, ii%2, &shorts[ii]))
  428. return (23 + ii);
  429. if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
  430. return (23 + ii);
  431. }
  432. pos = xdr_getpos(xdrs);
  433. (void) printf("xdr_encode ends at byte %u\n", pos);
  434. return 0;
  435. }
  436. static int
  437. cmp_chars(const char *c1, const char *c2, size_t nchars)
  438. {
  439. int status = 0;
  440. const char *const end = c1 + nchars;
  441. while(c1 < end)
  442. {
  443. if(*c1 != *c2)
  444. {
  445. (void) fprintf(stderr,
  446. "%c != %c char\n",
  447. *c1,
  448. *c2);
  449. if(status == 0)
  450. status = *c2 < *c1 ? -1 : 1;
  451. }
  452. c1++, c2++;
  453. }
  454. return status;
  455. }
  456. static int
  457. cmp_schars(const schar *b1, const schar *b2, size_t nbytes)
  458. {
  459. int status = 0;
  460. const schar *const end = b1 + nbytes;
  461. while(b1 < end)
  462. {
  463. if(*b1 != *b2)
  464. {
  465. (void) fprintf(stderr,
  466. "0x%02x != 0x%02x byte\n",
  467. (unsigned)(*b1),
  468. (unsigned)(*b2));
  469. if(status == 0)
  470. status = *b2 < *b1 ? -1 : 1;
  471. }
  472. b1++, b2++;
  473. }
  474. return status;
  475. }
  476. static int
  477. cmp_shorts(const short *s1, const short *s2, size_t nshorts)
  478. {
  479. int status = 0;
  480. const short *const end = s1 + nshorts;
  481. while(s1 < end)
  482. {
  483. if(*s1 != *s2)
  484. {
  485. (void) fprintf(stderr,
  486. "0x%04x != 0x%04x (%hd) short\n",
  487. (unsigned)(*s1),
  488. (unsigned)(*s2), *s2);
  489. if(status == 0)
  490. status = *s2 < *s1 ? -1 : 1;
  491. }
  492. s1++, s2++;
  493. }
  494. return status;
  495. }
  496. static int
  497. cmp_ints(const int *i1, const int *i2, size_t nints)
  498. {
  499. int status = 0;
  500. const int *const end = i1 + nints;
  501. while(i1 < end)
  502. {
  503. if(*i1 != *i2)
  504. {
  505. (void) fprintf(stderr,
  506. "0x%08x != 0x%08x int\n",
  507. (unsigned)(*i1),
  508. (unsigned)(*i2));
  509. if(status == 0)
  510. status = *i2 < *i1 ? -1 : 1;
  511. }
  512. i1++, i2++;
  513. }
  514. return status;
  515. }
  516. #ifndef NO_UNSIGNED
  517. static int
  518. cmp_u_ints(const unsigned int *i1, const unsigned int *i2, size_t nints)
  519. {
  520. int status = 0;
  521. const unsigned int *const end = i1 + nints;
  522. while(i1 < end)
  523. {
  524. if(*i1 != *i2)
  525. {
  526. (void) fprintf(stderr,
  527. "(%u) 0x%08x != 0x%08x (%u) uint\n",
  528. *i1, *i1,
  529. *i2, *i2);
  530. if(status == 0)
  531. status = *i2 < *i1 ? -1 : 1;
  532. }
  533. i1++, i2++;
  534. }
  535. return status;
  536. }
  537. #endif
  538. static int
  539. cmp_longs(const long *l1, const long *l2, size_t nlongs)
  540. {
  541. int status = 0;
  542. const long *const end = l1 + nlongs;
  543. while(l1 < end)
  544. {
  545. if(*l1 != *l2)
  546. {
  547. (void) fprintf(stderr,
  548. "0x%016lx != 0x%016lx long\n",
  549. (unsigned long)(*l1),
  550. (unsigned long)(*l2));
  551. if(status == 0)
  552. status = *l2 < *l1 ? -1 : 1;
  553. }
  554. l1++, l2++;
  555. }
  556. return status;
  557. }
  558. #ifndef NO_UNSIGNED_LONG
  559. static int
  560. cmp_u_longs(const unsigned long *l1, const unsigned long *l2, size_t nlongs)
  561. {
  562. int status = 0;
  563. const unsigned long *const end = l1 + nlongs;
  564. while(l1 < end)
  565. {
  566. if(*l1 != *l2)
  567. {
  568. (void) fprintf(stderr,
  569. "0x%016lx != 0x%016lx ulong\n",
  570. *l1,
  571. *l2);
  572. if(status == 0)
  573. status = *l2 < *l1 ? -1 : 1;
  574. }
  575. l1++, l2++;
  576. }
  577. return status;
  578. }
  579. #endif
  580. static int
  581. cmp_floats(const float *f1, const float *f2, size_t nfloats)
  582. {
  583. #define F_EPS 1.0e-6
  584. int status = 0;
  585. const float *const end = f1 + nfloats;
  586. while(f1 < end)
  587. {
  588. if(*f1 < *f2 && *f2 - *f1 > F_EPS)
  589. {
  590. (void) fprintf(stderr,
  591. "%.9e != %.9e float (diff %.9e)\n",
  592. *f1, *f2, *f1 - *f2);
  593. if(status == 0)
  594. status = 1;
  595. }
  596. else if( *f2 < *f1 && *f1 - *f2 > F_EPS)
  597. {
  598. (void) fprintf(stderr,
  599. "%.9e != %.9e float (diff %.9e)\n",
  600. *f1, *f2, *f1 - *f2);
  601. if(status == 0)
  602. status = -1;
  603. }
  604. f1++, f2++;
  605. }
  606. return status;
  607. }
  608. static int
  609. cmp_doubles(const double *d1, const double *d2, size_t ndoubles)
  610. {
  611. #define D_EPS 1.0e-15
  612. int status = 0;
  613. const double *const end = d1 + ndoubles;
  614. while(d1 < end)
  615. {
  616. if(*d1 < *d2 && *d2 - *d1 > D_EPS)
  617. {
  618. (void) fprintf(stderr,
  619. "%.17e != %.17e double (diff %.17e)\n",
  620. *d1, *d2, *d1 - *d2);
  621. if(status == 0)
  622. status = 1;
  623. }
  624. else if( *d2 < *d1 && *d1 - *d2 > D_EPS)
  625. {
  626. (void) fprintf(stderr,
  627. "%.17e != %.17e double (diff %.17e)\n",
  628. *d1, *d2, *d1 - *d2);
  629. if(status == 0)
  630. status = -1;
  631. }
  632. d1++, d2++;
  633. }
  634. return status;
  635. }
  636. /*
  637. * Verify that data in buf is as encoded
  638. * by xdr_encode() above.
  639. * Returns zero on sucess.
  640. */
  641. static int
  642. xdr_check(char *buf, u_int sz)
  643. {
  644. XDR xdrs[1];
  645. char tbuf[XBSZ];
  646. u_int pos;
  647. int ii;
  648. int jj;
  649. xdrmem_create(xdrs, buf, sz, XDR_DECODE);
  650. (void) memset(tbuf, 0, sizeof(text)+4);
  651. if(!xdr_opaque(xdrs, (caddr_t)tbuf, (u_int)sizeof(text))
  652. || cmp_chars(tbuf, text,
  653. sizeof(text)) != 0)
  654. return 1;
  655. (void) memset(tbuf, 0, sizeof(schars)+4);
  656. if(!xdr_opaque(xdrs, (caddr_t)tbuf, (u_int)sizeof(schars))
  657. || cmp_schars((schar *)tbuf, schars,
  658. sizeof(schars)) != 0)
  659. return 2;
  660. (void) memset(tbuf, 0, sizeof(shorts)+4);
  661. if(!xdr_shorts(xdrs, (short *)tbuf, uiArraySize(shorts))
  662. || cmp_shorts((short *)tbuf, shorts,
  663. ArraySize(shorts)) != 0)
  664. return 3;
  665. (void) memset(tbuf, 0, sizeof(ints)+4);
  666. if(!xdr_vector(xdrs, tbuf,
  667. uiArraySize(ints), eSizeOf(ints),
  668. (xdrproc_t)xdr_int)
  669. || cmp_ints((int *)tbuf, ints,
  670. ArraySize(ints)) != 0)
  671. return 4;
  672. /* double the ints to check both ncx_ interfaces */
  673. (void) memset(tbuf, 0, sizeof(ints)+4);
  674. if(!xdr_vector(xdrs, tbuf,
  675. uiArraySize(ints), eSizeOf(ints),
  676. (xdrproc_t)xdr_int)
  677. || cmp_ints((int *)tbuf, ints,
  678. ArraySize(ints)) != 0)
  679. return 5;
  680. #ifndef NO_UNSIGNED
  681. (void) memset(tbuf, 0, sizeof(u_ints)+4);
  682. if(!xdr_vector(xdrs, tbuf,
  683. uiArraySize(u_ints), eSizeOf(u_ints),
  684. (xdrproc_t)xdr_u_int)
  685. || cmp_u_ints((unsigned int *)tbuf, u_ints,
  686. ArraySize(u_ints)) != 0)
  687. return 6;
  688. #endif
  689. (void) memset(tbuf, 0, sizeof(longs)+4);
  690. if(!xdr_vector(xdrs, tbuf,
  691. uiArraySize(longs), eSizeOf(longs), (xdrproc_t)xdr_long)
  692. || cmp_longs((long *)tbuf, longs,
  693. ArraySize(longs)) != 0)
  694. return 7;
  695. #ifndef NO_UNSIGNED_LONG
  696. (void) memset(tbuf, 0, sizeof(u_longs)+4);
  697. if(!xdr_vector(xdrs, tbuf,
  698. uiArraySize(u_longs), eSizeOf(u_longs), (xdrproc_t)xdr_u_long)
  699. || cmp_u_longs((unsigned long *)tbuf, u_longs,
  700. ArraySize(u_longs)) != 0)
  701. return 9;
  702. #endif
  703. (void) memset(tbuf, 0, sizeof(floats)+4);
  704. if(!xdr_vector(xdrs, tbuf,
  705. uiArraySize(floats), eSizeOf(floats), (xdrproc_t)xdr_float)
  706. || cmp_floats((float *)tbuf, floats,
  707. ArraySize(floats)) != 0)
  708. return 10;
  709. (void) memset(tbuf, 0, sizeof(doubles)+4);
  710. if(!xdr_vector(xdrs, tbuf,
  711. uiArraySize(doubles), eSizeOf(doubles), (xdrproc_t)xdr_double)
  712. || cmp_doubles((double *)tbuf, doubles,
  713. ArraySize(doubles)) != 0)
  714. return 11;
  715. for(ii = 1; ii < 5; ii++)
  716. {
  717. char tx[4];
  718. short sh[4];
  719. schar by[4];
  720. if(
  721. !xdr_opaque(xdrs, (caddr_t)tx, ii)
  722. || !xdr_shorts(xdrs, sh, ii)
  723. || !xdr_opaque(xdrs, (caddr_t)by, ii)
  724. )
  725. return (11 + ii);
  726. for(jj = 0; jj < ii; jj++)
  727. {
  728. if(tx[jj] != text[jj])
  729. {
  730. (void) fprintf(stderr, "\txdr %c != %c text[%d]\n",
  731. tx[jj], text[jj], jj);
  732. return (11 + ii);
  733. }
  734. /* else */
  735. if(sh[jj] != shorts[jj])
  736. {
  737. (void) fprintf(stderr, "\txdr %hd != %hd shorts[%d]\n",
  738. sh[jj], shorts[jj], jj);
  739. return (11 + ii);
  740. }
  741. /* else */
  742. if(by[jj] != schars[jj])
  743. {
  744. (void) fprintf(stderr,
  745. "\txdr 0x%02x != 0x%02x schars[%d]\n",
  746. (unsigned) by[jj],
  747. (unsigned) schars[jj], jj);
  748. return (11 + ii);
  749. }
  750. /* else */
  751. }
  752. }
  753. /*
  754. * Test non-aligned unit ops used by netcdf.
  755. */
  756. for(ii = 1; ii < 5; ii++)
  757. {
  758. pos = xdr_getpos(xdrs);
  759. (void) memset(tbuf, 0, BYTES_PER_XDR_UNIT);
  760. if(!xdr_NCvbyte(xdrs, ii, BYTES_PER_XDR_UNIT -ii, tbuf)
  761. || cmp_chars(&text[ii], tbuf,
  762. BYTES_PER_XDR_UNIT -ii) != 0)
  763. return (15 + ii);
  764. if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
  765. return (15 + ii);
  766. }
  767. for(ii = 1; ii < 5; ii++)
  768. {
  769. pos = xdr_getpos(xdrs);
  770. (void) memset(tbuf, 0, BYTES_PER_XDR_UNIT);
  771. if(!xdr_NCvbyte(xdrs, ii, BYTES_PER_XDR_UNIT -ii, tbuf)
  772. || cmp_schars((schar *)tbuf, &schars[ii],
  773. BYTES_PER_XDR_UNIT -ii) != 0)
  774. return (19 + ii);
  775. if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
  776. return (19 + ii);
  777. }
  778. for(ii = 1; ii < 3; ii++)
  779. {
  780. pos = xdr_getpos(xdrs);
  781. (void) memset(tbuf, 0, BYTES_PER_XDR_UNIT);
  782. if(!xdr_NCvshort(xdrs, ii%2, (short *)tbuf)
  783. || cmp_shorts((short *)tbuf, &shorts[ii], 1))
  784. return (23 + ii);
  785. if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
  786. return (23 + ii);
  787. }
  788. pos = xdr_getpos(xdrs);
  789. (void) printf("xdr_check ends at byte %u\n", pos);
  790. return 0;
  791. }
  792. /* Poor man's template */
  793. #define NCX_VEC(xpp, nn, vecp, TYPE, proc, step) \
  794. { \
  795. \
  796. size_t nelems = (nn); \
  797. TYPE *elemp = (vecp); \
  798. \
  799. while(nelems != 0) \
  800. { \
  801. status = (proc)((*(xpp)), elemp); \
  802. if(status != ENOERR) \
  803. break; \
  804. (*(xpp)) = (void *)((char *)(*(xpp)) + (step)); \
  805. elemp ++; \
  806. nelems--; \
  807. } \
  808. }
  809. /*
  810. * Use ncx interface
  811. * to encode data to 'buf'
  812. * Returns zero on success.
  813. */
  814. static int
  815. ncx_encode(char *buf)
  816. {
  817. int status = ENOERR;
  818. void *vp = buf;
  819. int ii;
  820. if(ncx_pad_putn_text(&vp, sizeof(text), text))
  821. return 1;
  822. if(ncx_pad_putn_schar_schar(&vp, sizeof(schars), schars))
  823. return 2;
  824. if(ncx_pad_putn_short_short(&vp, ArraySize(shorts), shorts))
  825. return 3;
  826. if(ncx_putn_int_int(&vp, ArraySize(ints), ints))
  827. return 4;
  828. NCX_VEC(&vp, ArraySize(ints), ints,
  829. int, ncx_put_int_int, X_SIZEOF_INT);
  830. if(status != ENOERR)
  831. return 5;
  832. #ifndef NO_UNSIGNED
  833. NCX_VEC(&vp, ArraySize(u_ints), u_ints,
  834. unsigned int, ncx_put_uint_uint, X_SIZEOF_INT);
  835. if(status != ENOERR)
  836. return 6;
  837. #endif
  838. if(ncx_putn_int_long(&vp, ArraySize(longs), longs))
  839. return 7;
  840. #ifndef NO_UNSIGNED_LONG
  841. NCX_VEC(&vp, ArraySize(u_longs), u_longs,
  842. unsigned long, ncx_put_ulong_ulong, X_SIZEOF_LONG);
  843. if(status != ENOERR)
  844. return 9;
  845. #endif
  846. if(ncx_putn_float_float(&vp, ArraySize(floats), floats))
  847. return 10;
  848. if(ncx_putn_double_double(&vp, ArraySize(doubles), doubles))
  849. return 11;
  850. /* mix it up */
  851. for(ii = 1; ii < 5; ii++)
  852. {
  853. if(
  854. ncx_pad_putn_text(&vp, ii, text)
  855. || ncx_pad_putn_short_short(&vp, ii, shorts)
  856. || ncx_pad_putn_schar_schar(&vp, ii, schars)
  857. )
  858. return (11 + ii);
  859. }
  860. /*
  861. * Test non-aligned unit ops used by netcdf.
  862. */
  863. for(ii = 1; ii < 5; ii++)
  864. {
  865. vp = (char *)vp + ii;
  866. if(ncx_putn_text(&vp, X_ALIGN - ii, &text[ii]))
  867. return (15 + ii);
  868. }
  869. for(ii = 1; ii < 5; ii++)
  870. {
  871. vp = (char *)vp + ii;
  872. if(ncx_putn_schar_schar(&vp, X_ALIGN - ii, &schars[ii]))
  873. return (19 + ii);
  874. }
  875. for(ii = 1; ii < 3; ii++)
  876. {
  877. char *pos = vp;
  878. vp = (char *)vp + (ii%2) * 2;
  879. if(ncx_putn_short_short(&vp, 1, &shorts[ii]))
  880. return (23 + ii);
  881. vp = pos + X_ALIGN;
  882. }
  883. (void) printf("ncx_encode ends at byte %u\n",
  884. (unsigned)(((char *)vp) - buf));
  885. return 0;
  886. }
  887. /*
  888. * Verify the ncx_getn_xxx() routines.
  889. * Returns zero on success.
  890. */
  891. static int
  892. ncx_check(char *buf)
  893. {
  894. int status = ENOERR;
  895. const void *vp = buf;
  896. char tbuf[XBSZ];
  897. int ii;
  898. int jj;
  899. (void) memset(tbuf, 0, sizeof(text)+4);
  900. if(ncx_pad_getn_text(&vp, sizeof(text), tbuf)
  901. || cmp_chars(tbuf, text,
  902. sizeof(text)) != 0)
  903. return 1;
  904. (void) memset(tbuf, 0, sizeof(schars)+4);
  905. if(ncx_pad_getn_schar_schar(&vp, sizeof(schars), (schar *)tbuf)
  906. || cmp_schars((schar *)tbuf, schars,
  907. sizeof(schars)) != 0)
  908. return 2;
  909. (void) memset(tbuf, 0, sizeof(shorts)+4);
  910. if(ncx_pad_getn_short_short(&vp, ArraySize(shorts), (short *)tbuf)
  911. || cmp_shorts((short *)tbuf, shorts,
  912. ArraySize(shorts)) != 0)
  913. return 3;
  914. (void) memset(tbuf, 0, sizeof(ints)+4);
  915. if(ncx_getn_int_int(&vp, ArraySize(ints), (int *)tbuf)
  916. || cmp_ints((int *)tbuf, ints,
  917. ArraySize(ints)) != 0)
  918. return 4;
  919. (void) memset(tbuf, 0, sizeof(ints)+4);
  920. NCX_VEC(&vp, ArraySize(ints), (int *)tbuf,
  921. int, ncx_get_int_int, X_SIZEOF_INT);
  922. if(status != ENOERR
  923. || cmp_ints((int *)tbuf, ints,
  924. ArraySize(ints)) != 0)
  925. return 5;
  926. #ifndef NO_UNSIGNED
  927. (void) memset(tbuf, 0, sizeof(u_ints)+4);
  928. NCX_VEC(&vp, ArraySize(u_ints), (unsigned int *)tbuf,
  929. unsigned, ncx_get_uint_uint, X_SIZEOF_INT);
  930. if(status != ENOERR
  931. || cmp_u_ints((unsigned int *)tbuf, u_ints,
  932. ArraySize(u_ints)) != 0)
  933. return 6;
  934. #endif
  935. (void) memset(tbuf, 0, sizeof(longs)+4);
  936. if(ncx_getn_int_long(&vp, ArraySize(longs), (long *)tbuf)
  937. || cmp_longs((long *)tbuf, longs,
  938. ArraySize(longs)) != 0)
  939. return 7;
  940. #ifndef NO_UNSIGNED_LONG
  941. (void) memset(tbuf, 0, sizeof(u_longs)+4);
  942. NCX_VEC(&vp, ArraySize(u_longs), (unsigned long *)tbuf,
  943. unsigned long, ncx_get_ulong_ulong, X_SIZEOF_LONG);
  944. if(status != ENOERR
  945. || cmp_u_longs((unsigned long *)tbuf, u_longs,
  946. ArraySize(u_longs)) != 0)
  947. return 9;
  948. #endif
  949. (void) memset(tbuf, 0, sizeof(floats)+4);
  950. if(ncx_getn_float_float(&vp, ArraySize(floats), (float *)tbuf)
  951. || cmp_floats((float *)tbuf, floats,
  952. ArraySize(floats)) != 0)
  953. return 10;
  954. (void) memset(tbuf, 0, sizeof(doubles)+4);
  955. if(ncx_getn_double_double(&vp, ArraySize(doubles), (double *)tbuf)
  956. || cmp_doubles((double *)tbuf, doubles,
  957. ArraySize(doubles)) != 0)
  958. return 11;
  959. for(ii = 1; ii < 5; ii++)
  960. {
  961. char tx[4];
  962. short sh[4];
  963. schar by[4];
  964. if(
  965. ncx_pad_getn_text(&vp, ii, tx)
  966. || ncx_pad_getn_short_short(&vp, ii, sh)
  967. || ncx_pad_getn_schar_schar(&vp, ii, by)
  968. )
  969. return (11 + ii);
  970. for(jj = 0; jj < ii; jj++)
  971. {
  972. if(tx[jj] != text[jj])
  973. {
  974. (void) fprintf(stderr,
  975. "\tncx %c != %c text[%d]\n",
  976. tx[jj], text[jj], jj);
  977. return (11 + ii);
  978. }
  979. /* else */
  980. if(sh[jj] != shorts[jj])
  981. {
  982. (void) fprintf(stderr,
  983. "\tncx %hd != %hd shorts[%d]\n",
  984. sh[jj], shorts[jj], jj);
  985. return (11 + ii);
  986. }
  987. /* else */
  988. if((unsigned)by[jj] != (unsigned)schars[jj])
  989. {
  990. (void) fprintf(stderr,
  991. "\tncx 0x%02x != 0x%02x schars[%d] %d\n",
  992. by[jj], schars[jj], jj, ii);
  993. return (11 + ii);
  994. }
  995. }
  996. }
  997. /*
  998. * Test non-aligned unit ops used by netcdf.
  999. */
  1000. for(ii = 1; ii < 5; ii++)
  1001. {
  1002. (void) memset(tbuf, 0, X_ALIGN);
  1003. vp = (char *)vp + ii;
  1004. if(ncx_getn_text(&vp, X_ALIGN -ii, tbuf)
  1005. || cmp_chars(tbuf, &text[ii],
  1006. X_ALIGN -ii) != 0)
  1007. return (15 + ii);
  1008. }
  1009. for(ii = 1; ii < 5; ii++)
  1010. {
  1011. (void) memset(tbuf, 0, X_ALIGN);
  1012. vp = (char *)vp + ii;
  1013. if(ncx_getn_schar_schar(&vp, X_ALIGN -ii, (schar *)tbuf)
  1014. || cmp_schars((schar *)tbuf, &schars[ii],
  1015. X_ALIGN -ii) != 0)
  1016. return (19 + ii);
  1017. }
  1018. for(ii = 1; ii < 3; ii++)
  1019. {
  1020. const char *pos = vp;
  1021. (void) memset(tbuf, 0, X_ALIGN);
  1022. vp = (char *)vp + (ii%2) *2;
  1023. if(ncx_getn_short_short(&vp, 1, (short *)tbuf)
  1024. || cmp_shorts((short *)tbuf, &shorts[ii],
  1025. 1) != 0)
  1026. return (23 + ii);
  1027. vp = pos + X_ALIGN;
  1028. }
  1029. (void) printf("ncx_check ends at byte %u\n",
  1030. (unsigned)(((char *)vp) - buf));
  1031. return 0;
  1032. }
  1033. int
  1034. main(int ac, char *av[])
  1035. {
  1036. int status;
  1037. status = xdr_encode(xdrb, sizeof(xdrb));
  1038. if(status)
  1039. {
  1040. (void) fprintf(stderr,
  1041. "xdr_encode failed %d\n", status);
  1042. return 1;
  1043. }
  1044. status = xdr_check(xdrb, sizeof(xdrb));
  1045. if(status)
  1046. {
  1047. (void) fprintf(stderr,
  1048. "xdr_check of xdrb failed %d\n", status);
  1049. return 1;
  1050. }
  1051. status = ncx_encode(ncxb);
  1052. if(status)
  1053. {
  1054. (void) fprintf(stderr,
  1055. "ncx_encode failed %d\n", status);
  1056. return 1;
  1057. }
  1058. /* cross check */
  1059. status = xdr_check(ncxb, sizeof(ncxb));
  1060. if(status)
  1061. {
  1062. (void) fprintf(stderr,
  1063. "xdr_check of ncxb failed %d\n", status);
  1064. return 1;
  1065. }
  1066. status = ncx_check(xdrb);
  1067. if(status)
  1068. {
  1069. (void) fprintf(stderr,
  1070. "ncx_check of xdrb failed %d\n", status);
  1071. return 1;
  1072. }
  1073. status = ncx_check(ncxb);
  1074. if(status)
  1075. {
  1076. (void) fprintf(stderr,
  1077. "ncx_check of ncxb failed %d\n", status);
  1078. return 1;
  1079. }
  1080. return 0;
  1081. }