1 /* $NetBSD: tvtots.c,v 1.2 2020/05/25 20:47:36 christos Exp $ */ 2 3 #include "config.h" 4 5 #include "lfptest.h" 6 #include "timevalops.h" 7 8 #include "unity.h" 9 10 #include <math.h> /* Required on Solaris for ldexp. */ 11 12 void test_Seconds(void); 13 void test_MicrosecondsRounded(void); 14 void test_MicrosecondsExact(void); 15 16 void 17 test_Seconds(void) 18 { 19 struct timeval input = {500, 0}; /* 500.0 s */ 20 l_fp expected = {{500}, 0}; 21 l_fp actual; 22 23 TVTOTS(&input, &actual); 24 25 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 26 } 27 28 29 void 30 test_MicrosecondsRounded(void) 31 { 32 /* 0.0005 can not be represented exact in a l_fp structure. 33 * It would equal to 2147483,648. This means that 34 * HALF_PROMILLE_UP (which is 2147484) should be 35 * the correct rounding. */ 36 37 struct timeval input = {0, 500}; /* 0.0005 exact */ 38 l_fp expected = {{0}, HALF_PROMILLE_UP}; 39 l_fp actual; 40 41 TVTOTS(&input, &actual); 42 43 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 44 } 45 46 47 void 48 test_MicrosecondsExact(void) 49 { 50 /* 0.5 can be represented exact in both l_fp and timeval. */ 51 const struct timeval input = {10, 500000}; /* 0.5 exact */ 52 const l_fp expected = {{10}, HALF}; /* 0.5 exact */ 53 l_fp actual; 54 55 TVTOTS(&input, &actual); 56 57 /* Compare the fractional part with an absolute error given. */ 58 TEST_ASSERT_EQUAL_UINT(expected.l_ui, actual.l_ui); 59 60 double expectedDouble, actualDouble; 61 M_LFPTOD(0, expected.l_uf, expectedDouble); 62 M_LFPTOD(0, actual.l_uf, actualDouble); 63 64 /* The error should be less than 0.5 us */ 65 TEST_ASSERT_DOUBLE_WITHIN(0.0000005, expectedDouble, actualDouble); 66 } 67