tstotv.c revision 1.1.1.2 1 /* $NetBSD: tstotv.c,v 1.1.1.2 2015/07/10 13:11:14 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
11 test_Seconds(void) {
12 const l_fp input = {50, 0}; // 50.0 s
13 const struct timeval expected = {50, 0};
14 struct timeval actual;
15
16 TSTOTV(&input, &actual);
17
18 TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
19 TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);
20 }
21
22 void
23 test_MicrosecondsExact(void) {
24 const u_long HALF = 2147483648UL;
25 const l_fp input = {50, HALF}; // 50.5 s
26 const struct timeval expected = {50, 500000};
27 struct timeval actual;
28
29 TSTOTV(&input, &actual);
30
31 TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
32 TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);
33
34 }
35
36 void
37 test_MicrosecondsRounding(void) {
38 const l_fp input = {50, 3865471UL}; // Should round to 50.0009
39 const struct timeval expected = {50, 900};
40 struct timeval actual;
41
42 TSTOTV(&input, &actual);
43
44 TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
45 TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);
46 }
47