xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/buftvtots.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
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 
14 void test_ZeroBuffer(void);
15 void test_IntegerAndFractionalBuffer(void);
16 void test_IllegalMicroseconds(void);
17 void test_AlwaysFalseOnWindows(void);
18 
19 
20 void
21 test_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 
39 void
40 test_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 
65 void
66 test_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 
82 void
83 test_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