From: Tomek Mrugalski Date: Fri, 19 Jun 2015 20:31:16 +0000 (+0200) Subject: converted tvtots from gtest to unity X-Git-Tag: NTP_4_3_43~2^2~21^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4be11f0f1511359e89e709b113a6757597e41131;p=thirdparty%2Fntp.git converted tvtots from gtest to unity bk: 55847c14kjOWpLCTHD8X1xEsBM2LRQ --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index ac509eb17..4fcbfab12 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -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/tvtots.cpp b/tests/libntp/g_tvtots.cpp similarity index 100% rename from tests/libntp/tvtots.cpp rename to tests/libntp/g_tvtots.cpp diff --git a/tests/libntp/run-tvtots.c b/tests/libntp/run-tvtots.c new file mode 100644 index 000000000..0aa987afc --- /dev/null +++ b/tests/libntp/run-tvtots.c @@ -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 +#include + +//=======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 index 000000000..2f311f55b --- /dev/null +++ b/tests/libntp/tvtots.c @@ -0,0 +1,53 @@ +#include "config.h" +#include "c_lfptest.h" +#include "timevalops.h" +#include "unity.h" +#include // 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); +}