buftvtots.c revision 289999
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
21289999Sglebiustest_ZeroBuffer(void) {
22284990Scy#ifndef SYS_WINNT
23284990Scy	const struct timeval input = {0, 0};
24289999Sglebius	const l_fp expected = {{0 + JAN_1970}, 0};
25284990Scy
26284990Scy	l_fp actual;
27284990Scy
28284990Scy	TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
29284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
30284990Scy#else
31284990Scy	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
32284990Scy#endif
33284990Scy}
34284990Scy
35289999Sglebiusvoid
36289999Sglebiustest_IntegerAndFractionalBuffer(void) {
37284990Scy#ifndef SYS_WINNT
38289999Sglebius	const struct timeval input = {5, 500000}; /* 5.5 */
39289999Sglebius	const l_fp expected = {{5 + JAN_1970}, HALF};
40289999Sglebius	double expectedDouble, actualDouble;
41284990Scy	l_fp actual;
42284990Scy
43284990Scy	TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
44284990Scy
45289999Sglebius	/* Compare the fractional part with an absolute error given. */
46284990Scy	TEST_ASSERT_EQUAL(expected.l_ui, actual.l_ui);
47284990Scy
48284990Scy	M_LFPTOD(0, expected.l_uf, expectedDouble);
49284990Scy	M_LFPTOD(0, actual.l_uf, actualDouble);
50284990Scy
51289999Sglebius	/* The error should be less than 0.5 us */
52289999Sglebius	TEST_ASSERT_DOUBLE_WITHIN(0.0000005, expectedDouble, actualDouble);
53284990Scy#else
54284990Scy	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
55284990Scy#endif
56284990Scy}
57284990Scy
58289999Sglebiusvoid
59289999Sglebiustest_IllegalMicroseconds(void) {
60284990Scy#ifndef SYS_WINNT
61289999Sglebius	const struct timeval input = {0, 1100000}; /* > 999 999 microseconds. */
62284990Scy
63284990Scy	l_fp actual;
64284990Scy
65284990Scy	TEST_ASSERT_FALSE(buftvtots((const char*)(&input), &actual));
66284990Scy#else
67284990Scy	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
68284990Scy#endif
69284990Scy}
70284990Scy
71284990Scy
72289999Sglebiusvoid
73289999Sglebiustest_AlwaysFalseOnWindows(void) {
74284990Scy#ifdef SYS_WINNT
75284990Scy	/*
76284990Scy	 * Under Windows, buftvtots will just return
77284990Scy	 * 0 (false).
78284990Scy	 */
79284990Scy	l_fp actual;
80284990Scy	TEST_ASSERT_FALSE(buftvtots("", &actual));
81284990Scy#else
82284990Scy	TEST_IGNORE_MESSAGE("Non-Windows test, skipping...");
83284990Scy#endif
84284990Scy}
85284990Scy
86