source: trunk/test/Suite.h @ 2143

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

fixes #449

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.9 KB
Line 
1#ifndef _theplu_yat_test_suite_
2#define _theplu_yat_test_suite_
3
4// $Id: Suite.h 2143 2010-01-15 14:12:56Z 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    template<typename Container2D>
127    void test_concept_container2d(const Container2D&);
128
129    template<typename MutableContainer2D>
130    void test_concept_mutable_container2d(MutableContainer2D&);
131
132    /**
133       Function writes to a stream using operator<<, creates a new
134       object using stream constructor, and the new object is written
135       to another stream, and function check if the two outputs are
136       equal.
137    */
138    template<class T>
139    bool test_stream(const T&) const;
140
141    /**
142       This function is similar to add(bool) and could be used to
143       detect/count known issues. When the issue is fixed, one can
144       replace the call to xadd(bool) with a call to add(bool).
145
146       If \a b is false a counter is incremented, which is used to in
147       return_value() to generate some printout on how many known
148       issues were detected.
149
150       If \a b is true, ok_ is set to false, becasue the known issue
151       is no longer an issue and one should replace the call with a
152       call to add(bool).
153     */
154    bool xadd(bool b);
155
156  private:
157    std::ofstream* dev_null_;
158    unsigned int known_issues_;
159    bool ok_;
160    bool verbose_;
161
162  };
163
164  /**
165     \return absolute path to file
166     \param local_path path relative to srcdir
167   */
168  std::string filename(const std::string& local_path);
169
170  template<typename Iterator1, typename Iterator2>
171  bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
172                          unsigned int N)
173  {
174    while (first1!=last1){
175      if (!this->equal(*first1, *first2, N) )  {
176        return false;
177      }
178      ++first1;
179      ++first2;
180    }
181    return true;
182  }
183
184
185  template<typename Iterator1, typename Iterator2>
186  bool Suite::equal_range_fix(Iterator1 first1, Iterator1 last1, 
187                              Iterator2 first2, double margin)
188  {
189    while (first1!=last1){
190      if (!this->equal_fix(*first1, *first2, margin) )   {
191        return false;
192      }
193      ++first1;
194      ++first2;
195    }
196    return true;
197  }
198
199
200  // return true if we can write to a write-protected file
201  bool run_as_root(void);
202
203  template<class T>
204  bool Suite::test_stream(const T& t) const
205  {
206    this->err() << "Checking that output stream is valid as an input stream\n";
207    std::stringstream ss;
208    this->err() << "writing to output\n";
209    ss << t;
210    this->err() << "creating a new object from output\n";
211    T t2(ss);
212    std::stringstream ss2;
213    this->err() << "writing to output\n";
214    ss2 << t2;
215    bool ok = ss2.str()==ss.str();
216    if (!ok) {
217      this->err() << "ERROR: first object gave following output:\n" 
218                  << ss.str() << "\n"
219                  << "ERROR: and second object gave following output:\n" 
220                  << ss2.str() << "\n";
221    }
222    return ok;
223  }
224
225  template<typename TrivialIterator>
226  void Suite::test_trivial_iterator(const TrivialIterator& iter)
227  {
228    err() << "  testing Trivial features" << std::endl;
229    typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
230    add(tmp==*iter);
231  }
232 
233  template<typename InputIterator>
234  void Suite::test_input_iterator(InputIterator& iter)
235  {
236    test_trivial_iterator(iter);
237    err() << "  testing Input features" << std::endl;
238    // just to check compilation
239    if (false) {
240      ++iter;
241      iter++;
242    }
243  }
244 
245  template<typename OutputIterator>
246  void Suite::test_output_iterator(OutputIterator& iter)
247  {
248    test_trivial_iterator(iter);
249    err() << "  testing Output features" << std::endl;
250  }
251 
252  template<typename ForwardIterator>
253  void Suite::test_forward_iterator(ForwardIterator iter)
254  {
255    test_output_iterator(iter);
256    test_input_iterator(iter);
257    err() << "  testing Forward features" << std::endl;
258   
259    typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
260    // testing multiple traversing is possible and does not change the data
261    ForwardIterator iter1 = iter;
262    ++iter1;
263    add(iter!=iter1);
264    ForwardIterator iter2 = iter;
265    ++iter2;
266    add(tmp==*iter);
267  }
268 
269  template<typename BidirectionalIterator>
270  void Suite::test_bidirectional_iterator(BidirectionalIterator iter)
271  {
272    test_forward_iterator(iter);
273    bool ok_cached = ok();
274    err() << "  testing Bidirectional features" << std::endl;
275    const BidirectionalIterator i = iter;
276    BidirectionalIterator tmp = iter++;
277    if (!add(tmp==i)) {
278      err() << "iter++ does not return iter\n";
279    }
280    if (!add(++tmp==iter)) {
281      err() << "++iter failed\n";
282    }
283    if (!add(--tmp==i)) {
284      err() << "--iter failed\n";
285    }
286    tmp = iter--;
287    if (!add(--tmp==iter))
288      err() << "iter-- failed" << std::endl;
289    if (ok_cached && !ok())
290      err() << "failed" << std::endl;
291  }
292
293  template<typename RandomAccessIterator>
294  void Suite::test_random_access_iterator(RandomAccessIterator iter)
295  {
296    test_bidirectional_iterator(iter);
297    err() << "  testing RandomAccess features" << std::endl;
298    bool ok_cached = ok();
299    RandomAccessIterator iter2 = iter;
300    iter2 += 1;
301    iter2 -= 1;
302    RandomAccessIterator& iter3 = (iter2 += 1);
303    RandomAccessIterator& iter4 = (iter3 -= 1);
304    if (!add(iter2 == iter4))
305      err() << "operator-(int) failed" << std::endl;
306    add(++iter2 == iter3);
307   
308    RandomAccessIterator iter5 = iter + 0;
309    RandomAccessIterator iter6 = 0 + iter;
310    add(iter6 == iter5);
311   
312    RandomAccessIterator iter7 = iter - 0;
313    add(iter7 == iter);
314    add(iter7 - iter == 0);
315    add(! (iter7<iter));
316   
317    typename RandomAccessIterator::value_type tmp = iter[0];
318    typename RandomAccessIterator::value_type tmp2 = *iter;
319    tmp = tmp; // avoid compiler warning
320    if (!add(tmp == tmp2))
321      err() << "operator[] failed" << std::endl;
322    if (!add(iter[0] == *iter))
323      err() << "operator[] failed" << std::endl;
324    if (ok_cached && !ok())
325      err() << "failed" << std::endl;
326  }
327
328  template<typename Container2D>
329  void Suite::test_concept_container2d(const Container2D& c)
330  {
331    typedef typename Container2D::value_type value_type;
332    typedef typename Container2D::const_reference const_reference;
333    typedef typename Container2D::const_iterator const_iterator;
334    typedef typename Container2D::const_column_iterator const_column_iterator;
335    typedef typename Container2D::const_row_iterator const_row_iterator;
336    const_iterator ci = c.begin();
337    const_column_iterator cci = c.begin_column(0);
338    const_row_iterator cri = c.begin_row(0);
339    ci = c.end();
340    cci = c.end_column(0);
341    cri = c.end_row(0);
342    size_t cols = c.columns();
343    size_t rows = c.rows();
344    cols = rows; // just to avoid compiler warning
345    const_reference x = c(0,0);
346    value_type y;
347    y = x;
348  }
349
350  template<typename MutableContainer2D>
351  void Suite::test_concept_mutable_container2d(MutableContainer2D& mc)
352  {
353    test_concept_container2d(mc);
354    typedef typename MutableContainer2D::reference reference;
355    typedef typename MutableContainer2D::iterator iterator;
356    typedef typename MutableContainer2D::column_iterator column_iterator;
357    typedef typename MutableContainer2D::row_iterator row_iterator;
358    iterator i = mc.begin();
359    column_iterator ci = mc.begin_column(0);
360    row_iterator ri = mc.begin_row(0);
361    *i = *ci; 
362    *ci = *i; 
363    *ri = *i; 
364    i = mc.end();
365    ci = mc.end_column(0);
366    ri = mc.end_row(0);
367    reference x = mc(0,0);
368    x = *mc.begin();
369  }
370
371}}}
372
373#endif
Note: See TracBrowser for help on using the repository browser.