]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/buftvtots.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / buftvtots.c
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 #ifndef SYS_WINNT
23         const struct timeval input = {0, 0};
24         const l_fp expected = {{0 + JAN_1970}, 0};
25
26         l_fp actual;
27
28         TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
29         TEST_ASSERT_TRUE(IsEqual(expected, actual));
30 #else
31         TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
32 #endif
33 }
34
35 void
36 test_IntegerAndFractionalBuffer(void) {
37 #ifndef SYS_WINNT
38         const struct timeval input = {5, 500000}; /* 5.5 */
39         const l_fp expected = {{5 + JAN_1970}, HALF};
40         double expectedDouble, actualDouble;
41         l_fp actual;
42
43         TEST_ASSERT_TRUE(buftvtots((const char*)(&input), &actual));
44
45         /* Compare the fractional part with an absolute error given. */
46         TEST_ASSERT_EQUAL(expected.l_ui, actual.l_ui);
47
48         M_LFPTOD(0, expected.l_uf, expectedDouble);
49         M_LFPTOD(0, actual.l_uf, actualDouble);
50
51         /* The error should be less than 0.5 us */
52         TEST_ASSERT_DOUBLE_WITHIN(0.0000005, expectedDouble, actualDouble);
53 #else
54         TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
55 #endif
56 }
57
58 void
59 test_IllegalMicroseconds(void) {
60 #ifndef SYS_WINNT
61         const struct timeval input = {0, 1100000}; /* > 999 999 microseconds. */
62         
63         l_fp actual;
64
65         TEST_ASSERT_FALSE(buftvtots((const char*)(&input), &actual));
66 #else
67         TEST_IGNORE_MESSAGE("Test only for Windows, skipping...");
68 #endif
69 }
70
71
72 void
73 test_AlwaysFalseOnWindows(void) {
74 #ifdef SYS_WINNT
75         /*
76          * Under Windows, buftvtots will just return
77          * 0 (false).
78          */
79         l_fp actual;
80         TEST_ASSERT_FALSE(buftvtots("", &actual));
81 #else
82         TEST_IGNORE_MESSAGE("Non-Windows test, skipping...");
83 #endif
84 }
85