source: trunk/test/Suite.h @ 1360

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

working on ticket #214

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1#ifndef _theplu_yat_test_suite_
2#define _theplu_yat_test_suite_
3
4// $Id: Suite.h 1360 2008-06-26 01:17:58Z peter $
5
6/*
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include <fstream>
28#include <sstream>
29
30namespace theplu {
31namespace yat {
32namespace test {
33
34  /**
35     \internal utility class for tests
36   */
37  class Suite
38  {
39  public:
40    Suite(int argc, char* argv[]);
41
42    /**
43     */
44    ~Suite(void);
45
46    /**
47       set ok to 'b && ok'
48
49       \return b
50    */
51    bool add(bool b);
52
53    /**
54       \return In verbose mode std::cerr, else a ofstream to "/dev/null".
55    */
56    std::ostream& err(void) const;
57
58    /**
59       \return true if \f$ |a-b| <= N * \epsilon * min(|a|,|b|) \f$
60       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
61    */
62    bool equal(double a, double b, unsigned long int N=1);
63
64    /**
65       \return true if \f$ |a-b| <= N * sqrt(\epsilon) * min(|a|,|b|) \f$
66       where \f$ \epsilon \f$ is std::numeric_limits<double>().epsilon()
67    */
68    bool equal_sqrt(double a, double b, unsigned long int N=1);
69
70    template<typename Iterator1, typename Iterator2>
71    bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
72                     unsigned int N=1);
73
74    /**
75      \return true if test is ok
76    */
77    bool ok(void) const;
78
79    /**
80       \return In verbose mode std::cout, else a ofstream to "/dev/null".
81    */
82    std::ostream& out(void) const;
83   
84    /**
85       In verbose mode a final message is sent to std::cout.
86
87       If ok() is true: "Test is ok." otherwise
88       "Test failed."
89
90       \return 0 if ok.
91     */
92    int return_value(void) const;
93
94    /**
95       Function writes to a stream using operator<<, creates a new
96       object using stream constructor, and the new object is written
97       to another stream, and function check if the two outputs are
98       equal.
99    */
100    template<class T>
101    bool test_stream(const T&) const;
102
103  private:
104    bool verbose_;
105    bool ok_;
106    std::ofstream* dev_null_;
107
108  };
109
110  /**
111     \return absolute path to file
112     \param local_path path relative to srcdir
113   */
114  std::string filename(const std::string& local_path);
115
116  template<typename Iterator1, typename Iterator2>
117  bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2,
118                          unsigned int N)
119  {
120    while (first1!=last1){
121      if (!this->equal(*first1, *first2, N) )  {
122        return false;
123      }
124      ++first1;
125      ++first2;
126    }
127    return true;
128  }
129
130
131  template<class T>
132  bool Suite::test_stream(const T& t) const
133  {
134    this->err() << "Checking that output stream is valid as an input stream\n";
135    std::stringstream ss;
136    this->err() << "writing to output\n";
137    ss << t;
138    this->err() << "creating a new object from output\n";
139    T t2(ss);
140    std::stringstream ss2;
141    this->err() << "writing to output\n";
142    ss2 << t2;
143    bool ok = ss2.str()==ss.str();
144    if (!ok) {
145      this->err() << "ERROR: first object gave following output:\n" 
146                  << ss.str() << "\n"
147                  << "ERROR: and second object gave following output:\n" 
148                  << ss2.str() << "\n";
149    }
150    return ok;
151  }
152
153}}}
154
155#endif
Note: See TracBrowser for help on using the repository browser.