summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/tests/libntp/tstotv.c
blob: dac7e53b5e47c27ec21cc3fba36569424e1001fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "config.h"

#include "ntp_fp.h"
#include "timevalops.h"

#include "unity.h"

void test_Seconds(void);
void test_MicrosecondsExact(void);
void test_MicrosecondsRounding(void);


void
test_Seconds(void) {
	const l_fp input = {{50}, 0}; /* 50.0 s */
	const struct timeval expected = {50, 0};
	struct timeval actual;

	TSTOTV(&input, &actual);

	TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
	TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);
}

void
test_MicrosecondsExact(void) {
	const u_long HALF = 2147483648UL;
	const l_fp input = {{50}, HALF}; /* 50.5 s */
	const struct timeval expected = {50, 500000};
	struct timeval actual;

	TSTOTV(&input, &actual);

	TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
	TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);

}

void
test_MicrosecondsRounding(void) {
	const l_fp input = {{50}, 3865471UL}; /* Should round to 50.0009 */
	const struct timeval expected = {50, 900};
	struct timeval actual;

	TSTOTV(&input, &actual);

	TEST_ASSERT_EQUAL(expected.tv_sec, actual.tv_sec);
	TEST_ASSERT_EQUAL(expected.tv_usec, actual.tv_usec);
}
OpenPOWER on IntegriCloud