2 * timestructs.h -- test bed adaptors for time structs.
4 * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5 * The contents of 'html/copyright.html' apply.
7 * Some wrapper classes and a closeness predicate that are used to
8 * bridge the gap between the goggletest framework and the structs used
9 * for representing time stamps (l_fp, struct timeval, struct timespec).
11 * Some ostream conversion operators are provided to give diagnostic
12 * output on errors. The normal string conversion functions will give
13 * HRVs (human readable values) but we might also be interested in the
14 * machine representation for diagnostic purposes.
23 namespace timeStruct {
25 // wrap a l_fp struct with common operations
32 l_fp_wrap(u_int32 hi, u_int32 lo)
33 { V.l_ui = hi; V.l_uf = lo; }
34 l_fp_wrap(const l_fp &rhs)
36 bool operator == (const l_fp_wrap& rhs) const
37 { return L_ISEQU(&V, &rhs.V); }
42 l_fp_wrap & operator = (const l_fp_wrap& rhs)
43 { V = rhs.V; return *this; }
44 l_fp_wrap& operator = (const l_fp& rhs)
45 { V = rhs; return *this; }
48 // wrap a 'struct timeval' with common operations
55 timeval_wrap(time_t hi, long lo)
56 { V.tv_sec = hi; V.tv_usec = lo; }
57 timeval_wrap(const struct timeval & rhs)
59 timeval_wrap(const timeval_wrap & rhs)
61 bool operator == (const timeval_wrap& rhs) const
62 { return V.tv_sec == rhs.V.tv_sec &&
63 V.tv_usec == rhs.V.tv_usec ; }
65 { return V.tv_usec >= 0 && V.tv_usec < 1000000; }
66 operator struct timeval* ()
68 operator struct timeval& ()
70 timeval_wrap& operator = (const timeval_wrap& rhs)
71 { V = rhs.V; return *this; }
72 timeval_wrap& operator = (const struct timeval& rhs)
73 { V = rhs; return *this; }
76 // wrap a 'struct timespec' with common operations
83 timespec_wrap(time_t hi, long lo)
84 { V.tv_sec = hi; V.tv_nsec = lo; }
85 timespec_wrap(const struct timespec & rhs)
87 timespec_wrap(const timespec_wrap & rhs)
89 bool operator == (const timespec_wrap& rhs) const
90 { return V.tv_sec == rhs.V.tv_sec &&
91 V.tv_nsec == rhs.V.tv_nsec ; }
93 { return V.tv_nsec >= 0 && V.tv_nsec < 1000000000; }
94 operator struct timespec* ()
96 operator struct timespec& ()
98 timespec_wrap& operator = (const timespec_wrap& rhs)
99 { V = rhs.V; return *this; }
100 timespec_wrap& operator = (const struct timespec& rhs)
101 { V = rhs; return *this; }
104 // l_fp closeness testing predicate
106 // This predicate is used for the closeness ('near') testing of l_fp
107 // values. Once constructed with a limit, it can be used to check the
108 // absolute difference of two l_fp structs against that limit; if the
109 // difference is less or equal to this limit, the test passes.
110 class AssertFpClose {
115 AssertFpClose(u_int32 hi, u_int32 lo);
117 ::testing::AssertionResult
118 operator()(const char* m_expr, const char* n_expr,
119 const l_fp & m, const l_fp & n);
123 // timeval closeness testing predicate
125 // CAVEAT: This class uses the timevalops functions
130 // This creates a dependency loop of sorts. The loop is defused by the
131 // fact that these basic operations can be tested by exact value tests,
132 // so once the basic timeval operations passed it's safe to use this
134 class AssertTimevalClose {
136 struct timeval limit;
139 // note: (hi,lo) should be a positive normalised timeval;
140 // the constructor does not normalise the values!
141 AssertTimevalClose(time_t hi, int32 lo);
143 ::testing::AssertionResult
144 operator()(const char* m_expr, const char* n_expr,
145 const struct timeval & m, const struct timeval & n);
149 // timespec closeness testing predicate
151 // CAVEAT: This class uses the timespecops functions
156 // See the equivalent timeval helper.
157 class AssertTimespecClose {
159 struct timespec limit;
162 // note: (hi,lo) should be a positive normalised timespec;
163 // the constructor does not normalise the values!
164 AssertTimespecClose(time_t hi, int32 lo);
166 ::testing::AssertionResult
167 operator()(const char* m_expr, const char* n_expr,
168 const struct timespec & m, const struct timespec & n);
172 // since googletest wants to string format items, we declare the
173 // necessary operators. Since all adaptors have only public members
174 // there is need for friend declarations anywhere.
176 extern std::ostream& operator << (std::ostream& os,
177 const timeStruct::l_fp_wrap& val);
178 extern std::ostream& operator << (std::ostream& os,
179 const timeStruct::timeval_wrap& val);
180 extern std::ostream& operator << (std::ostream& os,
181 const timeStruct::timespec_wrap& val);
183 } // namespace timeStruct
185 #endif // TIMESTRUCTS_H