source: trunk/test/Suite.h @ 2202

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

merging release 0.6 into trunk

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