PosixTime_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 "PosixTime.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:
52  unsigned initializationTest (void)
53  {
54  TUDEF("PosixTime", "Constructor");
55 
56 
57  PosixTime compare(1350000,1,TimeSystem(2)); // Initialize an object
58 
59  // Were the attributes set to expectation with the explicit
60  // constructor?
61  TUASSERTE(time_t, 1350000, compare.ts.tv_sec);
62  TUASSERTE(long, 1, compare.ts.tv_nsec);
64 
65  TUCSM("PosixTime(PosixTime)");
66  PosixTime copy(compare); // Initialize with copy constructor
67  // Were the attributes set to expectation with the copy constructor?
68  TUASSERTE(time_t, 1350000, copy.ts.tv_sec);
69  TUASSERTE(long, 1, copy.ts.tv_nsec);
71 
72  TUCSM("operator=");
73  PosixTime assigned;
74  assigned = compare;
75  // Were the attributes set to expectation with the Set Operator?
76  TUASSERTE(time_t, 1350000, assigned.ts.tv_sec);
77  TUASSERTE(long, 1, assigned.ts.tv_nsec);
79 
80  TURETURN();
81  }
82 
83 
84  // Check if PosixTime variable can be set from a map. Test also
85  // implicity tests whether the != operator functions.
86  unsigned setFromInfoTest (void)
87  {
88  TUDEF("PosixTime", "setFromInfo");
89 
90  PosixTime setFromInfo1;
91  PosixTime setFromInfo2;
92  PosixTime compare(1350000,1,TimeSystem(2)), compare2(0,1,TimeSystem(2));
94 
95  id['W'] = "1350000";
96  id['N'] = "1";
97  id['P'] = "GPS";
98 
99  // Does a proper setFromInfo work with all information provided?
100  TUASSERT(setFromInfo1.setFromInfo(id));
101  TUASSERTE(PosixTime, compare, setFromInfo1);
102 
103  id.erase('W');
104  // Does a proper setFromInfo work with missing information?
105  TUASSERT(setFromInfo2.setFromInfo(id));
106  TUASSERTE(PosixTime, compare2, setFromInfo2);
107 
108  TURETURN();
109  }
110 
111 
112  // Check if the ways to initialize and set an PosixTime object.
113  // Test also tests whether the comparison operators and isValid
114  // method function.
115  unsigned operatorTest (void)
116  {
117  TUDEF("PosixTime", "moo");
118 
119  PosixTime compare(1350000,100); // Initialize with value
120  PosixTime lessThanSec(1340000, 100); // Initialize with fewer seconds
121  PosixTime lessThanNanoSec(1350000,0); // Initialize with fewer nanoseconds
122  PosixTime compareCopy(compare); // Initialize with copy constructor
123 
124  TUCSM("operator==");
125  TUASSERTE(PosixTime, compare, compareCopy);
126  TUASSERT(!(compare == lessThanSec));
127  TUASSERT(!(compare == lessThanNanoSec));
128 
129  TUCSM("operator!=");
130  TUASSERT(!(compare != compareCopy));
131  TUASSERT(compare != lessThanSec);
132  TUASSERT(compare != lessThanNanoSec);
133 
134  TUCSM("operator<");
135  TUASSERT(lessThanSec < compare);
136  TUASSERT(lessThanNanoSec < compare);
137  TUASSERT(!(compare < lessThanSec));
138  TUASSERT(!(compare < lessThanNanoSec));
139  TUASSERT(!(compare < compareCopy));
140 
141  TUCSM("operator>");
142  TUASSERT(!(lessThanSec > compare));
143  TUASSERT(!(lessThanNanoSec > compare));
144  TUASSERT(compare > lessThanSec);
145  TUASSERT(compare > lessThanNanoSec);
146  TUASSERT(!(compare > compareCopy));
147 
148  TUCSM("operator<=");
149  TUASSERT(lessThanSec <= compare);
150  TUASSERT(lessThanNanoSec <= compare);
151  TUASSERT(!(compare <= lessThanSec));
152  TUASSERT(!(compare <= lessThanNanoSec));
153  TUASSERT(compare <= compareCopy);
154 
155  TUCSM("operator>=");
156  TUASSERT(!(lessThanSec >= compare));
157  TUASSERT(!(lessThanNanoSec >= compare));
158  TUASSERT(compare >= lessThanSec);
159  TUASSERT(compare >= lessThanNanoSec);
160  TUASSERT(compare >= compareCopy);
161 
162  TURETURN();
163  }
164 
165 
166  // Check the reset method.
167  unsigned resetTest (void)
168  {
169  TUDEF("PosixTime", "reset" );
170 
171  PosixTime compare(1350000,0,TimeSystem(2)); // Initialize an object
172 
173  compare.reset(); // Reset it
174 
175  // Were the attributes reset to expectation?
177  TUASSERTE(time_t, 0, compare.ts.tv_sec);
178  TUASSERTE(long, 0, compare.ts.tv_nsec);
179 
180  TURETURN();
181  }
182 
183 
184  // Check converting to/from CommonTime.
185  unsigned toFromCommonTimeTest (void)
186  {
187  TUDEF("PosixTime", "isValid");
188 
189  PosixTime compare(1350000,0,TimeSystem(2)); // Initialize an object
190  CommonTime test = compare.convertToCommonTime(); // Convert to
191 
192  // Is the time after the BEGINNING_OF_TIME?
193  // Is this even a useful test?
195 
196  // Is the set object valid?
197  TUASSERT(compare.isValid());
198 
199  PosixTime test2;
200  test2.convertFromCommonTime(test); // Convert From
201  TUCSM("convertFromCommonTime");
202  // Is the result of conversion the same?
203  TUASSERTE(TimeSystem, compare.getTimeSystem(), test2.getTimeSystem());
204  TUASSERTE(time_t, compare.ts.tv_sec, test2.ts.tv_sec);
205  TUASSERTE(long, compare.ts.tv_nsec, test2.ts.tv_nsec);
206 
207  TURETURN();
208  }
209 
210 
211  // Check the TimeSystem comparisons when using the comparison
212  // operators.
213  unsigned timeSystemTest (void)
214  {
215  TUDEF("PosixTime", "OperatorEquivalentWithDifferingTimeSystem");
216 
217  PosixTime GPS1(1350000,0,TimeSystem::GPS);
218  PosixTime GPS2(1340000,0,TimeSystem::GPS);
219  PosixTime UTC1(1350000,0,TimeSystem::UTC);
220  PosixTime UNKNOWN(1350000,0,TimeSystem::Unknown);
221  PosixTime ANY(1350000,0,TimeSystem::Any);
222  PosixTime ANY2(1340000,0,TimeSystem::Any);
223 
224  // Verify differing TimeSystem sets equivalence operator to false
225  // Note that the operator test checks for == in ALL members
226  TUASSERT(!(GPS1 == UTC1));
227  TUASSERT(GPS1 == ANY);
228  TUASSERT(UTC1 == ANY);
229  TUASSERT(UNKNOWN == ANY);
230 
231  TUCSM("OperatorNotEquivalentWithDifferingTimeSystem");
232  // Verify different Time System but same time inequality
233  TUASSERT(GPS1 != UTC1);
234  TUASSERT(GPS1 != UNKNOWN);
235  TUASSERT(!(GPS1 != ANY));
236 
237  TUCSM("OperatorLessThanWithDifferingTimeSystem");
238  // Verify TimeSystem=ANY does not matter in other operator comparisons
239  TUASSERT(ANY2 < GPS1);
240  TUASSERT(GPS2 < ANY);
241 
242  TUCSM("setTimeSystem");
243  UNKNOWN.setTimeSystem(TimeSystem(2)); // Set the Unknown TimeSystem
244  // Ensure resetting a Time System changes it
246 
247  TURETURN();
248  }
249 
250 
251  // Test for the formatted printing of PosixTime objects
252  unsigned printfTest (void)
253  {
254  TUDEF("PosixTime", "printf");
255 
256 
257  PosixTime GPS1(1350000,0,TimeSystem::GPS);
258  PosixTime UTC1(1350000,0,TimeSystem::UTC);
259 
260  // Verify printed output matches expectation
261  TUASSERTE(string, "1350000 00 GPS", GPS1.printf("%07W %02N %02P"));
262  TUASSERTE(string, "1350000 00 UTC", UTC1.printf("%07W %02N %02P"));
263 
264  // Verify printed error message matches expectation
265  TUASSERTE(string, "ErrorBadTime ErrorBadTime ErrorBadTime",
266  GPS1.printError("%07W %02N %02P"));
267  TUASSERTE(string, "ErrorBadTime ErrorBadTime ErrorBadTime",
268  UTC1.printError("%07W %02N %02P"));
269 
270  TURETURN();
271  }
272 };
273 
274 int main() // Main function to initialize and run all tests above
275 {
276  int errorTotal = 0;
277  PosixTime_T testClass;
278 
279  errorTotal += testClass.initializationTest();
280  errorTotal += testClass.operatorTest();
281  errorTotal += testClass.setFromInfoTest();
282  errorTotal += testClass.resetTest();
283  errorTotal += testClass.timeSystemTest();
284  errorTotal += testClass.toFromCommonTimeTest();
285  errorTotal += testClass.printfTest();
286 
287  std::cout << "Total Failures for " << __FILE__ << ": " << errorTotal
288  << std::endl;
289 
290  return errorTotal; // Return the total number of errors
291 }
PosixTime_T::timeSystemTest
unsigned timeSystemTest(void)
Definition: PosixTime_T.cpp:213
TUCSM
#define TUCSM(METHOD)
Definition: TestUtil.hpp:59
PosixTime_T::operatorTest
unsigned operatorTest(void)
Definition: PosixTime_T.cpp:115
PosixTime_T::printfTest
unsigned printfTest(void)
Definition: PosixTime_T.cpp:252
PosixTime_T
Definition: PosixTime_T.cpp:48
gnsstk::PosixTime::printError
virtual std::string printError(const std::string &fmt) const
Definition: PosixTime.cpp:123
gnsstk::TimeTag::setTimeSystem
void setTimeSystem(const TimeSystem &timeSys)
Set method for internal variable timeSystem (enum).
Definition: TimeTag.hpp:165
main
int main()
Definition: PosixTime_T.cpp:274
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
PosixTime_T::initializationTest
unsigned initializationTest(void)
initializationTest ensures the constructors set the values properly
Definition: PosixTime_T.cpp:52
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
gnsstk::PosixTime::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: PosixTime.cpp:144
gnsstk::PosixTime::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: PosixTime.cpp:54
gnsstk::TimeSystem::Any
@ Any
wildcard; allows comparison with any other type
gnsstk::CommonTime::BEGINNING_OF_TIME
static const GNSSTK_EXPORT CommonTime BEGINNING_OF_TIME
earliest representable CommonTime
Definition: CommonTime.hpp:102
PosixTime_T::setFromInfoTest
unsigned setFromInfoTest(void)
Definition: PosixTime_T.cpp:86
gnsstk::TimeSystem::Unknown
@ Unknown
unknown time frame; for legacy code compatibility
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
PosixTime.hpp
gnsstk::PosixTime::ts
struct timespec ts
Definition: PosixTime.hpp:187
TUASSERT
#define TUASSERT(EXPR)
Definition: TestUtil.hpp:63
TestUtil.hpp
TURETURN
#define TURETURN()
Definition: TestUtil.hpp:232
gnsstk::PosixTime::printf
virtual std::string printf(const std::string &fmt) const
Definition: PosixTime.cpp:102
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::TimeSystem
TimeSystem
Definition of various time systems.
Definition: TimeSystem.hpp:51
TimeTag.hpp
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
gnsstk::TimeSystem::UTC
@ UTC
Coordinated Universal Time (e.g., from NTP)
PosixTime_T::resetTest
unsigned resetTest(void)
Definition: PosixTime_T.cpp:167
gnsstk::TimeSystem::GPS
@ GPS
GPS system time.
std
Definition: Angle.hpp:142
gnsstk::PosixTime::isValid
virtual bool isValid() const
Returns true if this object's members are valid, false otherwise.
Definition: PosixTime.cpp:173
gnsstk::PosixTime::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: PosixTime.cpp:71
gnsstk::PosixTime
Definition: PosixTime.hpp:62
PosixTime_T::toFromCommonTimeTest
unsigned toFromCommonTimeTest(void)
Definition: PosixTime_T.cpp:185
gnsstk::PosixTime::reset
virtual void reset()
Reset this object to the default state.
Definition: PosixTime.cpp:184
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