From: Linux Karlsson Date: Wed, 23 Jun 2010 12:21:42 +0000 (+0200) Subject: Added tests for statestr, tstotv and tvtots. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=50633c33b030a83cf56ecbed6c8aacf43553c44a;p=thirdparty%2Fntp.git Added tests for statestr, tstotv and tvtots. bk: 4c21fc56jY6sEviFKVgNqN9YZdsuPA --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index 560bef55d3..6b4e2fd2f9 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -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 diff --git a/tests/libntp/lfptest.h b/tests/libntp/lfptest.h index 56c7c85ade..95ae13fbfa 100644 --- a/tests/libntp/lfptest.h +++ b/tests/libntp/lfptest.h @@ -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 index 0000000000..506b079aa4 --- /dev/null +++ b/tests/libntp/statestr.cpp @@ -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 index 0000000000..04b3b73c56 --- /dev/null +++ b/tests/libntp/tstotv.cpp @@ -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 index 0000000000..27d8bb220f --- /dev/null +++ b/tests/libntp/tvtots.cpp @@ -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)); +}