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