blob: 8e71c02c0f3849633d0c37521b240aacec39057b [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (C) Copyright Gennadiy Rozental 2001-2008.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6// See http://www.boost.org/libs/test for the library home page.
7//
8// File : $RCSfile$
9//
10// Version : $Revision: 54633 $
11//
12// Description : contains definition for all test tools in test toolbox
13// ***************************************************************************
14
Jeff Thompson3d613fd2013-10-15 15:39:04 -070015#ifndef NDNBOOST_TEST_TEST_TOOLS_HPP_012705GER
16#define NDNBOOST_TEST_TEST_TOOLS_HPP_012705GER
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070017
18// Boost.Test
19#include <ndnboost/test/predicate_result.hpp>
20#include <ndnboost/test/unit_test_log.hpp>
21#include <ndnboost/test/floating_point_comparison.hpp>
22
23#include <ndnboost/test/detail/config.hpp>
24#include <ndnboost/test/detail/global_typedef.hpp>
25#include <ndnboost/test/detail/workaround.hpp>
26
27#include <ndnboost/test/utils/wrap_stringstream.hpp>
28#include <ndnboost/test/utils/basic_cstring/io.hpp>
29#include <ndnboost/test/utils/lazy_ostream.hpp>
30
31// Boost
32#include <ndnboost/preprocessor/seq/for_each.hpp>
33#include <ndnboost/preprocessor/seq/size.hpp>
34#include <ndnboost/preprocessor/seq/enum.hpp>
35#include <ndnboost/preprocessor/repetition/repeat.hpp>
36#include <ndnboost/preprocessor/punctuation/comma_if.hpp>
37#include <ndnboost/preprocessor/arithmetic/add.hpp>
38
39#include <ndnboost/limits.hpp>
40
41#include <ndnboost/type_traits/is_array.hpp>
42#include <ndnboost/type_traits/is_function.hpp>
43#include <ndnboost/type_traits/is_abstract.hpp>
44
45#include <ndnboost/mpl/or.hpp>
46
47// STL
48#include <cstddef> // for std::size_t
49#include <iosfwd>
50#include <ios> // for std::boolalpha
51#include <climits> // for CHAR_BIT
52
Jeff Thompson3d613fd2013-10-15 15:39:04 -070053#ifdef NDNBOOST_MSVC
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070054# pragma warning(disable: 4127) // conditional expression is constant
55#endif
56
57#include <ndnboost/test/detail/suppress_warnings.hpp>
58
59//____________________________________________________________________________//
60
61// ************************************************************************** //
62// ************** TOOL BOX ************** //
63// ************************************************************************** //
64
65// In macros below following argument abbreviations are used:
66// P - predicate
67// M - message
68// S - statement
69// E - exception
70// L - left argument
71// R - right argument
72// TL - tool level
73// CT - check type
74// ARGS - arguments list
75
Jeff Thompson3d613fd2013-10-15 15:39:04 -070076#define NDNBOOST_TEST_TOOL_IMPL( func, P, check_descr, TL, CT ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070077 ::ndnboost::test_tools::tt_detail::func( \
78 P, \
79 ::ndnboost::unit_test::lazy_ostream::instance() << check_descr, \
Jeff Thompson3d613fd2013-10-15 15:39:04 -070080 NDNBOOST_TEST_L(__FILE__), \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070081 static_cast<std::size_t>(__LINE__), \
82 ::ndnboost::test_tools::tt_detail::TL, \
83 ::ndnboost::test_tools::tt_detail::CT \
84/**/
85
86//____________________________________________________________________________//
87
Jeff Thompson3d613fd2013-10-15 15:39:04 -070088#define NDNBOOST_CHECK_IMPL( P, check_descr, TL, CT ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070089do { \
Jeff Thompson3d613fd2013-10-15 15:39:04 -070090 NDNBOOST_TEST_PASSPOINT(); \
91 NDNBOOST_TEST_TOOL_IMPL( check_impl, P, check_descr, TL, CT ), 0 );\
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070092} while( ::ndnboost::test_tools::dummy_cond ) \
93/**/
94
95//____________________________________________________________________________//
96
Jeff Thompson3d613fd2013-10-15 15:39:04 -070097#define NDNBOOST_TEST_PASS_ARG_INFO( r, data, arg ) , arg, NDNBOOST_STRINGIZE( arg )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070098
Jeff Thompson3d613fd2013-10-15 15:39:04 -070099#define NDNBOOST_CHECK_WITH_ARGS_IMPL( P, check_descr, TL, CT, ARGS ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700100do { \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700101 NDNBOOST_TEST_PASSPOINT(); \
102 NDNBOOST_TEST_TOOL_IMPL( check_frwd, P, check_descr, TL, CT ) \
103 NDNBOOST_PP_SEQ_FOR_EACH( NDNBOOST_TEST_PASS_ARG_INFO, '_', ARGS ) ); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700104} while( ::ndnboost::test_tools::dummy_cond ) \
105/**/
106
107//____________________________________________________________________________//
108
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700109#define NDNBOOST_WARN( P ) NDNBOOST_CHECK_IMPL( (P), NDNBOOST_TEST_STRINGIZE( P ), WARN, CHECK_PRED )
110#define NDNBOOST_CHECK( P ) NDNBOOST_CHECK_IMPL( (P), NDNBOOST_TEST_STRINGIZE( P ), CHECK, CHECK_PRED )
111#define NDNBOOST_REQUIRE( P ) NDNBOOST_CHECK_IMPL( (P), NDNBOOST_TEST_STRINGIZE( P ), REQUIRE, CHECK_PRED )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700112
113//____________________________________________________________________________//
114
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700115#define NDNBOOST_WARN_MESSAGE( P, M ) NDNBOOST_CHECK_IMPL( (P), M, WARN, CHECK_MSG )
116#define NDNBOOST_CHECK_MESSAGE( P, M ) NDNBOOST_CHECK_IMPL( (P), M, CHECK, CHECK_MSG )
117#define NDNBOOST_REQUIRE_MESSAGE( P, M ) NDNBOOST_CHECK_IMPL( (P), M, REQUIRE, CHECK_MSG )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700118
119//____________________________________________________________________________//
120
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700121#define NDNBOOST_ERROR( M ) NDNBOOST_CHECK_MESSAGE( false, M )
122#define NDNBOOST_FAIL( M ) NDNBOOST_REQUIRE_MESSAGE( false, M )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700123
124//____________________________________________________________________________//
125
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700126#define NDNBOOST_CHECK_THROW_IMPL( S, E, P, prefix, TL ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700127 try { \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700128 NDNBOOST_TEST_PASSPOINT(); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700129 S; \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700130 NDNBOOST_CHECK_IMPL( false, "exception " NDNBOOST_STRINGIZE( E ) " is expected", TL, CHECK_MSG ); } \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700131 catch( E const& ex ) { \
132 ::ndnboost::unit_test::ut_detail::ignore_unused_variable_warning( ex ); \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700133 NDNBOOST_CHECK_IMPL( P, prefix NDNBOOST_STRINGIZE( E ) " is caught", TL, CHECK_MSG ); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700134 } \
135/**/
136
137//____________________________________________________________________________//
138
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700139#define NDNBOOST_WARN_THROW( S, E ) NDNBOOST_CHECK_THROW_IMPL( S, E, true, "exception ", WARN )
140#define NDNBOOST_CHECK_THROW( S, E ) NDNBOOST_CHECK_THROW_IMPL( S, E, true, "exception ", CHECK )
141#define NDNBOOST_REQUIRE_THROW( S, E ) NDNBOOST_CHECK_THROW_IMPL( S, E, true, "exception ", REQUIRE )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700142
143//____________________________________________________________________________//
144
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700145#define NDNBOOST_WARN_EXCEPTION( S, E, P ) NDNBOOST_CHECK_THROW_IMPL( S, E, P( ex ), "incorrect exception ", WARN )
146#define NDNBOOST_CHECK_EXCEPTION( S, E, P ) NDNBOOST_CHECK_THROW_IMPL( S, E, P( ex ), "incorrect exception ", CHECK )
147#define NDNBOOST_REQUIRE_EXCEPTION( S, E, P ) NDNBOOST_CHECK_THROW_IMPL( S, E, P( ex ), "incorrect exception ", REQUIRE )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700148
149//____________________________________________________________________________//
150
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700151#define NDNBOOST_CHECK_NO_THROW_IMPL( S, TL ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700152 try { \
153 S; \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700154 NDNBOOST_CHECK_IMPL( true, "no exceptions thrown by " NDNBOOST_STRINGIZE( S ), TL, CHECK_MSG ); } \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700155 catch( ... ) { \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700156 NDNBOOST_CHECK_IMPL( false, "exception thrown by " NDNBOOST_STRINGIZE( S ), TL, CHECK_MSG ); \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700157 } \
158/**/
159
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700160#define NDNBOOST_WARN_NO_THROW( S ) NDNBOOST_CHECK_NO_THROW_IMPL( S, WARN )
161#define NDNBOOST_CHECK_NO_THROW( S ) NDNBOOST_CHECK_NO_THROW_IMPL( S, CHECK )
162#define NDNBOOST_REQUIRE_NO_THROW( S ) NDNBOOST_CHECK_NO_THROW_IMPL( S, REQUIRE )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700163
164//____________________________________________________________________________//
165
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700166#define NDNBOOST_WARN_EQUAL( L, R ) \
167 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::equal_impl_frwd(), "", WARN, CHECK_EQUAL, (L)(R) )
168#define NDNBOOST_CHECK_EQUAL( L, R ) \
169 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::equal_impl_frwd(), "", CHECK, CHECK_EQUAL, (L)(R) )
170#define NDNBOOST_REQUIRE_EQUAL( L, R ) \
171 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::equal_impl_frwd(), "", REQUIRE, CHECK_EQUAL, (L)(R) )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700172
173//____________________________________________________________________________//
174
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700175#define NDNBOOST_WARN_NE( L, R ) \
176 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::ne_impl(), "", WARN, CHECK_NE, (L)(R) )
177#define NDNBOOST_CHECK_NE( L, R ) \
178 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::ne_impl(), "", CHECK, CHECK_NE, (L)(R) )
179#define NDNBOOST_REQUIRE_NE( L, R ) \
180 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::ne_impl(), "", REQUIRE, CHECK_NE, (L)(R) )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700181
182//____________________________________________________________________________//
183
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700184#define NDNBOOST_WARN_LT( L, R ) \
185 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::lt_impl(), "", WARN, CHECK_LT, (L)(R) )
186#define NDNBOOST_CHECK_LT( L, R ) \
187 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::lt_impl(), "", CHECK, CHECK_LT, (L)(R) )
188#define NDNBOOST_REQUIRE_LT( L, R ) \
189 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::lt_impl(), "", REQUIRE, CHECK_LT, (L)(R) )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700190
191//____________________________________________________________________________//
192
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700193#define NDNBOOST_WARN_LE( L, R ) \
194 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::le_impl(), "", WARN, CHECK_LE, (L)(R) )
195#define NDNBOOST_CHECK_LE( L, R ) \
196 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::le_impl(), "", CHECK, CHECK_LE, (L)(R) )
197#define NDNBOOST_REQUIRE_LE( L, R ) \
198 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::le_impl(), "", REQUIRE, CHECK_LE, (L)(R) )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700199
200//____________________________________________________________________________//
201
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700202#define NDNBOOST_WARN_GT( L, R ) \
203 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::gt_impl(), "", WARN, CHECK_GT, (L)(R) )
204#define NDNBOOST_CHECK_GT( L, R ) \
205 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::gt_impl(), "", CHECK, CHECK_GT, (L)(R) )
206#define NDNBOOST_REQUIRE_GT( L, R ) \
207 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::gt_impl(), "", REQUIRE, CHECK_GT, (L)(R) )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700208
209//____________________________________________________________________________//
210
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700211#define NDNBOOST_WARN_GE( L, R ) \
212 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::ge_impl(), "", WARN, CHECK_GE, (L)(R) )
213#define NDNBOOST_CHECK_GE( L, R ) \
214 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::ge_impl(), "", CHECK, CHECK_GE, (L)(R) )
215#define NDNBOOST_REQUIRE_GE( L, R ) \
216 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::tt_detail::ge_impl(), "", REQUIRE, CHECK_GE, (L)(R) )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700217
218//____________________________________________________________________________//
219
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700220#define NDNBOOST_WARN_CLOSE( L, R, T ) \
221 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_close, "", WARN, CHECK_CLOSE, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700222 (L)(R)(::ndnboost::test_tools::percent_tolerance(T)) )
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700223#define NDNBOOST_CHECK_CLOSE( L, R, T ) \
224 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_close, "", CHECK, CHECK_CLOSE, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700225 (L)(R)(::ndnboost::test_tools::percent_tolerance(T)) )
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700226#define NDNBOOST_REQUIRE_CLOSE( L, R, T ) \
227 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_close, "", REQUIRE, CHECK_CLOSE, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700228 (L)(R)(::ndnboost::test_tools::percent_tolerance(T)) )
229
230//____________________________________________________________________________//
231
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700232#define NDNBOOST_WARN_CLOSE_FRACTION( L, R, T ) \
233 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_close, "", WARN, CHECK_CLOSE_FRACTION, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700234 (L)(R)(::ndnboost::test_tools::fraction_tolerance(T)) )
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700235#define NDNBOOST_CHECK_CLOSE_FRACTION( L, R, T ) \
236 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_close, "", CHECK, CHECK_CLOSE_FRACTION, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700237 (L)(R)(::ndnboost::test_tools::fraction_tolerance(T)) )
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700238#define NDNBOOST_REQUIRE_CLOSE_FRACTION( L, R, T ) \
239 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_close, "", REQUIRE, CHECK_CLOSE_FRACTION, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700240 (L)(R)(::ndnboost::test_tools::fraction_tolerance(T)) )
241
242//____________________________________________________________________________//
243
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700244#define NDNBOOST_WARN_SMALL( FPV, T ) \
245 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_small, "", WARN, CHECK_SMALL, (FPV)(T) )
246#define NDNBOOST_CHECK_SMALL( FPV, T ) \
247 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_small, "", CHECK, CHECK_SMALL, (FPV)(T) )
248#define NDNBOOST_REQUIRE_SMALL( FPV, T ) \
249 NDNBOOST_CHECK_WITH_ARGS_IMPL( ::ndnboost::test_tools::check_is_small, "", REQUIRE, CHECK_SMALL, (FPV)(T) )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700250
251//____________________________________________________________________________//
252
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700253#define NDNBOOST_WARN_PREDICATE( P, ARGS ) \
254 NDNBOOST_CHECK_WITH_ARGS_IMPL( P, NDNBOOST_TEST_STRINGIZE( P ), WARN, CHECK_PRED_WITH_ARGS, ARGS )
255#define NDNBOOST_CHECK_PREDICATE( P, ARGS ) \
256 NDNBOOST_CHECK_WITH_ARGS_IMPL( P, NDNBOOST_TEST_STRINGIZE( P ), CHECK, CHECK_PRED_WITH_ARGS, ARGS )
257#define NDNBOOST_REQUIRE_PREDICATE( P, ARGS ) \
258 NDNBOOST_CHECK_WITH_ARGS_IMPL( P, NDNBOOST_TEST_STRINGIZE( P ), REQUIRE, CHECK_PRED_WITH_ARGS, ARGS )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700259
260//____________________________________________________________________________//
261
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700262#define NDNBOOST_EQUAL_COLLECTIONS_IMPL( L_begin, L_end, R_begin, R_end, TL ) \
263 NDNBOOST_TEST_TOOL_IMPL( check_impl, ::ndnboost::test_tools::tt_detail::equal_coll_impl( \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700264 (L_begin), (L_end), (R_begin), (R_end) ), "", TL, CHECK_EQUAL_COLL ), \
265 4, \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700266 NDNBOOST_STRINGIZE( L_begin ), NDNBOOST_STRINGIZE( L_end ), \
267 NDNBOOST_STRINGIZE( R_begin ), NDNBOOST_STRINGIZE( R_end ) ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700268/**/
269
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700270#define NDNBOOST_WARN_EQUAL_COLLECTIONS( L_begin, L_end, R_begin, R_end ) \
271 NDNBOOST_EQUAL_COLLECTIONS_IMPL( L_begin, L_end, R_begin, R_end, WARN )
272#define NDNBOOST_CHECK_EQUAL_COLLECTIONS( L_begin, L_end, R_begin, R_end ) \
273 NDNBOOST_EQUAL_COLLECTIONS_IMPL( L_begin, L_end, R_begin, R_end, CHECK )
274#define NDNBOOST_REQUIRE_EQUAL_COLLECTIONS( L_begin, L_end, R_begin, R_end ) \
275 NDNBOOST_EQUAL_COLLECTIONS_IMPL( L_begin, L_end, R_begin, R_end, REQUIRE )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700276
277//____________________________________________________________________________//
278
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700279#define NDNBOOST_BITWISE_EQUAL_IMPL( L, R, TL ) \
280 NDNBOOST_TEST_TOOL_IMPL( check_impl, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700281 ::ndnboost::test_tools::tt_detail::bitwise_equal_impl( (L), (R) ), \
282 "", TL, CHECK_BITWISE_EQUAL ), \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700283 2, NDNBOOST_STRINGIZE( L ), NDNBOOST_STRINGIZE( R ) ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700284/**/
285
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700286#define NDNBOOST_WARN_BITWISE_EQUAL( L, R ) NDNBOOST_BITWISE_EQUAL_IMPL( L, R, WARN )
287#define NDNBOOST_CHECK_BITWISE_EQUAL( L, R ) NDNBOOST_BITWISE_EQUAL_IMPL( L, R, CHECK )
288#define NDNBOOST_REQUIRE_BITWISE_EQUAL( L, R ) NDNBOOST_BITWISE_EQUAL_IMPL( L, R, REQUIRE )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700289
290//____________________________________________________________________________//
291
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700292#define NDNBOOST_IS_DEFINED( symb ) ::ndnboost::test_tools::tt_detail::is_defined_impl( #symb, NDNBOOST_STRINGIZE(= symb) )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700293
294//____________________________________________________________________________//
295
296// ***************************** //
297// deprecated interface
298
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700299#define NDNBOOST_BITWISE_EQUAL( L, R ) NDNBOOST_CHECK_BITWISE_EQUAL( L, R )
300#define NDNBOOST_MESSAGE( M ) NDNBOOST_TEST_MESSAGE( M )
301#define NDNBOOST_CHECKPOINT( M ) NDNBOOST_TEST_CHECKPOINT( M )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700302
303namespace ndnboost {
304
305namespace test_tools {
306
307typedef unit_test::const_string const_string;
308
309namespace { bool dummy_cond = false; }
310
311// ************************************************************************** //
312// ************** print_log_value ************** //
313// ************************************************************************** //
314
315template<typename T>
316struct print_log_value {
317 void operator()( std::ostream& ostr, T const& t )
318 {
319 // avoid warning: 'ndnboost::test_tools::<unnamed>::dummy_cond' defined but not used
320 if (::ndnboost::test_tools::dummy_cond) {}
321
322 typedef typename mpl::or_<is_array<T>,is_function<T>,is_abstract<T> >::type cant_use_nl;
323
324 set_precision( ostr, cant_use_nl() );
325
326 ostr << t; // by default print the value
327 }
328
329 void set_precision( std::ostream& ostr, mpl::false_ )
330 {
331 if( std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::radix == 2 )
332 ostr.precision( 2 + std::numeric_limits<T>::digits * 301/1000 );
333 }
334
335 void set_precision( std::ostream&, mpl::true_ ) {}
336};
337
338//____________________________________________________________________________//
339
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700340#define NDNBOOST_TEST_DONT_PRINT_LOG_VALUE( the_type ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700341namespace ndnboost { namespace test_tools { \
342template<> \
343struct print_log_value<the_type > { \
344 void operator()( std::ostream&, the_type const& ) {} \
345}; \
346}} \
347/**/
348
349//____________________________________________________________________________//
350
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700351#if NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x564))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700352template<typename T, std::size_t N >
353struct print_log_value< T[N] > {
354 void operator()( std::ostream& ostr, T const* t )
355 {
356 ostr << t;
357 }
358};
359#endif
360
361//____________________________________________________________________________//
362
363template<>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700364struct NDNBOOST_TEST_DECL print_log_value<bool> {
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700365 void operator()( std::ostream& ostr, bool t )
366 {
367 ostr << std::boolalpha << t;
368 }
369};
370
371//____________________________________________________________________________//
372
373template<>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700374struct NDNBOOST_TEST_DECL print_log_value<char> {
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700375 void operator()( std::ostream& ostr, char t );
376};
377
378//____________________________________________________________________________//
379
380template<>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700381struct NDNBOOST_TEST_DECL print_log_value<unsigned char> {
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700382 void operator()( std::ostream& ostr, unsigned char t );
383};
384
385//____________________________________________________________________________//
386
387template<>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700388struct NDNBOOST_TEST_DECL print_log_value<char const*> {
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700389 void operator()( std::ostream& ostr, char const* t );
390};
391
392//____________________________________________________________________________//
393
394template<>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700395struct NDNBOOST_TEST_DECL print_log_value<wchar_t const*> {
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700396 void operator()( std::ostream& ostr, wchar_t const* t );
397};
398
399//____________________________________________________________________________//
400
401namespace tt_detail {
402
403// ************************************************************************** //
404// ************** tools classification ************** //
405// ************************************************************************** //
406
407enum check_type {
408 CHECK_PRED,
409 CHECK_MSG,
410 CHECK_EQUAL,
411 CHECK_NE,
412 CHECK_LT,
413 CHECK_LE,
414 CHECK_GT,
415 CHECK_GE,
416 CHECK_CLOSE,
417 CHECK_CLOSE_FRACTION,
418 CHECK_SMALL,
419 CHECK_BITWISE_EQUAL,
420 CHECK_PRED_WITH_ARGS,
421 CHECK_EQUAL_COLL
422};
423
424enum tool_level {
425 WARN, CHECK, REQUIRE, PASS
426};
427
428// ************************************************************************** //
429// ************** print_helper ************** //
430// ************************************************************************** //
431// Adds level of indirection to the output operation, allowing us to customize
432// it for types that do not support operator << directly or for any other reason
433
434template<typename T>
435struct print_helper_t {
436 explicit print_helper_t( T const& t ) : m_t( t ) {}
437
438 T const& m_t;
439};
440
441//____________________________________________________________________________//
442
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700443#if NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x564))
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700444// Borland suffers premature pointer decay passing arrays by reference
445template<typename T, std::size_t N >
446struct print_helper_t< T[N] > {
447 explicit print_helper_t( T const * t ) : m_t( t ) {}
448
449 T const * m_t;
450};
451#endif
452
453//____________________________________________________________________________//
454
455template<typename T>
456inline print_helper_t<T> print_helper( T const& t )
457{
458 return print_helper_t<T>( t );
459}
460
461//____________________________________________________________________________//
462
463template<typename T>
464inline std::ostream&
465operator<<( std::ostream& ostr, print_helper_t<T> const& ph )
466{
467 print_log_value<T>()( ostr, ph.m_t );
468
469 return ostr;
470}
471
472//____________________________________________________________________________//
473
474// ************************************************************************** //
475// ************** TOOL BOX Implementation ************** //
476// ************************************************************************** //
477
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700478NDNBOOST_TEST_DECL
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700479bool check_impl( predicate_result const& pr, ::ndnboost::unit_test::lazy_ostream const& check_descr,
480 const_string file_name, std::size_t line_num,
481 tool_level tl, check_type ct,
482 std::size_t num_args, ... );
483
484//____________________________________________________________________________//
485
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700486#define TEMPL_PARAMS( z, m, dummy ) , typename NDNBOOST_JOIN( Arg, m )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700487#define FUNC_PARAMS( z, m, dummy ) \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700488 , NDNBOOST_JOIN( Arg, m ) const& NDNBOOST_JOIN( arg, m ) \
489 , char const* NDNBOOST_JOIN( NDNBOOST_JOIN( arg, m ), _descr ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700490/**/
491
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700492#define PRED_PARAMS( z, m, dummy ) NDNBOOST_PP_COMMA_IF( m ) NDNBOOST_JOIN( arg, m )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700493
494#define ARG_INFO( z, m, dummy ) \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700495 , NDNBOOST_JOIN( NDNBOOST_JOIN( arg, m ), _descr ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700496 , &static_cast<const unit_test::lazy_ostream&>(unit_test::lazy_ostream::instance() \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700497 << ::ndnboost::test_tools::tt_detail::print_helper( NDNBOOST_JOIN( arg, m ) )) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700498/**/
499
500#define IMPL_FRWD( z, n, dummy ) \
501template<typename Pred \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700502 NDNBOOST_PP_REPEAT_ ## z( NDNBOOST_PP_ADD( n, 1 ), TEMPL_PARAMS, _ )> \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700503inline bool \
504check_frwd( Pred P, unit_test::lazy_ostream const& check_descr, \
505 const_string file_name, std::size_t line_num, \
506 tool_level tl, check_type ct \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700507 NDNBOOST_PP_REPEAT_ ## z( NDNBOOST_PP_ADD( n, 1 ), FUNC_PARAMS, _ ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700508) \
509{ \
510 return \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700511 check_impl( P( NDNBOOST_PP_REPEAT_ ## z( NDNBOOST_PP_ADD( n, 1 ), PRED_PARAMS, _ ) ), \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700512 check_descr, file_name, line_num, tl, ct, \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700513 NDNBOOST_PP_ADD( n, 1 ) \
514 NDNBOOST_PP_REPEAT_ ## z( NDNBOOST_PP_ADD( n, 1 ), ARG_INFO, _ ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700515 ); \
516} \
517/**/
518
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700519#ifndef NDNBOOST_TEST_MAX_PREDICATE_ARITY
520#define NDNBOOST_TEST_MAX_PREDICATE_ARITY 5
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700521#endif
522
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700523NDNBOOST_PP_REPEAT( NDNBOOST_TEST_MAX_PREDICATE_ARITY, IMPL_FRWD, _ )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700524
525#undef TEMPL_PARAMS
526#undef FUNC_PARAMS
527#undef PRED_INFO
528#undef ARG_INFO
529#undef IMPL_FRWD
530
531//____________________________________________________________________________//
532
533template <class Left, class Right>
534predicate_result equal_impl( Left const& left, Right const& right )
535{
536 return left == right;
537}
538
539//____________________________________________________________________________//
540
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700541predicate_result NDNBOOST_TEST_DECL equal_impl( char const* left, char const* right );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700542inline predicate_result equal_impl( char* left, char const* right ) { return equal_impl( static_cast<char const*>(left), static_cast<char const*>(right) ); }
543inline predicate_result equal_impl( char const* left, char* right ) { return equal_impl( static_cast<char const*>(left), static_cast<char const*>(right) ); }
544inline predicate_result equal_impl( char* left, char* right ) { return equal_impl( static_cast<char const*>(left), static_cast<char const*>(right) ); }
545
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700546#if !defined( NDNBOOST_NO_CWCHAR )
547predicate_result NDNBOOST_TEST_DECL equal_impl( wchar_t const* left, wchar_t const* right );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700548inline predicate_result equal_impl( wchar_t* left, wchar_t const* right ) { return equal_impl( static_cast<wchar_t const*>(left), static_cast<wchar_t const*>(right) ); }
549inline predicate_result equal_impl( wchar_t const* left, wchar_t* right ) { return equal_impl( static_cast<wchar_t const*>(left), static_cast<wchar_t const*>(right) ); }
550inline predicate_result equal_impl( wchar_t* left, wchar_t* right ) { return equal_impl( static_cast<wchar_t const*>(left), static_cast<wchar_t const*>(right) ); }
551#endif
552
553//____________________________________________________________________________//
554
555struct equal_impl_frwd {
556 template <typename Left, typename Right>
557 inline predicate_result
558 call_impl( Left const& left, Right const& right, mpl::false_ ) const
559 {
560 return equal_impl( left, right );
561 }
562
563 template <typename Left, typename Right>
564 inline predicate_result
565 call_impl( Left const& left, Right const& right, mpl::true_ ) const
566 {
567 return (*this)( right, &left[0] );
568 }
569
570 template <typename Left, typename Right>
571 inline predicate_result
572 operator()( Left const& left, Right const& right ) const
573 {
574 typedef typename is_array<Left>::type left_is_array;
575 return call_impl( left, right, left_is_array() );
576 }
577};
578
579//____________________________________________________________________________//
580
581struct ne_impl {
582 template <class Left, class Right>
583 predicate_result operator()( Left const& left, Right const& right )
584 {
585 return !equal_impl_frwd()( left, right );
586 }
587};
588
589//____________________________________________________________________________//
590
591struct lt_impl {
592 template <class Left, class Right>
593 predicate_result operator()( Left const& left, Right const& right )
594 {
595 return left < right;
596 }
597};
598
599//____________________________________________________________________________//
600
601struct le_impl {
602 template <class Left, class Right>
603 predicate_result operator()( Left const& left, Right const& right )
604 {
605 return left <= right;
606 }
607};
608
609//____________________________________________________________________________//
610
611struct gt_impl {
612 template <class Left, class Right>
613 predicate_result operator()( Left const& left, Right const& right )
614 {
615 return left > right;
616 }
617};
618
619//____________________________________________________________________________//
620
621struct ge_impl {
622 template <class Left, class Right>
623 predicate_result operator()( Left const& left, Right const& right )
624 {
625 return left >= right;
626 }
627};
628
629//____________________________________________________________________________//
630
631template <typename Left, typename Right>
632inline predicate_result
633equal_coll_impl( Left left_begin, Left left_end, Right right_begin, Right right_end )
634{
635 predicate_result res( true );
636 std::size_t pos = 0;
637
638 for( ; left_begin != left_end && right_begin != right_end; ++left_begin, ++right_begin, ++pos ) {
639 if( *left_begin != *right_begin ) {
640 res = false;
641 res.message() << "\nMismatch in a position " << pos << ": " << *left_begin << " != " << *right_begin;
642 }
643 }
644
645 if( left_begin != left_end ) {
646 std::size_t r_size = pos;
647 while( left_begin != left_end ) {
648 ++pos;
649 ++left_begin;
650 }
651
652 res = false;
653 res.message() << "\nCollections size mismatch: " << pos << " != " << r_size;
654 }
655
656 if( right_begin != right_end ) {
657 std::size_t l_size = pos;
658 while( right_begin != right_end ) {
659 ++pos;
660 ++right_begin;
661 }
662
663 res = false;
664 res.message() << "\nCollections size mismatch: " << l_size << " != " << pos;
665 }
666
667 return res;
668}
669
670//____________________________________________________________________________//
671
672template <class Left, class Right>
673inline predicate_result
674bitwise_equal_impl( Left const& left, Right const& right )
675{
676 predicate_result res( true );
677
678 std::size_t left_bit_size = sizeof(Left)*CHAR_BIT;
679 std::size_t right_bit_size = sizeof(Right)*CHAR_BIT;
680
681 static Left const leftOne( 1 );
682 static Right const rightOne( 1 );
683
684 std::size_t total_bits = left_bit_size < right_bit_size ? left_bit_size : right_bit_size;
685
686 for( std::size_t counter = 0; counter < total_bits; ++counter ) {
687 if( ( left & ( leftOne << counter ) ) != ( right & ( rightOne << counter ) ) ) {
688 res = false;
689 res.message() << "\nMismatch in a position " << counter;
690 }
691 }
692
693 if( left_bit_size != right_bit_size ) {
694 res = false;
695 res.message() << "\nOperands bit sizes mismatch: " << left_bit_size << " != " << right_bit_size;
696 }
697
698 return res;
699}
700
701//____________________________________________________________________________//
702
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700703bool NDNBOOST_TEST_DECL is_defined_impl( const_string symbol_name, const_string symbol_value );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700704
705//____________________________________________________________________________//
706
707} // namespace tt_detail
708
709} // namespace test_tools
710
711namespace test_toolbox = test_tools;
712
713} // namespace ndnboost
714
715//____________________________________________________________________________//
716
717#include <ndnboost/test/detail/enable_warnings.hpp>
718
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700719#endif // NDNBOOST_TEST_TEST_TOOLS_HPP_012705GER