GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 _______________________________________________ */
31 class GTestSuite;
32 
33 /* __ Test suite pointer _________________________________________________ */
34 typedef 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  ***************************************************************************/
53 class GTestSuite : public GBase {
54 
55 public:
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 
123 protected:
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  ***************************************************************************/
147 inline
148 int GTestSuite::size(void) const
149 {
150  return (int)m_tests.size();
151 }
152 
153 
154 /***********************************************************************//**
155  * @brief Return test suite name
156  ***************************************************************************/
157 inline
158 const 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  ***************************************************************************/
169 inline
170 void 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  ***************************************************************************/
184 inline
185 void 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  ***************************************************************************/
197 inline
198 const 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  ***************************************************************************/
209 inline
210 const 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  ***************************************************************************/
223 inline
224 const time_t& GTestSuite::timestamp(void) const
225 {
226  return m_timestamp;
227 }
228 
229 #endif /* GTESTSUITE_HPP */
std::string print(const GChatter &chatter=NORMAL) const
Print test suite information.
Definition: GTestSuite.cpp:971
void append(pfunction function, const std::string &name)
Append test functions to test suite.
Definition: GTestSuite.cpp:234
void test_assert(const bool &result, const std::string &name, const std::string &message="")
Test an assert.
Definition: GTestSuite.cpp:368
const int & errors(void) const
Return the number of errors.
Definition: GTestSuite.hpp:198
int success(void) const
Return the number of successful tests.
Definition: GTestSuite.cpp:938
int m_index
Index of actual test function.
Definition: GTestSuite.hpp:136
Test class.
Definition: GTestCase.hpp:47
void cout(const bool &flag)
Enables/disables logging into standard output stream.
Definition: GTestSuite.hpp:185
double duration(void) const
Return the total duration of all tests.
Definition: GTestSuite.cpp:950
void init_members(void)
Initialise class members.
void test_value(const int &value, const int &expected, const std::string &name="", const std::string &message="")
Test an integer value.
Definition: GTestSuite.cpp:406
void cout(const bool &flag)
Set standard output stream (cout) flag.
Definition: GLog.hpp:275
virtual ~GTestSuite(void)
Destructor.
Definition: GTestSuite.cpp:117
GException::test_error & exception_error(const std::string &message)
Return an error exception.
Definition: GTestSuite.cpp:928
Information logger interface definition.
Definition: GLog.hpp:62
Test case class interface definition.
void test_try(const std::string &name)
Test an try block.
Definition: GTestSuite.cpp:727
void test_try_success(void)
Notice when a try block succeeded.
Definition: GTestSuite.cpp:762
std::vector< pfunction > m_functions
Test functions of this suite.
Definition: GTestSuite.hpp:133
void free_members(void)
Delete class members.
int m_errors
Number of errors.
Definition: GTestSuite.hpp:138
GTestSuite & operator=(const GTestSuite &testsuite)
Assignment operator.
Definition: GTestSuite.cpp:139
std::string format_name(const std::string &name)
Format Name.
GTestCase & operator[](const int &index)
Returns reference to test case.
Definition: GTestSuite.cpp:168
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
virtual std::string classname(void) const =0
Return class name.
void copy_members(const GTestSuite &testsuite)
Copy class members.
std::vector< std::string > m_names
Names of test functions.
Definition: GTestSuite.hpp:132
bool run(void)
Run all tests in test suite.
Definition: GTestSuite.cpp:253
GChatter
Definition: GTypemaps.hpp:33
Abstract test suite class for unit testing on GammaLib fixtures.
Definition: GTestSuite.hpp:53
time_t m_timestamp
Timestamp.
Definition: GTestSuite.hpp:140
const std::string & name(void) const
Return test suite name.
Definition: GTestSuite.hpp:158
int size(void) const
Return number of tests in test suite.
Definition: GTestSuite.hpp:148
GException::test_failure & exception_failure(const std::string &message)
Return a failure exception.
Definition: GTestSuite.cpp:901
int m_failures
Number of failures.
Definition: GTestSuite.hpp:137
const time_t & timestamp(void) const
Return the timestamp.
Definition: GTestSuite.hpp:224
void(GTestSuite::* pfunction)(void)
Definition: GTestSuite.hpp:34
Information logger class definition.
virtual void set(void)=0
GTestSuite(void)
Void constructor.
Definition: GTestSuite.cpp:68
Definition of GammaLib typemaps.
GLog m_log
Log.
Definition: GTestSuite.hpp:139
const int & failures(void) const
Return the number of failures.
Definition: GTestSuite.hpp:210
std::string m_name
Name of the test suite.
Definition: GTestSuite.hpp:131
std::vector< GTestCase * > m_stack_try
Stack for nested try blocks.
Definition: GTestSuite.hpp:135
Exception handler interface definition.
void clear(void)
Clear test suite.
Definition: GTestSuite.cpp:212
std::vector< GTestCase * > m_tests
List of test results.
Definition: GTestSuite.hpp:134
virtual GTestSuite * clone(void) const =0
Clones object.
void test_try_failure(const std::string &message="", const std::string &type="")
Notice when a try block failed.
Definition: GTestSuite.cpp:809