libDwm-0.9.45
DwmUnitAssert.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2006-2007, 2021
5// 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
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
40//---------------------------------------------------------------------------
41
42#ifndef _DWMUNITASSERT_HH_
43#define _DWMUNITASSERT_HH_
44
45#include <cstdint>
46#include <iostream>
47#include <map>
48#include <mutex>
49#include <string>
50
51namespace Dwm {
52
53 //--------------------------------------------------------------------------
55 //--------------------------------------------------------------------------
57 {
58 public:
59 //------------------------------------------------------------------------
61 //------------------------------------------------------------------------
63
64 //------------------------------------------------------------------------
66 //------------------------------------------------------------------------
67 uint64_t Failed() const;
68
69 //------------------------------------------------------------------------
71 //------------------------------------------------------------------------
72 uint64_t Passed() const;
73
74 //------------------------------------------------------------------------
76 //------------------------------------------------------------------------
77 void AddFailed();
78
79 //------------------------------------------------------------------------
81 //------------------------------------------------------------------------
82 void AddPassed();
83
84 //------------------------------------------------------------------------
86 //------------------------------------------------------------------------
87 AssertionCounter & operator += (const AssertionCounter & assertionCounter);
88
89 //------------------------------------------------------------------------
91 //------------------------------------------------------------------------
92 friend std::ostream &
93 operator << (std::ostream & os, const AssertionCounter & assertionCounter);
94
95 private:
96 uint64_t _passed;
97 uint64_t _failed;
98 };
99
100 //--------------------------------------------------------------------------
102 //--------------------------------------------------------------------------
104 {
105 public:
106 //------------------------------------------------------------------------
108 //------------------------------------------------------------------------
109 Assertion(const std::string & filename, const std::string & function,
110 int line, const std::string & expression);
111
112 //------------------------------------------------------------------------
114 //------------------------------------------------------------------------
115 const std::string & Filename();
116
117 //------------------------------------------------------------------------
119 //------------------------------------------------------------------------
120 const std::string & Function();
121
122 //------------------------------------------------------------------------
124 //------------------------------------------------------------------------
125 int Line() const;
126
127 //------------------------------------------------------------------------
129 //------------------------------------------------------------------------
130 const std::string & Expression() const;
131
132 //------------------------------------------------------------------------
134 //------------------------------------------------------------------------
135 bool operator < (const Assertion & assertion) const;
136
137 //------------------------------------------------------------------------
139 //------------------------------------------------------------------------
140 friend std::ostream &
141 operator << (std::ostream & os, const Assertion & assertion);
142
143 private:
144 std::string _filename;
145 std::string _function;
146 int _line;
147 std::string _expression;
148 };
149
150 //--------------------------------------------------------------------------
153 //--------------------------------------------------------------------------
155 {
156 public:
157 //------------------------------------------------------------------------
159 //------------------------------------------------------------------------
160 void Failed(const std::string & filename, const std::string & function,
161 int lineNumber, const std::string & test);
162
163 //------------------------------------------------------------------------
165 //------------------------------------------------------------------------
166 void Passed(const std::string & filename, const std::string & function,
167 int lineNumber, const std::string & test);
168
169 //------------------------------------------------------------------------
171 //------------------------------------------------------------------------
173
174 //------------------------------------------------------------------------
176 //------------------------------------------------------------------------
177 std::ostream & Print(std::ostream & os, bool onlyFailed = false) const;
178
179 //------------------------------------------------------------------------
181 //------------------------------------------------------------------------
182 std::ostream &
183 PrintJson(std::ostream & os, bool onlyFailed = false) const;
184
185 //------------------------------------------------------------------------
187 //------------------------------------------------------------------------
188 std::ostream &
189 PrintXML(std::ostream & os, bool onlyFailed = false) const;
190
191 private:
192 std::string _function;
193 std::map<Assertion,AssertionCounter> _assertions;
194 };
195
196 //--------------------------------------------------------------------------
199 //--------------------------------------------------------------------------
201 {
202 public:
203 //------------------------------------------------------------------------
205 //------------------------------------------------------------------------
206 void Failed(const std::string & filename, const std::string & function,
207 int lineNumber, const std::string & test);
208
209 //------------------------------------------------------------------------
211 //------------------------------------------------------------------------
212 void Passed(const std::string & filename, const std::string & function,
213 int lineNumber, const std::string & test);
214
215 //------------------------------------------------------------------------
217 //------------------------------------------------------------------------
219
220 //------------------------------------------------------------------------
222 //------------------------------------------------------------------------
223 std::ostream & Print(std::ostream & os, bool onlyFailed = false) const;
224
225 //------------------------------------------------------------------------
227 //------------------------------------------------------------------------
228 std::ostream & PrintJson(std::ostream & os, bool onlyFailed = false) const;
229
230 //------------------------------------------------------------------------
232 //------------------------------------------------------------------------
233 std::ostream & PrintXML(std::ostream & os, bool onlyFailed = false) const;
234
235 private:
236 std::string _filename;
237 std::map<std::string,FunctionAssertions> _assertions;
238 };
239
240 //--------------------------------------------------------------------------
242 //--------------------------------------------------------------------------
244 {
245 public:
246 //------------------------------------------------------------------------
249 //------------------------------------------------------------------------
250 static bool Failed(const std::string & filename,
251 const std::string & function,
252 int lineNumber, const std::string & test);
253
254 //------------------------------------------------------------------------
257 //------------------------------------------------------------------------
258 static bool Passed(const std::string & filename,
259 const std::string & function,
260 int lineNumber, const std::string & test);
261
262 //------------------------------------------------------------------------
264 //------------------------------------------------------------------------
266
267 //------------------------------------------------------------------------
270 //------------------------------------------------------------------------
271 static std::ostream & Print(std::ostream & os, bool onlyFailed = false);
272
273 //------------------------------------------------------------------------
279 //------------------------------------------------------------------------
280 static int PrintAndReturn();
281
282 //------------------------------------------------------------------------
284 //------------------------------------------------------------------------
285 static std::ostream & PrintJson(std::ostream & os,
286 bool onlyFailed = false);
287
288 //------------------------------------------------------------------------
290 //------------------------------------------------------------------------
291 static std::ostream & PrintXML(std::ostream & os, bool onlyFailed = false);
292
293 private:
294 static std::map<std::string,FileAssertions> _assertions;
295 static std::mutex _mutex;
296 };
297
298 //--------------------------------------------------------------------------
302 //--------------------------------------------------------------------------
303#define UnitAssert(e) ((e) ? \
304 Dwm::Assertions::Passed(__FILE__, __PRETTY_FUNCTION__, __LINE__, #e) : \
305 Dwm::Assertions::Failed(__FILE__, __PRETTY_FUNCTION__, __LINE__, #e))
306
307} // namespace Dwm
308
309#endif // _DWMUNITASSERT_HH_
310
311//---------------------------- emacs settings -----------------------------
312// Local Variables:
313// mode: C++
314// tab-width: 2
315// indent-tabs-mode: nil
316// c-basic-offset: 2
317// End:
318//-------------------------------------------------------------------------
Holds 'passed' and 'failed' counters.
Definition DwmUnitAssert.hh:57
AssertionCounter()
Constructor.
void AddFailed()
Increments the failed counter.
uint64_t Passed() const
Returns the passed counter.
AssertionCounter & operator+=(const AssertionCounter &assertionCounter)
operator +=
void AddPassed()
Increments the passed counter.
friend std::ostream & operator<<(std::ostream &os, const AssertionCounter &assertionCounter)
Prints to an ostream.
uint64_t Failed() const
Returns the failed counter.
Holds assertion information.
Definition DwmUnitAssert.hh:104
const std::string & Filename()
Returns the name of the file where the assertion is located.
friend std::ostream & operator<<(std::ostream &os, const Assertion &assertion)
Prints to an ostream.
const std::string & Expression() const
Returns the assertion test expression.
const std::string & Function()
Returns the function where the assertion is located.
int Line() const
Returns the line number where the assertion is located.
Assertion(const std::string &filename, const std::string &function, int line, const std::string &expression)
Constructor.
bool operator<(const Assertion &assertion) const
operator <
A container to hold all assertions. Note everything is static here.
Definition DwmUnitAssert.hh:244
static int PrintAndReturn()
Prints failed tests to cerr and successful test count to cout in a simple form that I use in unit tes...
static bool Failed(const std::string &filename, const std::string &function, int lineNumber, const std::string &test)
Adds a failed assertion.
static std::ostream & Print(std::ostream &os, bool onlyFailed=false)
Prints to an ostream.
static AssertionCounter Total()
Returns the total passed and failed counter.
static bool Passed(const std::string &filename, const std::string &function, int lineNumber, const std::string &test)
Adds a passed assertion.
static std::ostream & PrintXML(std::ostream &os, bool onlyFailed=false)
Prints to an ostream in XML format.
static std::ostream & PrintJson(std::ostream &os, bool onlyFailed=false)
Prints to an ostream in JSON format.
A container class to hold all assertions called from a single source file.
Definition DwmUnitAssert.hh:201
void Failed(const std::string &filename, const std::string &function, int lineNumber, const std::string &test)
Add a failed assertion.
void Passed(const std::string &filename, const std::string &function, int lineNumber, const std::string &test)
Add a passed assertion.
std::ostream & PrintJson(std::ostream &os, bool onlyFailed=false) const
Prints JSON to an ostream.
AssertionCounter Total() const
Returns the total passed and failed counter.
std::ostream & PrintXML(std::ostream &os, bool onlyFailed=false) const
Prints XML to an ostream.
std::ostream & Print(std::ostream &os, bool onlyFailed=false) const
Prints to an ostream.
A container class to hold all assertions called from a single function.
Definition DwmUnitAssert.hh:155
void Failed(const std::string &filename, const std::string &function, int lineNumber, const std::string &test)
Add a failed assertion.
std::ostream & PrintJson(std::ostream &os, bool onlyFailed=false) const
Prints to an ostream in JSON format.
std::ostream & PrintXML(std::ostream &os, bool onlyFailed=false) const
Prints to an ostream in XML format.
AssertionCounter Total() const
Returns the total passed and failed counter.
std::ostream & Print(std::ostream &os, bool onlyFailed=false) const
Prints to an ostream.
void Passed(const std::string &filename, const std::string &function, int lineNumber, const std::string &test)
Add a passed assertion.