sfptostr.cpp \
socktoa.cpp \
ssl_init.cpp \
+ statestr.cpp \
strtolfp.cpp \
tsftomsu.cpp \
+ tstotv.cpp \
+ tvtots.cpp \
uglydate.cpp \
uinttoa.cpp \
ymd2yd.cpp
} else {
return ::testing::AssertionFailure()
<< " expected: " << lfptoa(&expected, FRACTION_PREC)
- << " but was: " << lfptoa(&actual, FRACTION_PREC);
+ << " (" << expected.l_ui << "." << expected.l_uf << ")"
+ << " but was: " << lfptoa(&actual, FRACTION_PREC)
+ << " (" << actual.l_ui << "." << actual.l_uf << ")";
}
}
};
--- /dev/null
+#include "libntptest.h"
+
+extern "C" {
+#include "ntp.h" // Needed for MAX_MAC_LEN used in ntp_control.h
+#include "ntp_control.h"
+};
+
+class statestrTest : public libntptest {
+};
+
+// eventstr()
+TEST_F(statestrTest, PeerRestart) {
+ EXPECT_STREQ("restart", eventstr(PEVNT_RESTART));
+}
+
+TEST_F(statestrTest, SysUnspecified) {
+ EXPECT_STREQ("unspecified", eventstr(EVNT_UNSPEC));
+}
+
+// ceventstr()
+TEST_F(statestrTest, ClockCodeExists) {
+ EXPECT_STREQ("clk_unspec", ceventstr(CTL_CLK_OKAY));
+}
+
+TEST_F(statestrTest, ClockCodeUnknown) {
+ EXPECT_STREQ("clk_-1", ceventstr(-1));
+}
--- /dev/null
+#include "libntptest.h"
+
+extern "C" {
+#include "ntp_fp.h"
+#include "ntp_unixtime.h"
+};
+
+class tstotvTest : public libntptest {
+protected:
+ ::testing::AssertionResult IsEqual(const timeval& expected,
+ const timeval& actual) {
+ if (expected.tv_sec == actual.tv_sec &&
+ expected.tv_usec == actual.tv_usec) {
+ // Success
+ return ::testing::AssertionSuccess();
+ } else {
+ return ::testing::AssertionFailure()
+ << "expected: " << expected.tv_sec << "."
+ << expected.tv_usec
+ << " but was: " << actual.tv_sec << "."
+ << actual.tv_usec;
+ }
+ }
+
+ static const u_long HALF = 2147483648UL;
+};
+
+TEST_F(tstotvTest, Seconds) {
+ const l_fp input = {50, 0}; // 50.0 s
+ const timeval expected = {50, 0};
+ timeval actual;
+
+ TSTOTV(&input, &actual);
+
+ EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(tstotvTest, MicrosecondsExact) {
+ const l_fp input = {50, HALF}; // 10.5 s
+ const timeval expected = {50, 500000};
+ timeval actual;
+
+ TSTOTV(&input, &actual);
+
+ EXPECT_TRUE(IsEqual(expected, actual));
+
+}
+
+TEST_F(tstotvTest, MicrosecondsRounding) {
+ const l_fp input = {50, 3865471UL}; // Should round to 50.0009
+ const timeval expected = {50, 900};
+ timeval actual;
+
+ TSTOTV(&input, &actual);
+
+ EXPECT_TRUE(IsEqual(expected, actual));
+}
--- /dev/null
+#include "lfptest.h"
+
+extern "C" {
+#include "ntp_unixtime.h"
+};
+
+class tvtotsTest : public lfptest {
+protected:
+ static const u_long HALF = 2147483648UL;
+ static const u_long HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005
+ static const u_long HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005
+};
+
+TEST_F(tvtotsTest, Seconds) {
+ timeval input = {500, 0}; // 500.0 s
+ l_fp expected = {500, 0};
+ l_fp actual;
+
+ TVTOTS(&input, &actual);
+
+ EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(tvtotsTest, MicrosecondsRounded) {
+ /* 0.0005 can not be represented exact in a l_fp structure.
+ * It would equal to 2147483,648. This means that
+ * HALF_PROMILLE_UP (which is 2147484) should be
+ * the correct rounding. */
+
+ timeval input = {0, 500}; // 0.0005 exact
+ l_fp expected = {0, HALF_PROMILLE_UP};
+ l_fp actual;
+
+ TVTOTS(&input, &actual);
+ EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(tvtotsTest, MicrosecondsExact) {
+ // 0.5 can be represented exact in both l_fp and timeval.
+
+ timeval input = {10, 500000}; // 0.5 exact
+ l_fp expected = {10, HALF}; // 0.5 exact
+ l_fp actual;
+
+ TVTOTS(&input, &actual);
+ EXPECT_TRUE(IsEqual(expected, actual));
+}