source: trunk/test/Suite.h @ 1607

Last change on this file since 1607 was 1569, checked in by Jari Häkkinen, 13 years ago

Prefer forward declaration.

  • 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 1569 2008-10-13 18:34:20Z jari $
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 \f$ |a-b| <= N * sqrt(\epsilon) * min(|a|,|b|) \f$
64       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
65    */
66    bool equal_sqrt(double a, double b, unsigned long int N=1);
67
68    template<typename Iterator1, typename Iterator2>
69    bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
70                     unsigned int N=1);
71
72    /**
73      \return true if test is ok
74    */
75    bool ok(void) const;
76
77    /**
78       \return In verbose mode std::cout, else a ofstream to "/dev/null".
79    */
80    std::ostream& out(void) const;
81   
82    /**
83       In verbose mode a final message is sent to std::cout.
84
85       If ok() is true: "Test is ok." otherwise
86       "Test failed."
87
88       \return 0 if ok.
89     */
90    int return_value(void) const;
91
92    /**
93       Function writes to a stream using operator<<, creates a new
94       object using stream constructor, and the new object is written
95       to another stream, and function check if the two outputs are
96       equal.
97    */
98    template<class T>
99    bool test_stream(const T&) const;
100
101    /**
102       This function is similar to add(bool) and could be used to
103       detect/count known issues. When the issue is fixed, one can
104       replace the call to xadd(bool) with a call to add(bool).
105
106       If \a b is false a counter is incremented, which is used to in
107       return_value() to generate some printout on how many known
108       issues were detected.
109
110       If \a b is true, ok_ is set to false, becasue the known issue
111       is no longer an issue and one should replace the call with a
112       call to add(bool).
113     */
114    bool xadd(bool b);
115
116  private:
117    std::ofstream* dev_null_;
118    unsigned int known_issues_;
119    bool ok_;
120    bool verbose_;
121
122  };
123
124  /**
125     \return absolute path to file
126     \param local_path path relative to srcdir
127   */
128  std::string filename(const std::string& local_path);
129
130  template<typename Iterator1, typename Iterator2>
131  bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
132                          unsigned int N)
133  {
134    while (first1!=last1){
135      if (!this->equal(*first1, *first2, N) )  {
136        return false;
137      }
138      ++first1;
139      ++first2;
140    }
141    return true;
142  }
143
144
145  template<class T>
146  bool Suite::test_stream(const T& t) const
147  {
148    this->err() << "Checking that output stream is valid as an input stream\n";
149    std::stringstream ss;
150    this->err() << "writing to output\n";
151    ss << t;
152    this->err() << "creating a new object from output\n";
153    T t2(ss);
154    std::stringstream ss2;
155    this->err() << "writing to output\n";
156    ss2 << t2;
157    bool ok = ss2.str()==ss.str();
158    if (!ok) {
159      this->err() << "ERROR: first object gave following output:\n" 
160                  << ss.str() << "\n"
161                  << "ERROR: and second object gave following output:\n" 
162                  << ss2.str() << "\n";
163    }
164    return ok;
165  }
166
167}}}
168
169#endif
Note: See TracBrowser for help on using the repository browser.