1 | #ifndef _theplu_yat_test_suite_ |
---|
2 | #define _theplu_yat_test_suite_ |
---|
3 | |
---|
4 | // $Id: Suite.h 1877 2009-03-21 04:40:44Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
---|
8 | Copyright (C) 2009 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 3 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | */ |
---|
25 | |
---|
26 | #include <iosfwd> |
---|
27 | #include <sstream> |
---|
28 | |
---|
29 | namespace theplu { |
---|
30 | namespace yat { |
---|
31 | namespace test { |
---|
32 | |
---|
33 | /** |
---|
34 | \internal utility class for tests |
---|
35 | */ |
---|
36 | class Suite |
---|
37 | { |
---|
38 | public: |
---|
39 | Suite(int argc, char* argv[]); |
---|
40 | |
---|
41 | /** |
---|
42 | */ |
---|
43 | ~Suite(void); |
---|
44 | |
---|
45 | /** |
---|
46 | set ok to 'b && ok' |
---|
47 | |
---|
48 | \return b |
---|
49 | */ |
---|
50 | bool add(bool b); |
---|
51 | |
---|
52 | /** |
---|
53 | \return In verbose mode std::cerr, else a ofstream to "/dev/null". |
---|
54 | */ |
---|
55 | std::ostream& err(void) const; |
---|
56 | |
---|
57 | /** |
---|
58 | \return true if \f$ |a-b| <= N * \epsilon * min(|a|,|b|) \f$ |
---|
59 | where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon() |
---|
60 | */ |
---|
61 | bool equal(double a, double b, unsigned long int N=1); |
---|
62 | |
---|
63 | /** |
---|
64 | \return true if |\a a - \a b| <= \a margin |
---|
65 | */ |
---|
66 | bool equal_fix(double a, double b, double margin=0); |
---|
67 | |
---|
68 | /** |
---|
69 | \return true if \f$ |a-b| <= N * sqrt(\epsilon) * min(|a|,|b|) \f$ |
---|
70 | where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon() |
---|
71 | */ |
---|
72 | bool equal_sqrt(double a, double b, unsigned long int N=1); |
---|
73 | |
---|
74 | /** |
---|
75 | apply equal on ranges [first1, last1) and [first2, ...) |
---|
76 | */ |
---|
77 | template<typename Iterator1, typename Iterator2> |
---|
78 | bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2, |
---|
79 | unsigned int N=1); |
---|
80 | |
---|
81 | /** |
---|
82 | apply equal_fix on ranges [first1, last1) and [first2, ...) |
---|
83 | */ |
---|
84 | template<typename Iterator1, typename Iterator2> |
---|
85 | bool equal_range_fix(Iterator1 first1, Iterator1 last1, Iterator2 first2, |
---|
86 | double margin); |
---|
87 | |
---|
88 | /** |
---|
89 | \return true if test is ok |
---|
90 | */ |
---|
91 | bool ok(void) const; |
---|
92 | |
---|
93 | /** |
---|
94 | \return In verbose mode std::cout, else a ofstream to "/dev/null". |
---|
95 | */ |
---|
96 | std::ostream& out(void) const; |
---|
97 | |
---|
98 | /** |
---|
99 | In verbose mode a final message is sent to std::cout. |
---|
100 | |
---|
101 | If ok() is true: "Test is ok." otherwise |
---|
102 | "Test failed." |
---|
103 | |
---|
104 | \return 0 if ok. |
---|
105 | */ |
---|
106 | int return_value(void) const; |
---|
107 | |
---|
108 | template<typename TrivialIterator> |
---|
109 | void test_trivial_iterator(const TrivialIterator&); |
---|
110 | |
---|
111 | template<typename InputIterator> |
---|
112 | void test_input_iterator(InputIterator&); |
---|
113 | |
---|
114 | template<typename OutputIterator> |
---|
115 | void test_output_iterator(OutputIterator&); |
---|
116 | |
---|
117 | template<typename ForwardIterator> |
---|
118 | void test_forward_iterator(ForwardIterator); |
---|
119 | |
---|
120 | template<typename BidirectionalIterator> |
---|
121 | void test_bidirectional_iterator(BidirectionalIterator); |
---|
122 | |
---|
123 | template<typename RandomAccessIterator> |
---|
124 | void test_random_access_iterator(RandomAccessIterator); |
---|
125 | |
---|
126 | /** |
---|
127 | Function writes to a stream using operator<<, creates a new |
---|
128 | object using stream constructor, and the new object is written |
---|
129 | to another stream, and function check if the two outputs are |
---|
130 | equal. |
---|
131 | */ |
---|
132 | template<class T> |
---|
133 | bool test_stream(const T&) const; |
---|
134 | |
---|
135 | /** |
---|
136 | This function is similar to add(bool) and could be used to |
---|
137 | detect/count known issues. When the issue is fixed, one can |
---|
138 | replace the call to xadd(bool) with a call to add(bool). |
---|
139 | |
---|
140 | If \a b is false a counter is incremented, which is used to in |
---|
141 | return_value() to generate some printout on how many known |
---|
142 | issues were detected. |
---|
143 | |
---|
144 | If \a b is true, ok_ is set to false, becasue the known issue |
---|
145 | is no longer an issue and one should replace the call with a |
---|
146 | call to add(bool). |
---|
147 | */ |
---|
148 | bool xadd(bool b); |
---|
149 | |
---|
150 | private: |
---|
151 | std::ofstream* dev_null_; |
---|
152 | unsigned int known_issues_; |
---|
153 | bool ok_; |
---|
154 | bool verbose_; |
---|
155 | |
---|
156 | }; |
---|
157 | |
---|
158 | /** |
---|
159 | \return absolute path to file |
---|
160 | \param local_path path relative to srcdir |
---|
161 | */ |
---|
162 | std::string filename(const std::string& local_path); |
---|
163 | |
---|
164 | template<typename Iterator1, typename Iterator2> |
---|
165 | bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2, |
---|
166 | unsigned int N) |
---|
167 | { |
---|
168 | while (first1!=last1){ |
---|
169 | if (!this->equal(*first1, *first2, N) ) { |
---|
170 | return false; |
---|
171 | } |
---|
172 | ++first1; |
---|
173 | ++first2; |
---|
174 | } |
---|
175 | return true; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | template<typename Iterator1, typename Iterator2> |
---|
180 | bool Suite::equal_range_fix(Iterator1 first1, Iterator1 last1, |
---|
181 | Iterator2 first2, double margin) |
---|
182 | { |
---|
183 | while (first1!=last1){ |
---|
184 | if (!this->equal_fix(*first1, *first2, margin) ) { |
---|
185 | return false; |
---|
186 | } |
---|
187 | ++first1; |
---|
188 | ++first2; |
---|
189 | } |
---|
190 | return true; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | template<class T> |
---|
195 | bool Suite::test_stream(const T& t) const |
---|
196 | { |
---|
197 | this->err() << "Checking that output stream is valid as an input stream\n"; |
---|
198 | std::stringstream ss; |
---|
199 | this->err() << "writing to output\n"; |
---|
200 | ss << t; |
---|
201 | this->err() << "creating a new object from output\n"; |
---|
202 | T t2(ss); |
---|
203 | std::stringstream ss2; |
---|
204 | this->err() << "writing to output\n"; |
---|
205 | ss2 << t2; |
---|
206 | bool ok = ss2.str()==ss.str(); |
---|
207 | if (!ok) { |
---|
208 | this->err() << "ERROR: first object gave following output:\n" |
---|
209 | << ss.str() << "\n" |
---|
210 | << "ERROR: and second object gave following output:\n" |
---|
211 | << ss2.str() << "\n"; |
---|
212 | } |
---|
213 | return ok; |
---|
214 | } |
---|
215 | |
---|
216 | template<typename TrivialIterator> |
---|
217 | void Suite::test_trivial_iterator(const TrivialIterator& iter) |
---|
218 | { |
---|
219 | err() << " testing Trivial features" << std::endl; |
---|
220 | typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter; |
---|
221 | add(tmp==*iter); |
---|
222 | } |
---|
223 | |
---|
224 | template<typename InputIterator> |
---|
225 | void Suite::test_input_iterator(InputIterator& iter) |
---|
226 | { |
---|
227 | test_trivial_iterator(iter); |
---|
228 | err() << " testing Input features" << std::endl; |
---|
229 | // just to check compilation |
---|
230 | if (false) { |
---|
231 | ++iter; |
---|
232 | iter++; |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | template<typename OutputIterator> |
---|
237 | void Suite::test_output_iterator(OutputIterator& iter) |
---|
238 | { |
---|
239 | test_trivial_iterator(iter); |
---|
240 | err() << " testing Output features" << std::endl; |
---|
241 | } |
---|
242 | |
---|
243 | template<typename ForwardIterator> |
---|
244 | void Suite::test_forward_iterator(ForwardIterator iter) |
---|
245 | { |
---|
246 | test_output_iterator(iter); |
---|
247 | test_input_iterator(iter); |
---|
248 | err() << " testing Forward features" << std::endl; |
---|
249 | |
---|
250 | typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter; |
---|
251 | // testing multiple traversing is possible and does not change the data |
---|
252 | ForwardIterator iter1 = iter; |
---|
253 | ++iter1; |
---|
254 | add(iter!=iter1); |
---|
255 | ForwardIterator iter2 = iter; |
---|
256 | ++iter2; |
---|
257 | add(tmp==*iter); |
---|
258 | } |
---|
259 | |
---|
260 | template<typename BidirectionalIterator> |
---|
261 | void Suite::test_bidirectional_iterator(BidirectionalIterator iter) |
---|
262 | { |
---|
263 | test_forward_iterator(iter); |
---|
264 | bool ok_cached = ok(); |
---|
265 | err() << " testing Bidirectional features" << std::endl; |
---|
266 | const BidirectionalIterator i = iter; |
---|
267 | BidirectionalIterator tmp = iter--; |
---|
268 | add(tmp==i); |
---|
269 | ++iter; |
---|
270 | tmp = --iter; |
---|
271 | if (!add(tmp==iter)) |
---|
272 | err() << "operator-- failed" << std::endl; |
---|
273 | add(++tmp==i); |
---|
274 | if (ok_cached && !ok()) |
---|
275 | err() << "failed" << std::endl; |
---|
276 | } |
---|
277 | |
---|
278 | template<typename RandomAccessIterator> |
---|
279 | void Suite::test_random_access_iterator(RandomAccessIterator iter) |
---|
280 | { |
---|
281 | test_bidirectional_iterator(iter); |
---|
282 | err() << " testing RandomAccess features" << std::endl; |
---|
283 | bool ok_cached = ok(); |
---|
284 | RandomAccessIterator iter2 = iter; |
---|
285 | iter2 += 1; |
---|
286 | iter2 -= 1; |
---|
287 | RandomAccessIterator& iter3 = (iter2 += 1); |
---|
288 | RandomAccessIterator& iter4 = (iter3 -= 1); |
---|
289 | if (!add(iter2 == iter4)) |
---|
290 | err() << "operator-(int) failed" << std::endl; |
---|
291 | add(++iter2 == iter3); |
---|
292 | |
---|
293 | RandomAccessIterator iter5 = iter + 0; |
---|
294 | RandomAccessIterator iter6 = 0 + iter; |
---|
295 | add(iter6 == iter5); |
---|
296 | |
---|
297 | RandomAccessIterator iter7 = iter - 0; |
---|
298 | add(iter7 == iter); |
---|
299 | add(iter7 - iter == 0); |
---|
300 | add(! (iter7<iter)); |
---|
301 | |
---|
302 | typename RandomAccessIterator::value_type tmp = iter[0]; |
---|
303 | typename RandomAccessIterator::value_type tmp2 = *iter; |
---|
304 | tmp = tmp; // avoid compiler warning |
---|
305 | if (!add(tmp == tmp2)) |
---|
306 | err() << "operator[] failed" << std::endl; |
---|
307 | if (!add(iter[0] == *iter)) |
---|
308 | err() << "operator[] failed" << std::endl; |
---|
309 | if (ok_cached && !ok()) |
---|
310 | err() << "failed" << std::endl; |
---|
311 | } |
---|
312 | |
---|
313 | }}} |
---|
314 | |
---|
315 | #endif |
---|