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


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