8 #include "xclox/ntp/timestamp.hpp"
10 #include "tools/helper.hpp"
12 using namespace xclox::ntp;
13 using namespace std::chrono;
15 TEST_SUITE(
"Timestamp")
17 constexpr uint32_t ShortFormMax { std::numeric_limits<uint32_t>::max() };
18 constexpr uint32_t ShortFormMid { ShortFormMax / 2 + 1 };
19 constexpr uint64_t ShortFormLen { ShortFormMax + 1ull };
20 constexpr uint64_t LongFormMax { std::numeric_limits<uint64_t>::max() };
22 TEST_CASE(
"constant values")
24 CHECK(ShortFormMid == 0x80000000);
25 CHECK(ShortFormLen == 0x100000000);
28 TEST_CASE(
"default constructible to zero")
31 CHECK(t.
value() == 0);
34 TEST_CASE(
"constructible from a raw timestamp value")
37 CHECK(t1.value() == 0);
40 CHECK(t2.value() == LongFormMax);
43 TEST_CASE(
"constructible from seconds and a fraction")
46 CHECK(t1.value() == 0);
49 CHECK(t2.value() == ShortFormMax);
52 CHECK(t3.value() == LongFormMax);
55 TEST_CASE(
"constructible from a duration")
58 CHECK(t1.value() == 0);
61 CHECK(t2.value() == ShortFormLen);
64 CHECK(t3.value() == ShortFormMid);
67 CHECK(t4.value() == 0);
69 Timestamp t5(seconds { ShortFormLen + 1 });
70 CHECK(t5.value() == ShortFormLen);
73 TEST_CASE(
"constructible from a time point")
75 Timestamp t1(system_clock::time_point {});
76 CHECK(t1.seconds() == xclox::ntp::internal::EpochDeltaSeconds.count());
77 CHECK(t1.fraction() == 0);
79 Timestamp t2(system_clock::time_point { milliseconds(500) - xclox::ntp::internal::EpochDeltaSeconds });
80 CHECK(t2.seconds() == 0);
81 CHECK(t2.fraction() == ShortFormMid);
83 Timestamp t3(system_clock::time_point { seconds(ShortFormLen) - xclox::ntp::internal::EpochDeltaSeconds + milliseconds(500) });
84 CHECK(t3.seconds() == 0);
85 CHECK(t3.fraction() == ShortFormMid);
88 TEST_CASE(
"breakable down into seconds and a fraction")
91 CHECK(t1.seconds() == 0);
92 CHECK(t1.fraction() == 0);
95 CHECK(t2.seconds() == 0);
96 CHECK(t2.fraction() == ShortFormMax);
99 CHECK(t3.seconds() == 1);
100 CHECK(t3.fraction() == 0);
103 CHECK(t4.seconds() == ShortFormMax);
104 CHECK(t4.fraction() == ShortFormMax);
107 TEST_CASE(
"convertible into duration")
110 CHECK(t1.duration() == seconds(0));
113 CHECK(t2.duration() == seconds(1));
116 CHECK(t3.duration() == microseconds(500000));
119 CHECK(t4.duration() == seconds(ShortFormLen) - system_clock::duration(1));
122 TEST_CASE(
"retains duration with system clock precision ± 1 unit")
124 auto currentDuration = system_clock::duration(0);
125 const auto& endDuration = currentDuration + milliseconds(1);
126 while (currentDuration < endDuration) {
127 CHECK(abs((currentDuration -
Timestamp(currentDuration).duration()).count()) <= 1);
128 currentDuration = currentDuration + system_clock::duration(1);
132 TEST_CASE(
"comparable")
142 CHECK_FALSE(
Timestamp(system_clock::duration(1)) !=
Timestamp(system_clock::duration(1)));
145 TEST_CASE(
"subtractable")
Timestamp is an immutable class representing a NTP timestamp.
Definition: timestamp.hpp:45
uint64_t value() const
Returns the NTP timestamp in long format.
Definition: timestamp.hpp:87