source: trunk/test/Suite.h @ 1659

Last change on this file since 1659 was 1658, checked in by Peter, 13 years ago

Adding functions to use for fix precision comparisons (refs #470)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1#ifndef _theplu_yat_test_suite_
2#define _theplu_yat_test_suite_
3
4// $Id: Suite.h 1658 2008-12-17 15:02:00Z peter $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include <iosfwd>
26#include <sstream>
27
28namespace theplu {
29namespace yat {
30namespace test {
31
32  /**
33     \internal utility class for tests
34   */
35  class Suite
36  {
37  public:
38    Suite(int argc, char* argv[]);
39
40    /**
41     */
42    ~Suite(void);
43
44    /**
45       set ok to 'b && ok'
46
47       \return b
48    */
49    bool add(bool b);
50
51    /**
52       \return In verbose mode std::cerr, else a ofstream to "/dev/null".
53    */
54    std::ostream& err(void) const;
55
56    /**
57       \return true if \f$ |a-b| <= N * \epsilon * min(|a|,|b|) \f$
58       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
59    */
60    bool equal(double a, double b, unsigned long int N=1);
61
62    /**
63       \return true if |\a a - \a b| <= \a margin
64    */
65    bool equal_fix(double a, double b, double margin=0);
66
67    /**
68       \return true if \f$ |a-b| <= N * sqrt(\epsilon) * min(|a|,|b|) \f$
69       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
70    */
71    bool equal_sqrt(double a, double b, unsigned long int N=1);
72
73    /**
74       apply equal on ranges [first1, last1) and [first2, ...)
75    */
76    template<typename Iterator1, typename Iterator2>
77    bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
78                     unsigned int N=1);
79
80    /**
81       apply equal_fix on ranges [first1, last1) and [first2, ...)
82    */
83    template<typename Iterator1, typename Iterator2>
84    bool equal_range_fix(Iterator1 first1, Iterator1 last1, Iterator2 first2,
85                         double margin);
86
87    /**
88      \return true if test is ok
89    */
90    bool ok(void) const;
91
92    /**
93       \return In verbose mode std::cout, else a ofstream to "/dev/null".
94    */
95    std::ostream& out(void) const;
96   
97    /**
98       In verbose mode a final message is sent to std::cout.
99
100       If ok() is true: "Test is ok." otherwise
101       "Test failed."
102
103       \return 0 if ok.
104     */
105    int return_value(void) const;
106
107    /**
108       Function writes to a stream using operator<<, creates a new
109       object using stream constructor, and the new object is written
110       to another stream, and function check if the two outputs are
111       equal.
112    */
113    template<class T>
114    bool test_stream(const T&) const;
115
116    /**
117       This function is similar to add(bool) and could be used to
118       detect/count known issues. When the issue is fixed, one can
119       replace the call to xadd(bool) with a call to add(bool).
120
121       If \a b is false a counter is incremented, which is used to in
122       return_value() to generate some printout on how many known
123       issues were detected.
124
125       If \a b is true, ok_ is set to false, becasue the known issue
126       is no longer an issue and one should replace the call with a
127       call to add(bool).
128     */
129    bool xadd(bool b);
130
131  private:
132    std::ofstream* dev_null_;
133    unsigned int known_issues_;
134    bool ok_;
135    bool verbose_;
136
137  };
138
139  /**
140     \return absolute path to file
141     \param local_path path relative to srcdir
142   */
143  std::string filename(const std::string& local_path);
144
145  template<typename Iterator1, typename Iterator2>
146  bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
147                          unsigned int N)
148  {
149    while (first1!=last1){
150      if (!this->equal(*first1, *first2, N) )  {
151        return false;
152      }
153      ++first1;
154      ++first2;
155    }
156    return true;
157  }
158
159
160  template<typename Iterator1, typename Iterator2>
161  bool Suite::equal_range_fix(Iterator1 first1, Iterator1 last1, 
162                              Iterator2 first2, double margin)
163  {
164    while (first1!=last1){
165      if (!this->equal_fix(*first1, *first2, margin) )   {
166        return false;
167      }
168      ++first1;
169      ++first2;
170    }
171    return true;
172  }
173
174
175  template<class T>
176  bool Suite::test_stream(const T& t) const
177  {
178    this->err() << "Checking that output stream is valid as an input stream\n";
179    std::stringstream ss;
180    this->err() << "writing to output\n";
181    ss << t;
182    this->err() << "creating a new object from output\n";
183    T t2(ss);
184    std::stringstream ss2;
185    this->err() << "writing to output\n";
186    ss2 << t2;
187    bool ok = ss2.str()==ss.str();
188    if (!ok) {
189      this->err() << "ERROR: first object gave following output:\n" 
190                  << ss.str() << "\n"
191                  << "ERROR: and second object gave following output:\n" 
192                  << ss2.str() << "\n";
193    }
194    return ok;
195  }
196
197}}}
198
199#endif
Note: See TracBrowser for help on using the repository browser.