libDwm-0.6.0
DwmUnitAssert.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmUnitAssert.hh 8401 $
3 // @(#) $Id: DwmUnitAssert.hh 8401 2016-04-17 06:44:31Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2006-2007
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
42 
43 #ifndef _DWMUNITASSERT_HH_
44 #define _DWMUNITASSERT_HH_
45 
46 extern "C" {
47 #include <inttypes.h>
48 }
49 
50 #include <iostream>
51 #include <map>
52 #include <mutex>
53 #include <string>
54 
55 namespace Dwm {
56 
57  //--------------------------------------------------------------------------
59  //--------------------------------------------------------------------------
61  {
62  public:
63  //------------------------------------------------------------------------
65  //------------------------------------------------------------------------
67 
68  //------------------------------------------------------------------------
70  //------------------------------------------------------------------------
71  uint64_t Failed() const;
72 
73  //------------------------------------------------------------------------
75  //------------------------------------------------------------------------
76  uint64_t Passed() const;
77 
78  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
81  void AddFailed();
82 
83  //------------------------------------------------------------------------
85  //------------------------------------------------------------------------
86  void AddPassed();
87 
88  //------------------------------------------------------------------------
90  //------------------------------------------------------------------------
91  AssertionCounter & operator += (const AssertionCounter & assertionCounter);
92 
93  //------------------------------------------------------------------------
95  //------------------------------------------------------------------------
96  friend std::ostream &
97  operator << (std::ostream & os, const AssertionCounter & assertionCounter);
98 
99  private:
100  uint64_t _passed;
101  uint64_t _failed;
102  };
103 
104  //--------------------------------------------------------------------------
106  //--------------------------------------------------------------------------
107  class Assertion
108  {
109  public:
110  //------------------------------------------------------------------------
112  //------------------------------------------------------------------------
113  Assertion(const std::string & filename, const std::string & function,
114  int line, const std::string & expression);
115 
116  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
119  const std::string & Filename();
120 
121  //------------------------------------------------------------------------
123  //------------------------------------------------------------------------
124  const std::string & Function();
125 
126  //------------------------------------------------------------------------
128  //------------------------------------------------------------------------
129  int Line() const;
130 
131  //------------------------------------------------------------------------
133  //------------------------------------------------------------------------
134  const std::string & Expression() const;
135 
136  //------------------------------------------------------------------------
138  //------------------------------------------------------------------------
139  bool operator < (const Assertion & assertion) const;
140 
141  //------------------------------------------------------------------------
143  //------------------------------------------------------------------------
144  friend std::ostream &
145  operator << (std::ostream & os, const Assertion & assertion);
146 
147  private:
148  std::string _filename;
149  std::string _function;
150  int _line;
151  std::string _expression;
152  };
153 
154  //--------------------------------------------------------------------------
157  //--------------------------------------------------------------------------
159  {
160  public:
161  //------------------------------------------------------------------------
163  //------------------------------------------------------------------------
164  void Failed(const std::string & filename, const std::string & function,
165  int lineNumber, const std::string & test);
166 
167  //------------------------------------------------------------------------
169  //------------------------------------------------------------------------
170  void Passed(const std::string & filename, const std::string & function,
171  int lineNumber, const std::string & test);
172 
173  //------------------------------------------------------------------------
175  //------------------------------------------------------------------------
176  AssertionCounter Total() const;
177 
178  //------------------------------------------------------------------------
180  //------------------------------------------------------------------------
181  std::ostream & Print(std::ostream & os, bool onlyFailed = false) const;
182 
183  std::ostream & PrintXML(std::ostream & os, bool onlyFailed = false) const;
184 
185  private:
186  std::string _function;
187  std::map<Assertion,AssertionCounter> _assertions;
188  };
189 
190  //--------------------------------------------------------------------------
193  //--------------------------------------------------------------------------
195  {
196  public:
197  //------------------------------------------------------------------------
199  //------------------------------------------------------------------------
200  void Failed(const std::string & filename, const std::string & function,
201  int lineNumber, const std::string & test);
202 
203  //------------------------------------------------------------------------
205  //------------------------------------------------------------------------
206  void Passed(const std::string & filename, const std::string & function,
207  int lineNumber, const std::string & test);
208 
209  //------------------------------------------------------------------------
211  //------------------------------------------------------------------------
212  AssertionCounter Total() const;
213 
214  //------------------------------------------------------------------------
216  //------------------------------------------------------------------------
217  std::ostream & Print(std::ostream & os, bool onlyFailed = false) const;
218 
219  //------------------------------------------------------------------------
221  //------------------------------------------------------------------------
222  std::ostream & PrintXML(std::ostream & os, bool onlyFailed = false) const;
223 
224  private:
225  std::string _filename;
226  std::map<std::string,FunctionAssertions> _assertions;
227  };
228 
229  //--------------------------------------------------------------------------
231  //--------------------------------------------------------------------------
233  {
234  public:
235  //------------------------------------------------------------------------
238  //------------------------------------------------------------------------
239  static bool Failed(const std::string & filename,
240  const std::string & function,
241  int lineNumber, const std::string & test);
242 
243  //------------------------------------------------------------------------
246  //------------------------------------------------------------------------
247  static bool Passed(const std::string & filename,
248  const std::string & function,
249  int lineNumber, const std::string & test);
250 
251  //------------------------------------------------------------------------
253  //------------------------------------------------------------------------
254  static AssertionCounter Total();
255 
256  //------------------------------------------------------------------------
259  //------------------------------------------------------------------------
260  static std::ostream & Print(std::ostream & os, bool onlyFailed = false);
261 
262  //------------------------------------------------------------------------
264  //------------------------------------------------------------------------
265  static std::ostream & PrintXML(std::ostream & os, bool onlyFailed = false);
266 
267  private:
268  static std::map<std::string,FileAssertions> _assertions;
269  static std::mutex _mutex;
270  };
271 
272  //--------------------------------------------------------------------------
276  //--------------------------------------------------------------------------
277 #define UnitAssert(e) ((e) ? \
278  Dwm::Assertions::Passed(__FILE__, __PRETTY_FUNCTION__, __LINE__, #e) : \
279  Dwm::Assertions::Failed(__FILE__, __PRETTY_FUNCTION__, __LINE__, #e))
280 
281 } // namespace Dwm
282 
283 #endif // _DWMUNITASSERT_HH_
284 
285 //---------------------------- emacs settings -----------------------------
286 // Local Variables:
287 // mode: C++/la
288 // tab-width: 2
289 // indent-tabs-mode: nil
290 // c-basic-offset: 2
291 // End:
292 //-------------------------------------------------------------------------
void AddPassed()
Increments the passed counter.
A container to hold all assertions. Note everything is static here.
Definition: DwmUnitAssert.hh:232
uint64_t Passed() const
Returns the passed counter.
friend std::ostream & operator<<(std::ostream &os, const AssertionCounter &assertionCounter)
Prints to an ostream.
Definition: DwmBZ2IO.hh:67
void AddFailed()
Increments the failed counter.
A container class to hold all assertions called from a single source file.
Definition: DwmUnitAssert.hh:194
Holds assertion information.
Definition: DwmUnitAssert.hh:107
AssertionCounter()
Constructor.
uint64_t Failed() const
Returns the failed counter.
A container class to hold all assertions called from a single function.
Definition: DwmUnitAssert.hh:158
AssertionCounter & operator+=(const AssertionCounter &assertionCounter)
operator +=
Holds &#39;passed&#39; and &#39;failed&#39; counters.
Definition: DwmUnitAssert.hh:60