GCC Code Coverage Report


Directory: ./
File: Fw/Time/Time.cpp
Date: 2026-09-03 21:13:48
Exec Total Coverage
Lines: 164 165 99.4%
Functions: 38 38 100.0%
Branches: 78 80 97.5%

Line Branch Exec Source
1 #include <Fw/FPrimeBasicTypes.hpp>
2 #include <Fw/Time/Time.hpp>
3
4 namespace Fw {
5 const Time ZERO_TIME = Time();
6
7
1/1
✓ Branch 11 taken 2554272 times.
2554272 Time::Time() : m_val() {
8
1/1
✓ Branch 6 taken 2554272 times.
2554272 m_val.set_timeBase(TimeBase::TB_NONE);
9
1/1
✓ Branch 6 taken 2554272 times.
2554272 m_val.set_timeContext(0);
10
1/1
✓ Branch 6 taken 2554272 times.
2554272 m_val.set_seconds(0);
11
1/1
✓ Branch 6 taken 2554272 times.
2554272 m_val.set_useconds(0);
12 2554272 }
13
14 17871028 Time::~Time() {}
15
16
1/1
✓ Branch 10 taken 1202518 times.
1202518 Time::Time(U32 seconds, U32 useconds) {
17
2/2
✓ Branch 5 taken 1202518 times.
✓ Branch 8 taken 1202518 times.
1202518 this->set(TimeBase::TB_NONE, 0, seconds, useconds);
18 1202518 }
19
20
1/1
✓ Branch 10 taken 1449 times.
1449 Time::Time(TimeBase timeBase, U32 seconds, U32 useconds) {
21
2/2
✓ Branch 5 taken 1449 times.
✓ Branch 8 taken 1449 times.
1449 this->set(timeBase, 0, seconds, useconds);
22 1449 }
23
24 1306801 void Time::set(U32 seconds, U32 useconds) {
25
2/2
✓ Branch 17 taken 1306801 times.
✓ Branch 20 taken 1306801 times.
1306801 this->set(this->m_val.get_timeBase(), this->m_val.get_timeContext(), seconds, useconds);
26 1306801 }
27
28 16 void Time::set(TimeBase timeBase, U32 seconds, U32 useconds) {
29
2/2
✓ Branch 11 taken 16 times.
✓ Branch 14 taken 16 times.
16 this->set(timeBase, this->m_val.get_timeContext(), seconds, useconds);
30 16 }
31
32
1/1
✓ Branch 10 taken 1364004 times.
1364004 Time::Time(TimeBase timeBase, FwTimeContextStoreType context, U32 seconds, U32 useconds) {
33
2/2
✓ Branch 5 taken 1364004 times.
✓ Branch 8 taken 1364004 times.
1364004 this->set(timeBase, context, seconds, useconds);
34 1364004 }
35
36 3874792 void Time::set(TimeBase timeBase, FwTimeContextStoreType context, U32 seconds, U32 useconds) {
37 // Assert microseconds portion is less than 10^6
38 3874792 FW_ASSERT(useconds < 1000000, static_cast<FwAssertArgType>(useconds));
39 3874792 this->m_val.set(timeBase, context, seconds, useconds);
40 3874792 }
41
42
1/1
✓ Branch 10 taken 2 times.
2 Fw::Time::Time(F64 seconds) {
43
1/1
✓ Branch 4 taken 2 times.
2 this->set(seconds);
44 2 }
45
46 4 void Fw::Time::set(F64 seconds) {
47 4 U32 parsedSeconds = this->parseSeconds(seconds);
48 4 U32 parsedUseconds = this->parseUSeconds(seconds);
49 // parseUSeconds rounds up to a whole second at 999999.5 or above; carry it as add() does
50
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (parsedUseconds >= 1000000) {
51 1 parsedSeconds++;
52 1 parsedUseconds -= 1000000;
53 }
54 4 this->set(parsedSeconds, parsedUseconds);
55 4 }
56
57 270193 Time& Time::operator=(const Time& other) {
58
1/2
✓ Branch 0 taken 270193 times.
✗ Branch 1 not taken.
270193 if (this != &other) {
59 270193 this->m_val = other.m_val;
60 }
61 270193 return *this;
62 }
63
64 1 Time& Fw::Time::operator=(F64 seconds) {
65 1 this->set(seconds);
66 1 return *this;
67 }
68
69 1 Time& Fw::Time::operator+=(F64 seconds) {
70 1 this->add(seconds);
71 1 return *this;
72 }
73
74 10427 bool Time::operator==(const Time& other) const {
75
1/1
✓ Branch 3 taken 10427 times.
10427 return (Time::compare(*this, other) == TimeComparison::EQ);
76 }
77
78 2 bool Time::operator!=(const Time& other) const {
79
1/1
✓ Branch 3 taken 2 times.
2 return (Time::compare(*this, other) != TimeComparison::EQ);
80 }
81
82 1 bool Time::operator>(const Time& other) const {
83
1/1
✓ Branch 3 taken 1 times.
1 return (Time::compare(*this, other) == TimeComparison::GT);
84 }
85
86 21 bool Time::operator<(const Time& other) const {
87
1/1
✓ Branch 3 taken 21 times.
21 return (Time::compare(*this, other) == TimeComparison::LT);
88 }
89
90 2385129 bool Time::operator>=(const Time& other) const {
91
1/1
✓ Branch 3 taken 2385129 times.
2385129 TimeComparison c = Time::compare(*this, other);
92
4/4
✓ Branch 2 taken 2369237 times.
✓ Branch 3 taken 15892 times.
✓ Branch 6 taken 8954 times.
✓ Branch 7 taken 2360283 times.
4770258 return ((TimeComparison::GT == c) or (TimeComparison::EQ == c));
93 2385129 }
94
95 6456 bool Time::operator<=(const Time& other) const {
96
1/1
✓ Branch 3 taken 6456 times.
6456 TimeComparison c = Time::compare(*this, other);
97
4/4
✓ Branch 2 taken 6448 times.
✓ Branch 3 taken 8 times.
✓ Branch 6 taken 1112 times.
✓ Branch 7 taken 5336 times.
12912 return ((TimeComparison::LT == c) or (TimeComparison::EQ == c));
98 6456 }
99
100 1 Fw::Time::operator F64() const {
101 1 const U32 seconds = this->m_val.get_seconds();
102 1 const U32 useconds = this->m_val.get_useconds();
103 1 return static_cast<F64>(seconds) + (static_cast<F64>(useconds) / 1000000.0);
104 }
105
106 1 TimeValue Time::asTimeValue() const {
107 1 return this->m_val;
108 }
109
110 33061 SerializeStatus Time::serializeTo(SerialBufferBase& buffer, Fw::Endianness mode) const {
111 33061 return this->m_val.serializeTo(buffer, mode);
112 }
113
114 43337 SerializeStatus Time::deserializeFrom(SerialBufferBase& buffer, Fw::Endianness mode) {
115
1/1
✓ Branch 2 taken 43337 times.
43337 TimeValue value;
116
1/1
✓ Branch 2 taken 43337 times.
43337 const SerializeStatus status = value.deserializeFrom(buffer, mode);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43337 times.
43337 if (status != FW_SERIALIZE_OK) {
118 return status;
119 }
120 // Reject out-of-range microseconds rather than admitting a value that asserts on later use
121
2/2
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 43336 times.
43337 if (value.get_useconds() >= 1000000) {
122 1 return FW_DESERIALIZE_FORMAT_ERROR;
123 }
124
1/1
✓ Branch 6 taken 43336 times.
43336 this->m_val = value;
125 43336 return FW_SERIALIZE_OK;
126 43337 }
127
128 7521198 U32 Time::getSeconds() const {
129 7521198 return this->m_val.get_seconds();
130 }
131
132 7523369 U32 Time::getUSeconds() const {
133 7523369 return this->m_val.get_useconds();
134 }
135
136 8810676 TimeBase Time::getTimeBase() const {
137 8810676 return this->m_val.get_timeBase();
138 }
139
140 3971620 FwTimeContextStoreType Time::getContext() const {
141 3971620 return this->m_val.get_timeContext();
142 }
143
144 40134 Time Time ::zero(TimeBase timeBase) {
145
2/2
✓ Branch 2 taken 40134 times.
✓ Branch 5 taken 40134 times.
40134 Time time(timeBase, 0, 0, 0);
146 40134 return time;
147 }
148
149 2402060 TimeComparison Time ::compare(const Time& time1, const Time& time2) {
150
4/4
✓ Branch 2 taken 2402060 times.
✓ Branch 10 taken 2402060 times.
✓ Branch 22 taken 1 times.
✓ Branch 23 taken 2402059 times.
2402060 if (time1.getTimeBase() != time2.getTimeBase()) {
151 1 return TimeComparison::INCOMPARABLE;
152 }
153
154 // Do not compare time context
155
156 2402059 const U32 s1 = time1.getSeconds();
157 2402059 const U32 s2 = time2.getSeconds();
158 2402059 const U32 us1 = time1.getUSeconds();
159 2402059 const U32 us2 = time2.getUSeconds();
160
161
2/2
✓ Branch 0 taken 2324188 times.
✓ Branch 1 taken 77871 times.
2402059 if (s1 < s2) {
162 2324188 return TimeComparison::LT;
163
2/2
✓ Branch 0 taken 8537 times.
✓ Branch 1 taken 69334 times.
77871 } else if (s1 > s2) {
164 8537 return TimeComparison::GT;
165
2/2
✓ Branch 0 taken 36118 times.
✓ Branch 1 taken 33216 times.
69334 } else if (us1 < us2) {
166 36118 return TimeComparison::LT;
167
2/2
✓ Branch 0 taken 12703 times.
✓ Branch 1 taken 20513 times.
33216 } else if (us1 > us2) {
168 12703 return TimeComparison::GT;
169 } else {
170 20513 return TimeComparison::EQ;
171 }
172 }
173
174 1322734 Time Time ::add(const Time& a, const Time& b) {
175 1322734 FW_ASSERT(a.getTimeBase() == b.getTimeBase(), static_cast<FwAssertArgType>(a.getTimeBase()),
176 static_cast<FwAssertArgType>(b.getTimeBase()));
177 // Do not assert on time context match
178
179 1322734 U32 seconds = a.getSeconds() + b.getSeconds();
180 1322734 U32 uSeconds = a.getUSeconds() + b.getUSeconds();
181 1322734 FW_ASSERT(uSeconds < 1999999);
182
2/2
✓ Branch 0 taken 1109 times.
✓ Branch 1 taken 1321625 times.
1322734 if (uSeconds >= 1000000) {
183 1109 ++seconds;
184 1109 uSeconds -= 1000000;
185 }
186
187 // Return a time context of 0 if they do not match
188 1322734 FwTimeContextStoreType context = a.getContext();
189
2/2
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1322733 times.
1322734 if (a.getContext() != b.getContext()) {
190 1 context = 0;
191 }
192
193
2/2
✓ Branch 2 taken 1322734 times.
✓ Branch 5 taken 1322734 times.
1322734 Time c(a.getTimeBase(), context, seconds, uSeconds);
194 1322734 return c;
195 }
196
197 1101 Time Time ::sub(const Time& minuend, //!< Time minuend
198 const Time& subtrahend //!< Time subtrahend
199 ) {
200 1101 FW_ASSERT(minuend.getTimeBase() == subtrahend.getTimeBase(), static_cast<FwAssertArgType>(minuend.getTimeBase()),
201 static_cast<FwAssertArgType>(subtrahend.getTimeBase()));
202 // Do not assert on time context match
203 // Assert minuend is greater than subtrahend
204 1101 FW_ASSERT(minuend >= subtrahend);
205
206 1101 U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
207 U32 uSeconds;
208
2/2
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1100 times.
1101 if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
209 1 seconds--;
210 1 uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
211 } else {
212 1100 uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
213 }
214
215 // Return a time context of 0 if they do not match
216 1101 FwTimeContextStoreType context = minuend.getContext();
217
2/2
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1100 times.
1101 if (minuend.getContext() != subtrahend.getContext()) {
218 1 context = 0;
219 }
220
221
2/2
✓ Branch 2 taken 1101 times.
✓ Branch 5 taken 1101 times.
2202 return Time(minuend.getTimeBase(), context, seconds, static_cast<U32>(uSeconds));
222 }
223
224 9 U32 Fw::Time::parseSeconds(F64 seconds) {
225 // Assert negative value
226 9 FW_ASSERT(seconds >= static_cast<F64>(0.0));
227 9 return static_cast<U32>(seconds);
228 }
229
230 11 U32 Fw::Time::parseUSeconds(F64 seconds) {
231 // Assert negative value
232 11 FW_ASSERT(seconds >= static_cast<F64>(0.0));
233 11 U32 parsedSeconds = static_cast<U32>(seconds);
234 11 const F64 fractionalPart = seconds - static_cast<F64>(parsedSeconds);
235 // Add 0.5 to round to nearest microsecond (e.g, 999999.9999 = 1000000)
236 11 U32 parsedUSeconds = static_cast<U32>(fractionalPart * static_cast<F64>(1000000.0) + static_cast<F64>(0.5));
237 11 return parsedUSeconds;
238 }
239
240 1300277 void Time::add(U32 seconds, U32 useconds) {
241 1300277 U32 newSeconds = this->m_val.get_seconds() + seconds;
242 1300277 U32 newUSeconds = this->m_val.get_useconds() + useconds;
243 1300277 FW_ASSERT(newUSeconds < 1999999, static_cast<FwAssertArgType>(newUSeconds));
244
2/2
✓ Branch 0 taken 390000 times.
✓ Branch 1 taken 910277 times.
1300277 if (newUSeconds >= 1000000) {
245 390000 newSeconds += 1;
246 390000 newUSeconds -= 1000000;
247 }
248 1300277 this->set(newSeconds, newUSeconds);
249 1300277 }
250
251 4 void Time::add(F64 seconds) {
252 4 U32 parsedSeconds = this->parseSeconds(seconds);
253 4 U32 parsedUseconds = this->parseUSeconds(seconds);
254 // Add parsed seconds and useconds
255 4 this->add(parsedSeconds, parsedUseconds);
256 4 }
257
258 136 void Time::setTimeBase(TimeBase timeBase) {
259 136 this->m_val.set_timeBase(timeBase);
260 136 }
261
262 144 void Time::setTimeContext(FwTimeContextStoreType context) {
263 144 this->m_val.set_timeContext(context);
264 144 }
265
266 #ifdef BUILD_UT
267 2 std::ostream& operator<<(std::ostream& os, const Time& val) {
268
7/7
✓ Branch 5 taken 2 times.
✓ Branch 9 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 23 taken 2 times.
✓ Branch 27 taken 2 times.
✓ Branch 37 taken 2 times.
✓ Branch 41 taken 2 times.
2 os << "(" << val.getTimeBase() << "," << val.getUSeconds() << "," << val.getSeconds() << ")";
269 2 return os;
270 }
271 #endif
272
273 } // namespace Fw
274