GammaLib 2.0.0
Loading...
Searching...
No Matches
GTestSuite.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GTestSuite.hpp - Abstract test suite base class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2012-2016 by Jean-Baptiste Cayrou *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GTestSuite.hpp
23 * @brief Abstract test suite base class definition
24 * @author Jean-Baptiste Cayrou
25 */
26
27#ifndef GTESTSUITE_HPP
28#define GTESTSUITE_HPP
29
30/* __ Forward declarations _______________________________________________ */
31class GTestSuite;
32
33/* __ Test suite pointer _________________________________________________ */
34typedef void (GTestSuite::*pfunction)(void);
35
36/* __ Includes ___________________________________________________________ */
37#include <complex>
38#include <vector>
39#include <string>
40#include "GLog.hpp"
41#include "GException.hpp"
42#include "GTestCase.hpp"
43#include "GTypemaps.hpp"
44
45
46/***********************************************************************//**
47 * @class GTestSuite
48 *
49 * @brief Abstract test suite class for unit testing on GammaLib fixtures
50 *
51 * @todo Detailed explanation.
52 ***************************************************************************/
53class GTestSuite : public GBase {
54
55public:
56 // Constructors and destructors
57 GTestSuite(void);
58 GTestSuite(const GTestSuite& testsuite);
59 GTestSuite(const std::string& name);
60 virtual ~GTestSuite(void);
61
62 // Operators
63 GTestSuite& operator=(const GTestSuite& testsuite);
64 GTestCase& operator[](const int& index);
65 const GTestCase& operator[](const int& index) const;
66
67 // Pure virtual methods
68 virtual GTestSuite* clone(void) const = 0;
69 virtual std::string classname(void) const = 0;
70 virtual void set(void) = 0;
71
72 // Other methods
73 void clear(void);
74 int size(void) const;
75 void append(pfunction function, const std::string& name);
76 bool run(void);
77 const std::string& name(void) const;
78 void name(const std::string& name);
79 void cout(const bool& flag);
80 void test_assert(const bool& result,
81 const std::string& name,
82 const std::string& message = "");
83 void test_value(const int& value,
84 const int& expected,
85 const std::string& name = "",
86 const std::string& message = "");
87 void test_value(const double& value,
88 const double& expected,
89 const std::string& name = "",
90 const std::string& message = "");
91 void test_value(const double& value,
92 const double& expected,
93 const double& eps,
94 const std::string& name = "",
95 const std::string& message = "");
96 void test_value(const std::complex<double>& value,
97 const std::complex<double>& expected,
98 const std::string& name = "",
99 const std::string& message = "");
100 void test_value(const std::complex<double>& value,
101 const std::complex<double>& expected,
102 const double& eps,
103 const std::string& name = "",
104 const std::string& message = "");
105 void test_value(const std::string& value,
106 const std::string& expected,
107 const std::string& name = "",
108 const std::string& message = "");
109 void test_try(const std::string& name);
110 void test_try_success(void);
111 void test_try_failure(const std::string& message = "",
112 const std::string& type = "");
113 void test_try_failure(const std::exception& e);
114 GException::test_failure& exception_failure(const std::string& message);
115 GException::test_error& exception_error(const std::string& message);
116 const int& errors(void) const;
117 const int& failures(void) const;
118 int success(void) const;
119 const time_t& timestamp(void) const;
120 double duration(void) const;
121 std::string print(const GChatter& chatter = NORMAL) const;
122
123protected:
124 // Protected methods
125 void init_members(void);
126 void copy_members(const GTestSuite& testsuite);
127 void free_members(void);
128 std::string format_name(const std::string& name);
129
130 // Protected members
131 std::string m_name; //!< Name of the test suite
132 std::vector<std::string> m_names; //!< Names of test functions
133 std::vector<pfunction> m_functions; //!< Test functions of this suite
134 std::vector<GTestCase*> m_tests; //!< List of test results
135 std::vector<GTestCase*> m_stack_try; //!< Stack for nested try blocks
136 int m_index; //!< Index of actual test function
137 int m_failures; //!< Number of failures
138 int m_errors; //!< Number of errors
139 GLog m_log; //!< Log
140 time_t m_timestamp; //!< Timestamp
141};
142
143
144/***********************************************************************//**
145 * @brief Return number of tests in test suite
146 ***************************************************************************/
147inline
148int GTestSuite::size(void) const
149{
150 return (int)m_tests.size();
151}
152
153
154/***********************************************************************//**
155 * @brief Return test suite name
156 ***************************************************************************/
157inline
158const std::string& GTestSuite::name(void) const
159{
160 return m_name;
161}
162
163
164/***********************************************************************//**
165 * @brief Set Test Suite name
166 *
167 * @param[in] name Test suite name.
168 ***************************************************************************/
169inline
170void GTestSuite::name(const std::string& name)
171{
172 m_name = name;
173 return;
174}
175
176
177/***********************************************************************//**
178 * @brief Enables/disables logging into standard output stream
179 *
180 * @param[in] flag Enable/disable logging (true/false).
181 *
182 * Enables or disables logging into the standard output stream.
183 ***************************************************************************/
184inline
185void GTestSuite::cout(const bool& flag)
186{
187 m_log.cout(flag);
188 return;
189}
190
191
192/***********************************************************************//**
193 * @brief Return the number of errors
194 *
195 * @return Number of errors.
196 ***************************************************************************/
197inline
198const int& GTestSuite::errors(void) const
199{
200 return m_errors;
201}
202
203
204/***********************************************************************//**
205 * @brief Return the number of failures
206 *
207 * @return Number of failures.
208 ***************************************************************************/
209inline
210const int& GTestSuite::failures(void) const
211{
212 return m_failures;
213}
214
215
216/***********************************************************************//**
217 * @brief Return the timestamp
218 *
219 * @return Timestamp.
220 *
221 * The timestamp is set at the construction of the object.
222 ***************************************************************************/
223inline
224const time_t& GTestSuite::timestamp(void) const
225{
226 return m_timestamp;
227}
228
229#endif /* GTESTSUITE_HPP */
Exception handler interface definition.
Information logger class definition.
Test case class interface definition.
void(GTestSuite::* pfunction)(void)
Definition of GammaLib typemaps.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Information logger interface definition.
Definition GLog.hpp:62
void cout(const bool &flag)
Set standard output stream (cout) flag.
Definition GLog.hpp:275
Test class.
Definition GTestCase.hpp:47
Abstract test suite class for unit testing on GammaLib fixtures.
void test_try_failure(const std::string &message="", const std::string &type="")
Notice when a try block failed.
bool run(void)
Run all tests in test suite.
void test_try_success(void)
Notice when a try block succeeded.
void clear(void)
Clear test suite.
GException::test_failure & exception_failure(const std::string &message)
Return a failure exception.
int m_errors
Number of errors.
const int & failures(void) const
Return the number of failures.
void test_value(const int &value, const int &expected, const std::string &name="", const std::string &message="")
Test an integer value.
void copy_members(const GTestSuite &testsuite)
Copy class members.
std::vector< GTestCase * > m_tests
List of test results.
std::vector< pfunction > m_functions
Test functions of this suite.
void test_assert(const bool &result, const std::string &name, const std::string &message="")
Test an assert.
int m_failures
Number of failures.
void init_members(void)
Initialise class members.
void free_members(void)
Delete class members.
void append(pfunction function, const std::string &name)
Append test functions to test suite.
std::vector< GTestCase * > m_stack_try
Stack for nested try blocks.
GTestCase & operator[](const int &index)
Returns reference to test case.
virtual void set(void)=0
time_t m_timestamp
Timestamp.
virtual std::string classname(void) const =0
Return class name.
int size(void) const
Return number of tests in test suite.
std::string print(const GChatter &chatter=NORMAL) const
Print test suite information.
GTestSuite(void)
Void constructor.
GTestSuite & operator=(const GTestSuite &testsuite)
Assignment operator.
const int & errors(void) const
Return the number of errors.
std::string m_name
Name of the test suite.
void cout(const bool &flag)
Enables/disables logging into standard output stream.
virtual ~GTestSuite(void)
Destructor.
GException::test_error & exception_error(const std::string &message)
Return an error exception.
void test_try(const std::string &name)
Test an try block.
int success(void) const
Return the number of successful tests.
const time_t & timestamp(void) const
Return the timestamp.
GLog m_log
Log.
virtual GTestSuite * clone(void) const =0
Clones object.
int m_index
Index of actual test function.
std::string format_name(const std::string &name)
Format Name.
const std::string & name(void) const
Return test suite name.
double duration(void) const
Return the total duration of all tests.
std::vector< std::string > m_names
Names of test functions.