htester.c

Go to the documentation of this file.
00001 /*                       H T E S T E R . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup hton */
00023 /*@{*/
00024 
00025 /** @file htester.c
00026  * @brief
00027  *  Test network float conversion.
00028  * 
00029  *  Expected to be used in pipes, or with TTCP links to other machines,
00030  *  or with files RCP'ed between machines.
00031  *
00032  *  @author
00033  *      Michael John Muuss
00034  *
00035  *  @par Source -
00036  *      SECAD/VLD Computing Consortium, Bldg 394
00037  *@n    The U. S. Army Ballistic Research Laboratory
00038  *@n    Aberdeen Proving Ground, Maryland  21005-5066
00039  */
00040 /*@}*/
00041 
00042 #ifndef lint
00043 static char RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/htester.c,v 14.1 2006/09/15 04:17:18 brlcad Exp $ (BRL)";
00044 #endif
00045 
00046 #include "common.h"
00047 
00048 #include <stdlib.h>
00049 #include <stdio.h>
00050 
00051 
00052 #define NUM     3000
00053 double  orig[NUM], after[NUM];
00054 
00055 char    buf[NUM*8];
00056 
00057 main(int argc, char **argv)
00058 {
00059         register int i;
00060         register int nbytes;
00061 
00062         if( argc != 2 || argv[1][0] != '-' )  {
00063                 fprintf(stderr,"Usage:  htester [-i|-o]\n");
00064                 exit(1);
00065         }
00066 
00067         /* First stage, generate the reference pattern */
00068         for( i=0; i<1000; i++ )  {
00069                 orig[i] = ((double)i)/(7*16);
00070         }
00071         for( i=1000; i<2000; i++ )  {
00072                 orig[i] = -1.0/(i-1000+1);
00073         }
00074         orig[2000] = -1;
00075         for( i=2001; i<2035; i++ )  {
00076                 orig[i] = orig[i-1] * -0.1;
00077         }
00078         for( i=2035; i<3000; i++ )  {
00079                 orig[i] = orig[i-1000] + (i&1)?(-1):1;
00080         }
00081 
00082         /* Second stage, write out, or read and compare */
00083         if( argv[1][1] == 'o' )  {
00084                 /* Write out */
00085                 htond( buf, (char *)orig, NUM );
00086                 fwrite( buf, 8, NUM, stdout );
00087                 exit(0);
00088         }
00089 
00090         /* Read and compare */
00091         if( sizeof(double) >= 8 )
00092                 nbytes = 6;
00093         else
00094                 nbytes = 4;
00095         fread( buf, 8, NUM, stdin );
00096 /*      ntohd( (char *)after, buf, NUM );       /* bulk conversion */
00097         for( i=0; i<NUM; i++ )  {
00098                 ntohd( (char *)&after[i], &buf[i*8], 1 );       /* incremental */
00099                 /* Floating point compare */
00100                 if( orig[i] == after[i] )  continue;
00101 
00102                 /* Byte-for-byte compare */
00103                 if( ckbytes( &orig[i], &after[i], nbytes ) == 0 )
00104                         continue;
00105 
00106                 /* Wrong */
00107                 printf("%4d: calc ", i);
00108                 flpr( &orig[i] );
00109                 printf(" %g\n", orig[i]);
00110                 printf("      aftr ");
00111                 flpr( &after[i] );
00112                 printf(" %g\n", after[i] );
00113                 printf("      buf  ");
00114                 flpr( &buf[i*8] );
00115                 printf("\n");
00116         }
00117         exit(0);
00118 }
00119 
00120 flpr(register unsigned char *cp)
00121 {
00122         register int i;
00123         for( i=0; i<sizeof(double); i++ )  {
00124                 putchar("0123456789ABCDEFx"[*cp>>4]);
00125                 putchar("0123456789ABCDEFx"[*cp&0xF]);
00126                 cp++;
00127         }
00128 }
00129 
00130 ckbytes(register unsigned char *a, register unsigned char *b, register int n)
00131 {
00132 #ifndef vax
00133         while( n-- > 0 )  {
00134                 if( *a++ != *b++ )
00135                         return(-1);     /* BAD */
00136         }
00137         return(0);                      /* OK */
00138 #else
00139         /* VAX floating point has bytes swapped, vis-a-vis normal VAX order */
00140         register int i;
00141         for( i=0; i<n; i++ )  {
00142                 if( a[i^1] != b[i^1] )
00143                         return(-1);     /* BAD */
00144         }
00145         return(0);                      /* OK */
00146 #endif
00147 }
00148 /*@}*/
00149 /*
00150  * Local Variables:
00151  * mode: C
00152  * tab-width: 8
00153  * c-basic-offset: 4
00154  * indent-tabs-mode: t
00155  * End:
00156  * ex: shiftwidth=4 tabstop=8
00157  */

Generated on Mon Sep 18 01:24:48 2006 for BRL-CAD by  doxygen 1.4.6