source: branches/0.13-stable/test/Suite.h @ 3431

Last change on this file since 3431 was 3431, checked in by Peter, 7 years ago

revert r3426 and some cosmetic changes (refs #794)

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