]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_timestructs.h
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_timestructs.h
1 /*
2  * timestructs.h -- 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  * 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).
10  *
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.
15  */
16 #ifndef TIMESTRUCTS_H
17 #define TIMESTRUCTS_H
18
19 extern "C" {
20 #include "ntp_fp.h"
21 }
22
23 namespace timeStruct {
24
25 // wrap a l_fp struct with common operations
26 class l_fp_wrap {
27   public:
28         l_fp V;
29         
30         l_fp_wrap()
31                 { ZERO(V); }
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)
35                 { V = rhs; }
36         bool operator == (const l_fp_wrap& rhs) const
37                 { return L_ISEQU(&V, &rhs.V); }
38         operator l_fp* () 
39                 { return &V; }
40         operator l_fp& ()
41                 { return 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; }
46         };
47         
48 // wrap a 'struct timeval' with common operations
49 class timeval_wrap {
50 public:
51         struct timeval V;
52
53         timeval_wrap()
54                 { ZERO(V); }
55         timeval_wrap(time_t hi, long lo)
56                 { V.tv_sec = hi; V.tv_usec = lo; }
57         timeval_wrap(const struct timeval & rhs)
58                 { V = rhs; }
59         timeval_wrap(const timeval_wrap & rhs)
60                 { V = rhs.V; }
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 ; }
64         bool valid() const
65                 { return V.tv_usec >= 0 && V.tv_usec < 1000000; }
66         operator struct timeval* () 
67                 { return &V; }
68         operator struct timeval& ()
69                 { return V; }
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; }
74 };
75
76 // wrap a 'struct timespec' with common operations
77 class timespec_wrap {
78 public:
79         struct timespec V;
80
81         timespec_wrap()
82                 { ZERO(V); }
83         timespec_wrap(time_t hi, long lo)
84                 { V.tv_sec = hi; V.tv_nsec = lo; }
85         timespec_wrap(const struct timespec & rhs)
86                 { V = rhs; }
87         timespec_wrap(const timespec_wrap & rhs)
88                 { V = rhs.V; }
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 ; }
92         bool valid() const
93                 { return V.tv_nsec >= 0 && V.tv_nsec < 1000000000; }
94         operator struct timespec* () 
95                 { return &V; }
96         operator struct timespec& ()
97                 { return V;     }
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; }
102 };
103
104 // l_fp closeness testing predicate
105 //
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 {
111 private:
112         l_fp limit;
113
114 public:
115         AssertFpClose(u_int32 hi, u_int32 lo);
116
117         ::testing::AssertionResult
118         operator()(const char* m_expr, const char* n_expr,
119                    const l_fp & m, const l_fp & n);
120 };
121
122
123 // timeval closeness testing predicate
124 //
125 // CAVEAT: This class uses the timevalops functions
126 // - sub_tval
127 // - abs_tval
128 // - cmp_tval
129 //
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
133 // predicate.
134 class AssertTimevalClose {
135 private:
136         struct timeval limit;
137
138 public:
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);
142
143         ::testing::AssertionResult
144         operator()(const char* m_expr, const char* n_expr,
145                    const struct timeval & m, const struct timeval & n);
146 };
147
148
149 // timespec closeness testing predicate
150 //
151 // CAVEAT: This class uses the timespecops functions
152 // - sub_tspec
153 // - abs_tspec
154 // - cmp_tspec
155 //
156 // See the equivalent timeval helper.
157 class AssertTimespecClose {
158 private:
159         struct timespec limit;
160
161 public:
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);
165
166         ::testing::AssertionResult
167         operator()(const char* m_expr, const char* n_expr,
168                    const struct timespec & m, const struct timespec & n);
169 };
170
171
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.
175
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);
182
183 } // namespace timeStruct
184
185 #endif // TIMESTRUCTS_H