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