source: trunk/test/Suite.h @ 2228

Last change on this file since 2228 was 2228, checked in by Peter, 12 years ago

Using the parallel-tests driver. All tests are now by default verbose,
but 'make check' is still silent and the output is collected in log
files. Please refer to test/README or the Automake manual for more
details.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.8 KB
Line 
1#ifndef _theplu_yat_test_suite_
2#define _theplu_yat_test_suite_
3
4// $Id: Suite.h 2228 2010-03-25 12:28:14Z peter $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010 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    unsigned int known_issues_;
158    bool ok_;
159  };
160
161  /**
162     \return absolute path to file
163     \param local_path path relative to srcdir
164   */
165  std::string filename(const std::string& local_path);
166
167  /*
168    class to test (at compile time) that a function (or class) works
169    with a Container2D. Do not run any test using this class because
170    the class is not really functional at run time.
171
172    \see boost/concept_archetype.hpp
173   */
174  template<typename T>
175  class container2d_archetype
176  {
177  public:
178    typedef T value_type;
179    typedef const T& const_reference;
180    typedef const T* const_iterator; 
181    typedef const_iterator const_column_iterator;
182    typedef const_iterator const_row_iterator;
183    const_iterator begin(void) const { return &element_; }
184    const_column_iterator begin_column(size_t) const { return &element_; }
185    const_iterator begin_row(size_t) const { return &element_; }
186    const_iterator end(void) const { return NULL; }
187    const_column_iterator end_column(size_t) const { return NULL; }
188    const_iterator end_row(size_t) const { return NULL; }
189    size_t columns(void) const { return 0; }
190    size_t rows(void) const { return 0; }
191    const_reference operator()(size_t row, size_t column) const 
192    { return element_; }
193
194  protected:
195    T element_;
196  };
197
198  /*
199    class to test (at compile time) that a function (or class) works
200    with a MutableContainer2D. Do not run any test using this class because
201    the class is not really functional at run time.
202
203    \see boost/concept_archetype.hpp
204   */
205  template<typename T>
206  class mutable_container2d_archetype : public container2d_archetype<T>
207  {
208  public:
209    typedef T& reference;
210    typedef T* iterator; 
211    typedef iterator column_iterator;
212    typedef iterator row_iterator;
213    iterator begin(void) { return &this->element_; }
214    column_iterator begin_column(size_t) { return &this->element_; }
215    iterator begin_row(size_t) { return &this->element_; }
216    iterator end(void) { return NULL; }
217    column_iterator end_column(size_t) { return NULL; }
218    iterator end_row(size_t) { return NULL; }
219    reference operator()(size_t row, size_t column) const 
220    { return this->element_; }
221  };
222
223  // template implementations
224
225  template<typename Iterator1, typename Iterator2>
226  bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
227                          unsigned int N)
228  {
229    while (first1!=last1){
230      if (!this->equal(*first1, *first2, N) )  {
231        return false;
232      }
233      ++first1;
234      ++first2;
235    }
236    return true;
237  }
238
239
240  template<typename Iterator1, typename Iterator2>
241  bool Suite::equal_range_fix(Iterator1 first1, Iterator1 last1, 
242                              Iterator2 first2, double margin)
243  {
244    while (first1!=last1){
245      if (!this->equal_fix(*first1, *first2, margin) )   {
246        return false;
247      }
248      ++first1;
249      ++first2;
250    }
251    return true;
252  }
253
254
255  // return true if we can write to a write-protected file
256  bool run_as_root(void);
257
258  template<class T>
259  bool Suite::test_stream(const T& t) const
260  {
261    this->err() << "Checking that output stream is valid as an input stream\n";
262    std::stringstream ss;
263    this->err() << "writing to output\n";
264    ss << t;
265    this->err() << "creating a new object from output\n";
266    T t2(ss);
267    std::stringstream ss2;
268    this->err() << "writing to output\n";
269    ss2 << t2;
270    bool ok = ss2.str()==ss.str();
271    if (!ok) {
272      this->err() << "ERROR: first object gave following output:\n" 
273                  << ss.str() << "\n"
274                  << "ERROR: and second object gave following output:\n" 
275                  << ss2.str() << "\n";
276    }
277    return ok;
278  }
279
280  template<typename TrivialIterator>
281  void Suite::test_trivial_iterator(const TrivialIterator& iter)
282  {
283    err() << "  testing Trivial features" << std::endl;
284    typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
285    add(tmp==*iter);
286    TrivialIterator default_constructed;
287    default_constructed == default_constructed; // avoid compiler warning
288  }
289 
290  template<typename InputIterator>
291  void Suite::test_input_iterator(InputIterator& iter)
292  {
293    test_trivial_iterator(iter);
294    err() << "  testing Input features" << std::endl;
295    // just to check compilation
296    if (false) {
297      ++iter;
298      iter++;
299    }
300  }
301 
302  template<typename OutputIterator>
303  void Suite::test_output_iterator(OutputIterator& iter)
304  {
305    test_trivial_iterator(iter);
306    err() << "  testing Output features" << std::endl;
307  }
308 
309  template<typename ForwardIterator>
310  void Suite::test_forward_iterator(ForwardIterator iter)
311  {
312    test_output_iterator(iter);
313    test_input_iterator(iter);
314    err() << "  testing Forward features" << std::endl;
315   
316    typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
317    // testing multiple traversing is possible and does not change the data
318    ForwardIterator iter1 = iter;
319    ++iter1;
320    add(iter!=iter1);
321    ForwardIterator iter2 = iter;
322    ++iter2;
323    add(tmp==*iter);
324  }
325 
326  template<typename BidirectionalIterator>
327  void Suite::test_bidirectional_iterator(BidirectionalIterator iter)
328  {
329    test_forward_iterator(iter);
330    bool ok_cached = ok();
331    err() << "  testing Bidirectional features" << std::endl;
332    const BidirectionalIterator i = iter;
333    BidirectionalIterator tmp = iter++;
334    if (!add(tmp==i)) {
335      err() << "iter++ does not return iter\n";
336    }
337    if (!add(++tmp==iter)) {
338      err() << "++iter failed\n";
339    }
340    if (!add(--tmp==i)) {
341      err() << "--iter failed\n";
342    }
343    tmp = iter--;
344    if (!add(--tmp==iter))
345      err() << "iter-- failed" << std::endl;
346    if (ok_cached && !ok())
347      err() << "failed" << std::endl;
348  }
349
350  template<typename RandomAccessIterator>
351  void Suite::test_random_access_iterator(RandomAccessIterator iter)
352  {
353    test_bidirectional_iterator(iter);
354    err() << "  testing RandomAccess features" << std::endl;
355    bool ok_cached = ok();
356    RandomAccessIterator iter2 = iter;
357    iter2 += 1;
358    iter2 -= 1;
359    RandomAccessIterator& iter3 = (iter2 += 1);
360    RandomAccessIterator& iter4 = (iter3 -= 1);
361    if (!add(iter2 == iter4))
362      err() << "operator-(int) failed" << std::endl;
363    add(++iter2 == iter3);
364   
365    RandomAccessIterator iter5 = iter + 0;
366    RandomAccessIterator iter6 = 0 + iter;
367    add(iter6 == iter5);
368   
369    RandomAccessIterator iter7 = iter - 0;
370    add(iter7 == iter);
371    add(iter7 - iter == 0);
372    add(! (iter7<iter));
373   
374    typename RandomAccessIterator::value_type tmp = iter[0];
375    typename RandomAccessIterator::value_type tmp2 = *iter;
376    tmp = tmp; // avoid compiler warning
377    if (!add(tmp == tmp2))
378      err() << "operator[] failed" << std::endl;
379    if (!add(iter[0] == *iter))
380      err() << "operator[] failed" << std::endl;
381    if (ok_cached && !ok())
382      err() << "failed" << std::endl;
383  }
384
385  template<typename Container2D>
386  void Suite::test_concept_container2d(const Container2D& c)
387  {
388    typedef typename Container2D::value_type value_type;
389    typedef typename Container2D::const_reference const_reference;
390    typedef typename Container2D::const_iterator const_iterator;
391    typedef typename Container2D::const_column_iterator const_column_iterator;
392    typedef typename Container2D::const_row_iterator const_row_iterator;
393    const_iterator ci = c.begin();
394    const_column_iterator cci = c.begin_column(0);
395    const_row_iterator cri = c.begin_row(0);
396    ci = c.end();
397    cci = c.end_column(0);
398    cri = c.end_row(0);
399    size_t cols = c.columns();
400    size_t rows = c.rows();
401    cols = rows; // just to avoid compiler warning
402    const_reference x = c(0,0);
403    value_type y;
404    y = x;
405  }
406
407  template<typename MutableContainer2D>
408  void Suite::test_concept_mutable_container2d(MutableContainer2D& mc)
409  {
410    test_concept_container2d(mc);
411    typedef typename MutableContainer2D::reference reference;
412    typedef typename MutableContainer2D::iterator iterator;
413    typedef typename MutableContainer2D::column_iterator column_iterator;
414    typedef typename MutableContainer2D::row_iterator row_iterator;
415    iterator i = mc.begin();
416    column_iterator ci = mc.begin_column(0);
417    row_iterator ri = mc.begin_row(0);
418    *i = *ci; 
419    *ci = *i; 
420    *ri = *i; 
421    i = mc.end();
422    ci = mc.end_column(0);
423    ri = mc.end_row(0);
424    reference x = mc(0,0);
425    x = *mc.begin();
426  }
427
428}}}
429
430#endif
Note: See TracBrowser for help on using the repository browser.