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