summaryrefslogtreecommitdiff
path: root/tests/libntp/tstotv.cpp
blob: 41f44d70e00b3038521b415e3b94257c3654ff77 (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
50
51
52
53
54
55
56
57
#include "libntptest.h"

extern "C" {
#include "ntp_fp.h"
#include "timevalops.h"
};

class tstotvTest : public libntptest {
protected:
	::testing::AssertionResult IsEqual(const timeval& expected,
									   const timeval& actual) {
		if (expected.tv_sec == actual.tv_sec &&
			expected.tv_usec == actual.tv_usec) {
			// Success
			return ::testing::AssertionSuccess();
		} else {
			return ::testing::AssertionFailure()
				<< "expected: " << expected.tv_sec << "."
				<< expected.tv_usec
				<< " but was: " << actual.tv_sec << "."
				<< actual.tv_usec;
		}
	}

	static const u_long HALF = 2147483648UL;
};

TEST_F(tstotvTest, Seconds) {
	const l_fp input = {50, 0}; // 50.0 s
	const timeval expected = {50, 0};
	timeval actual;

	TSTOTV(&input, &actual);

	EXPECT_TRUE(IsEqual(expected, actual));
}

TEST_F(tstotvTest, MicrosecondsExact) {
	const l_fp input = {50, HALF}; // 10.5 s
	const timeval expected = {50, 500000};
	timeval actual;

	TSTOTV(&input, &actual);

	EXPECT_TRUE(IsEqual(expected, actual));

}

TEST_F(tstotvTest, MicrosecondsRounding) {
	const l_fp input = {50, 3865471UL}; // Should round to 50.0009
	const timeval expected = {50, 900};
	timeval actual;

	TSTOTV(&input, &actual);

	EXPECT_TRUE(IsEqual(expected, actual));
}