buftvtots.c revision 293893
1#include "config.h"
2#include "ntp_types.h"
3#include "ntp_stdlib.h"
4
5#include "lfptest.h"
6
7#include "ntp_unixtime.h"
8
9#include "unity.h"
10
11/* Required for Solaris. */
12#include <math.h>
13
14void test_ZeroBuffer(void);
15void test_IntegerAndFractionalBuffer(void);
16void test_IllegalMicroseconds(void);
17void test_AlwaysFalseOnWindows(void);
18
19
20void
21test_ZeroBuffer(void)
22{
23#ifndef SYS_WINNT
24	const struct timeval input = {0, 0};
25	const l_fp expected = {{0 + JAN_1970}, 0};
26
27	l_fp actual;
28
29	TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
30	TEST_ASSERT_TRUE(IsEqual(expected, actual));
31#else
32	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
33#endif
34
35	return;
36}
37
38
39void
40test_IntegerAndFractionalBuffer(void)
41{
42#ifndef SYS_WINNT
43	const struct timeval input = {5, 500000}; /* 5.5 */
44	const l_fp expected = {{5 + JAN_1970}, HALF};
45	double expectedDouble, actualDouble;
46	l_fp actual;
47
48	TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
49
50	/* Compare the fractional part with an absolute error given. */
51	TEST_ASSERT_EQUAL(expected.l_ui, actual.l_ui);
52
53	M_LFPTOD(0, expected.l_uf, expectedDouble);
54	M_LFPTOD(0, actual.l_uf, actualDouble);
55
56	/* The error should be less than 0.5 us */
57	TEST_ASSERT_DOUBLE_WITHIN(0.0000005, expectedDouble, actualDouble);
58#else
59	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
60#endif
61
62	return;
63}
64
65void
66test_IllegalMicroseconds(void)
67{
68#ifndef SYS_WINNT
69	const struct timeval input = {0, 1100000}; /* > 999 999 microseconds. */
70
71	l_fp actual;
72
73	TEST_ASSERT_FALSE(buftvtots((const char*)(&input), &actual));
74#else
75	TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
76#endif
77
78	return;
79}
80
81
82void
83test_AlwaysFalseOnWindows(void)
84{
85#ifdef SYS_WINNT
86	/*
87	 * Under Windows, buftvtots will just return
88	 * 0 (false).
89	 */
90	l_fp actual;
91	TEST_ASSERT_FALSE(buftvtots("", &actual));
92#else
93	TEST_IGNORE_MESSAGE("Non-Windows test, skipping...");
94#endif
95
96	return;
97}
98