00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include <stdlib.h>
00023 #include <time.h>
00024
00025 #include <lal/LALStdlib.h>
00026 #include <lal/Date.h>
00027
00028 INT4 lalDebugLevel = 0;
00029
00030 NRCSID (LALTESTGPSTOFLOATC, "$Id: TestGPStoFloat.c,v 1.6 2008/04/29 22:02:47 kipp Exp $");
00031
00032
00033 static int test_random_doubles(unsigned int seed)
00034 {
00035 int i;
00036
00037 srandom(seed);
00038
00039 for(i = 0; i < 1000000; i++) {
00040 double in;
00041 double out;
00042 LIGOTimeGPS gps;
00043
00044 in = random() * 1e-12;
00045
00046 out = XLALGPSGetREAL8(XLALGPSSetREAL8(&gps, in));
00047
00048
00049 if(fabs(in - out) > .5001e-9) {
00050 fprintf(stderr, "XLALGPSSetREAL8() + XLALGPSGetREAL8() failed: input = %.17g s, output = %.17g s, difference = %.16g ns\n", in, out, (in - out) * 1e9);
00051 return -1;
00052 }
00053 }
00054
00055 return 0;
00056 }
00057
00058
00059
00060 int main(int argc, char *argv[])
00061 {
00062 static LALStatus status;
00063 LIGOTimeGPS gpsTime = {0, 0};
00064 REAL8 realTime = 0.;
00065 LALTimeInterval interval = {0, 0};
00066 REAL8 deltaT = 0.;
00067
00068 if (argc > 1)
00069 lalDebugLevel = atoi(argv[1]);
00070
00071
00072 gpsTime.gpsSeconds = 987654321;
00073 gpsTime.gpsNanoSeconds = 123456789;
00074 LALGPStoFloat(&status, &realTime, &gpsTime);
00075
00076 if (status.statusCode && lalDebugLevel)
00077 {
00078 fprintf(stderr, "TestGPStoFloat: LALGPStoFloat() failed; line %i, %s\n",
00079 __LINE__, LALTESTGPSTOFLOATC);
00080 REPORTSTATUS(&status);
00081 return status.statusCode;
00082 }
00083
00084 if (lalDebugLevel)
00085 {
00086 printf("TestGPStoFloat: expected %22.9f\n got %22.9f\n",
00087 987654321.123456789, realTime);
00088 }
00089
00090 if (realTime != 987654321.123456789)
00091 {
00092 fprintf(stderr, "TestGPStoFloat: LALGPStoFloat() returned wrong value; expected %.17g, got %.17g\n",
00093 987654321.123456789, realTime);
00094 return 1;
00095 }
00096
00097
00098
00099 realTime = 54321.123456789;
00100 LALFloatToGPS(&status, &gpsTime, &realTime);
00101
00102 if (status.statusCode && lalDebugLevel)
00103 {
00104 fprintf(stderr, "TestGPStoFloat: LALFloatToGPS() failed; line %i, %s\n",
00105 __LINE__, LALTESTGPSTOFLOATC);
00106 REPORTSTATUS(&status);
00107 return status.statusCode;
00108 }
00109
00110 if (lalDebugLevel)
00111 {
00112 printf("TestFloatToGPS: expected (%d, %d)\n got (%d, %d)\n",
00113 54321, 123456789, gpsTime.gpsSeconds, gpsTime.gpsNanoSeconds);
00114 }
00115
00116 if (gpsTime.gpsSeconds != 54321 || gpsTime.gpsNanoSeconds != 123456789)
00117 {
00118 fprintf(stderr, "TestGPStoFloat: LALFloatToGPS() returned wrong value; expected (%d, %d), got (%d, %d)\n",
00119 54321, 123456789, gpsTime.gpsSeconds, gpsTime.gpsNanoSeconds);
00120 return 2;
00121 }
00122
00123
00124 interval.seconds = 123;
00125 interval.nanoSeconds = 654;
00126
00127 LALIntervalToFloat(&status, &deltaT, &interval);
00128
00129 if (status.statusCode && lalDebugLevel)
00130 {
00131 fprintf(stderr, "TestGPStoFloat: LALIntervalToFloat() failed; line %i, %s\n",
00132 __LINE__, LALTESTGPSTOFLOATC);
00133 REPORTSTATUS(&status);
00134 return status.statusCode;
00135 }
00136
00137 if (lalDebugLevel)
00138 {
00139 printf("TestGPStoFloat: expected (%18.9f)\n", 123.000000654);
00140 printf(" got (%18.9f)\n", deltaT);
00141 }
00142
00143 if (deltaT != 123.000000654)
00144 {
00145 fprintf(stderr, "TestGPStoFloat: LALIntervalToFloat() returned wrong value; ");
00146 fprintf(stderr, "expected %18.9f, got %18.9f\n", 123.000000654, deltaT);
00147 return 3;
00148 }
00149
00150
00151 LALFloatToInterval(&status, &interval, &deltaT);
00152
00153 if (status.statusCode && lalDebugLevel)
00154 {
00155 fprintf(stderr, "TestGPStoFloat: LALIntervalToFloat() failed; line %i, %s\n",
00156 __LINE__, LALTESTGPSTOFLOATC);
00157 REPORTSTATUS(&status);
00158 return status.statusCode;
00159 }
00160
00161 if (lalDebugLevel)
00162 {
00163 printf("TestGPStoFloat: expected (%9d:%09d)\n", 123, 654);
00164 printf(" got (%9d:%09d)\n", interval.seconds, interval.nanoSeconds);
00165 }
00166
00167 if (interval.seconds != 123 || interval.nanoSeconds != 654)
00168 {
00169 fprintf(stderr, "TestGPStoFloat: LALFloatToInterval() returned wrong value; ");
00170 fprintf(stderr, "expected (%9d:%09d), got (%9d:%09d)\n", 123, 654, interval.seconds,
00171 interval.nanoSeconds);
00172 return 4;
00173 }
00174
00175
00176 if(test_random_doubles(0)) {
00177 return 5;
00178 }
00179
00180 return 0;
00181 }