blob: 40e7cd685050a2686492c3fb179c5a8ed7a39f88 [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
2#define NDNBOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED
Jeff Thompsona28eed82013-08-22 16:21:10 -07003
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10//
Jeff Thompson9939dcd2013-10-15 15:12:24 -070011// ndnboost/detail/lightweight_test.hpp - lightweight test library
Jeff Thompsona28eed82013-08-22 16:21:10 -070012//
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//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070026// NDNBOOST_TEST(expression)
Jeff Thompsona28eed82013-08-22 16:21:10 -070027//
28// ---------------
29//
30// Increases error count and outputs a message containing 'message'
31//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070032// NDNBOOST_ERROR(message)
Jeff Thompsona28eed82013-08-22 16:21:10 -070033//
34// ---------------
35//
36// If 'expr1' != 'expr2' increases the error count
37// and outputs a message containing both expressions
38//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070039// NDNBOOST_TEST_EQ(expr1, expr2)
Jeff Thompsona28eed82013-08-22 16:21:10 -070040//
41// ---------------
42//
43// If 'expr1' == 'expr2' increases the error count
44// and outputs a message containing both expressions
45//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070046// NDNBOOST_TEST_NE(expr1, expr2)
Jeff Thompsona28eed82013-08-22 16:21:10 -070047//
48// ---------------
49//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070050// If NDNBOOST_NO_EXCEPTIONS is NOT defined and if 'expr' does not
Jeff Thompsona28eed82013-08-22 16:21:10 -070051// throw an exception of type 'excep', increases the error count
52// and outputs a message containing the expression.
53//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070054// If NDNBOOST_NO_EXCEPTIONS is defined, this macro expands to nothing
Jeff Thompsona28eed82013-08-22 16:21:10 -070055// and 'expr' is not evaluated.
56//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070057// NDNBOOST_TEST_THROWS(expr, excep)
Jeff Thompsona28eed82013-08-22 16:21:10 -070058//
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:
Jeff Thompson3d613fd2013-10-15 15:39:04 -070073#ifndef NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM
74# define NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
Jeff Thompsona28eed82013-08-22 16:21:10 -070075#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 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070089 NDNBOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
Jeff Thompsona28eed82013-08-22 16:21:10 -070090 }
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{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700108 NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM
Jeff Thompsona28eed82013-08-22 16:21:10 -0700109 << 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{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700116 NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM
Jeff Thompsona28eed82013-08-22 16:21:10 -0700117 << 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{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700124 NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM
Jeff Thompsona28eed82013-08-22 16:21:10 -0700125 << 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 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700138 NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM
Jeff Thompsona28eed82013-08-22 16:21:10 -0700139 << 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 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700154 NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM
Jeff Thompsona28eed82013-08-22 16:21:10 -0700155 << 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 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700172 NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM
Jeff Thompsona28eed82013-08-22 16:21:10 -0700173 << "No errors detected." << std::endl;
174 return 0;
175 }
176 else
177 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700178 NDNBOOST_LIGHTWEIGHT_TEST_OSTREAM
Jeff Thompsona28eed82013-08-22 16:21:10 -0700179 << errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
180 return 1;
181 }
182}
183
184} // namespace ndnboost
185
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700186#define NDNBOOST_TEST(expr) ((expr)? (void)0: ::ndnboost::detail::test_failed_impl(#expr, __FILE__, __LINE__, NDNBOOST_CURRENT_FUNCTION))
187#define NDNBOOST_ERROR(msg) ::ndnboost::detail::error_impl(msg, __FILE__, __LINE__, NDNBOOST_CURRENT_FUNCTION)
188#define NDNBOOST_TEST_EQ(expr1,expr2) ( ::ndnboost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, NDNBOOST_CURRENT_FUNCTION, expr1, expr2) )
189#define NDNBOOST_TEST_NE(expr1,expr2) ( ::ndnboost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, NDNBOOST_CURRENT_FUNCTION, expr1, expr2) )
190#ifndef NDNBOOST_NO_EXCEPTIONS
191 #define NDNBOOST_TEST_THROWS( EXPR, EXCEP ) \
Jeff Thompsona28eed82013-08-22 16:21:10 -0700192 try { \
193 EXPR; \
194 ::ndnboost::detail::throw_failed_impl \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700195 (#EXCEP, __FILE__, __LINE__, NDNBOOST_CURRENT_FUNCTION); \
Jeff Thompsona28eed82013-08-22 16:21:10 -0700196 } \
197 catch(EXCEP const&) { \
198 } \
199 catch(...) { \
200 ::ndnboost::detail::throw_failed_impl \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700201 (#EXCEP, __FILE__, __LINE__, NDNBOOST_CURRENT_FUNCTION); \
Jeff Thompsona28eed82013-08-22 16:21:10 -0700202 } \
203 //
204#else
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700205 #define NDNBOOST_TEST_THROWS( EXPR, EXCEP )
Jeff Thompsona28eed82013-08-22 16:21:10 -0700206#endif
207
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700208#endif // #ifndef NDNBOOST_DETAIL_LIGHTWEIGHT_TEST_HPP_INCLUDED