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