JulianDate_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include "JulianDate.hpp"
40 #include "TimeTag.hpp"
41 #include "TestUtil.hpp"
42 #include <iostream>
43 #include <fstream>
44 #include <string>
45 #include <iomanip>
46 #include <sstream>
47 #include <cmath>
48 using namespace gnsstk;
49 using namespace std;
50 
51 
53 {
54  public:
55  JulianDate_T() {eps = 1E-12;}
57 
58 //==========================================================================================================================
59 // initializationTest ensures the constructors set the values properly
60 //==========================================================================================================================
61  int initializationTest (void)
62  {
63  TestUtil testFramework( "JulianDate", "Constructor", __FILE__, __LINE__ );
64 
65 
66  JulianDate Compare(1350000,TimeSystem(2)); //Initialize an object
67 
68  //---------------------------------------------------------------------
69  //Were the attributes set to expectation with the explicit constructor?
70  //---------------------------------------------------------------------
71  testFramework.assert(fabs((long double)1350000 - Compare.jd) < eps, "Explicit constructor did not set the jd value properly", __LINE__);
72  testFramework.assert(TimeSystem(2) == Compare.getTimeSystem(), "Explicit constructor did not set the TimeSystem properly", __LINE__);
73 
74 
75  testFramework.changeSourceMethod("ConstructorCopy");
76  JulianDate Copy(Compare); // Initialize with copy constructor
77  //---------------------------------------------------------------------
78  //Were the attributes set to expectation with the copy constructor?
79  //---------------------------------------------------------------------
80  testFramework.assert(fabs((long double)1350000 - Copy.jd) < eps, "Copy constructor did not set the jd value properly", __LINE__);
81  testFramework.assert(TimeSystem(2) == Copy.getTimeSystem(), "Copy constructor did not set the TimeSystem properly", __LINE__);
82 
83 
84  testFramework.changeSourceMethod("OperatorSet");
85  JulianDate Assigned;
86  Assigned = Compare;
87  //---------------------------------------------------------------------
88  //Were the attributes set to expectation with the Set Operator?
89  //---------------------------------------------------------------------
90  testFramework.assert(fabs((long double)1350000 - Assigned.jd) < eps, "Set Operator did not set the jd value properly", __LINE__);
91  testFramework.assert(TimeSystem(2) == Assigned.getTimeSystem(), "Set Operator did not set the TimeSystem properly", __LINE__);
92 
93  return testFramework.countFails();
94  }
95 
96 
97 //==========================================================================================================================
98 // Test will check if JulianDate variable can be set from a map.
99 // Test also implicity tests whether the != operator functions.
100 //==========================================================================================================================
101 
102  int setFromInfoTest (void)
103  {
104 
105  TestUtil testFramework( "JulianDate", "setFromInfo", __FILE__, __LINE__ );
106 
107 
108  JulianDate setFromInfo1;
109  JulianDate setFromInfo2;
110  JulianDate Compare(1350000,TimeSystem(2)), Compare2(0,TimeSystem(2));
112  Id['J'] = "1350000";
113  Id['P'] = "GPS";
114 
115  //---------------------------------------------------------------------
116  //Does a proper setFromInfo work with all information provided?
117  //---------------------------------------------------------------------
118  testFramework.assert(setFromInfo1.setFromInfo(Id), "setFromInfo experienced an error and returned false", __LINE__);
119  testFramework.assert(Compare == setFromInfo1, "setFromInfo did not set all of the values properly", __LINE__);
120 
121 
122  Id.erase('J');
123  //---------------------------------------------------------------------
124  //Does a proper setFromInfo work with missing information?
125  //---------------------------------------------------------------------
126  testFramework.assert(setFromInfo2.setFromInfo(Id), "setFromInfo experienced an error and returned false", __LINE__);
127  testFramework.assert(Compare2 == setFromInfo2, "setFromInfo did not set all of the values properly", __LINE__);
128 
129  return testFramework.countFails();
130  }
131 
132 
133 //==========================================================================================================================
134 // Test will check if the ways to initialize and set an JulianDate object.
135 // Test also tests whether the comparison operators and isValid method function.
136 //==========================================================================================================================
137  int operatorTest (void)
138  {
139  TestUtil testFramework( "JulianDate", "OperatorEquivalent", __FILE__, __LINE__ );
140 
141 
142  JulianDate Compare(1350000); // Initialize with value
143  JulianDate LessThanJD(134000); // Initialize with value
144  JulianDate CompareCopy(Compare); // Initialize with copy constructor
145 
146  //---------------------------------------------------------------------
147  //Does the == Operator function?
148  //---------------------------------------------------------------------
149  testFramework.assert( Compare == CompareCopy, "Equivalence operator found equivalent objects to be not equivalent", __LINE__);
150  testFramework.assert(!(Compare == LessThanJD), "Equivalence operator found different jd objects to be equivalent", __LINE__);
151 
152 
153  testFramework.changeSourceMethod("OperatorNotEquivalent");
154  //---------------------------------------------------------------------
155  //Does the != Operator function?
156  //---------------------------------------------------------------------
157  testFramework.assert( Compare != LessThanJD, "Not-equal operator found different jd objects to be equivalent", __LINE__);
158  testFramework.assert(!(Compare != Compare), "Not-equal operator found equivalent objects to not be equivalent", __LINE__);
159 
160 
161  testFramework.changeSourceMethod("OperatorLessThan");
162  //---------------------------------------------------------------------
163  //Does the < Operator function?
164  //---------------------------------------------------------------------
165  testFramework.assert( LessThanJD < Compare, "Less-than operator found less-than jd object to not be less than", __LINE__);
166  testFramework.assert(!(Compare < LessThanJD), "Less-than operator found greater-than jd object to be less than", __LINE__);
167  testFramework.assert(!(Compare < CompareCopy), "Less-than operator found equivalent object to be less than", __LINE__);
168 
169 
170  testFramework.changeSourceMethod("OperatorGreaterThan");
171  //---------------------------------------------------------------------
172  //Does the > Operator function?
173  //---------------------------------------------------------------------
174  testFramework.assert(!(LessThanJD > Compare), "Greater-than operator found less-than jd object to be greater than", __LINE__);
175  testFramework.assert( Compare > LessThanJD, "Greater-than operator found greater-than jd object to not be greater than", __LINE__);
176  testFramework.assert(!(Compare > CompareCopy), "Greater-than operator found equivalent object to be greater than", __LINE__);
177 
178 
179  testFramework.changeSourceMethod("OperatorLessThanOrEqualTo");
180  //---------------------------------------------------------------------
181  //Does the <= Operator function?
182  //---------------------------------------------------------------------
183  testFramework.assert( LessThanJD <= Compare, "Less-than-or-equal-to operator found less-than jd object to not be less than or equal to", __LINE__);
184  testFramework.assert(!(Compare <= LessThanJD), "Less-than-or-equal-to operator found greater-than jd object to be less than or equal to", __LINE__);
185  testFramework.assert( Compare <= CompareCopy, "Less-than-or-equal-to operator found equivalent object to not be less than or equal to", __LINE__);
186 
187 
188  testFramework.changeSourceMethod("OperatorGreaterThanOrEqualTo");
189  //---------------------------------------------------------------------
190  //Does the >= Operator function?
191  //---------------------------------------------------------------------
192  testFramework.assert(!(LessThanJD >= Compare), "Greater-than-or-equal-to operator found less-than jd object to be greater than or equal to", __LINE__);
193  testFramework.assert( Compare >= LessThanJD, "Greater-than-or-equal-to operator found greater-than jd object to not be greater than or equal to", __LINE__);
194  testFramework.assert( Compare >= CompareCopy, "Greater-than-or-equal-to operator found equivalent object to not be greater than or equal to", __LINE__);
195 
196  return testFramework.countFails();
197  }
198 
199 
200 //==========================================================================================================================
201 // Test will check the reset method.
202 //==========================================================================================================================
203  int resetTest (void)
204  {
205  TestUtil testFramework( "JulianDate", "reset", __FILE__, __LINE__ );
206 
207 
208  JulianDate Compare(1350000,TimeSystem(2)); //Initialize an object
209 
210  Compare.reset(); // Reset it
211 
212  //---------------------------------------------------------------------
213  //Were the attributes reset to expectation?
214  //---------------------------------------------------------------------
215  testFramework.assert(Compare.jd==0, "reset() did not set the jd value to 0", __LINE__);
216  testFramework.assert(TimeSystem(0) == Compare.getTimeSystem(), "reset() did not set the TimeSystem to UNK", __LINE__);
217 
218  return testFramework.countFails();
219  }
220 
221 
222 //==========================================================================================================================
223 // Test will check converting to/from CommonTime.
224 //==========================================================================================================================
226  {
227  TestUtil testFramework( "JulianDate", "isValid", __FILE__, __LINE__ );
228 
229 
230  JulianDate Compare(1350000,TimeSystem(2)); //Initialize an object
231 
232  //---------------------------------------------------------------------
233  //Is the time after the BEGINNING_OF_TIME?
234  //---------------------------------------------------------------------
235  testFramework.assert(Compare.convertToCommonTime() > CommonTime::BEGINNING_OF_TIME, "Time provided found to be less than the beginning of time", __LINE__);
236 
237 
238  //---------------------------------------------------------------------
239  //Is the set object valid?
240  //---------------------------------------------------------------------
241  testFramework.assert(Compare.isValid(), "Time provided found to be unable to convert to/from CommonTime", __LINE__);
242 
243 
244  CommonTime Test = Compare.convertToCommonTime(); //Convert to
245 
246  JulianDate Test2;
247  Test2.convertFromCommonTime(Test); //Convert From
248 
249  testFramework.changeSourceMethod("CommonTimeConversion");
250  //---------------------------------------------------------------------
251  //Is the result of conversion the same?
252  //---------------------------------------------------------------------
253  testFramework.assert(Compare.getTimeSystem()== Test2.getTimeSystem(), "TimeSystem provided found to be different after converting to and from CommonTime", __LINE__);
254  testFramework.assert(fabs(Test2.jd - Compare.jd) < eps, "JD provided found to be different after converting to and from CommonTime", __LINE__);
255 
256  return testFramework.countFails();
257  }
258 
259 
260 //==========================================================================================================================
261 // Test will check the TimeSystem comparisons when using the comparison operators.
262 //==========================================================================================================================
263  int timeSystemTest (void)
264  {
265  TestUtil testFramework( "JulianDate", "OperatorEquivalentWithDifferingTimeSystem", __FILE__, __LINE__ );
266 
267 
268  JulianDate GPS1(1350000,TimeSystem(2));
269  JulianDate GPS2(1340000,TimeSystem(2));
270  JulianDate UTC1(1350000,TimeSystem(7));
271  JulianDate UNKNOWN(1350000,TimeSystem(0));
272  JulianDate ANY(1350000,TimeSystem(1));
273  JulianDate ANY2(1340000,TimeSystem(1));
274 
275  //---------------------------------------------------------------------
276  //Verify differing TimeSystem sets equivalence operator to false
277  //Note that the operator test checks for == in ALL members
278  //---------------------------------------------------------------------
279  testFramework.assert(!(GPS1 == UTC1), "Equivalence operator found objects with differing TimeSystems to be the same", __LINE__);
280  testFramework.assert(GPS1 == ANY, "Differing TimeSystems where one is TimeSystem::Any is not ignored for equals", __LINE__);
281  testFramework.assert(UTC1 == ANY, "Differing TimeSystems where one is TimeSystem::Any is not ignored for equals", __LINE__);
282  testFramework.assert(UNKNOWN == ANY, "Differing TimeSystems where one is TimeSystem::Any is not ignored for equals", __LINE__);
283 
284  testFramework.changeSourceMethod("OperatorNotEquivalentWithDifferingTimeSystem");
285  //---------------------------------------------------------------------
286  //Verify different Time System but same time inequality
287  //---------------------------------------------------------------------
288  testFramework.assert(GPS1 != UTC1, "Equivalent objects with differing TimeSystems are found to be equal", __LINE__);
289  testFramework.assert(GPS1 != UNKNOWN, "Equivalent objects with differing TimeSystems are found to be equal", __LINE__);
290  testFramework.assert(!(GPS1 != ANY), "Equivalent objects with differing TimeSystems where one is TimeSystem::Any are found to be not-equal", __LINE__);
291 
292  testFramework.changeSourceMethod("OperatorLessThanWithDifferingTimeSystem");
293  //---------------------------------------------------------------------
294  //Verify TimeSystem=ANY does not matter in other operator comparisons
295  //---------------------------------------------------------------------
296  testFramework.assert(ANY2 < GPS1, "Less than object with Any TimeSystem is not found to be less than", __LINE__);
297  testFramework.assert(GPS2 < ANY,"Less than object with GPS TimeSystem is not found to be less-than a greater object with Any TimeSystem", __LINE__);
298 
299  testFramework.changeSourceMethod("setTimeSystem");
300  UNKNOWN.setTimeSystem(TimeSystem(2)); //Set the Unknown TimeSystem
301  //---------------------------------------------------------------------
302  //Ensure resetting a Time System changes it
303  //---------------------------------------------------------------------
304  testFramework.assert(UNKNOWN.getTimeSystem()==TimeSystem(2), "setTimeSystem was unable to set the TimeSystem", __LINE__);
305 
306  return testFramework.countFails();
307  }
308 
309 
310 //==========================================================================================================================
311 // Test for the formatted printing of JulianDate objects
312 //==========================================================================================================================
313  int printfTest (void)
314  {
315  TestUtil testFramework( "JulianDate", "printf", __FILE__, __LINE__ );
316 
317 
318  JulianDate GPS1(1350000,TimeSystem::GPS);
319  JulianDate UTC1(1350000,TimeSystem::UTC);
320 
321  //---------------------------------------------------------------------
322  //Verify printed output matches expectation
323  //---------------------------------------------------------------------
324  testFramework.assert(GPS1.printf("%08J %02P") == (std::string)"1350000.000000 GPS", "printf did not output in the proper format", __LINE__);
325  testFramework.assert(UTC1.printf("%08J %02P") == (std::string)"1350000.000000 UTC", "printf did not output in the proper format", __LINE__);
326 
327 
328  testFramework.changeSourceMethod("printError");
329  //---------------------------------------------------------------------
330  //Verify printed error message matches expectation
331  //---------------------------------------------------------------------
332  testFramework.assert(GPS1.printError("%08J %02P") == (std::string)"ErrorBadTime ErrorBadTime", "printError did not output in the proper format", __LINE__);
333  testFramework.assert(UTC1.printError("%08J %02P") == (std::string)"ErrorBadTime ErrorBadTime", "printError did not output in the proper format", __LINE__);
334 
335  return testFramework.countFails();
336  }
337 
338  private:
339  double eps;
340 };
341 
342 
343 int main() //Main function to initialize and run all tests above
344 {
345  int check, errorCounter = 0;
346  JulianDate_T testClass;
347 
348  check = testClass.initializationTest();
349  errorCounter += check;
350 
351  check = testClass.operatorTest();
352  errorCounter += check;
353 
354  check = testClass.setFromInfoTest();
355  errorCounter += check;
356 
357  check = testClass.resetTest();
358  errorCounter += check;
359 
360  check = testClass.timeSystemTest();
361  errorCounter += check;
362 
363  check = testClass.toFromCommonTimeTest();
364  errorCounter += check;
365 
366  check = testClass.printfTest();
367  errorCounter += check;
368 
369  std::cout << "Total Failures for " << __FILE__ << ": " << errorCounter << std::endl;
370 
371  return errorCounter; //Return the total number of errors
372 }
JulianDate_T::printfTest
int printfTest(void)
Definition: JulianDate_T.cpp:313
gnsstk::JulianDate::reset
virtual void reset()
Reset this object to the default state.
Definition: JulianDate.cpp:162
gnsstk::JulianDate::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: JulianDate.cpp:76
gnsstk::TimeTag::setTimeSystem
void setTimeSystem(const TimeSystem &timeSys)
Set method for internal variable timeSystem (enum).
Definition: TimeTag.hpp:165
gnsstk::TestUtil::countFails
int countFails(void)
Definition: TestUtil.hpp:771
gnsstk::JulianDate::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: JulianDate.cpp:54
JulianDate_T::eps
double eps
Definition: JulianDate_T.cpp:339
gnsstk::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
gnsstk::JulianDate::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: JulianDate.cpp:126
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
JulianDate_T::JulianDate_T
JulianDate_T()
Definition: JulianDate_T.cpp:55
gnsstk::CommonTime::BEGINNING_OF_TIME
static const GNSSTK_EXPORT CommonTime BEGINNING_OF_TIME
earliest representable CommonTime
Definition: CommonTime.hpp:102
gnsstk::JulianDate
Definition: JulianDate.hpp:89
JulianDate_T
Definition: JulianDate_T.cpp:52
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::JulianDate::printError
virtual std::string printError(const std::string &fmt) const
Definition: JulianDate.cpp:107
JulianDate_T::setFromInfoTest
int setFromInfoTest(void)
Definition: JulianDate_T.cpp:102
gnsstk::TestUtil::changeSourceMethod
void changeSourceMethod(const std::string &newMethod)
Definition: TestUtil.hpp:785
JulianDate_T::~JulianDate_T
~JulianDate_T()
Definition: JulianDate_T.cpp:56
TestUtil.hpp
JulianDate.hpp
JulianDate_T::resetTest
int resetTest(void)
Definition: JulianDate_T.cpp:203
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::TimeSystem
TimeSystem
Definition of various time systems.
Definition: TimeSystem.hpp:51
TimeTag.hpp
gnsstk::TimeSystem::UTC
@ UTC
Coordinated Universal Time (e.g., from NTP)
JulianDate_T::timeSystemTest
int timeSystemTest(void)
Definition: JulianDate_T.cpp:263
gnsstk::TimeSystem::GPS
@ GPS
GPS system time.
std
Definition: Angle.hpp:142
gnsstk::JulianDate::jd
long double jd
Definition: JulianDate.hpp:202
JulianDate_T::initializationTest
int initializationTest(void)
Definition: JulianDate_T.cpp:61
gnsstk::JulianDate::isValid
virtual bool isValid() const
Returns true if this object's members are valid, false otherwise.
Definition: JulianDate.cpp:151
JulianDate_T::toFromCommonTimeTest
int toFromCommonTimeTest(void)
Definition: JulianDate_T.cpp:225
gnsstk::JulianDate::printf
virtual std::string printf(const std::string &fmt) const
Definition: JulianDate.cpp:88
main
int main()
Definition: JulianDate_T.cpp:343
gnsstk::TestUtil
Definition: TestUtil.hpp:265
JulianDate_T::operatorTest
int operatorTest(void)
Definition: JulianDate_T.cpp:137
gnsstk::TimeTag::getTimeSystem
TimeSystem getTimeSystem() const
Obtain time system info (enum).
Definition: TimeTag.hpp:169


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:39