source: trunk/test/Suite.h @ 2960

Last change on this file since 2960 was 2960, checked in by Peter, 10 years ago

merge patch release 0.10.1 into trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.8 KB
Line 
1#ifndef _theplu_yat_test_suite_
2#define _theplu_yat_test_suite_
3
4// $Id: Suite.h 2960 2013-01-17 08:19:07Z peter $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 2012, 2013 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#define YAT_TEST_PROLOGUE "\n=== " << __func__ << " ===\n"
27
28// used to tell automake that test should be skipped
29#define EXIT_SKIP 77
30
31// SKIP_BAM_TEST is defined if we should skip bam tests
32#ifndef HAVE_LIBBAM
33#define SKIP_BAM_TEST
34#endif
35#ifndef HAVE_SAMTOOLS
36#define SKIP_BAM_TEST
37#endif
38
39#include <yat/utility/VectorMutable.h>
40#include <yat/classifier/Target.h>
41
42#include <boost/concept_archetype.hpp>
43
44#include <iosfwd>
45#include <sstream>
46#include <vector>
47
48namespace theplu {
49namespace yat {
50namespace test {
51
52  /**
53     \internal utility class for tests
54   */
55  class Suite
56  {
57  public:
58    Suite(int argc, char* argv[]);
59
60    /**
61     */
62    ~Suite(void);
63
64    /**
65       set ok to 'b && ok'
66
67       \return b
68    */
69    bool add(bool b);
70
71    /**
72       \return In verbose mode std::cerr, else a ofstream to "/dev/null".
73    */
74    std::ostream& err(void) const;
75
76    /**
77       \return true if \f$ |a-b| <= N * \epsilon * min(|a|,|b|) \f$
78       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
79    */
80    bool equal(double a, double b, unsigned long int N=1);
81
82    /**
83       \return true if |\a a - \a b| <= \a margin
84    */
85    bool equal_fix(double a, double b, double margin=0);
86
87    /**
88       \return true if \f$ |a-b| <= N * sqrt(\epsilon) * min(|a|,|b|) \f$
89       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
90    */
91    bool equal_sqrt(double a, double b, unsigned long int N=1);
92
93    /**
94       apply equal on ranges [first1, last1) and [first2, ...)
95    */
96    template<typename Iterator1, typename Iterator2>
97    bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
98                     unsigned int N=1);
99
100    /**
101       apply equal_fix on ranges [first1, last1) and [first2, ...)
102    */
103    template<typename Iterator1, typename Iterator2>
104    bool equal_range_fix(Iterator1 first1, Iterator1 last1, Iterator2 first2,
105                         double margin);
106
107    /**
108      \return true if test is ok
109    */
110    bool ok(void) const;
111
112    /**
113       \return In verbose mode std::cout, else a ofstream to "/dev/null".
114    */
115    std::ostream& out(void) const;
116   
117    /**
118       In verbose mode a final message is sent to std::cout.
119
120       If ok() is true: "Test is ok." otherwise
121       "Test failed."
122
123       \return 0 if ok.
124     */
125    int return_value(void) const;
126
127    template<typename TrivialIterator>
128    void test_trivial_iterator(const TrivialIterator&);
129   
130    template<typename InputIterator>
131    void test_input_iterator(InputIterator&);
132   
133    template<typename OutputIterator>
134    void test_output_iterator(OutputIterator&);
135 
136    template<typename ForwardIterator>
137    void test_forward_iterator(ForwardIterator);
138   
139    template<typename BidirectionalIterator>
140    void test_bidirectional_iterator(BidirectionalIterator);
141   
142    template<typename RandomAccessIterator>
143    void test_random_access_iterator(RandomAccessIterator);
144
145    template<typename Container2D>
146    void test_concept_container2d(const Container2D&);
147
148    template<typename MutableContainer2D>
149    void test_concept_mutable_container2d(MutableContainer2D&);
150
151    /**
152       Function writes to a stream using operator<<, creates a new
153       object using stream constructor, and the new object is written
154       to another stream, and function check if the two outputs are
155       equal.
156    */
157    template<class T>
158    bool test_stream(const T&) const;
159
160    /**
161       This function is similar to add(bool) and could be used to
162       detect/count known issues. When the issue is fixed, one can
163       replace the call to xadd(bool) with a call to add(bool).
164
165       If \a b is false a counter is incremented, which is used to in
166       return_value() to generate some printout on how many known
167       issues were detected.
168
169       If \a b is true, ok_ is set to false, becasue the known issue
170       is no longer an issue and one should replace the call with a
171       call to add(bool).
172     */
173    bool xadd(bool b);
174
175  private:
176    unsigned int known_issues_;
177    bool ok_;
178  };
179
180  /**
181     \return absolute path to test src dir
182  */
183  std::string abs_srcdir(void);
184
185  /**
186     \return absolute path to file
187     \param local_path path relative to srcdir
188   */
189  std::string filename(const std::string& local_path);
190
191  /*
192    class to test (at compile time) that a function (or class) works
193    with a Container2D. Do not run any test using this class because
194    the class is not really functional at run time.
195
196    \see boost/concept_archetype.hpp
197   */
198  template<typename T>
199  class container2d_archetype
200  {
201  public:
202    typedef T value_type;
203    typedef const T& const_reference;
204    typedef const T* const_iterator; 
205    typedef const_iterator const_column_iterator;
206    typedef const_iterator const_row_iterator;
207    const_iterator begin(void) const { return &element_; }
208    const_column_iterator begin_column(size_t) const { return &element_; }
209    const_iterator begin_row(size_t) const { return &element_; }
210    const_iterator end(void) const { return NULL; }
211    const_column_iterator end_column(size_t) const { return NULL; }
212    const_iterator end_row(size_t) const { return NULL; }
213    size_t columns(void) const { return 0; }
214    size_t rows(void) const { return 0; }
215    const_reference operator()(size_t row, size_t column) const 
216    { return element_; }
217
218  protected:
219    T element_;
220  };
221
222  /*
223    class to test (at compile time) that a function (or class) works
224    with a MutableContainer2D. Do not run any test using this class because
225    the class is not really functional at run time.
226
227    \see boost/concept_archetype.hpp
228   */
229  template<typename T>
230  class mutable_container2d_archetype : public container2d_archetype<T>
231  {
232  public:
233    typedef T& reference;
234    typedef T* iterator; 
235    typedef iterator column_iterator;
236    typedef iterator row_iterator;
237    iterator begin(void) { return &this->element_; }
238    column_iterator begin_column(size_t) { return &this->element_; }
239    iterator begin_row(size_t) { return &this->element_; }
240    iterator end(void) { return NULL; }
241    column_iterator end_column(size_t) { return NULL; }
242    iterator end_row(size_t) { return NULL; }
243    reference operator()(size_t row, size_t column)
244    { return this->element_; }
245  };
246
247  /*
248    class to test (at compile time) that a function (or class) works
249    with a Distance. Do not run any test using this class because
250    the class is not really functional at run time.
251
252    \see boost/concept_archetype.hpp
253   */
254  class distance_archetype
255  {
256  public:
257    /// class must be constructible somehow, but we don't wanna assume
258    /// void constructor or any other common constructor so we use the
259    /// signature to allow construction without assuming too much.
260    distance_archetype(const boost::detail::dummy_constructor&) {};
261    distance_archetype(const distance_archetype&) {};
262    template<typename T1, typename T2>
263    double operator()(T1 first1, T1 last1, T2 first2) const { return 0.0; }
264  private:
265    distance_archetype(void);
266    distance_archetype& operator=(const distance_archetype&);
267  };
268
269  /*
270    class to test (at compile time) that a function (or class) works
271    with a NeighborWeighting. Do not run any test using this class because
272    the class is not really functional at run time.
273
274    \see boost/concept_archetype.hpp
275   */
276  class neighbor_weighting_archetype
277  {
278  public:
279    neighbor_weighting_archetype(void) {}
280    void operator()(const utility::VectorBase& distance, 
281                    const std::vector<size_t>& k_sorted,
282                    const classifier::Target& target, 
283                    utility::VectorMutable& prediction) const {}
284    neighbor_weighting_archetype& operator=(const neighbor_weighting_archetype&)
285    { return *this; }
286  private:
287    neighbor_weighting_archetype(const neighbor_weighting_archetype&) {};
288  };
289
290  // template implementations
291
292  template<typename Iterator1, typename Iterator2>
293  bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
294                          unsigned int N)
295  {
296    while (first1!=last1){
297      if (!this->equal(*first1, *first2, N) )  {
298        return false;
299      }
300      ++first1;
301      ++first2;
302    }
303    return true;
304  }
305
306
307  template<typename Iterator1, typename Iterator2>
308  bool Suite::equal_range_fix(Iterator1 first1, Iterator1 last1, 
309                              Iterator2 first2, double margin)
310  {
311    while (first1!=last1){
312      if (!this->equal_fix(*first1, *first2, margin) )   {
313        return false;
314      }
315      ++first1;
316      ++first2;
317    }
318    return true;
319  }
320
321
322  // return true if we can write to a write-protected file
323  bool run_as_root(void);
324
325  template<class T>
326  bool Suite::test_stream(const T& t) const
327  {
328    this->err() << "Checking that output stream is valid as an input stream\n";
329    std::stringstream ss;
330    this->err() << "writing to output\n";
331    ss << t;
332    this->err() << "creating a new object from output\n";
333    T t2(ss);
334    std::stringstream ss2;
335    this->err() << "writing to output\n";
336    ss2 << t2;
337    bool ok = ss2.str()==ss.str();
338    if (!ok) {
339      this->err() << "ERROR: first object gave following output:\n" 
340                  << ss.str() << "\n"
341                  << "ERROR: and second object gave following output:\n" 
342                  << ss2.str() << "\n";
343    }
344    return ok;
345  }
346
347  template<typename TrivialIterator>
348  void Suite::test_trivial_iterator(const TrivialIterator& iter)
349  {
350    err() << "  testing Trivial features" << std::endl;
351    typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
352    add(tmp==*iter);
353    TrivialIterator default_constructed;
354    default_constructed == default_constructed; // avoid compiler warning
355  }
356 
357  template<typename InputIterator>
358  void Suite::test_input_iterator(InputIterator& iter)
359  {
360    test_trivial_iterator(iter);
361    err() << "  testing Input features" << std::endl;
362    // just to check compilation
363    if (false) {
364      ++iter;
365      iter++;
366    }
367  }
368 
369  template<typename OutputIterator>
370  void Suite::test_output_iterator(OutputIterator& iter)
371  {
372    test_trivial_iterator(iter);
373    err() << "  testing Output features" << std::endl;
374  }
375 
376  template<typename ForwardIterator>
377  void Suite::test_forward_iterator(ForwardIterator iter)
378  {
379    test_output_iterator(iter);
380    test_input_iterator(iter);
381    err() << "  testing Forward features" << std::endl;
382   
383    typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
384    // testing multiple traversing is possible and does not change the data
385    ForwardIterator iter1 = iter;
386    ++iter1;
387    add(iter!=iter1);
388    ForwardIterator iter2 = iter;
389    ++iter2;
390    add(tmp==*iter);
391  }
392 
393  template<typename BidirectionalIterator>
394  void Suite::test_bidirectional_iterator(BidirectionalIterator iter)
395  {
396    test_forward_iterator(iter);
397    bool ok_cached = ok();
398    err() << "  testing Bidirectional features" << std::endl;
399    const BidirectionalIterator i = iter;
400    BidirectionalIterator tmp = iter++;
401    if (!add(tmp==i)) {
402      err() << "iter++ does not return iter\n";
403    }
404    if (!add(++tmp==iter)) {
405      err() << "++iter failed\n";
406    }
407    if (!add(--tmp==i)) {
408      err() << "--iter failed\n";
409    }
410    tmp = iter--;
411    if (!add(--tmp==iter))
412      err() << "iter-- failed" << std::endl;
413    if (ok_cached && !ok())
414      err() << "failed" << std::endl;
415  }
416
417  template<typename RandomAccessIterator>
418  void Suite::test_random_access_iterator(RandomAccessIterator iter)
419  {
420    test_bidirectional_iterator(iter);
421    err() << "  testing RandomAccess features" << std::endl;
422    bool ok_cached = ok();
423    RandomAccessIterator iter2 = iter;
424    iter2 += 1;
425    iter2 -= 1;
426    RandomAccessIterator& iter3 = (iter2 += 1);
427    RandomAccessIterator& iter4 = (iter3 -= 1);
428    if (!add(iter2 == iter4))
429      err() << "operator-(int) failed" << std::endl;
430    add(++iter2 == iter3);
431   
432    RandomAccessIterator iter5 = iter + 0;
433    RandomAccessIterator iter6 = 0 + iter;
434    add(iter6 == iter5);
435   
436    RandomAccessIterator iter7 = iter - 0;
437    add(iter7 == iter);
438    add(iter7 - iter == 0);
439    add(! (iter7<iter));
440   
441    typename RandomAccessIterator::value_type tmp = iter[0];
442    typename RandomAccessIterator::value_type tmp2 = *iter;
443    tmp = tmp; // avoid compiler warning
444    if (!add(tmp == tmp2))
445      err() << "operator[] failed" << std::endl;
446    if (!add(iter[0] == *iter))
447      err() << "operator[] failed" << std::endl;
448    if (ok_cached && !ok())
449      err() << "failed" << std::endl;
450  }
451
452  template<typename Container2D>
453  void Suite::test_concept_container2d(const Container2D& c)
454  {
455    typedef typename Container2D::value_type value_type;
456    typedef typename Container2D::const_reference const_reference;
457    typedef typename Container2D::const_iterator const_iterator;
458    typedef typename Container2D::const_column_iterator const_column_iterator;
459    typedef typename Container2D::const_row_iterator const_row_iterator;
460    const_iterator ci = c.begin();
461    const_column_iterator cci = c.begin_column(0);
462    const_row_iterator cri = c.begin_row(0);
463    ci = c.end();
464    cci = c.end_column(0);
465    cri = c.end_row(0);
466    size_t cols = c.columns();
467    size_t rows = c.rows();
468    cols = rows; // just to avoid compiler warning
469    const_reference x = c(0,0);
470    value_type y;
471    y = x;
472  }
473
474  template<typename MutableContainer2D>
475  void Suite::test_concept_mutable_container2d(MutableContainer2D& mc)
476  {
477    test_concept_container2d(mc);
478    typedef typename MutableContainer2D::reference reference;
479    typedef typename MutableContainer2D::iterator iterator;
480    typedef typename MutableContainer2D::column_iterator column_iterator;
481    typedef typename MutableContainer2D::row_iterator row_iterator;
482    iterator i = mc.begin();
483    column_iterator ci = mc.begin_column(0);
484    row_iterator ri = mc.begin_row(0);
485    *i = *ci; 
486    *ci = *i; 
487    *ri = *i; 
488    i = mc.end();
489    ci = mc.end_column(0);
490    ri = mc.end_row(0);
491    reference x = mc(0,0);
492    x = *mc.begin();
493  }
494
495}}}
496
497#endif
Note: See TracBrowser for help on using the repository browser.