From: Linux Karlsson Date: Sat, 19 Jun 2010 08:57:33 +0000 (+0200) Subject: Added tests for more prettydate, refnumtoa, uflydate, uinttoa and ymd2yd. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=60eef163c92e7531d2cd065d39d097633f8287c0;p=thirdparty%2Fntp.git Added tests for more prettydate, refnumtoa, uflydate, uinttoa and ymd2yd. bk: 4c1c867dAiRLRig8EvA6yzY-Lpcf2w --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index 2113852b58..0f7d58b1b9 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -19,11 +19,16 @@ tests_SOURCES = ../main.cpp \ numtoa.cpp \ numtohost.cpp \ octtoint.cpp \ + prettydate.cpp \ + refnumtoa.cpp \ sfptostr.cpp \ socktoa.cpp \ ssl_init.cpp \ strtolfp.cpp \ - uinttoa.cpp + uglydate.cpp \ + uinttoa.cpp \ + ymd2yd.cpp + INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/lib/isc/include \ -I$(top_srcdir)/lib/isc/nothreads/include \ diff --git a/tests/libntp/prettydate.cpp b/tests/libntp/prettydate.cpp new file mode 100644 index 0000000000..bf2ef2ae8e --- /dev/null +++ b/tests/libntp/prettydate.cpp @@ -0,0 +1,20 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp_fp.h" +}; + +class prettydateTest : public libntptest { +protected: + static const int HALF = -2147483648L; +}; + +TEST_F(prettydateTest, ConstantDate) { + l_fp time = {3485080800UL, HALF}; // 2010-06-09 14:00:00.5 + + ASSERT_STREQ("cfba1ce0.80000000 Wed, Jun 9 2010 14:00:00.500", gmprettydate(&time)); +} + +TEST_F(prettydateTest, ) { + +} diff --git a/tests/libntp/refnumtoa.cpp b/tests/libntp/refnumtoa.cpp new file mode 100644 index 0000000000..20260f5722 --- /dev/null +++ b/tests/libntp/refnumtoa.cpp @@ -0,0 +1,48 @@ +#include "libntptest.h" + +#include "ntp_net.h" +#include "ntp_refclock.h" + +#include + +class refnumtoaTest : public libntptest { +protected: + /* Might need to be updated if a new refclock gets this id. */ + static const int UNUSED_REFCLOCK_ID = 250; +}; + +TEST_F(refnumtoaTest, LocalClock) { + /* We test with a refclock address of type LOCALCLOCK. + * with id 8 + */ + u_int32 addr = REFCLOCK_ADDR; + addr |= REFCLK_LOCALCLOCK << 8; + addr |= 0x8; + + sockaddr_u address; + address.sa4.sin_family = AF_INET; + address.sa4.sin_addr.s_addr = htonl(addr); + + std::ostringstream expected; + expected << clockname(REFCLK_LOCALCLOCK) + << "(8)"; + + EXPECT_STREQ(expected.str().c_str(), refnumtoa(&address)); +} + +TEST_F(refnumtoaTest, UnknownId) { + /* We test with a currently unused refclock ID */ + u_int32 addr = REFCLOCK_ADDR; + addr |= UNUSED_REFCLOCK_ID << 8; + addr |= 0x4; + + sockaddr_u address; + address.sa4.sin_family = AF_INET; + address.sa4.sin_addr.s_addr = htonl(addr); + + std::ostringstream expected; + expected << "REFCLK(" << UNUSED_REFCLOCK_ID + << ",4)"; + + EXPECT_STREQ(expected.str().c_str(), refnumtoa(&address)); +} diff --git a/tests/libntp/uglydate.cpp b/tests/libntp/uglydate.cpp new file mode 100644 index 0000000000..d400619b95 --- /dev/null +++ b/tests/libntp/uglydate.cpp @@ -0,0 +1,18 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp_fp.h" +}; + +class uglydateTest : public libntptest { +protected: + static const int HALF = -2147483648L; +}; + +TEST_F(uglydateTest, ConstantDateTime) { + l_fp time = {3485080800UL, HALF}; // 2010-06-09 14:00:00.5 + + EXPECT_STREQ("3485080800.500000 10:159:14:00:00.500", + uglydate(&time)); +} + diff --git a/tests/libntp/ymd2yd.cpp b/tests/libntp/ymd2yd.cpp new file mode 100644 index 0000000000..e6f46f8c08 --- /dev/null +++ b/tests/libntp/ymd2yd.cpp @@ -0,0 +1,23 @@ +#include "libntptest.h" + +class ymd2ydTest : public libntptest { +}; + +TEST_F(ymd2ydTest, NonLeapYearFebruary) { + EXPECT_EQ(31+20, ymd2yd(2010,2,20)); //2010-02-20 +} + +TEST_F(ymd2ydTest, NonLeapYearJune) { + int expected = 31+28+31+30+31+18; // 18 June non-leap year + EXPECT_EQ(expected, ymd2yd(2011,6,18)); +} + +TEST_F(ymd2ydTest, LeapYearFebruary) { + EXPECT_EQ(31+20, ymd2yd(2012,2,20)); //2012-02-20 (leap year) +} + +TEST_F(ymd2ydTest, LeapYearDecember) { + // 2012-12-31 + int expected = 31+29+31+30+31+30+31+31+30+31+30+31; + EXPECT_EQ(expected, ymd2yd(2012,12,31)); +}