1284990Scy#include "config.h"
2284990Scy#include "ntp_types.h"
3284990Scy#include "ntp_stdlib.h"
4284990Scy
5284990Scy#include "lfptest.h"
6284990Scy
7284990Scy#include "ntp_unixtime.h"
8284990Scy
9284990Scy#include "unity.h"
10284990Scy
11289999Sglebius/* Required for Solaris. */
12284990Scy#include <math.h>
13284990Scy
14289999Sglebiusvoid test_ZeroBuffer(void);
15289999Sglebiusvoid test_IntegerAndFractionalBuffer(void);
16289999Sglebiusvoid test_IllegalMicroseconds(void);
17289999Sglebiusvoid test_AlwaysFalseOnWindows(void);
18284990Scy
19284990Scy
20289999Sglebiusvoid
21293893Sglebiustest_ZeroBuffer(void)
22293893Sglebius{
23284990Scy#ifndef SYS_WINNT
24284990Scy	const struct timeval input = {0, 0};
25289999Sglebius	const l_fp expected = {{0 + JAN_1970}, 0};
26284990Scy
27284990Scy	l_fp actual;
28284990Scy
29284990Scy	TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
30284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
31284990Scy#else
32284990Scy	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
33284990Scy#endif
34293893Sglebius
35293893Sglebius	return;
36284990Scy}
37284990Scy
38293893Sglebius
39289999Sglebiusvoid
40293893Sglebiustest_IntegerAndFractionalBuffer(void)
41293893Sglebius{
42284990Scy#ifndef SYS_WINNT
43289999Sglebius	const struct timeval input = {5, 500000}; /* 5.5 */
44289999Sglebius	const l_fp expected = {{5 + JAN_1970}, HALF};
45289999Sglebius	double expectedDouble, actualDouble;
46284990Scy	l_fp actual;
47284990Scy
48284990Scy	TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
49284990Scy
50289999Sglebius	/* Compare the fractional part with an absolute error given. */
51284990Scy	TEST_ASSERT_EQUAL(expected.l_ui, actual.l_ui);
52284990Scy
53284990Scy	M_LFPTOD(0, expected.l_uf, expectedDouble);
54284990Scy	M_LFPTOD(0, actual.l_uf, actualDouble);
55284990Scy
56289999Sglebius	/* The error should be less than 0.5 us */
57289999Sglebius	TEST_ASSERT_DOUBLE_WITHIN(0.0000005, expectedDouble, actualDouble);
58284990Scy#else
59284990Scy	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
60284990Scy#endif
61293893Sglebius
62293893Sglebius	return;
63284990Scy}
64284990Scy
65289999Sglebiusvoid
66293893Sglebiustest_IllegalMicroseconds(void)
67293893Sglebius{
68284990Scy#ifndef SYS_WINNT
69289999Sglebius	const struct timeval input = {0, 1100000}; /* > 999 999 microseconds. */
70293893Sglebius
71284990Scy	l_fp actual;
72284990Scy
73284990Scy	TEST_ASSERT_FALSE(buftvtots((const char*)(&input), &actual));
74284990Scy#else
75284990Scy	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
76284990Scy#endif
77293893Sglebius
78293893Sglebius	return;
79284990Scy}
80284990Scy
81284990Scy
82289999Sglebiusvoid
83293893Sglebiustest_AlwaysFalseOnWindows(void)
84293893Sglebius{
85284990Scy#ifdef SYS_WINNT
86284990Scy	/*
87284990Scy	 * Under Windows, buftvtots will just return
88284990Scy	 * 0 (false).
89284990Scy	 */
90284990Scy	l_fp actual;
91284990Scy	TEST_ASSERT_FALSE(buftvtots("", &actual));
92284990Scy#else
93284990Scy	TEST_IGNORE_MESSAGE("Non-Windows test, skipping...");
94284990Scy#endif
95293893Sglebius
96293893Sglebius	return;
97284990Scy}
98