source: trunk/test/Suite.h @ 2119

Last change on this file since 2119 was 2119, checked in by Peter, 13 years ago

converted files to utf-8. fixes #577

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1#ifndef _theplu_yat_test_suite_
2#define _theplu_yat_test_suite_
3
4// $Id: Suite.h 2119 2009-12-12 23:11:43Z 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
29namespace theplu {
30namespace yat {
31namespace 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  // return true if we can write to a write-protected file
195  bool run_as_root(void);
196
197  template<class T>
198  bool Suite::test_stream(const T& t) const
199  {
200    this->err() << "Checking that output stream is valid as an input stream\n";
201    std::stringstream ss;
202    this->err() << "writing to output\n";
203    ss << t;
204    this->err() << "creating a new object from output\n";
205    T t2(ss);
206    std::stringstream ss2;
207    this->err() << "writing to output\n";
208    ss2 << t2;
209    bool ok = ss2.str()==ss.str();
210    if (!ok) {
211      this->err() << "ERROR: first object gave following output:\n" 
212                  << ss.str() << "\n"
213                  << "ERROR: and second object gave following output:\n" 
214                  << ss2.str() << "\n";
215    }
216    return ok;
217  }
218
219  template<typename TrivialIterator>
220  void Suite::test_trivial_iterator(const TrivialIterator& iter)
221  {
222    err() << "  testing Trivial features" << std::endl;
223    typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
224    add(tmp==*iter);
225  }
226 
227  template<typename InputIterator>
228  void Suite::test_input_iterator(InputIterator& iter)
229  {
230    test_trivial_iterator(iter);
231    err() << "  testing Input features" << std::endl;
232    // just to check compilation
233    if (false) {
234      ++iter;
235      iter++;
236    }
237  }
238 
239  template<typename OutputIterator>
240  void Suite::test_output_iterator(OutputIterator& iter)
241  {
242    test_trivial_iterator(iter);
243    err() << "  testing Output features" << std::endl;
244  }
245 
246  template<typename ForwardIterator>
247  void Suite::test_forward_iterator(ForwardIterator iter)
248  {
249    test_output_iterator(iter);
250    test_input_iterator(iter);
251    err() << "  testing Forward features" << std::endl;
252   
253    typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
254    // testing multiple traversing is possible and does not change the data
255    ForwardIterator iter1 = iter;
256    ++iter1;
257    add(iter!=iter1);
258    ForwardIterator iter2 = iter;
259    ++iter2;
260    add(tmp==*iter);
261  }
262 
263  template<typename BidirectionalIterator>
264  void Suite::test_bidirectional_iterator(BidirectionalIterator iter)
265  {
266    test_forward_iterator(iter);
267    bool ok_cached = ok();
268    err() << "  testing Bidirectional features" << std::endl;
269    const BidirectionalIterator i = iter;
270    BidirectionalIterator tmp = iter++;
271    if (!add(tmp==i)) {
272      err() << "iter++ does not return iter\n";
273    }
274    if (!add(++tmp==iter)) {
275      err() << "++iter failed\n";
276    }
277    if (!add(--tmp==i)) {
278      err() << "--iter failed\n";
279    }
280    tmp = iter--;
281    if (!add(--tmp==iter))
282      err() << "iter-- failed" << std::endl;
283    if (ok_cached && !ok())
284      err() << "failed" << std::endl;
285  }
286
287  template<typename RandomAccessIterator>
288  void Suite::test_random_access_iterator(RandomAccessIterator iter)
289  {
290    test_bidirectional_iterator(iter);
291    err() << "  testing RandomAccess features" << std::endl;
292    bool ok_cached = ok();
293    RandomAccessIterator iter2 = iter;
294    iter2 += 1;
295    iter2 -= 1;
296    RandomAccessIterator& iter3 = (iter2 += 1);
297    RandomAccessIterator& iter4 = (iter3 -= 1);
298    if (!add(iter2 == iter4))
299      err() << "operator-(int) failed" << std::endl;
300    add(++iter2 == iter3);
301   
302    RandomAccessIterator iter5 = iter + 0;
303    RandomAccessIterator iter6 = 0 + iter;
304    add(iter6 == iter5);
305   
306    RandomAccessIterator iter7 = iter - 0;
307    add(iter7 == iter);
308    add(iter7 - iter == 0);
309    add(! (iter7<iter));
310   
311    typename RandomAccessIterator::value_type tmp = iter[0];
312    typename RandomAccessIterator::value_type tmp2 = *iter;
313    tmp = tmp; // avoid compiler warning
314    if (!add(tmp == tmp2))
315      err() << "operator[] failed" << std::endl;
316    if (!add(iter[0] == *iter))
317      err() << "operator[] failed" << std::endl;
318    if (ok_cached && !ok())
319      err() << "failed" << std::endl;
320  }
321
322}}}
323
324#endif
Note: See TracBrowser for help on using the repository browser.