]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Added tests for statestr, tstotv and tvtots.
authorLinux Karlsson <karlsson@ntp.org>
Wed, 23 Jun 2010 12:21:42 +0000 (14:21 +0200)
committerLinux Karlsson <karlsson@ntp.org>
Wed, 23 Jun 2010 12:21:42 +0000 (14:21 +0200)
bk: 4c21fc56jY6sEviFKVgNqN9YZdsuPA

tests/libntp/Makefile.am
tests/libntp/lfptest.h
tests/libntp/statestr.cpp [new file with mode: 0644]
tests/libntp/tstotv.cpp [new file with mode: 0644]
tests/libntp/tvtots.cpp [new file with mode: 0644]

index 560bef55d35854bb52571d6881853a4c0bd6aec9..6b4e2fd2f9d53a787507fad25900e5bc81d88f20 100644 (file)
@@ -25,8 +25,11 @@ tests_SOURCES = ../main.cpp          \
                sfptostr.cpp            \
                socktoa.cpp             \
                ssl_init.cpp            \
+               statestr.cpp            \
                strtolfp.cpp            \
                tsftomsu.cpp            \
+               tstotv.cpp              \
+               tvtots.cpp              \
                uglydate.cpp            \
                uinttoa.cpp             \
                ymd2yd.cpp
index 56c7c85aded4bf3ab6696a2e87d724d7ca5eaae1..95ae13fbfacfd643cf040fd476ce3e04cf8c83eb 100644 (file)
@@ -15,7 +15,9 @@ protected:
                } 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 << ")";
                }
        }
 };
diff --git a/tests/libntp/statestr.cpp b/tests/libntp/statestr.cpp
new file mode 100644 (file)
index 0000000..506b079
--- /dev/null
@@ -0,0 +1,27 @@
+#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));
+}
diff --git a/tests/libntp/tstotv.cpp b/tests/libntp/tstotv.cpp
new file mode 100644 (file)
index 0000000..04b3b73
--- /dev/null
@@ -0,0 +1,57 @@
+#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));
+}
diff --git a/tests/libntp/tvtots.cpp b/tests/libntp/tvtots.cpp
new file mode 100644 (file)
index 0000000..27d8bb2
--- /dev/null
@@ -0,0 +1,47 @@
+#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));
+}