]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
converted tvtots from gtest to unity
authorTomek Mrugalski <tomasz@isc.org>
Fri, 19 Jun 2015 20:31:16 +0000 (22:31 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Fri, 19 Jun 2015 20:31:16 +0000 (22:31 +0200)
bk: 55847c14kjOWpLCTHD8X1xEsBM2LRQ

tests/libntp/Makefile.am
tests/libntp/g_tvtots.cpp [moved from tests/libntp/tvtots.cpp with 100% similarity]
tests/libntp/run-tvtots.c [new file with mode: 0644]
tests/libntp/tvtots.c [new file with mode: 0644]

index ac509eb1783d709e69897fdf3cd7345e9b3845ef..4fcbfab124e7f5b1a95d94905461dd545538fe4e 100644 (file)
@@ -29,6 +29,7 @@ check_PROGRAMS =              \
        test-statestr           \
        test-timespecops        \
        test-timevalops         \
+       test-tvtots                     \
        test-uglydate           \
        test-vi64ops            \
        test-ymd2yd             \
@@ -106,7 +107,7 @@ tests_SOURCES =                                     \
        timestructs.cpp         \
        g_timevalops.cpp        \
        tstotv.cpp              \
-       tvtots.cpp              \
+       g_tvtots.cpp            \
        g_uglydate.cpp          \
        g_vi64ops.cpp           \
        g_ymd2yd.cpp            \
@@ -135,6 +136,7 @@ BUILT_SOURCES +=                    \
        $(srcdir)/run-statestr.c        \
        $(srcdir)/run-timevalops.c      \
        $(srcdir)/run-timespecops.c     \
+       $(srcdir)/run-tvtots.c          \
        $(srcdir)/run-uglydate.c        \
        $(srcdir)/run-vi64ops.c         \
        $(srcdir)/run-ymd2yd.c          \
@@ -386,6 +388,17 @@ test_ssl_init_LDADD =                              \
        $(top_builddir)/sntp/unity/libunity.a   \
        $(NULL)
 
+test_tvtots_CFLAGS =                           \
+       -I$(top_srcdir)/sntp/unity              \
+       -DUNITY_INCLUDE_DOUBLE                  \
+       $(NULL)
+
+test_tvtots_LDADD =                            \
+       $(LDADD)                                \
+       $(top_builddir)/sntp/unity/libunity.a   \
+       $(NULL)
+
+
 
 test_modetoa_SOURCES =                 \
        modetoa.c                       \
@@ -521,6 +534,10 @@ test_ssl_init_SOURCES =                    \
        run-ssl_init.c                  \
        $(NULL)
 
+test_tvtots_SOURCES =                  \
+       tvtots.c                        \
+       run-tvtots.c                    \
+       $(NULL)
 
 $(srcdir)/run-modetoa.c: $(srcdir)/modetoa.c $(std_unity_list)
        $(run_unity) modetoa.c run-modetoa.c
@@ -600,6 +617,9 @@ $(srcdir)/run-timespecops.c: $(srcdir)/timespecops.c $(std_unity_list)
 $(srcdir)/run-ssl_init.c: $(srcdir)/ssl_init.c $(std_unity_list)
        $(run_unity) ssl_init.c run-ssl_init.c
 
+$(srcdir)/run-tvtots.c: $(srcdir)/tvtots.c $(std_unity_list)
+       $(run_unity) tvtots.c run-tvtots.c
+
 TESTS =
 
 if !NTP_CROSSCOMPILE
diff --git a/tests/libntp/run-tvtots.c b/tests/libntp/run-tvtots.c
new file mode 100644 (file)
index 0000000..0aa987a
--- /dev/null
@@ -0,0 +1,55 @@
+/* AUTOGENERATED FILE. DO NOT EDIT. */
+
+//=======Test Runner Used To Run Each Test Below=====
+#define RUN_TEST(TestFunc, TestLineNum) \
+{ \
+  Unity.CurrentTestName = #TestFunc; \
+  Unity.CurrentTestLineNumber = TestLineNum; \
+  Unity.NumberOfTests++; \
+  if (TEST_PROTECT()) \
+  { \
+      setUp(); \
+      TestFunc(); \
+  } \
+  if (TEST_PROTECT() && !TEST_IS_IGNORED) \
+  { \
+    tearDown(); \
+  } \
+  UnityConcludeTest(); \
+}
+
+//=======Automagically Detected Files To Include=====
+#include "unity.h"
+#include <setjmp.h>
+#include <stdio.h>
+
+//=======External Functions This Runner Calls=====
+extern void setUp(void);
+extern void tearDown(void);
+extern void test_Seconds(void);
+extern void test_MicrosecondsRounded(void);
+extern void test_MicrosecondsExact(void);
+
+
+//=======Test Reset Option=====
+void resetTest()
+{
+  tearDown();
+  setUp();
+}
+
+char *progname;
+
+
+//=======MAIN=====
+int main(int argc, char *argv[])
+{
+  progname = argv[0];
+  Unity.TestFile = "tvtots.c";
+  UnityBegin("tvtots.c");
+  RUN_TEST(test_Seconds, 9);
+  RUN_TEST(test_MicrosecondsRounded, 20);
+  RUN_TEST(test_MicrosecondsExact, 35);
+
+  return (UnityEnd());
+}
diff --git a/tests/libntp/tvtots.c b/tests/libntp/tvtots.c
new file mode 100644 (file)
index 0000000..2f311f5
--- /dev/null
@@ -0,0 +1,53 @@
+#include "config.h"
+#include "c_lfptest.h"
+#include "timevalops.h"
+#include "unity.h"
+#include <math.h>// Required on Solaris for ldexp.
+
+
+
+void test_Seconds(void)
+{
+       struct timeval input = {500, 0}; // 500.0 s
+       l_fp expected = {500, 0};
+       l_fp actual;
+
+       TVTOTS(&input, &actual);
+
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+}
+
+void test_MicrosecondsRounded(void)
+{
+       /* 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. */
+
+       struct timeval input = {0, 500}; // 0.0005 exact
+       l_fp expected = {0, HALF_PROMILLE_UP};
+       l_fp actual;
+
+       TVTOTS(&input, &actual);
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+}
+
+void test_MicrosecondsExact(void)
+{
+       // 0.5 can be represented exact in both l_fp and timeval.
+       const struct timeval input = {10, 500000}; // 0.5 exact
+       const l_fp expected = {10, HALF}; // 0.5 exact
+       l_fp actual;
+
+       TVTOTS(&input, &actual);
+
+       // Compare the fractional part with an absolute error given.
+       TEST_ASSERT_EQUAL_UINT(expected.l_ui, actual.l_ui);
+
+       double expectedDouble, actualDouble;
+       M_LFPTOD(0, expected.l_uf, expectedDouble);
+       M_LFPTOD(0, actual.l_uf, actualDouble);
+
+       // The error should be less than 0.5 us
+       TEST_ASSERT_DOUBLE_WITHIN(0000005, expectedDouble, actualDouble);
+}