1 /*        $NetBSD: tstotv.c,v 1.2 2020/05/25 20:47:36 christos Exp $  */
2 
3 #include "config.h"
4 
5 #include "ntp_fp.h"
6 #include "timevalops.h"
7 
8 #include "unity.h"
9 
10 void test_Seconds(void);
11 void test_MicrosecondsExact(void);
12 void test_MicrosecondsRounding(void);
13 
14 
15 void
test_Seconds(void)16 test_Seconds(void) {
17           const l_fp input = {{50}, 0}; /* 50.0 s */
18           const struct timeval expected = {50, 0};
19           struct timeval actual;
20 
21           TSTOTV(&input, &actual);
22 
23           TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
24           TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);
25 }
26 
27 void
test_MicrosecondsExact(void)28 test_MicrosecondsExact(void) {
29           const u_long HALF = 2147483648UL;
30           const l_fp input = {{50}, HALF}; /* 50.5 s */
31           const struct timeval expected = {50, 500000};
32           struct timeval actual;
33 
34           TSTOTV(&input, &actual);
35 
36           TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
37           TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);
38 
39 }
40 
41 void
test_MicrosecondsRounding(void)42 test_MicrosecondsRounding(void) {
43           const l_fp input = {{50}, 3865471UL}; /* Should round to 50.0009 */
44           const struct timeval expected = {50, 900};
45           struct timeval actual;
46 
47           TSTOTV(&input, &actual);
48 
49           TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
50           TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);
51 }
52