source: trunk/test/Suite.h @ 3322

Last change on this file since 3322 was 3322, checked in by Peter, 8 years ago

change name of test function to more descriptive 'avoid_compiler_warning'; test weighted_iterator_traits; refs #803

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