summaryrefslogtreecommitdiff
path: root/tests/libntp/tvtots.c
diff options
context:
space:
mode:
authorCy Schubert <cy@FreeBSD.org>2015-07-01 03:12:13 +0000
committerCy Schubert <cy@FreeBSD.org>2015-07-01 03:12:13 +0000
commit873997f35a991eee09ed91148a0cf332360380da (patch)
tree5b1ffa3ad0e56e0e9f2991011729791ee86d7632 /tests/libntp/tvtots.c
parent4ba32eb5a8bf3455c09d1513ed2af8d2c861a6ba (diff)
Notes
Diffstat (limited to 'tests/libntp/tvtots.c')
-rw-r--r--tests/libntp/tvtots.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/libntp/tvtots.c b/tests/libntp/tvtots.c
new file mode 100644
index 0000000000000..6c8345aaaa1b4
--- /dev/null
+++ b/tests/libntp/tvtots.c
@@ -0,0 +1,54 @@
+#include "config.h"
+
+#include "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);
+}