UnitTest.h Source File

Back to the index.

UnitTest.h
Go to the documentation of this file.
1 #ifndef UNITTEST_H
2 #define UNITTEST_H
3 
4 /*
5  * Copyright (C) 2007-2010 Anders Gavare. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include "misc.h"
32 
33 #include <exception>
34 
35 
36 /**
37  * \brief An exception thrown by unit test cases that fail.
38  */
40  : public std::exception
41 {
42 public:
43  UnitTestFailedException(const string& strMessage)
44  : m_strMessage(strMessage)
45  {
46  }
47 
48  virtual ~UnitTestFailedException() throw ()
49  {
50  }
51 
52  /**
53  * \brief Retrieves the error message associated with the exception.
54  *
55  * @return a const reference to the error message string
56  */
57  const string& GetMessage() const
58  {
59  return m_strMessage;
60  }
61 
62 private:
63  string m_strMessage;
64 };
65 
66 
67 /**
68  * \brief Base class for unit testable classes.
69  *
70  * A class which inherits from the UnitTestable class exposes a function,
71  * RunUnitTests(int&, int&), which runs unit tests, and returns the number
72  * of failed test cases.
73  */
75 {
76 public:
77  /**
78  * \brief Runs unit test cases.
79  *
80  * @param nSucceeded A reference to a variable which keeps count
81  * of the number of succeeded test cases.
82  * @param nFailures A reference to a variable which keeps count
83  * of the number of failed test cases.
84  */
85  static void RunUnitTests(int& nSucceeded, int& nFailures);
86 };
87 
88 
89 /**
90  * \brief A collection of helper functions, for writing simple unit tests.
91  */
92 class UnitTest
93 {
94 public:
95  /**
96  * \brief Runs all unit tests in %GXemul.
97  *
98  * If WITHUNITTESTS was not defined in config.h, nothing is tested,
99  * and zero is returned.
100  *
101  * Otherwise, unit tests for all classes that the <tt>configure</tt>
102  * script detected as using UNITTESTS(classname) are executed.
103  *
104  * If a test fails, the UNITTEST(testname) macro in the unit testing
105  * framework takes care of outputting a line identifying that test to
106  * std::cerr.
107  *
108  * @return zero if no unit tests failed, 1 otherwise.
109  */
110  static int RunTests();
111 
112 
113  /*******************************************************************/
114  /* Helper functions, used by unit test cases */
115  /*******************************************************************/
116 
117  /**
118  * \brief Asserts that a boolean condition is correct.
119  *
120  * If the condition is not true, then Fail is called with the failure
121  * message.
122  *
123  * The failure message can be something like "expected xyz",
124  * or "the list should be empty at this point".
125  *
126  * @param strFailMessage failure message to print to std::cerr
127  * @param condition The result of some operation, which should be true.
128  */
129  static void Assert(const string& strFailMessage, bool condition);
130 
131  /**
132  * \brief Asserts that two uint64_t values are equal.
133  *
134  * If the values are not equal, Fail is called with the failure message.
135  *
136  * The failure message can be something like "expected xyz",
137  * or "the list should be empty at this point".
138  *
139  * @param strFailMessage Failure message to print to std::cerr.
140  * @param actualValue The actual value.
141  * @param expectedValue The expected value.
142  */
143  static void Assert(const string& strFailMessage,
144  uint64_t actualValue, uint64_t expectedValue);
145 
146  /**
147  * \brief Asserts that two string values are equal.
148  *
149  * If the values are not equal, Fail is called with the failure message.
150  *
151  * The failure message can be something like "expected xyz",
152  * or "the list should be empty at this point".
153  *
154  * @param strFailMessage Failure message to print to std::cerr.
155  * @param actualValue The actual value.
156  * @param expectedValue The expected value.
157  */
158  static void Assert(const string& strFailMessage,
159  const string& actualValue, const string& expectedValue);
160 
161  /**
162  * \brief Fails a unit test unconditionally, by throwing a
163  * UnitTestFailedException.
164  *
165  * @param strMessage failure message
166  */
167  static void Fail(const string& strMessage);
168 };
169 
170 
171 #ifdef WITHUNITTESTS
172 #include <iostream>
173 /**
174  * \brief Helper for unit test case execution.
175  *
176  * The main purpose, appart from making the code look more compact,
177  * of having a macro for this is that the <tt>configure</tt> script finds
178  * all .cc files that use this macro, and generates lists of header files
179  * to include and lists of RunUnitTests functions to call. These are then
180  * included and run from UnitTest::RunTests.
181  *
182  * See the comment for UNITTEST for details on how to use it.
183  */
184 #define UNITTESTS(class) \
185  void class::RunUnitTests(int& nSucceeded, int& nFailures)
186 
187 /**
188  * \brief Helper for unit test case execution.
189  *
190  * For each test case that throws a UnitTestFailedException, the number
191  * of failures is increased. For test cases that don't fail, the number
192  * of successful test cases is increased instead.
193  *
194  * Usage: (usually at the end of a class implementation file)<pre>
195  * \#ifdef WITHUNITTESTS
196  *
197  * static void MyClass::Test_MyClass_SomeTest()
198  * {
199  * UnitTest::Assert("expected blah blah", bool_condition);
200  * ... more asserts here ...
201  * }
202  *
203  * ...
204  *
205  * UNITTESTS(MyClass)
206  * {
207  * UNITTEST(Test_MyClass_SomeTest);
208  * UNITTEST(Test_MyClass_AnotherTest);
209  * ... more test cases here ...
210  * }
211  *
212  * \#endif // WITHUNITTESTS</pre>
213  *
214  * Note that MyClass (in the example above) should inherit from the
215  * UnitTestable class.
216  */
217 #define UNITTEST(functionname) try { \
218  std::cout << "### " #functionname "\n"; \
219  (functionname)(); \
220  ++ (nSucceeded); \
221 } catch (UnitTestFailedException& ex) { \
222  std::cout.flush(); \
223  std::cerr << "\n### " #functionname " (" __FILE__ " line " \
224  << __LINE__ << ") failed!\n" \
225  " > " << ex.GetMessage() << "\n"; \
226  std::cerr.flush(); \
227  ++ (nFailures); \
228 }
229 
230 #endif
231 
232 
233 #endif // UNITTEST_H
const string & GetMessage() const
Retrieves the error message associated with the exception.
Definition: UnitTest.h:57
virtual ~UnitTestFailedException()
Definition: UnitTest.h:48
An exception thrown by unit test cases that fail.
Definition: UnitTest.h:39
UnitTestFailedException(const string &strMessage)
Definition: UnitTest.h:43
A collection of helper functions, for writing simple unit tests.
Definition: UnitTest.h:92
Base class for unit testable classes.
Definition: UnitTest.h:74

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13