blob: 2b87b2e3f9ff270e4b2a6e8d7ce1fd2da31b75de [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001#ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
2#define BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
3
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10//
11// boost/detail/lightweight_test.hpp - lightweight test library
12//
13// Copyright (c) 2002, 2009 Peter Dimov
14// Copyright (2) Beman Dawes 2010, 2011
15// Copyright (3) Ion Gaztanaga 2013
16//
17// Distributed under the Boost Software License, Version 1.0.
18// See accompanying file LICENSE_1_0.txt or copy at
19// http://www.boost.org/LICENSE_1_0.txt
20//
21// ---------------
22//
23// If expression is false increases the error count
24// and outputs a message containing 'expression'
25//
26// BOOST_TEST(expression)
27//
28// ---------------
29//
30// Increases error count and outputs a message containing 'message'
31//
32// BOOST_ERROR(message)
33//
34// ---------------
35//
36// If 'expr1' != 'expr2' increases the error count
37// and outputs a message containing both expressions
38//
39// BOOST_TEST_EQ(expr1, expr2)
40//
41// ---------------
42//
43// If 'expr1' == 'expr2' increases the error count
44// and outputs a message containing both expressions
45//
46// BOOST_TEST_NE(expr1, expr2)
47//
48// ---------------
49//
50// If BOOST_NO_EXCEPTIONS is NOT defined and if 'expr' does not
51// throw an exception of type 'excep', increases the error count
52// and outputs a message containing the expression.
53//
54// If BOOST_NO_EXCEPTIONS is defined, this macro expands to nothing
55// and 'expr' is not evaluated.
56//
57// BOOST_TEST_THROWS(expr, excep)
58//
59// ---------------
60//
61// Returns the error count
62//
63// int ndnboost::report_errors()
64//
65
66#include <iostream>
67#include <ndnboost/current_function.hpp>
68#include <ndnboost/assert.hpp>
69#include <ndnboost/detail/no_exceptions_support.hpp>
70
71// IDE's like Visual Studio perform better if output goes to std::cout or
72// some other stream, so allow user to configure output stream:
73#ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
74# define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
75#endif
76
77namespace ndnboost
78{
79
80namespace detail
81{
82
83struct report_errors_reminder
84{
85 bool called_report_errors_function;
86 report_errors_reminder() : called_report_errors_function(false) {}
87 ~report_errors_reminder()
88 {
89 BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
90 }
91};
92
93inline report_errors_reminder& report_errors_remind()
94{
95 static report_errors_reminder r;
96 return r;
97}
98
99inline int & test_errors()
100{
101 static int x = 0;
102 report_errors_remind();
103 return x;
104}
105
106inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
107{
108 BOOST_LIGHTWEIGHT_TEST_OSTREAM
109 << file << "(" << line << "): test '" << expr << "' failed in function '"
110 << function << "'" << std::endl;
111 ++test_errors();
112}
113
114inline void error_impl(char const * msg, char const * file, int line, char const * function)
115{
116 BOOST_LIGHTWEIGHT_TEST_OSTREAM
117 << file << "(" << line << "): " << msg << " in function '"
118 << function << "'" << std::endl;
119 ++test_errors();
120}
121
122inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
123{
124 BOOST_LIGHTWEIGHT_TEST_OSTREAM
125 << file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
126 << function << "'" << std::endl;
127 ++test_errors();
128}
129
130template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2,
131 char const * file, int line, char const * function, T const & t, U const & u )
132{
133 if( t == u )
134 {
135 }
136 else
137 {
138 BOOST_LIGHTWEIGHT_TEST_OSTREAM
139 << file << "(" << line << "): test '" << expr1 << " == " << expr2
140 << "' failed in function '" << function << "': "
141 << "'" << t << "' != '" << u << "'" << std::endl;
142 ++test_errors();
143 }
144}
145
146template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
147 char const * file, int line, char const * function, T const & t, U const & u )
148{
149 if( t != u )
150 {
151 }
152 else
153 {
154 BOOST_LIGHTWEIGHT_TEST_OSTREAM
155 << file << "(" << line << "): test '" << expr1 << " != " << expr2
156 << "' failed in function '" << function << "': "
157 << "'" << t << "' == '" << u << "'" << std::endl;
158 ++test_errors();
159 }
160}
161
162} // namespace detail
163
164inline int report_errors()
165{
166 detail::report_errors_remind().called_report_errors_function = true;
167
168 int errors = detail::test_errors();
169
170 if( errors == 0 )
171 {
172 BOOST_LIGHTWEIGHT_TEST_OSTREAM
173 << "No errors detected." << std::endl;
174 return 0;
175 }
176 else
177 {
178 BOOST_LIGHTWEIGHT_TEST_OSTREAM
179 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
180 return 1;
181 }
182}
183
184} // namespace ndnboost
185
186#define BOOST_TEST(expr) ((expr)? (void)0: ::ndnboost::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
187#define BOOST_ERROR(msg) ::ndnboost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
188#define BOOST_TEST_EQ(expr1,expr2) ( ::ndnboost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
189#define BOOST_TEST_NE(expr1,expr2) ( ::ndnboost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
190#ifndef BOOST_NO_EXCEPTIONS
191 #define BOOST_TEST_THROWS( EXPR, EXCEP ) \
192 try { \
193 EXPR; \
194 ::ndnboost::detail::throw_failed_impl \
195 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
196 } \
197 catch(EXCEP const&) { \
198 } \
199 catch(...) { \
200 ::ndnboost::detail::throw_failed_impl \
201 (#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
202 } \
203 //
204#else
205 #define BOOST_TEST_THROWS( EXPR, EXCEP )
206#endif
207
208#endif // #ifndef BOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED