]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_timestructs.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_timestructs.cpp
1 /*
2  * timestructs.cpp -- test bed adaptors for time structs.
3  *
4  * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5  * The contents of 'html/copyright.html' apply.
6  */
7 #include "g_libntptest.h"
8 #include "g_timestructs.h"
9
10 extern "C" {
11 #include "timetoa.h"
12 #include "timevalops.h"
13 #include "timespecops.h"
14 }
15
16 namespace timeStruct {
17
18 std::ostream&
19 operator << (std::ostream& os, const timeStruct::l_fp_wrap& val)
20 {
21         // raw data formatting
22         os << "0x" << std::hex << val.V.l_ui << ':'
23            << std::setfill('0') << std::setw(8) << val.V.l_uf
24            << std::dec;
25         // human-readable format
26         os << '[' << lfptoa(&val.V, 10) << ']';
27         return os;
28 }
29
30 std::ostream&
31 operator << (std::ostream& os, const timeStruct::timeval_wrap& val)
32 {
33         // raw data formatting
34         os << val.V.tv_sec << ':' << val.V.tv_usec;
35         // human-readable format
36         os << '['
37            << format_time_fraction(val.V.tv_sec, val.V.tv_usec, 6)
38            << ']';
39         return os;
40 }
41
42 std::ostream&
43 operator << (std::ostream& os, const timeStruct::timespec_wrap& val)
44 {
45         // raw data formatting
46         os << val.V.tv_sec << ':' << val.V.tv_nsec;
47         // human-readable format
48         os << '['
49            << format_time_fraction(val.V.tv_sec, val.V.tv_nsec, 9)
50            << ']';
51         return os;
52 }
53
54 // Implementation of the l_fp closeness predicate
55
56 AssertFpClose::AssertFpClose(
57         u_int32 hi,
58         u_int32 lo
59         )
60 {
61         limit.l_ui = hi;
62         limit.l_uf = lo;
63 }
64
65 ::testing::AssertionResult
66 AssertFpClose::operator()(
67         const char* m_expr,
68         const char* n_expr,
69         const l_fp & m,
70         const l_fp & n
71         )
72 {
73         l_fp diff;
74
75         if (L_ISGEQ(&m, &n)) {
76                 diff = m;
77                 L_SUB(&diff, &n);
78         } else {
79                 diff = n;
80                 L_SUB(&diff, &m);
81         }
82         if (L_ISGEQ(&limit, &diff))
83                 return ::testing::AssertionSuccess();
84
85         return ::testing::AssertionFailure()
86             << m_expr << " which is " << l_fp_wrap(m)
87             << "\nand\n"
88             << n_expr << " which is " << l_fp_wrap(n)
89             << "\nare not close; diff=" << l_fp_wrap(diff);
90 }
91
92 // Implementation of the timeval closeness predicate
93
94 AssertTimevalClose::AssertTimevalClose(
95         time_t hi,
96         int32  lo
97         )
98 {
99         limit.tv_sec = hi;
100         limit.tv_usec = lo;
101 }
102
103 ::testing::AssertionResult
104 AssertTimevalClose::operator()(
105         const char* m_expr,
106         const char* n_expr,
107         const struct timeval & m,
108         const struct timeval & n
109         )
110 {
111         struct timeval diff;
112
113         diff = abs_tval(sub_tval(m, n));
114         if (cmp_tval(limit, diff) >= 0)
115                 return ::testing::AssertionSuccess();
116
117         return ::testing::AssertionFailure()
118             << m_expr << " which is " << timeval_wrap(m)
119             << "\nand\n"
120             << n_expr << " which is " << timeval_wrap(n)
121             << "\nare not close; diff=" << timeval_wrap(diff);
122 }
123
124 // Implementation of the timespec closeness predicate
125
126 AssertTimespecClose::AssertTimespecClose(
127         time_t hi,
128         int32  lo
129         )
130 {
131         limit.tv_sec = hi;
132         limit.tv_nsec = lo;
133 }
134
135 ::testing::AssertionResult
136 AssertTimespecClose::operator()(
137         const char* m_expr,
138         const char* n_expr,
139         const struct timespec & m,
140         const struct timespec & n
141         )
142 {
143         struct timespec diff;
144
145         diff = abs_tspec(sub_tspec(m, n));
146         if (cmp_tspec(limit, diff) >= 0)
147                 return ::testing::AssertionSuccess();
148
149         return ::testing::AssertionFailure()
150             << m_expr << " which is " << timespec_wrap(m)
151             << "\nand\n"
152             << n_expr << " which is " << timespec_wrap(n)
153             << "\nare not close; diff=" << timespec_wrap(diff);
154 }
155
156 } // namespace timeStruct