source: trunk/test/Suite.h @ 2339

Last change on this file since 2339 was 2339, checked in by Peter, 11 years ago

refs #627 extending NeighborWeighting? concept to require defaulf constructor and assign operator. Added a archetype class in test.

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