StringConvertTest.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Jolien Creighton, Peter Shawhan
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 /********************************
00021 Author: Shawhan, P. S.
00022 $Id: StringConvertTest.c,v 1.6 2007/06/08 14:41:54 bema Exp $
00023 **************************************************** */
00024 
00025 /* <lalVerbatim> */
00026 #include <stdlib.h>
00027 #include <lal/LALStdlib.h>
00028 #include <lal/StringInput.h>
00029 
00030 NRCSID( STRINGCONVERTTESTC, "$Id: StringConvertTest.c,v 1.6 2007/06/08 14:41:54 bema Exp $" );
00031 
00032 #define LAL_INT4_MAX 2147483647
00033 #define LAL_INT4_ABSMIN LAL_UINT8_C(2147483648)
00034 
00035 #define MAXGPSSTRINGS 256
00036 #define SETGPSCASE( string, sec, ns, remain )                    \
00037     istring++;                                                   \
00038     if (istring >= MAXGPSSTRINGS) {                              \
00039         fprintf(stderr,"Too many GPS test cases; ABORTING\n");   \
00040         exit(-1);                                                 \
00041     }                                                            \
00042     strcpy( gpsString[istring], string );                       \
00043     gpsOutSec[istring] = sec ;                                   \
00044     gpsOutNS[istring] = ns ;                                     \
00045     strcpy( gpsOutRemainder[istring], remain );
00046 
00047 int lalDebugLevel = 0;
00048 
00049 int
00050 main( int argc, char **argv )
00051 {
00052   static LALStatus stat;
00053   CHAR gpsString[MAXGPSSTRINGS][256];
00054   INT4 gpsOutSec[MAXGPSSTRINGS];
00055   INT4 gpsOutNS[MAXGPSSTRINGS];
00056   CHAR gpsOutRemainder[MAXGPSSTRINGS][256];
00057 
00058   LIGOTimeGPS gps;
00059   CHAR *endptr;
00060   INT4 istring, remlength;
00061   INT4 nfailures = 0;
00062 
00063   /*------ Parse input line. ------*/
00064   if ( argc == 2 )
00065     lalDebugLevel = atoi( argv[1] );
00066   else if ( argc != 1 )
00067     {
00068       fprintf( stderr, "Usage: %s [ lalDebugLevel ]\n", argv[0] );
00069       return 0; /* so that test script won't fail */
00070     }
00071 
00072   /*------ Initialize arrays of example cases and expected outputs ------*/
00073   for ( istring=0; istring<MAXGPSSTRINGS; istring++ ) {
00074     gpsString[istring][0] = '\0';
00075     gpsOutSec[istring] = 0;
00076     gpsOutNS[istring] = 0;
00077     gpsOutRemainder[istring][0] = '\0';
00078   }
00079 
00080   /*------ Fill arrays of example cases and expected outputs ------*/
00081   istring = -1;    /* Crucial for proper operation of SETGPSCASE macro */
00082   SETGPSCASE( "1234.5", 1234, 500000000, "" );
00083   SETGPSCASE( "712345678", 712345678, 0, "" );
00084   SETGPSCASE( "00000000712346678", 712346678, 0, "" );
00085   SETGPSCASE( "000000000000000000000000000000000712347678", 712347678, 0, "" );
00086   SETGPSCASE( "000000000000000000712348678.00000000000000", 712348678, 0, "" );
00087   SETGPSCASE( "000000000000000000712349678.00000000000001", 712349678, 0, "" );
00088   SETGPSCASE( "722345678.", 722345678, 0, "" );
00089   SETGPSCASE( "1722346678.", 1722346678, 0, "" );
00090   SETGPSCASE( "01722347678.", 1722347678, 0, "" );
00091   SETGPSCASE( "001722348678.", 1722348678, 0, "" );
00092   SETGPSCASE( "732345678.0", 732345678, 0, "" );
00093   SETGPSCASE( "742345678.7", 742345678, 700000000, "" );
00094   SETGPSCASE( "752345678.000861", 752345678, 861000, "" );
00095   SETGPSCASE( "762345678.000862547", 762345678, 862547, "" );
00096   SETGPSCASE( "772345678.0008635474", 772345678, 863547, "" );
00097   SETGPSCASE( "782345678.0008645475", 782345678, 864548, "" );
00098   SETGPSCASE( "792345678.000865547687287", 792345678, 865548, "" );
00099   SETGPSCASE( "702345678.9999999994", 702345678, 999999999, "" );
00100   SETGPSCASE( "712345678.9999999995", 712345679, 0, "" );
00101   SETGPSCASE( "722345678.9999999996", 722345679, 0, "" );
00102   SETGPSCASE( "2000000000", 2000000000, 0, "" );
00103   SETGPSCASE( "7323456785", LAL_INT4_MAX, 999999999, "" );
00104   SETGPSCASE( "7423456785234", LAL_INT4_MAX, 999999999, "" );
00105   SETGPSCASE( "752345678e0", 752345678, 0, "" );
00106   SETGPSCASE( "762345678e+0", 762345678, 0, "" );
00107   SETGPSCASE( "772345678e-0", 772345678, 0, "" );
00108   SETGPSCASE( "782345678e00", 782345678, 0, "" );
00109   SETGPSCASE( "792345678e+00", 792345678, 0, "" );
00110   SETGPSCASE( "702345678e-00", 702345678, 0, "" );
00111   SETGPSCASE( "712345678.e0", 712345678, 0, "" );
00112   SETGPSCASE( "722345678.e+0", 722345678, 0, "" );
00113   SETGPSCASE( "732345678.e-0", 732345678, 0, "" );
00114   SETGPSCASE( "742345678.00e0", 742345678, 0, "" );
00115   SETGPSCASE( "752345678.00e+0", 752345678, 0, "" );
00116   SETGPSCASE( "762345678.00e-0", 762345678, 0, "" );
00117   SETGPSCASE( "772345678.06e0", 772345678, 60000000, "" );
00118   SETGPSCASE( "782345678.06e+0", 782345678, 60000000, "" );
00119   SETGPSCASE( "792345678.06e-0", 792345678, 60000000, "" );
00120   SETGPSCASE( "7023.45678e5", 702345678, 0, "" );
00121   SETGPSCASE( "7123.457785255e+05", 712345778, 525500000, "" );
00122   SETGPSCASE( "7223458785255e-4", 722345878, 525500000, "" );
00123   SETGPSCASE( "43d", 43, 0, "d" );
00124   SETGPSCASE( "44.3873qr", 44, 387300000, "qr" );
00125   SETGPSCASE( "45.3973 qr", 45, 397300000, " qr" );
00126   SETGPSCASE( "46.3073 e2", 46, 307300000, " e2" );
00127   SETGPSCASE( "47.3173e2", 4731, 730000000, "" );
00128   SETGPSCASE( "6.85e7", 68500000, 0, "" );
00129   SETGPSCASE( "6.9512345678901e7", 69512345, 678901000, "" );
00130   SETGPSCASE( "6.05e7dkjf", 60500000, 0, "dkjf" );
00131   SETGPSCASE( "6.15ex0", 6, 150000000, "ex0" );
00132   SETGPSCASE( "6.25E7", 62500000, 0, "" );
00133   SETGPSCASE( "6.35E7dkjf", 63500000, 0, "dkjf" );
00134   SETGPSCASE( "6.45Ex0", 6, 450000000, "Ex0" );
00135   SETGPSCASE( "752345678.5433e258", LAL_INT4_MAX, 999999999, "" );
00136   SETGPSCASE( "762345678.5533e258r574", LAL_INT4_MAX, 999999999, "r574" );
00137   SETGPSCASE( "772345678.5633e.258", 772345678, 563300000, "e.258" );
00138   SETGPSCASE( "782345678.5733.258", 782345678, 573300000, ".258" );
00139   SETGPSCASE( "792345678.5833+258", 792345678, 583300000, "+258" );
00140   SETGPSCASE( "702345678.5933-258", 702345678, 593300000, "-258" );
00141   SETGPSCASE( "712345678.5033.258E02", 712345678, 503300000, ".258E02" );
00142   SETGPSCASE( "-722345678.5133", -722345679, 486700000, "" );
00143   SETGPSCASE( "-73234567800.5233", (INT4) (-LAL_INT4_ABSMIN), 0, "" );
00144   SETGPSCASE( "-742345678.000000625", -742345679, 999999375, "" );
00145   SETGPSCASE( "-743345678.9999999994", -743345679, 1, "" );
00146   SETGPSCASE( "-744345678.9999999995", -744345679, 0, "" );
00147   SETGPSCASE( "-752345678.9999999996", -752345679, 0, "" );
00148   SETGPSCASE( "5e-2", 0, 50000000, "" );
00149   SETGPSCASE( "7e-7", 0, 700, "" );
00150   SETGPSCASE( "6e-10", 0, 1, "" );
00151   SETGPSCASE( "8e-11", 0, 0, "" );
00152   SETGPSCASE( "-7e-12", 0, 0, "" );
00153   SETGPSCASE( "-4e-6", -1, 999996000, "" );
00154   SETGPSCASE( "-4.2e-2", -1, 958000000, "" );
00155   SETGPSCASE( ".5244", 0, 524400000, "" );
00156   SETGPSCASE( "-.5244", -1, 475600000, "" );
00157   SETGPSCASE( "0", 0, 0, "" );
00158   SETGPSCASE( "+", 0, 0, "+" );
00159   SETGPSCASE( "-", 0, 0, "-" );
00160   SETGPSCASE( "e", 0, 0, "e" );
00161   SETGPSCASE( "e3", 0, 0, "e3" );
00162   SETGPSCASE( "x", 0, 0, "x" );
00163 
00164   /*------ Loop over GPS test strings ------*/
00165   for ( istring=0; istring<MAXGPSSTRINGS; istring++ ) {
00166     if ( gpsString[istring][0] == '\0' ) continue;
00167 
00168     LALStringToGPS( &stat, &gps, gpsString[istring], &endptr );
00169 
00170     /* Check for a LAL error condition */
00171     if ( stat.statusCode ) {
00172       if ( lalDebugLevel > 0 ) {
00173         fprintf( stderr,
00174                  "Error[0] 1: program %s, file %s, line %i, %s\n"
00175                  "         Function LALStringToGPS() failed\n",
00176                  argv[0], __FILE__, __LINE__, STRINGCONVERTTESTC );
00177         REPORTSTATUS( &stat );
00178         return stat.statusCode;
00179       }
00180     }
00181 
00182     /* Print result */
00183     if ( !stat.statusCode ) {
00184       fprintf( stdout, "For string '%s':\n", gpsString[istring] );
00185       fprintf( stdout,
00186                "  GPS time (sec,nsec) = (%11d,%10d), remainder '%s' ",
00187                gps.gpsSeconds, gps.gpsNanoSeconds, endptr );
00188     }
00189     /* Pad with spaces to line up PASS/FAIL text */
00190     remlength = strlen(endptr);
00191     while ( remlength < 10 ) { fprintf(stdout," "); remlength++; }
00192 
00193     /* Check correctness of result */
00194     if ( gps.gpsSeconds == gpsOutSec[istring] &&
00195          gps.gpsNanoSeconds == gpsOutNS[istring] &&
00196          strcmp( endptr, gpsOutRemainder[istring] ) == 0 ) {
00197       fprintf( stdout, "Pass\n" );
00198     } else {
00199       fprintf( stdout, "*Fail*\n" );
00200       nfailures++;
00201     }
00202 
00203 
00204   }
00205 
00206   /*-- Report if there were any failures --*/
00207   if ( nfailures ) {
00208     fprintf( stdout, "Summary of GPS string conversion tests: %d FAILURES\n",
00209              nfailures );
00210     return 9;
00211   } else {
00212     fprintf(stdout,"Summary of GPS string conversion tests: all succeeded\n");
00213   }
00214 
00215   return 0;
00216 }
00217 /* </lalVerbatim> */

Generated on Sun Sep 7 03:07:22 2008 for LAL by  doxygen 1.5.2