GCC Code Coverage Report


Directory: ./
File: TimeInterval.cpp
Date: 2026-09-03 22:13:06
Exec Total Coverage
Lines: 34 84 40.5%
Functions: 7 20 35.0%
Branches: 9 39 23.1%

Line Branch Exec Source
1 #include <Fw/FPrimeBasicTypes.hpp>
2 #include <Fw/Time/TimeInterval.hpp>
3
4 namespace Fw {
5 TimeInterval::TimeInterval(const TimeInterval& other) : Serializable() {
6 this->m_val = other.m_val;
7 }
8
9
1/1
✓ Branch 2 taken 13 times.
13 TimeInterval::TimeInterval(U32 seconds, U32 useconds) : Serializable() {
10
1/1
✓ Branch 1 taken 13 times.
13 this->set(seconds, useconds);
11 13 }
12
13 TimeInterval::TimeInterval(const Time& start, const Time& end)
14 : TimeInterval(TimeInterval::sub(TimeInterval(end.getSeconds(), end.getUSeconds()),
15 TimeInterval(start.getSeconds(), start.getUSeconds()))) {}
16
17 425 void TimeInterval::set(U32 seconds, U32 useconds) {
18 // Assert microseconds portion is less than 10^6
19 425 FW_ASSERT(useconds < 1000000, static_cast<FwAssertArgType>(useconds));
20 425 this->m_val.set(seconds, useconds);
21 425 }
22
23 TimeInterval& TimeInterval::operator=(const TimeInterval& other) {
24 if (this != &other) {
25 this->m_val = other.m_val;
26 }
27 return *this;
28 }
29
30 bool TimeInterval::operator==(const TimeInterval& other) const {
31 return (TimeInterval::compare(*this, other) == EQ);
32 }
33
34 bool TimeInterval::operator!=(const TimeInterval& other) const {
35 return (TimeInterval::compare(*this, other) != EQ);
36 }
37
38 bool TimeInterval::operator>(const TimeInterval& other) const {
39 return (TimeInterval::compare(*this, other) == GT);
40 }
41
42 107 bool TimeInterval::operator<(const TimeInterval& other) const {
43 107 return (TimeInterval::compare(*this, other) == LT);
44 }
45
46 bool TimeInterval::operator>=(const TimeInterval& other) const {
47 TimeInterval::Comparison c = TimeInterval::compare(*this, other);
48 return ((GT == c) or (EQ == c));
49 }
50
51 bool TimeInterval::operator<=(const TimeInterval& other) const {
52 TimeInterval::Comparison c = TimeInterval::compare(*this, other);
53 return ((LT == c) or (EQ == c));
54 }
55
56 TimeIntervalValue TimeInterval::asTimeIntervalValue() const {
57 return this->m_val;
58 }
59
60 SerializeStatus TimeInterval::serializeTo(SerialBufferBase& buffer, Fw::Endianness mode) const {
61 // Use TimeIntervalValue's built-in serialization
62 return this->m_val.serializeTo(buffer, mode);
63 }
64
65 SerializeStatus TimeInterval::deserializeFrom(SerialBufferBase& buffer, Fw::Endianness mode) {
66 // Use TimeIntervalValue's built-in deserialization
67 return this->m_val.deserializeFrom(buffer, mode);
68 }
69
70 5531 U32 TimeInterval::getSeconds() const {
71 5531 return this->m_val.get_seconds();
72 }
73
74 5531 U32 TimeInterval::getUSeconds() const {
75 5531 return this->m_val.get_useconds();
76 }
77
78 107 TimeInterval::Comparison TimeInterval ::compare(const TimeInterval& time1, const TimeInterval& time2) {
79 107 const U32 s1 = time1.getSeconds();
80 107 const U32 s2 = time2.getSeconds();
81 107 const U32 us1 = time1.getUSeconds();
82 107 const U32 us2 = time2.getUSeconds();
83
84
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 1 times.
107 if (s1 < s2) {
85 106 return LT;
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (s1 > s2) {
87 return GT;
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (us1 < us2) {
89 return LT;
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (us1 > us2) {
91 return GT;
92 } else {
93 1 return EQ;
94 }
95 }
96
97 TimeInterval TimeInterval ::add(const TimeInterval& a, const TimeInterval& b) {
98 U32 seconds = a.getSeconds() + b.getSeconds();
99 U32 uSeconds = a.getUSeconds() + b.getUSeconds();
100 FW_ASSERT(uSeconds < 1999999);
101 if (uSeconds >= 1000000) {
102 ++seconds;
103 uSeconds -= 1000000;
104 }
105 TimeInterval c(seconds, uSeconds);
106 return c;
107 }
108
109 TimeInterval TimeInterval ::sub(const TimeInterval& t1, //!< TimeInterval t1
110 const TimeInterval& t2 //!< TimeInterval t2
111 ) {
112 const TimeInterval& minuend = (t1 > t2) ? t1 : t2;
113 const TimeInterval& subtrahend = (t1 > t2) ? t2 : t1;
114
115 U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
116 U32 uSeconds;
117 if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
118 seconds--;
119 uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
120 } else {
121 uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
122 }
123 // Microseconds portion must be normalized to less than 10^6
124 FW_ASSERT(uSeconds < 1000000, static_cast<FwAssertArgType>(uSeconds));
125 return TimeInterval(seconds, static_cast<U32>(uSeconds));
126 }
127
128 105 void TimeInterval::add(U32 seconds, U32 useconds) {
129 105 U32 newSeconds = this->m_val.get_seconds() + seconds;
130 105 U32 newUSeconds = this->m_val.get_useconds() + useconds;
131 105 FW_ASSERT(newUSeconds < 1999999, static_cast<FwAssertArgType>(newUSeconds));
132
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 104 times.
105 if (newUSeconds >= 1000000) {
133 1 newSeconds += 1;
134 1 newUSeconds -= 1000000;
135 }
136 // Assert microseconds portion is less than 10^6
137 105 FW_ASSERT(newUSeconds < 1000000, static_cast<FwAssertArgType>(newUSeconds));
138 105 this->m_val.set(newSeconds, newUSeconds);
139 105 }
140
141 #ifdef BUILD_UT
142 std::ostream& operator<<(std::ostream& os, const TimeInterval& val) {
143 os << "(" << val.getSeconds() << "s," << val.getUSeconds() << "us)";
144 return os;
145 }
146 #endif
147
148 } // namespace Fw
149