summaryrefslogtreecommitdiff
path: root/tests/libntp/timestructs.cpp
blob: 3cd0c543fb004c272679235b197d11cd4b587d18 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * timestructs.cpp -- test bed adaptors for time structs.
 *
 * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
 * The contents of 'html/copyright.html' apply.
 */
#include "libntptest.h"
#include "timestructs.h"

extern "C" {
#include "timetoa.h"
#include "timevalops.h"
#include "timespecops.h"
}

namespace timeStruct {

std::ostream&
operator << (std::ostream& os, const timeStruct::l_fp_wrap& val)
{
	// raw data formatting
	os << "0x" << std::hex << val.V.l_ui << ':'
	   << std::setfill('0') << std::setw(8) << val.V.l_uf
	   << std::dec;
	// human-readable format
	os << '[' << lfptoa(&val.V, 10) << ']';
	return os;
}

std::ostream&
operator << (std::ostream& os, const timeStruct::timeval_wrap& val)
{
	// raw data formatting
	os << val.V.tv_sec << ':' << val.V.tv_usec;
	// human-readable format
	os << '['
	   << format_time_fraction(val.V.tv_sec, val.V.tv_usec, 6)
	   << ']';
	return os;
}

std::ostream&
operator << (std::ostream& os, const timeStruct::timespec_wrap& val)
{
	// raw data formatting
	os << val.V.tv_sec << ':' << val.V.tv_nsec;
	// human-readable format
	os << '['
	   << format_time_fraction(val.V.tv_sec, val.V.tv_nsec, 9)
	   << ']';
	return os;
}

// Implementation of the l_fp closeness predicate

AssertFpClose::AssertFpClose(
	u_int32 hi,
	u_int32 lo
	)
{
	limit.l_ui = hi;
	limit.l_uf = lo;
}

::testing::AssertionResult
AssertFpClose::operator()(
	const char* m_expr,
	const char* n_expr,
	const l_fp & m,
	const l_fp & n
	)
{
	l_fp diff;

	if (L_ISGEQ(&m, &n)) {
		diff = m;
		L_SUB(&diff, &n);
	} else {
		diff = n;
		L_SUB(&diff, &m);
	}
	if (L_ISGEQ(&limit, &diff))
		return ::testing::AssertionSuccess();

	return ::testing::AssertionFailure()
	    << m_expr << " which is " << l_fp_wrap(m)
	    << "\nand\n"
	    << n_expr << " which is " << l_fp_wrap(n)
	    << "\nare not close; diff=" << l_fp_wrap(diff);
}

// Implementation of the timeval closeness predicate

AssertTimevalClose::AssertTimevalClose(
	time_t hi,
	int32  lo
	)
{
	limit.tv_sec = hi;
	limit.tv_usec = lo;
}

::testing::AssertionResult
AssertTimevalClose::operator()(
	const char* m_expr,
	const char* n_expr,
	const struct timeval & m,
	const struct timeval & n
	)
{
	struct timeval diff;

	diff = abs_tval(sub_tval(m, n));
	if (cmp_tval(limit, diff) >= 0)
		return ::testing::AssertionSuccess();

	return ::testing::AssertionFailure()
	    << m_expr << " which is " << timeval_wrap(m)
	    << "\nand\n"
	    << n_expr << " which is " << timeval_wrap(n)
	    << "\nare not close; diff=" << timeval_wrap(diff);
}

// Implementation of the timespec closeness predicate

AssertTimespecClose::AssertTimespecClose(
	time_t hi,
	int32  lo
	)
{
	limit.tv_sec = hi;
	limit.tv_nsec = lo;
}

::testing::AssertionResult
AssertTimespecClose::operator()(
	const char* m_expr,
	const char* n_expr,
	const struct timespec & m,
	const struct timespec & n
	)
{
	struct timespec diff;

	diff = abs_tspec(sub_tspec(m, n));
	if (cmp_tspec(limit, diff) >= 0)
		return ::testing::AssertionSuccess();

	return ::testing::AssertionFailure()
	    << m_expr << " which is " << timespec_wrap(m)
	    << "\nand\n"
	    << n_expr << " which is " << timespec_wrap(n)
	    << "\nare not close; diff=" << timespec_wrap(diff);
}

} // namespace timeStruct