]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Added tests for more prettydate, refnumtoa, uflydate, uinttoa and ymd2yd.
authorLinux Karlsson <karlsson@ntp.org>
Sat, 19 Jun 2010 08:57:33 +0000 (10:57 +0200)
committerLinux Karlsson <karlsson@ntp.org>
Sat, 19 Jun 2010 08:57:33 +0000 (10:57 +0200)
bk: 4c1c867dAiRLRig8EvA6yzY-Lpcf2w

tests/libntp/Makefile.am
tests/libntp/prettydate.cpp [new file with mode: 0644]
tests/libntp/refnumtoa.cpp [new file with mode: 0644]
tests/libntp/uglydate.cpp [new file with mode: 0644]
tests/libntp/ymd2yd.cpp [new file with mode: 0644]

index 2113852b58d5fcb52ca5fd71b89ad672df965f2b..0f7d58b1b99cfd5424e0f0557964190e8c3f5331 100644 (file)
@@ -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 (file)
index 0000000..bf2ef2a
--- /dev/null
@@ -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 (file)
index 0000000..20260f5
--- /dev/null
@@ -0,0 +1,48 @@
+#include "libntptest.h"
+
+#include "ntp_net.h"
+#include "ntp_refclock.h"
+
+#include <sstream>
+
+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 (file)
index 0000000..d400619
--- /dev/null
@@ -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 (file)
index 0000000..e6f46f8
--- /dev/null
@@ -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));
+}