TestStrToGPS.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Kipp Cannon
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 #include <errno.h>
00021 #include <limits.h>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <lal/Date.h>
00025 #include <lal/LALStdio.h>
00026 #include <lal/XLALError.h>
00027 
00028 NRCSID(STRINGCONVERTTESTC, "$Id: TestStrToGPS.c,v 1.13 2008/04/29 22:23:46 kipp Exp $");
00029 
00030 
00031 int lalDebugLevel = 0;
00032 
00033 
00034 struct TESTCASE {
00035         const char *string;
00036         int sec, ns;
00037         const char *remainder;
00038         int xlal_errno;
00039 };
00040 
00041 
00042 static int runtest(const struct TESTCASE *testcase)
00043 {
00044         int retval;
00045         LIGOTimeGPS gps;
00046         LIGOTimeGPS gpsCorrect;
00047         char *endptr;
00048         int failure = 0;
00049 
00050         XLALGPSSet(&gpsCorrect, testcase->sec, testcase->ns);
00051 
00052         XLALClearErrno();
00053         retval = XLALStrToGPS(&gps, testcase->string, &endptr);
00054 
00055         if(retval == 0 && testcase->xlal_errno == 0) {
00056                 if(XLALGPSCmp(&gps, &gpsCorrect) || strcmp(endptr, testcase->remainder))
00057                         failure = 1;
00058         } else if(XLALGetBaseErrno() != testcase->xlal_errno)
00059                 failure = 1;
00060 
00061         if(lalDebugLevel || failure)
00062                 fprintf(stdout, "Input = \"%s\"\n\tOutput =\t%" LAL_INT8_FORMAT " ns with \"%s\" remainder, errno %d\n\tCorrect =\t%" LAL_INT8_FORMAT " ns with \"%s\" remainder, errno %d\n\t\t===> %s\n", testcase->string, XLALGPSToINT8NS(&gps), endptr, XLALGetBaseErrno(), XLALGPSToINT8NS(&gpsCorrect), testcase->remainder, testcase->xlal_errno, failure ? "*** FAIL ***" : "Pass");
00063 
00064         return failure;
00065 }
00066 
00067 
00068 int main(int argc, char *argv[])
00069 {
00070         /* Most of these test were shamelessly stolen from Peter's original
00071          * code for testing LALStringToGPS() */
00072         struct TESTCASE general_testcases[] = {
00073                 {"1234.5", 1234, 500000000, "", 0},
00074                 {"712345678", 712345678, 0, "", 0},
00075                 {"00000000712346678", 712346678, 0, "", 0},
00076                 {"000000000000000000000000000000000712347678", 712347678, 0, "", 0},
00077                 {"000000000000000000712348678.00000000000000", 712348678, 0, "", 0},
00078                 {"000000000000000000712349678.00000000000001", 712349678, 0, "", 0},
00079                 {"722345678.", 722345678, 0, "", 0},
00080                 {"1722346678.", 1722346678, 0, "", 0},
00081                 {"01722347678.", 1722347678, 0, "", 0},
00082                 {"001722348678.", 1722348678, 0, "", 0},
00083                 {"732345678.0", 732345678, 0, "", 0},
00084                 {"742345678.7", 742345678, 700000000, "", 0},
00085                 {"752345678.000861", 752345678, 861000, "", 0},
00086                 {"762345678.000862547", 762345678, 862547, "", 0},
00087                 {"772345678.0008635474", 772345678, 863547, "", 0},
00088                 /*{"782345678.0008645475", 782345678, 864548, "", 0},*/
00089                 {"792345678.000865547687287", 792345678, 865548, "", 0},
00090                 {"702345678.9999999994", 702345678, 999999999, "", 0},
00091                 /*{"712345678.9999999995", 712345679, 0, "", 0},*/
00092                 {"722345678.9999999996", 722345679, 0, "", 0},
00093                 {"2000000000", 2000000000, 0, "", 0},
00094                 {"752345678e0", 752345678, 0, "", 0},
00095                 {"762345678e+0", 762345678, 0, "", 0},
00096                 {"772345678e-0", 772345678, 0, "", 0},
00097                 {"782345678e00", 782345678, 0, "", 0},
00098                 {"792345678e+00", 792345678, 0, "", 0},
00099                 {"702345678e-00", 702345678, 0, "", 0},
00100                 {"712345678.e0", 712345678, 0, "", 0},
00101                 {"722345678.e+0", 722345678, 0, "", 0},
00102                 {"732345678.e-0", 732345678, 0, "", 0},
00103                 {"742345678.00e0", 742345678, 0, "", 0},
00104                 {"752345678.00e+0", 752345678, 0, "", 0},
00105                 {"762345678.00e-0", 762345678, 0, "", 0},
00106                 {"772345678.06e0", 772345678, 60000000, "", 0},
00107                 {"782345678.06e+0", 782345678, 60000000, "", 0},
00108                 {"792345678.06e-0", 792345678, 60000000, "", 0},
00109                 {"7023.45678e5", 702345678, 0, "", 0},
00110                 {"7123.457785255e+05", 712345778, 525500000, "", 0},
00111                 {"7223458785255e-4", 722345878, 525500000, "", 0},
00112                 {"43d", 43, 0, "d", 0},
00113                 {"44.3873qr", 44, 387300000, "qr", 0},
00114                 {"45.3973 qr", 45, 397300000, " qr", 0},
00115                 {"46.3073 e2", 46, 307300000, " e2", 0},
00116                 {"47.3173e2", 4731, 730000000, "", 0},
00117                 {"6.85e7", 68500000, 0, "", 0},
00118                 {"6.9512345678901e7", 69512345, 678901000, "", 0},
00119                 {"6.05e7dkjf", 60500000, 0, "dkjf", 0},
00120                 {"6.15ex0", 6, 150000000, "ex0", 0},
00121                 {"6.25E7", 62500000, 0, "", 0},
00122                 {"6.35E7dkjf", 63500000, 0, "dkjf", 0},
00123                 {"6.45Ex0", 6, 450000000, "Ex0", 0},
00124                 {"752345678.5433e258", LONG_MAX, 0, "", XLAL_ERANGE},
00125                 {"762345678.5533e258r574", LONG_MAX, 0, "r574", XLAL_ERANGE},
00126                 {"772345678.5633e.258", 772345678, 563300000, "e.258", 0},
00127                 {"782345678.5733.258", 782345678, 573300000, ".258", 0},
00128                 {"792345678.5833+258", 792345678, 583300000, "+258", 0},
00129                 {"702345678.5933-258", 702345678, 593300000, "-258", 0},
00130                 {"712345678.5033.258E02", 712345678, 503300000, ".258E02", 0},
00131                 {"-722345678.5133", -722345679, 486700000, "", 0},
00132                 {"-742345678.000000625", -742345679, 999999375, "", 0},
00133                 {"-743345678.9999999994", -743345679, 1, "", 0},
00134                 /*{"-744345678.9999999995", -744345678, -999999999, "", 0},*/
00135                 {"-752345678.9999999996", -752345679, 0, "", 0},
00136                 {"5e-2", 0, 50000000, "", 0},
00137                 {"7e-7", 0, 700, "", 0},
00138                 {"6e-10", 0, 1, "", 0},
00139                 {"8e-11", 0, 0, "", 0},
00140                 {"-7e-12", 0, 0, "", 0},
00141                 {"-4e-6", -1, 999996000, "", 0},
00142                 {"-4.2e-2", -1, 958000000, "", 0},
00143                 {".5244", 0, 524400000, "", 0},
00144                 {"-.5244", -1, 475600000, "", 0},
00145                 {"0", 0, 0, "", 0},
00146                 {"+", 0, 0, "+", 0},
00147                 {"-", 0, 0, "-", 0},
00148                 {"e", 0, 0, "e", 0},
00149                 {"e3", 0, 0, "e3", 0},
00150                 {"x", 0, 0, "x", 0},
00151                 {"0x", 0, 0, "x", 0},
00152                 {"0.0000000000000000000000000000000000000001e40", 1, 0, "", 0},
00153                 {"10000000000000000000000000000000000000000e-40", 1, 0, "", 0},
00154                 {NULL, 0, 0, NULL, 0}
00155         };
00156         struct TESTCASE overflow_testcases[] = {
00157                 {"7323456785", LONG_MAX, 0, "", XLAL_ERANGE},
00158                 {"7423456785234", LONG_MAX, 0, "", XLAL_ERANGE},
00159                 {"-73234567800.5233", LONG_MIN, 0, "", XLAL_ERANGE},
00160                 {NULL, 0, 0, NULL, 0}
00161         };
00162         struct TESTCASE hexfloat_testcases[] = {
00163                 {"0x0", 0, 0, "", 0},
00164                 {"0x00", 0, 0, "", 0},
00165                 {"00x0", 0, 0, "x0", 0},
00166                 {"00x00", 0, 0, "x00", 0},
00167                 {"0x2CD7E24E.8", 752345678, 500000000, "", 0},
00168                 {"0x10P-6", 0, 250000000, "", 0},
00169                 {NULL, 0, 0, NULL, 0}
00170         };
00171         struct TESTCASE *testcase;
00172         int failures = 0;
00173 
00174         /* set lalDebugLevel */
00175         if(argc > 1)
00176                 lalDebugLevel = atoi(argv[1]);
00177 
00178         /* run tests that all platforms must pass */
00179         for(testcase = general_testcases; testcase->string; testcase++)
00180                 failures += runtest(testcase);
00181 
00182         /* do extra tests if ints > 32 bits overflow strtol() */
00183         errno = 0;
00184         strtol("7323456785", NULL, 0);
00185         if(errno == ERANGE)
00186                 for(testcase = overflow_testcases; testcase->string; testcase++)
00187                         failures += runtest(testcase);
00188         else
00189                 fprintf(stderr, "WARNING: your C library can parse ints that LIGOTimeGPS can't store!\n");
00190         errno = 0;
00191 
00192         /* do more tests if C library is smart enough to handle hex floats */
00193         if(strtod("0x.8", NULL) == 0.5)
00194                 for(testcase = hexfloat_testcases; testcase->string; testcase++)
00195                         failures += runtest(testcase);
00196         else
00197                 fprintf(stderr, "WARNING: your C library can't parse hex floats!\n");
00198 
00199         fprintf(stdout, "Summary of GPS string conversion tests: ");
00200         if(failures) {
00201                 fprintf(stdout, "%d FAILURES\n", failures);
00202                 exit(9);
00203         } else
00204                 fprintf(stdout, "all succeeded\n");
00205 
00206         exit(0);
00207 }

Generated on Sun Oct 12 02:32:39 2008 for LAL by  doxygen 1.5.2