source: trunk/test/Suite.h @ 2943

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

merge release 0.10 into trunk

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