MJD_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 "MJD.hpp"
40 #include "TimeTag.hpp"
41 #include "TestUtil.hpp"
42 #include <iostream>
43 #include <fstream>
44 #include <cmath>
45 using namespace gnsstk;
46 using namespace std;
47 
48 class MJD_T
49 {
50  public:
51  MJD_T() {eps = 1E-12;}
52  ~MJD_T() {}
53 //==========================================================================================================================
54 // initializationTest ensures the constructors set the values properly
55 //==========================================================================================================================
56  int initializationTest (void)
57  {
58  TestUtil testFramework( "MJD", "Constructor", __FILE__, __LINE__ );
59 
60 
61  MJD Compare(135000.0,TimeSystem(2)); //Initialize an object
62 
63  //---------------------------------------------------------------------
64  //Were the attributes set to expectation with the explicit constructor?
65  //---------------------------------------------------------------------
66  testFramework.assert(fabs((long double)135000.0 - Compare.mjd) < eps, "Explicit constructor did not set the mjd value properly", __LINE__);
67  testFramework.assert(TimeSystem(2) == Compare.getTimeSystem(), "Explicit constructor did not set the TimeSystem properly", __LINE__);
68 
69 
70  testFramework.changeSourceMethod("ConstructorCopy");
71  MJD Copy(Compare); // Initialize with copy constructor
72  //---------------------------------------------------------------------
73  //Were the attributes set to expectation with the copy constructor?
74  //---------------------------------------------------------------------
75  testFramework.assert(fabs((long double)135000. - Copy.mjd) < eps, "Copy constructor did not set the mjd value properly", __LINE__);
76  testFramework.assert(TimeSystem(2) == Copy.getTimeSystem(), "Copy constructor did not set the TimeSystem properly", __LINE__);
77 
78 
79  testFramework.changeSourceMethod("OperatorSet");
80  MJD Assigned;
81  Assigned = Compare;
82  //---------------------------------------------------------------------
83  //Were the attributes set to expectation with the Set Operator?
84  //---------------------------------------------------------------------
85  testFramework.assert(fabs((long double)135000. - Assigned.mjd) < eps, "Set Operator did not set the mjd value properly", __LINE__);
86  testFramework.assert(TimeSystem(2) == Assigned.getTimeSystem(), "Set Operator did not set the TimeSystem properly", __LINE__);
87 
88  return testFramework.countFails();
89  }
90 
91 
92 //==========================================================================================================================
93 // Test will check if MJD variable can be set from a map.
94 // Test also implicity tests whether the != operator functions.
95 //==========================================================================================================================
96  int setFromInfoTest (void)
97  {
98  TestUtil testFramework( "MJD", "setFromInfo", __FILE__, __LINE__ );
99 
100 
101  MJD setFromInfo1;
102  MJD setFromInfo2;
103  MJD Compare(135000.0,TimeSystem(2)), Compare2(0.0,TimeSystem(2));
105  Id['Q'] = "135000.0";
106  Id['P'] = "GPS";
107  //---------------------------------------------------------------------
108  //Does a proper setFromInfo work with all information provided?
109  //---------------------------------------------------------------------
110  testFramework.assert(setFromInfo1.setFromInfo(Id), "setFromInfo experienced an error and returned false", __LINE__);
111  testFramework.assert(Compare == setFromInfo1, "setFromInfo did not set all of the values properly", __LINE__);
112 
113 
114  Id.erase('Q');
115  //---------------------------------------------------------------------
116  //Does a proper setFromInfo work with missing information?
117  //---------------------------------------------------------------------
118  testFramework.assert(setFromInfo2.setFromInfo(Id), "setFromInfo experienced an error and returned false", __LINE__);
119  testFramework.assert(Compare2 == setFromInfo2, "setFromInfo did not set all of the values properly", __LINE__);
120 
121  return testFramework.countFails();
122  }
123 
124 
125 //==========================================================================================================================
126 // Test will check if the ways to initialize and set an MJD object.
127 // Test also tests whether the comparison operators and isValid method function.
128 //==========================================================================================================================
129  int operatorTest (void)
130  {
131  TestUtil testFramework( "MJD", "OperatorEquivalent", __FILE__, __LINE__ );
132 
133 
134  gnsstk::MJD Compare(135000); // Initialize with value
135  gnsstk::MJD LessThanMJD(134000); // Initialize with value
136  gnsstk::MJD CompareCopy(Compare); // Initialize with copy constructor
137 
138  //---------------------------------------------------------------------
139  //Does the == Operator function?
140  //---------------------------------------------------------------------
141  testFramework.assert( Compare == CompareCopy, "Equivalence operator found equivalent objects to be not equivalent", __LINE__);
142  testFramework.assert(!(Compare == LessThanMJD), "Equivalence operator found different mjd objects to be equivalent", __LINE__);
143 
144 
145  testFramework.changeSourceMethod("OperatorNotEquivalent");
146  //---------------------------------------------------------------------
147  //Does the != Operator function?
148  //---------------------------------------------------------------------
149  testFramework.assert( Compare != LessThanMJD, "Not-equal operator found different mjd objects to be equivalent", __LINE__);
150  testFramework.assert(!(Compare != Compare), "Not-equal operator found equivalent objects to not be equivalent", __LINE__);
151 
152 
153  testFramework.changeSourceMethod("OperatorLessThan");
154  //---------------------------------------------------------------------
155  //Does the < Operator function?
156  //---------------------------------------------------------------------
157  testFramework.assert( LessThanMJD < Compare, "Less-than operator found less-than mjd object to not be less than", __LINE__);
158  testFramework.assert(!(Compare < LessThanMJD), "Less-than operator found greater-than mjd object to be less than", __LINE__);
159  testFramework.assert(!(Compare < CompareCopy), "Less-than operator found equivalent object to be less than", __LINE__);
160 
161 
162  testFramework.changeSourceMethod("OperatorGreaterThan");
163  //---------------------------------------------------------------------
164  //Does the > Operator function?
165  //---------------------------------------------------------------------
166  testFramework.assert(!(LessThanMJD > Compare), "Greater-than operator found less-than mjd object to be greater than", __LINE__);
167  testFramework.assert( Compare > LessThanMJD, "Greater-than operator found greater-than mjd object to not be greater than", __LINE__);
168  testFramework.assert(!(Compare > CompareCopy), "Greater-than operator found equivalent object to be greater than", __LINE__);
169 
170 
171  testFramework.changeSourceMethod("OperatorLessThanOrEqualTo");
172  //---------------------------------------------------------------------
173  //Does the <= Operator function?
174  //---------------------------------------------------------------------
175  testFramework.assert( LessThanMJD <= Compare, "Less-than-or-equal-to operator found less-than mjd object to not be less than or equal to", __LINE__);
176  testFramework.assert(!(Compare <= LessThanMJD), "Less-than-or-equal-to operator found greater-than mjd object to be less than or equal to", __LINE__);
177  testFramework.assert( Compare <= CompareCopy, "Less-than-or-equal-to operator found equivalent object to not be less than or equal to", __LINE__);
178 
179 
180  testFramework.changeSourceMethod("OperatorGreaterThanOrEqualTo");
181  //---------------------------------------------------------------------
182  //Does the >= Operator function?
183  //---------------------------------------------------------------------
184  testFramework.assert(!(LessThanMJD >= Compare), "Greater-than-or-equal-to operator found less-than mjd object to be greater than or equal to", __LINE__);
185  testFramework.assert( Compare >= LessThanMJD, "Greater-than-or-equal-to operator found greater-than mjd object to not be greater than or equal to", __LINE__);
186  testFramework.assert( Compare >= CompareCopy, "Greater-than-or-equal-to operator found equivalent object to not be greater than or equal to", __LINE__);
187 
188  return testFramework.countFails();
189  }
190 
191 
192 //==========================================================================================================================
193 // Test will check the reset method.
194 //==========================================================================================================================
195  int resetTest (void)
196  {
197  TestUtil testFramework( "MJD", "reset", __FILE__, __LINE__ );
198 
199 
200  MJD Compare(135000,TimeSystem(2)); //Initialize an object
201 
202  Compare.reset(); // Reset it
203 
204  //---------------------------------------------------------------------
205  //Were the attributes reset to expectation?
206  //---------------------------------------------------------------------
207  testFramework.assert(Compare.mjd==0, "reset() did not set the mjd value to 0", __LINE__);
208  testFramework.assert(TimeSystem(0) == Compare.getTimeSystem(), "reset() did not set the TimeSystem to UNK", __LINE__);
209 
210  return testFramework.countFails();
211  }
212 
213 
214 //==========================================================================================================================
215 // Test will check converting to/from CommonTime.
216 //==========================================================================================================================
218  {
219  TestUtil testFramework( "MJD", "isValid", __FILE__, __LINE__ );
220 
221 
222  MJD Compare(135000,TimeSystem(2)); //Initialize an object
223 
224  //---------------------------------------------------------------------
225  //Is the time after the BEGINNING_OF_TIME?
226  //---------------------------------------------------------------------
227  testFramework.assert(Compare.convertToCommonTime() > CommonTime::BEGINNING_OF_TIME, "Time provided found to be less than the beginning of time", __LINE__);
228 
229 
230  //---------------------------------------------------------------------
231  //Is the set object valid?
232  //---------------------------------------------------------------------
233  testFramework.assert(Compare.isValid(), "Time provided found to be unable to convert to/from CommonTime", __LINE__);
234 
235 
236  CommonTime Test = Compare.convertToCommonTime(); //Convert to
237 
238  MJD Test2;
239  Test2.convertFromCommonTime(Test); //Convert From
240 
241  testFramework.changeSourceMethod("CommonTimeConversion");
242  //---------------------------------------------------------------------
243  //Is the result of conversion the same?
244  //---------------------------------------------------------------------
245  testFramework.assert(Compare.getTimeSystem()== Test2.getTimeSystem(), "TimeSystem provided found to be different after converting to and from CommonTime", __LINE__);
246  testFramework.assert(fabs(Test2.mjd - Compare.mjd) < eps, "MJD provided found to be different after converting to and from CommonTime", __LINE__);
247 
248  return testFramework.countFails();
249  }
250 
251 
252 //==========================================================================================================================
253 // Test will check the TimeSystem comparisons when using the comparison operators.
254 //==========================================================================================================================
255  int timeSystemTest (void)
256  {
257  TestUtil testFramework( "MJD", "OperatorEquivalentWithDifferingTimeSystem", __FILE__, __LINE__ );
258 
259 
260  MJD GPS1(135000,TimeSystem(2));
261  MJD GPS2(134000,TimeSystem(2));
262  MJD UTC1(135000,TimeSystem(5));
263  MJD UNKNOWN(135000,TimeSystem(0));
264  MJD ANY(135000,TimeSystem(1));
265  MJD ANY2(134000,TimeSystem(1));
266 
267  //---------------------------------------------------------------------
268  //Verify differing TimeSystem sets equivalence operator to false
269  //Note that the operator test checks for == in ALL members
270  //---------------------------------------------------------------------
271  testFramework.assert(!(GPS1 == UTC1), "Equivalence operator found objects with differing TimeSystems to be the same", __LINE__);
272  testFramework.assert(GPS1 == ANY, "Differing TimeSystems where one is TimeSystem::Any is not ignored for equals", __LINE__);
273  testFramework.assert(UTC1 == ANY, "Differing TimeSystems where one is TimeSystem::Any is not ignored for equals", __LINE__);
274  testFramework.assert(UNKNOWN == ANY, "Differing TimeSystems where one is TimeSystem::Any is not ignored for equals", __LINE__);
275 
276  testFramework.changeSourceMethod("OperatorNotEquivalentWithDifferingTimeSystem");
277  //---------------------------------------------------------------------
278  //Verify different Time System but same time inequality
279  //---------------------------------------------------------------------
280  testFramework.assert(GPS1 != UTC1, "Equivalent objects with differing TimeSystems are found to be equal", __LINE__);
281  testFramework.assert(GPS1 != UNKNOWN, "Equivalent objects with differing TimeSystems are found to be equal", __LINE__);
282  testFramework.assert(!(GPS1 != ANY), "Equivalent objects with differing TimeSystems where one is TimeSystem::Any are found to be not-equal", __LINE__);
283 
284  testFramework.changeSourceMethod("OperatorLessThanWithDifferingTimeSystem");
285  //---------------------------------------------------------------------
286  //Verify TimeSystem=ANY does not matter in other operator comparisons
287  //---------------------------------------------------------------------
288  testFramework.assert(ANY2 < GPS1, "Less than object with Any TimeSystem is not found to be less than", __LINE__);
289  testFramework.assert(GPS2 < ANY,"Less than object with GPS TimeSystem is not found to be less-than a greater object with Any TimeSystem", __LINE__);
290 
291  testFramework.changeSourceMethod("setTimeSystem");
292  UNKNOWN.setTimeSystem(TimeSystem(2)); //Set the Unknown TimeSystem
293  //---------------------------------------------------------------------
294  //Ensure resetting a Time System changes it
295  //---------------------------------------------------------------------
296  testFramework.assert(UNKNOWN.getTimeSystem()==TimeSystem(2), "setTimeSystem was unable to set the TimeSystem", __LINE__);
297 
298  return testFramework.countFails();
299  }
300 
301 
302 //==========================================================================================================================
303 // Test for the formatted printing of MJD objects
304 //==========================================================================================================================
305  int printfTest (void)
306  {
307  TestUtil testFramework( "MJD", "printf", __FILE__, __LINE__ );
308 
309 
310  MJD GPS1(135000,TimeSystem::GPS);
311  MJD UTC1(135000,TimeSystem::UTC);
312 
313  //---------------------------------------------------------------------
314  //Verify printed output matches expectation
315  //---------------------------------------------------------------------
316  testFramework.assert(GPS1.printf("%08Q %02P") == (std::string)"135000.000000 GPS", "printf did not output in the proper format", __LINE__);
317  testFramework.assert(UTC1.printf("%08Q %02P") == (std::string)"135000.000000 UTC", "printf did not output in the proper format", __LINE__);
318 
319 
320  testFramework.changeSourceMethod("printError");
321  //---------------------------------------------------------------------
322  //Verify printed error message matches expectation
323  //---------------------------------------------------------------------
324  testFramework.assert(GPS1.printError("%08Q %02P") == (std::string)"ErrorBadTime ErrorBadTime", "printError did not output in the proper format", __LINE__);
325  testFramework.assert(UTC1.printError("%08Q %02P") == (std::string)"ErrorBadTime ErrorBadTime", "printError did not output in the proper format", __LINE__);
326 
327  return testFramework.countFails();
328  }
329 
330  private:
331  double eps;
332 };
333 
334 int main() //Main function to initialize and run all tests above
335 {
336  int check, errorCounter = 0;
337  MJD_T testClass;
338 
339  check = testClass.initializationTest();
340  errorCounter += check;
341 
342  check = testClass.operatorTest();
343  errorCounter += check;
344 
345  check = testClass.setFromInfoTest();
346  errorCounter += check;
347 
348  check = testClass.resetTest();
349  errorCounter += check;
350 
351  check = testClass.timeSystemTest();
352  errorCounter += check;
353 
354  check = testClass.toFromCommonTimeTest();
355  errorCounter += check;
356 
357  check = testClass.printfTest();
358  errorCounter += check;
359 
360  std::cout << "Total Failures for " << __FILE__ << ": " << errorCounter << std::endl;
361 
362  return errorCounter; //Return the total number of errors
363 }
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::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
gnsstk::MJD::reset
virtual void reset()
Reset this object to the default state.
Definition: MJD.cpp:169
MJD_T::printfTest
int printfTest(void)
Definition: MJD_T.cpp:305
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
gnsstk::MJD::printError
virtual std::string printError(const std::string &fmt) const
Definition: MJD.cpp:114
gnsstk::MJD::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: MJD.cpp:133
MJD_T::~MJD_T
~MJD_T()
Definition: MJD_T.cpp:52
MJD_T::MJD_T
MJD_T()
Definition: MJD_T.cpp:51
gnsstk::CommonTime::BEGINNING_OF_TIME
static const GNSSTK_EXPORT CommonTime BEGINNING_OF_TIME
earliest representable CommonTime
Definition: CommonTime.hpp:102
MJD_T::resetTest
int resetTest(void)
Definition: MJD_T.cpp:195
MJD_T::initializationTest
int initializationTest(void)
Definition: MJD_T.cpp:56
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
MJD.hpp
gnsstk::TestUtil::changeSourceMethod
void changeSourceMethod(const std::string &newMethod)
Definition: TestUtil.hpp:785
MJD_T
Definition: MJD_T.cpp:48
TestUtil.hpp
MJD_T::setFromInfoTest
int setFromInfoTest(void)
Definition: MJD_T.cpp:96
MJD_T::eps
double eps
Definition: MJD_T.cpp:331
MJD_T::toFromCommonTimeTest
int toFromCommonTimeTest(void)
Definition: MJD_T.cpp:217
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::TimeSystem
TimeSystem
Definition of various time systems.
Definition: TimeSystem.hpp:51
gnsstk::MJD::isValid
virtual bool isValid() const
Returns true if this object's members are valid, false otherwise.
Definition: MJD.cpp:158
TimeTag.hpp
MJD_T::operatorTest
int operatorTest(void)
Definition: MJD_T.cpp:129
gnsstk::MJD::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: MJD.cpp:84
gnsstk::TimeSystem::UTC
@ UTC
Coordinated Universal Time (e.g., from NTP)
gnsstk::MJD::mjd
long double mjd
Definition: MJD.hpp:169
gnsstk::MJD::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: MJD.cpp:54
main
int main()
Definition: MJD_T.cpp:334
gnsstk::TimeSystem::GPS
@ GPS
GPS system time.
std
Definition: Angle.hpp:142
gnsstk::MJD::printf
virtual std::string printf(const std::string &fmt) const
Definition: MJD.cpp:95
MJD_T::timeSystemTest
int timeSystemTest(void)
Definition: MJD_T.cpp:255
gnsstk::MJD
Definition: MJD.hpp:54
gnsstk::TestUtil
Definition: TestUtil.hpp:265
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:40