source: trunk/test/Suite.h @ 3149

Last change on this file since 3149 was 3149, checked in by Peter, 9 years ago

change test_output_iterator: there is requirement that output iterators have value_type

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.0 KB
Line 
1#ifndef _theplu_yat_test_suite_
2#define _theplu_yat_test_suite_
3
4// $Id: Suite.h 3149 2013-12-23 06:02:47Z peter $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2011, 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  // function that can be used to avoid warning: '-Wunused-but-set-variable'
326  template<typename T>
327  void dummie_function(T) {}
328
329  template<class T>
330  bool Suite::test_stream(const T& t) const
331  {
332    this->err() << "Checking that output stream is valid as an input stream\n";
333    std::stringstream ss;
334    this->err() << "writing to output\n";
335    ss << t;
336    this->err() << "creating a new object from output\n";
337    T t2(ss);
338    std::stringstream ss2;
339    this->err() << "writing to output\n";
340    ss2 << t2;
341    bool ok = ss2.str()==ss.str();
342    if (!ok) {
343      this->err() << "ERROR: first object gave following output:\n"
344                  << ss.str() << "\n"
345                  << "ERROR: and second object gave following output:\n"
346                  << ss2.str() << "\n";
347    }
348    return ok;
349  }
350
351  template<typename TrivialIterator>
352  void Suite::test_trivial_iterator(const TrivialIterator& iter)
353  {
354    err() << "  testing Trivial features" << std::endl;
355    typename std::iterator_traits<TrivialIterator>::value_type tmp = *iter;
356    add(tmp==*iter);
357    TrivialIterator default_constructed;
358    default_constructed == default_constructed; // avoid compiler warning
359  }
360
361  template<typename InputIterator>
362  void Suite::test_input_iterator(InputIterator& iter)
363  {
364    test_trivial_iterator(iter);
365    err() << "  testing Input features" << std::endl;
366    // just to check compilation
367    if (false) {
368      ++iter;
369      iter++;
370    }
371  }
372
373  template<typename OutputIterator>
374  void Suite::test_output_iterator(OutputIterator& iter)
375  {
376    err() << "  testing Output features" << std::endl;
377    // OutputIterator should be default constructible and assignable
378    OutputIterator oi;
379    oi = iter;
380    ++oi;
381  }
382
383  template<typename ForwardIterator>
384  void Suite::test_forward_iterator(ForwardIterator iter)
385  {
386    test_output_iterator(iter);
387    test_input_iterator(iter);
388    err() << "  testing Forward features" << std::endl;
389
390    typename std::iterator_traits<ForwardIterator>::value_type tmp = *iter;
391    // testing multiple traversing is possible and does not change the data
392    ForwardIterator iter1 = iter;
393    ++iter1;
394    add(iter!=iter1);
395    ForwardIterator iter2 = iter;
396    ++iter2;
397    add(tmp==*iter);
398  }
399
400  template<typename BidirectionalIterator>
401  void Suite::test_bidirectional_iterator(BidirectionalIterator iter)
402  {
403    test_forward_iterator(iter);
404    bool ok_cached = ok();
405    err() << "  testing Bidirectional features" << std::endl;
406    const BidirectionalIterator i = iter;
407    BidirectionalIterator tmp = iter++;
408    if (!add(tmp==i)) {
409      err() << "iter++ does not return iter\n";
410    }
411    if (!add(++tmp==iter)) {
412      err() << "++iter failed\n";
413    }
414    if (!add(--tmp==i)) {
415      err() << "--iter failed\n";
416    }
417    tmp = iter--;
418    if (!add(--tmp==iter))
419      err() << "iter-- failed" << std::endl;
420    if (ok_cached && !ok())
421      err() << "failed" << std::endl;
422  }
423
424  template<typename RandomAccessIterator>
425  void Suite::test_random_access_iterator(RandomAccessIterator iter)
426  {
427    test_bidirectional_iterator(iter);
428    err() << "  testing RandomAccess features" << std::endl;
429    bool ok_cached = ok();
430    RandomAccessIterator iter2 = iter;
431    iter2 += 1;
432    iter2 -= 1;
433    RandomAccessIterator& iter3 = (iter2 += 1);
434    RandomAccessIterator& iter4 = (iter3 -= 1);
435    if (!add(iter2 == iter4))
436      err() << "operator-(int) failed" << std::endl;
437    add(++iter2 == iter3);
438
439    RandomAccessIterator iter5 = iter + 0;
440    RandomAccessIterator iter6 = 0 + iter;
441    add(iter6 == iter5);
442
443    RandomAccessIterator iter7 = iter - 0;
444    add(iter7 == iter);
445    add(iter7 - iter == 0);
446    add(! (iter7<iter));
447
448    typename RandomAccessIterator::value_type tmp = iter[0];
449    typename RandomAccessIterator::value_type tmp2 = *iter;
450    tmp = tmp; // avoid compiler warning
451    if (!add(tmp == tmp2))
452      err() << "operator[] failed" << std::endl;
453    if (!add(iter[0] == *iter))
454      err() << "operator[] failed" << std::endl;
455    if (ok_cached && !ok())
456      err() << "failed" << std::endl;
457  }
458
459  template<typename Container2D>
460  void Suite::test_concept_container2d(const Container2D& c)
461  {
462    typedef typename Container2D::value_type value_type;
463    typedef typename Container2D::const_reference const_reference;
464    typedef typename Container2D::const_iterator const_iterator;
465    typedef typename Container2D::const_column_iterator const_column_iterator;
466    typedef typename Container2D::const_row_iterator const_row_iterator;
467    const_iterator ci = c.begin();
468    const_column_iterator cci = c.begin_column(0);
469    const_row_iterator cri = c.begin_row(0);
470    ci = c.end();
471    cci = c.end_column(0);
472    cri = c.end_row(0);
473    size_t cols = c.columns();
474    size_t rows = c.rows();
475    // just to avoid compiler warning
476    dummie_function(cols);
477    dummie_function(rows);
478    const_reference x = c(0,0);
479    value_type y;
480    y = x;
481    dummie_function(y);
482  }
483
484  template<typename MutableContainer2D>
485  void Suite::test_concept_mutable_container2d(MutableContainer2D& mc)
486  {
487    test_concept_container2d(mc);
488    typedef typename MutableContainer2D::reference reference;
489    typedef typename MutableContainer2D::iterator iterator;
490    typedef typename MutableContainer2D::column_iterator column_iterator;
491    typedef typename MutableContainer2D::row_iterator row_iterator;
492    iterator i = mc.begin();
493    column_iterator ci = mc.begin_column(0);
494    row_iterator ri = mc.begin_row(0);
495    *i = *ci;
496    *ci = *i;
497    *ri = *i;
498    i = mc.end();
499    ci = mc.end_column(0);
500    ri = mc.end_row(0);
501    reference x = mc(0,0);
502    x = *mc.begin();
503  }
504
505}}}
506
507#endif
Note: See TracBrowser for help on using the repository browser.