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


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