source: trunk/test/Suite.h @ 1509

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

fixes #437 and added support for having known issues in test::Suite

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1#ifndef _theplu_yat_test_suite_
2#define _theplu_yat_test_suite_
3
4// $Id: Suite.h 1509 2008-09-17 20:56:30Z peter $
5
6/*
7  Copyright (C) 2008 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 <fstream>
26#include <iostream>
27#include <sstream>
28
29namespace theplu {
30namespace yat {
31namespace test {
32
33  /**
34     \internal utility class for tests
35   */
36  class Suite
37  {
38  public:
39    Suite(int argc, char* argv[]);
40
41    /**
42     */
43    ~Suite(void);
44
45    /**
46       set ok to 'b && ok'
47
48       \return b
49    */
50    bool add(bool b);
51
52    /**
53       \return In verbose mode std::cerr, else a ofstream to "/dev/null".
54    */
55    std::ostream& err(void) const;
56
57    /**
58       \return true if \f$ |a-b| <= N * \epsilon * min(|a|,|b|) \f$
59       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
60    */
61    bool equal(double a, double b, unsigned long int N=1);
62
63    /**
64       \return true if \f$ |a-b| <= N * sqrt(\epsilon) * min(|a|,|b|) \f$
65       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
66    */
67    bool equal_sqrt(double a, double b, unsigned long int N=1);
68
69    template<typename Iterator1, typename Iterator2>
70    bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
71                     unsigned int N=1);
72
73    /**
74      \return true if test is ok
75    */
76    bool ok(void) const;
77
78    /**
79       \return In verbose mode std::cout, else a ofstream to "/dev/null".
80    */
81    std::ostream& out(void) const;
82   
83    /**
84       In verbose mode a final message is sent to std::cout.
85
86       If ok() is true: "Test is ok." otherwise
87       "Test failed."
88
89       \return 0 if ok.
90     */
91    int return_value(void) const;
92
93    /**
94       Function writes to a stream using operator<<, creates a new
95       object using stream constructor, and the new object is written
96       to another stream, and function check if the two outputs are
97       equal.
98    */
99    template<class T>
100    bool test_stream(const T&) const;
101
102    /**
103       This function is similar to add(bool) and could be used to
104       detect/count known issues. When the issue is fixed, one can
105       replace the call to xadd(bool) with a call to add(bool).
106
107       If \a b is false a counter is incremented, which is used to in
108       return_value() to generate some printout on how many known
109       issues were detected.
110
111       If \a b is true, ok_ is set to false, becasue the known issue
112       is no longer an issue and one should replace the call with a
113       call to add(bool).
114     */
115    bool xadd(bool b);
116
117  private:
118    std::ofstream* dev_null_;
119    unsigned int known_issues_;
120    bool ok_;
121    bool verbose_;
122
123  };
124
125  /**
126     \return absolute path to file
127     \param local_path path relative to srcdir
128   */
129  std::string filename(const std::string& local_path);
130
131  template<typename Iterator1, typename Iterator2>
132  bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
133                          unsigned int N)
134  {
135    while (first1!=last1){
136      if (!this->equal(*first1, *first2, N) )  {
137        return false;
138      }
139      ++first1;
140      ++first2;
141    }
142    return true;
143  }
144
145
146  template<class T>
147  bool Suite::test_stream(const T& t) const
148  {
149    this->err() << "Checking that output stream is valid as an input stream\n";
150    std::stringstream ss;
151    this->err() << "writing to output\n";
152    ss << t;
153    this->err() << "creating a new object from output\n";
154    T t2(ss);
155    std::stringstream ss2;
156    this->err() << "writing to output\n";
157    ss2 << t2;
158    bool ok = ss2.str()==ss.str();
159    if (!ok) {
160      this->err() << "ERROR: first object gave following output:\n" 
161                  << ss.str() << "\n"
162                  << "ERROR: and second object gave following output:\n" 
163                  << ss2.str() << "\n";
164    }
165    return ok;
166  }
167
168}}}
169
170#endif
Note: See TracBrowser for help on using the repository browser.