assertion.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2008 by Dr. Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef CXXTOOLS_ASSERTION_H
29 #define CXXTOOLS_ASSERTION_H
30 
31 #include "cxxtools/sourceinfo.h"
32 #include <stdexcept>
33 #include <iostream>
34 #include <sstream>
35 
36 namespace cxxtools {
37 
38 namespace unit {
39 
59  class Assertion
60  {
61  public:
72  Assertion(const std::string& what, const SourceInfo& si);
73 
74  const SourceInfo& sourceInfo() const;
75 
76  const char* what() const { return _what.c_str(); }
77 
78  private:
79  SourceInfo _sourceInfo;
80  std::string _what;
81  };
82 
83  class Skip
84  { };
85 
86  #define CXXTOOLS_UNIT_ASSERT(cond) \
87  do { \
88  if( !(cond) ) \
89  throw cxxtools::unit::Assertion(#cond, CXXTOOLS_SOURCEINFO); \
90  } while (false)
91 
92  #define CXXTOOLS_UNIT_ASSERT_MSG(cond, what) \
93  do { \
94  if( !(cond) ) \
95  { \
96  std::ostringstream _cxxtools_msg; \
97  _cxxtools_msg << what; \
98  throw cxxtools::unit::Assertion(_cxxtools_msg.str(), CXXTOOLS_SOURCEINFO); \
99  } \
100  } while (false)
101 
103  template <typename A, typename B>
104  void assertEquals(const char* cond1, const A& value1, const char* cond2, const B& value2, const SourceInfo& si)
105  {
106  if ( ! (value1 == value2) )
107  {
108  std::ostringstream _cxxtools_msg;
109  _cxxtools_msg << "not equal:\n\tvalue1 (" << cond1 << ")=\n\t\t<" << value1 << ">\n\tvalue2 (" << cond2 << ")=\n\t\t<" << value2 << '>';
110  throw cxxtools::unit::Assertion(_cxxtools_msg.str(), si);
111  }
112  }
113 
115  template <typename A, typename B, typename C>
116  void assertEquals(const char* cond1, const A& value1, const char* cond2, const B& value2, C outputFn, const SourceInfo& si)
117  {
118  if ( ! (value1 == value2) )
119  {
120  std::ostringstream _cxxtools_msg;
121  _cxxtools_msg << "not equal:\n\tvalue1 (" << cond1 << ")=\n\t\t<" << outputFn(value1) << ">\n\tvalue2 (" << cond2 << ")=\n\t\t<" << outputFn(value2) << '>';
122  throw cxxtools::unit::Assertion(_cxxtools_msg.str(), si);
123  }
124  }
125 
126  #define CXXTOOLS_UNIT_ASSERT_EQUALS(value1, value2) \
127  cxxtools::unit::assertEquals(#value1, (value1), #value2, (value2), CXXTOOLS_SOURCEINFO)
128 
129  #define CXXTOOLS_UNIT_ASSERT_EQUALS2(value1, value2, outputFn) \
130  cxxtools::unit::assertEquals(#value1, (value1), #value2, (value2), outputFn, CXXTOOLS_SOURCEINFO)
131 
132  #define CXXTOOLS_UNIT_ASSERT_THROW(cond, EX) \
133  do { \
134  try \
135  { \
136  cond; \
137  \
138  std::ostringstream _cxxtools_msg; \
139  _cxxtools_msg << "exception of type " #EX " expected in " #cond; \
140  throw cxxtools::unit::Assertion(_cxxtools_msg.str(), CXXTOOLS_SOURCEINFO); \
141  } \
142  catch(const EX &) \
143  {} \
144  } while (false)
145 
146  #define CXXTOOLS_UNIT_ASSERT_NOTHROW(cond) \
147  do { \
148  try { \
149  \
150  cond; \
151  } \
152  catch(const std::exception& e) \
153  { \
154  throw cxxtools::unit::Assertion( \
155  std::string("unexpected exception of type ") + typeid(e).name() + ": " + e.what(), \
156  CXXTOOLS_SOURCEINFO); \
157  } \
158  catch(const cxxtools::unit::Assertion&) \
159  { \
160  throw; \
161  } \
162  catch(...) \
163  { \
164  throw cxxtools::unit::Assertion("unexpected exception." , CXXTOOLS_SOURCEINFO); \
165  } \
166  } while (false)
167 
168  #define CXXTOOLS_UNIT_SKIP() throw cxxtools::unit::Skip()
169 
170  #define CXXTOOLS_UNIT_FAIL(what) \
171  do { \
172  std::ostringstream _cxxtools_msg; \
173  _cxxtools_msg << what; \
174  throw cxxtools::unit::Assertion(_cxxtools_msg.str(), CXXTOOLS_SOURCEINFO); \
175  } while (false)
176 
177 } // namespace unit
178 
179 } // namespace cxxtools
180 
181 #endif