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