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