1 // (C) Copyright Gennadiy Rozental 2001-2015.
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)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : tests all Test Tools but output_test_stream
13 // ***************************************************************************
16 #define BOOST_TEST_MAIN
17 #include <boost/test/unit_test.hpp>
18 #include <boost/test/unit_test_log.hpp>
19 #include <boost/test/tools/output_test_stream.hpp>
20 #include <boost/test/execution_monitor.hpp>
21 #include <boost/test/unit_test_parameters.hpp>
22 #include <boost/test/output/compiler_log_formatter.hpp>
23 #include <boost/test/framework.hpp>
24 #include <boost/core/noncopyable.hpp>
25 #include <boost/test/detail/suppress_warnings.hpp>
28 #include <boost/bind/bind.hpp>
29 #include <boost/noncopyable.hpp>
36 # pragma warning(push)
37 # pragma warning(disable: 4702) // unreachable code
40 #include "../framework-ts/logger-for-tests.hpp"
42 namespace ut
=boost::unit_test
;
43 namespace tt
=boost::test_tools
;
45 // GH-179 char can be unsigned on some archs
46 namespace boost
{ namespace test_tools
{ namespace tt_detail
{
48 struct print_log_value
<signed char> {
49 void operator()( std::ostream
& ostr
, signed char t
)
52 #if BOOST_TEST_USE_STD_LOCALE
57 << static_cast<int>(t
);
62 //____________________________________________________________________________//
64 #define CHECK_CRITICAL_TOOL_USAGE( tool_usage ) \
66 bool throw_ = false; \
69 } catch( boost::execution_aborted const& ) { \
73 BOOST_CHECK_MESSAGE( throw_, "not aborted" ); \
77 //____________________________________________________________________________//
79 // thanks to http://stackoverflow.com/questions/9226400/portable-printing-of-exponent-of-a-double-to-c-iostreams
80 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1900)
81 struct ScientificNotationExponentOutputNormalizer
{
82 ScientificNotationExponentOutputNormalizer() : m_old_format(_set_output_format(_TWO_DIGIT_EXPONENT
)) {}
84 ~ScientificNotationExponentOutputNormalizer() { _set_output_format(m_old_format
); }
86 unsigned m_old_format
;
89 struct ScientificNotationExponentOutputNormalizer
{};
92 //____________________________________________________________________________//
94 class bool_convertible
98 operator Tester
*() const { return static_cast<Tester
*>( 0 ) + 1; }
101 //____________________________________________________________________________//
103 struct shorten_lf
: public boost::unit_test::output::compiler_log_formatter
105 void print_prefix( std::ostream
& output
, boost::unit_test::const_string
, std::size_t line
)
110 //____________________________________________________________________________//
112 std::string
match_file_name( "./baseline-outputs/test_tools-test.pattern" );
113 std::string
save_file_name( "test_tools-test.pattern" );
115 static tt::output_test_stream
&
118 static boost::shared_ptr
<tt::output_test_stream
> inst
;
122 ut::framework::master_test_suite().argc
<= 1
123 ? new tt::output_test_stream( ut::runtime_config::save_pattern() ? save_file_name
: match_file_name
, !ut::runtime_config::save_pattern() )
124 : new tt::output_test_stream( ut::framework::master_test_suite().argv
[1], !ut::runtime_config::save_pattern() ) );
130 //____________________________________________________________________________//
132 #define TEST_CASE( name ) \
133 static void name ## _impl(); \
134 static void name ## _impl_defer(); \
136 BOOST_AUTO_TEST_CASE( name ) \
138 ut::test_case* impl = ut::make_test_case(&name ## _impl, \
142 impl->p_default_status.value = ut::test_unit::RS_ENABLED; \
144 /* to detect for issues concerning the file pattern, we take
146 tt::output_test_stream& stream = ots(); \
149 ut::log_level ll = ut::runtime_config::get<ut::log_level>( \
150 ut::runtime_config::btrt_log_level ); \
151 ut::unit_test_log.set_formatter( new shorten_lf ); \
152 log_setup_teardown holder(stream, \
155 ll != ut::invalid_log_level ? \
157 : ut::log_all_errors ); \
159 ut::framework::finalize_setup_phase( impl->p_id ); \
160 ut::framework::run( impl ); \
164 BOOST_CHECK( stream.match_pattern() ); \
167 void name ## _impl() \
169 ut::unit_test_log.set_threshold_level( ut::log_all_errors ); \
171 name ## _impl_defer(); \
173 ut::unit_test_log.set_threshold_level( ut::log_nothing ); \
176 void name ## _impl_defer() \
180 // should be line 162 _______________________________________________________//
182 TEST_CASE( test_BOOST_WARN
)
184 ut::unit_test_log
.set_threshold_level( ut::log_warnings
);
185 BOOST_WARN( sizeof(int) == sizeof(short) );
187 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
188 BOOST_WARN( sizeof(unsigned char) == sizeof(char) );
191 //____________________________________________________________________________//
193 TEST_CASE( test_BOOST_CHECK
)
195 // check correct behavior in if clause
199 // check correct behavior in else clause
210 BOOST_CHECK( false );
214 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
218 //____________________________________________________________________________//
220 TEST_CASE( test_BOOST_REQUIRE
)
222 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE( true ) );
224 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE( false ) );
228 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE( j
> 5 ) );
230 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
231 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE( j
< 5 ) );
234 //____________________________________________________________________________//
236 TEST_CASE( test_BOOST_WARN_MESSAGE
)
238 BOOST_WARN_MESSAGE( sizeof(int) == sizeof(short), "memory won't be used efficiently" );
241 BOOST_WARN_MESSAGE( obj_size
<= 8,
242 "object size " << obj_size
<< " is too big to be efficiently passed by value" );
244 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
245 BOOST_WARN_MESSAGE( obj_size
> 8, "object size " << obj_size
<< " is too small" );
248 //____________________________________________________________________________//
250 boost::test_tools::predicate_result
253 boost::test_tools::predicate_result
res( false );
255 res
.message() << "Some explanation";
260 TEST_CASE( test_BOOST_CHECK_MESSAGE
)
262 BOOST_CHECK_MESSAGE( 2+2 == 5, "Well, may be that what I believe in" );
264 BOOST_CHECK_MESSAGE( test_pred1(), "Checking predicate failed" );
266 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
267 BOOST_CHECK_MESSAGE( 2+2 == 4, "Could it fail?" );
271 std::string msg
= "some explanation";
272 BOOST_CHECK_MESSAGE( i
> j
, "Comparing " << i
<< " and " << j
<< ": " << msg
);
275 //____________________________________________________________________________//
277 TEST_CASE( test_BOOST_REQUIRE_MESSAGE
)
279 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_MESSAGE( false, "Here we should stop" ) );
281 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
282 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_MESSAGE( true, "That's OK" ) );
285 //____________________________________________________________________________//
287 TEST_CASE( test_BOOST_ERROR
)
289 BOOST_ERROR( "Fail to miss an error" );
292 //____________________________________________________________________________//
294 TEST_CASE( test_BOOST_FAIL
)
296 CHECK_CRITICAL_TOOL_USAGE( BOOST_FAIL( "No! No! Show must go on." ) );
299 //____________________________________________________________________________//
301 struct my_exception
{
302 explicit my_exception( int ec
= 0 ) : m_error_code( ec
) {}
307 bool is_critical( my_exception
const& ex
) { return ex
.m_error_code
< 0; }
309 TEST_CASE( test_BOOST_CHECK_THROW
)
312 BOOST_CHECK_THROW( i
++, my_exception
);
314 ut::unit_test_log
.set_threshold_level( ut::log_warnings
);
315 BOOST_WARN_THROW( i
++, my_exception
);
317 ut::unit_test_log
.set_threshold_level( ut::log_all_errors
);
318 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_THROW( i
++, my_exception
) );
320 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
324 BOOST_CHECK_THROW( throw my_exception(), my_exception
); // unreachable code warning is expected
327 //____________________________________________________________________________//
329 TEST_CASE( test_BOOST_CHECK_EXCEPTION
)
331 BOOST_CHECK_EXCEPTION( throw my_exception( 1 ), my_exception
, is_critical
); // unreachable code warning is expected
333 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
334 BOOST_CHECK_EXCEPTION( throw my_exception( -1 ), my_exception
, is_critical
); // unreachable code warning is expected
337 //____________________________________________________________________________//
339 TEST_CASE( test_BOOST_CHECK_NO_THROW
)
343 BOOST_CHECK_NO_THROW( i
++ );
346 BOOST_CHECK_NO_THROW( throw my_exception() ); // unreachable code warning is expected
349 //____________________________________________________________________________//
352 B( int i
) : m_i( i
) {}
357 bool operator==( B
const& b1
, B
const& b2
) { return b1
.m_i
== b2
.m_i
; }
358 std::ostream
& operator<<( std::ostream
& str
, B
const& b
) { return str
<< "B(" << b
.m_i
<< ")"; }
360 //____________________________________________________________________________//
363 C( int i
, int id
) : m_i( i
), m_id( id
) {}
369 boost::test_tools::predicate_result
370 operator==( C
const& c1
, C
const& c2
)
372 boost::test_tools::predicate_result
res( c1
.m_i
== c2
.m_i
&& c1
.m_id
== c2
.m_id
);
375 if( c1
.m_i
!= c2
.m_i
)
376 res
.message() << "Index mismatch";
378 res
.message() << "Id mismatch";
384 std::ostream
& operator<<( std::ostream
& str
, C
const& c
) { return str
<< "C(" << c
.m_i
<< ',' << c
.m_id
<< ")"; }
386 //____________________________________________________________________________//
388 TEST_CASE( test_BOOST_CHECK_EQUAL
)
392 BOOST_CHECK_EQUAL( i
, j
);
393 BOOST_CHECK_EQUAL( ++i
, j
);
394 BOOST_CHECK_EQUAL( i
++, j
);
396 char const* str1
= "test1";
397 char const* str2
= "test12";
398 BOOST_CHECK_EQUAL( str1
, str2
);
400 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
401 BOOST_CHECK_EQUAL( i
+1, j
);
403 char const* str3
= "1test1";
404 BOOST_CHECK_EQUAL( str1
, str3
+1 );
406 ut::unit_test_log
.set_threshold_level( ut::log_all_errors
);
409 BOOST_CHECK_EQUAL( str1
, str2
);
413 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_EQUAL( str1
, str2
) );
418 ut::unit_test_log
.set_threshold_level( ut::log_warnings
);
419 BOOST_WARN_EQUAL( b1
, b2
);
421 ut::unit_test_log
.set_threshold_level( ( ut::log_all_errors
) );
425 BOOST_CHECK_EQUAL( c1
, c3
);
426 BOOST_CHECK_EQUAL( c1
, c2
);
428 signed char ch1
= -2; // char is unsigned on some archs GH-179
429 signed char ch2
= -3;
430 BOOST_CHECK_EQUAL(ch1
, ch2
);
433 //____________________________________________________________________________//
435 TEST_CASE( test_BOOST_CHECK_LOGICAL_EXPR
)
439 BOOST_CHECK_NE( i
, j
);
441 BOOST_CHECK_NE( ++i
, j
);
443 BOOST_CHECK_LT( i
, j
);
444 BOOST_CHECK_GT( i
, j
);
446 BOOST_CHECK_LE( i
, j
);
447 BOOST_CHECK_GE( i
, j
);
451 BOOST_CHECK_LE( i
, j
);
452 BOOST_CHECK_GE( j
, i
);
454 char const* str1
= "test1";
455 char const* str2
= "test1";
457 BOOST_CHECK_NE( str1
, str2
);
460 //____________________________________________________________________________//
462 bool is_even( int i
) { return i
%2 == 0; }
463 int foo( int arg
, int mod
) { return arg
% mod
; }
464 bool moo( int arg1
, int arg2
, int mod
) { return ((arg1
+arg2
) % mod
) == 0; }
466 BOOST_TEST_DONT_PRINT_LOG_VALUE( std::list
<int> )
468 boost::test_tools::assertion_result
469 compare_lists( std::list
<int> const& l1
, std::list
<int> const& l2
)
471 if( l1
.size() != l2
.size() ) {
472 boost::test_tools::predicate_result
res( false );
474 res
.message() << "Different sizes [" << l1
.size() << "!=" << l2
.size() << "]";
482 TEST_CASE( test_BOOST_CHECK_PREDICATE
)
484 BOOST_CHECK_PREDICATE( is_even
, (14) );
487 BOOST_CHECK_PREDICATE( is_even
, (i
) );
489 using std::not_equal_to
;
490 BOOST_CHECK_PREDICATE( not_equal_to
<int>(), (i
)(17) );
492 using namespace boost::placeholders
;
494 BOOST_CHECK_PREDICATE( boost::bind( is_even
, boost::bind( &foo
, _1
, _2
) ), (i
)(j
) );
496 ut::unit_test_log
.set_threshold_level( ut::log_warnings
);
497 BOOST_WARN_PREDICATE( moo
, (12)(i
)(j
) );
499 ut::unit_test_log
.set_threshold_level( ( ut::log_all_errors
) );
500 std::list
<int> l1
, l2
, l3
;
505 BOOST_CHECK_PREDICATE( compare_lists
, (l1
)(l2
) );
508 //____________________________________________________________________________//
510 TEST_CASE( test_BOOST_REQUIRE_PREDICATE
)
515 using std::less_equal
;
516 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_PREDICATE( less_equal
<int>(), (arg1
)(arg2
) ) );
518 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_PREDICATE( less_equal
<int>(), (arg2
)(arg1
) ) );
521 //____________________________________________________________________________//
523 TEST_CASE( test_BOOST_CHECK_EQUAL_COLLECTIONS
)
525 ut::unit_test_log
.set_threshold_level( ( ut::log_all_errors
) );
527 int pattern
[] = { 1, 2, 3, 4, 5, 6, 7 };
529 std::list
<int> testlist
;
531 testlist
.push_back( 1 );
532 testlist
.push_back( 2 );
533 testlist
.push_back( 4 ); // 3
534 testlist
.push_back( 4 );
535 testlist
.push_back( 5 );
536 testlist
.push_back( 7 ); // 6
537 testlist
.push_back( 7 );
539 BOOST_CHECK_EQUAL_COLLECTIONS( testlist
.begin(), testlist
.end(), pattern
, pattern
+7 );
540 BOOST_CHECK_EQUAL_COLLECTIONS( testlist
.begin(), testlist
.end(), pattern
, pattern
+2 );
543 //____________________________________________________________________________//
545 TEST_CASE( test_BOOST_CHECK_BITWISE_EQUAL
)
547 BOOST_CHECK_BITWISE_EQUAL( 0x16, 0x16 );
549 BOOST_CHECK_BITWISE_EQUAL( (char)0x06, (char)0x16 );
551 ut::unit_test_log
.set_threshold_level( ut::log_warnings
);
552 BOOST_WARN_BITWISE_EQUAL( (char)0x26, (char)0x04 );
554 ut::unit_test_log
.set_threshold_level( ( ut::log_all_errors
) );
555 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_BITWISE_EQUAL( (char)0x26, (int)0x26 ) );
558 //____________________________________________________________________________//
561 friend std::ostream
& operator<<( std::ostream
& str
, A
const& ) { str
<< "struct A"; return str
;}
564 TEST_CASE( test_BOOST_TEST_MESSAGE
)
566 ut::unit_test_log
.set_threshold_level( ut::log_messages
);
568 BOOST_TEST_MESSAGE( "still testing" );
569 BOOST_TEST_MESSAGE( "1+1=" << 2 );
572 BOOST_TEST_MESSAGE( i
<< "+" << i
<< "=" << (i
+i
) );
575 BOOST_TEST_MESSAGE( a
);
577 #if BOOST_TEST_USE_STD_LOCALE
578 BOOST_TEST_MESSAGE( std::hex
<< std::showbase
<< 20 );
580 BOOST_TEST_MESSAGE( "0x14" );
583 BOOST_TEST_MESSAGE( std::setw( 4 ) << 20 );
586 //____________________________________________________________________________//
588 TEST_CASE( test_BOOST_TEST_CHECKPOINT
)
590 BOOST_TEST_CHECKPOINT( "Going to do a silly things" );
595 //____________________________________________________________________________//
597 bool foo() { throw 1; return true; }
599 TEST_CASE( test_BOOST_TEST_PASSPOINT
)
601 BOOST_CHECK( foo() );
604 //____________________________________________________________________________//
606 BOOST_AUTO_TEST_CASE( test_BOOST_IS_DEFINED
)
609 #define SYMBOL2 std::cout
610 #define ONE_ARG( arg ) arg
611 #define TWO_ARG( arg1, arg2 ) BOOST_JOIN( arg1, arg2 )
613 BOOST_CHECK( BOOST_IS_DEFINED(SYMBOL1
) );
614 BOOST_CHECK( BOOST_IS_DEFINED(SYMBOL2
) );
615 BOOST_CHECK( !BOOST_IS_DEFINED( SYMBOL3
) );
616 BOOST_CHECK( BOOST_IS_DEFINED( ONE_ARG(arg1
) ) );
617 BOOST_CHECK( !BOOST_IS_DEFINED( ONE_ARG1(arg1
) ) );
618 BOOST_CHECK( BOOST_IS_DEFINED( TWO_ARG(arg1
,arg2
) ) );
619 BOOST_CHECK( !BOOST_IS_DEFINED( TWO_ARG1(arg1
,arg2
) ) );
622 //____________________________________________________________________________//
624 TEST_CASE( test_context_logging
)
626 BOOST_TEST_INFO( "some context" );
627 BOOST_CHECK( false );
630 BOOST_TEST_INFO( "some more context: " << i
);
631 BOOST_CHECK( false );
633 BOOST_TEST_INFO( "info 1" );
634 BOOST_TEST_INFO( "info 2" );
635 BOOST_TEST_INFO( "info 3" );
636 BOOST_CHECK( false );
638 BOOST_TEST_CONTEXT( "some sticky context" ) {
639 BOOST_CHECK( false );
641 BOOST_TEST_INFO( "more context" );
642 BOOST_CHECK( false );
644 BOOST_TEST_INFO( "different subcontext" );
645 BOOST_CHECK( false );
648 BOOST_TEST_CONTEXT( "outer context" ) {
649 BOOST_CHECK( false );
651 BOOST_TEST_CONTEXT( "inner context" ) {
652 BOOST_CHECK( false );
655 BOOST_CHECK( false );
659 //____________________________________________________________________________//
663 FooType
& operator*() { return *this; }
664 operator bool() const { return false; }
665 int operator&() { return 10; }
668 TEST_CASE( test_BOOST_TEST_basic_arithmetic_op
)
670 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
);
680 BOOST_TEST( i
== 2 );
681 BOOST_TEST( i
!= 1 );
684 BOOST_TEST( i
<= 0 );
685 BOOST_TEST( i
>= 5 );
688 BOOST_TEST( i
+j
>= 5 );
689 BOOST_TEST( j
-i
== 2 );
692 BOOST_TEST( *p
== 2 );
694 BOOST_TEST( j
-*p
== 0 );
698 BOOST_TEST( FooType() );
702 BOOST_TEST( &F
> 100 );
703 BOOST_TEST( &*F
> 100 );
705 BOOST_TEST( (i
== 1) & (j
== 1) );
706 BOOST_TEST( (i
== 2) | (j
== 1) );
708 BOOST_TEST(( i
== 1 && j
== 1 ));
709 BOOST_TEST(( i
== 2 || j
== 1 ));
711 // check correct behavior in if clause
715 // check correct behavior in else clause
721 BOOST_TEST( i
+j
==15, "This message reported instead including " << i
<< " and " << j
);
724 // BOOST_TEST( i == 1 && j == 1 );
725 // BOOST_TEST( i == 2 || j == 1 );
726 // BOOST_TEST( i > 5 ? false : true );
729 BOOST_TEST_SPECIALIZED_COLLECTION_COMPARE(std::vector
<int>)
731 TEST_CASE( test_BOOST_TEST_collection_comp
)
738 std::vector
<int> v2
= v
;
745 BOOST_TEST( v
== l
);
746 BOOST_TEST( v
!= v2
);
750 BOOST_TEST( v
<= l
);
751 BOOST_TEST( v
>= l
);
754 //____________________________________________________________________________//
756 namespace boost
{ namespace test_tools
{ namespace tt_detail
{
758 struct print_log_value
<double> {
759 void operator()( std::ostream
& os
, double d
)
761 std::streamsize curr_prec
= os
.precision();
762 os
<< std::setprecision(3) << d
<< std::setprecision( curr_prec
);
767 TEST_CASE( test_BOOST_TEST_fpv_comp
)
769 ScientificNotationExponentOutputNormalizer norm
;
770 boost::ignore_unused( norm
);
773 double d2
= 1.101e-5;
776 BOOST_TEST( tt::fpc_tolerance
<double>() == 0 );
777 BOOST_TEST( d1
== d2
);
778 BOOST_TEST( tt::fpc_tolerance
<double>() == 0 );
779 BOOST_TEST( d1
== d2
, tt::tolerance( 1e-7 ) );
780 BOOST_TEST( tt::fpc_tolerance
<double>() == 0 );
781 BOOST_TEST( d1
!= f1
, tt::tolerance( 1e-7 ) );
782 BOOST_TEST( tt::fpc_tolerance
<double>() == 0 );
783 BOOST_TEST( tt::fpc_tolerance
<float>() == 0 );
785 BOOST_TEST( d1
> d2
);
786 BOOST_TEST( d1
+1./1e20
> d2
, 1e-5% tt::tolerance() );
787 BOOST_TEST( tt::fpc_tolerance
<double>() == 0 );
788 BOOST_TEST( d2
<= d1
, tt::tolerance( tt::fpc::percent_tolerance( 1e-5 ) ) );
789 BOOST_TEST( tt::fpc_tolerance
<double>() == 0 );
791 BOOST_TEST( d1
-1e-5 == 0., tt::tolerance( 1e-7 ) );
792 BOOST_TEST( d1
-1e-5 != 0., tt::tolerance( 1e-4 ) );
793 BOOST_TEST( 0. != 1e-5-d1
, tt::tolerance( 1e-4 ) );
794 BOOST_TEST( d2
-1e-5 < 0., tt::tolerance( 1e-6 ) );
796 const double cd
= 1.0;
797 BOOST_TEST( cd
== 1.0);
798 BOOST_TEST( 1.0 == cd
);
799 BOOST_TEST( cd
== 1.01, 10.% tt::tolerance());
800 BOOST_TEST( 1.01 == cd
, 0.1% tt::tolerance() );
802 BOOST_TEST( 0.0 == 0.0);
805 //____________________________________________________________________________//
807 TEST_CASE( test_BOOST_TEST_cstring_comp
)
809 char const* str1
= "test1";
810 char const* str2
= "test12";
811 std::string str3
= "test2";
812 char str4
[] = "test3";
813 char const* str5
= NULL
;
814 char const* str6
= "1test1";
816 BOOST_TEST( str1
== str2
);
817 BOOST_TEST( str1
== str3
);
818 BOOST_TEST( str3
== str2
);
819 BOOST_TEST( str1
== str4
);
820 BOOST_TEST( str3
== str4
);
821 BOOST_TEST( str1
== str5
);
823 BOOST_TEST( str1
!= (str6
+1) );
824 BOOST_TEST( str5
!= str5
);
826 BOOST_TEST( str3
< str1
);
827 BOOST_TEST( str1
>= str2
);
830 //____________________________________________________________________________//
832 TEST_CASE( string_comparison_per_element
)
834 using namespace boost::test_tools
;
836 std::string s1
= "asdfhjk";
837 std::string s2
= "asdfgjk";
839 BOOST_TEST( s1
== s2
, tt::per_element() );
841 std::string s3
= "hello world";
842 std::string s4
= "helko worlt";
844 BOOST_TEST( s3
== s4
, tt::per_element() );
847 //____________________________________________________________________________//
849 TEST_CASE( test_BOOST_TEST_bitwise
)
854 // decltype is needed for this to work. Not the case for eg. MSVC 2008.
855 BOOST_TEST( a
== b
, tt::bitwise() );
856 BOOST_TEST( c
== b
, tt::bitwise() );
859 //____________________________________________________________________________//
867 struct copy_counter
: boost::noncopyable
{
871 copy_counter( copy_counter
const& ) { s_value
++; }
872 copy_counter( copy_counter
&& ) {}
875 int copy_counter::s_value
= 0;
877 bool operator==( copy_counter
const&, copy_counter
const& ) { return true; }
878 std::ostream
& operator<<( std::ostream
& os
, copy_counter
const& ) { return os
<< "copy_counter"; }
880 BOOST_AUTO_TEST_CASE( test_argument_handling
)
882 BOOST_TEST( goo() == 0 );
883 BOOST_TEST( goo() == 1 );
884 BOOST_TEST( 2 == goo() );
885 BOOST_TEST( 3 == goo() );
886 BOOST_TEST( goo() != 5 );
887 BOOST_TEST( copy_counter() == copy_counter() );
888 BOOST_TEST( copy_counter::s_value
== 0 );
891 //____________________________________________________________________________//
893 BOOST_AUTO_TEST_CASE( test_precision_mutation
, * ut::expected_failures( 1 ) )
895 std::streamsize initial_precition
= std::cout
.precision();
896 std::cout
.precision(initial_precition
);
898 BOOST_TEST( 1.2 == 2.3, 10.% tt::tolerance() );
900 BOOST_TEST( initial_precition
== std::cout
.precision() );
903 //____________________________________________________________________________//
905 // addresses issue #11887
906 #if !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && \
907 !defined(BOOST_NO_CXX11_LAMBDAS) && \
908 !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
909 !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && \
910 !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
912 struct rv_erasure_test
{
913 rv_erasure_test() : value( 1 ) {}
914 ~rv_erasure_test() { value
= 0; }
919 BOOST_AUTO_TEST_CASE( test_rvalue_erasure
)
921 auto erase_rv
= []( rv_erasure_test
const& arg
) -> rv_erasure_test
const& { return arg
; };
923 BOOST_TEST( 1 == erase_rv( rv_erasure_test
{} ).value
);
929 TEST_CASE( test_BOOST_TEST_fpv_comp_on_collections
)
931 ScientificNotationExponentOutputNormalizer norm
;
932 boost::ignore_unused( norm
);
934 using namespace boost::test_tools
;
935 ut::unit_test_log
.set_threshold_level( ut::log_successful_tests
); // we log everything
941 std::vector
<double> v1(3, d1
), v2(3, d2
), v3(3, d2
);
942 std::vector
<float> v1f(3, static_cast<float>(d1
)), v2f(3, static_cast<float>(d2
));
944 BOOST_TEST( d1
== d2
, tt::tolerance( 1e-3 ) );
945 BOOST_TEST( d1
== d2
, tt::tolerance( 1e-2 ) );
946 BOOST_TEST( v1
== v2
, tt::tolerance( 1e-3 ) );
948 // tolerance and per element
949 BOOST_TEST( v1
== v2
, tt::tolerance( 1e-3 ) << tt::per_element() );
950 BOOST_TEST( v1f
== v2f
, tt::tolerance( 1e-3 ) << tt::per_element() ); // tolerance is on double, does not apply to vectors.
951 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-3 ) << tt::per_element() );
952 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-3 ) << tt::per_element() );
953 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-2 ) << tt::per_element() ); // high tolerance
954 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-2 ) << tt::per_element() );
955 BOOST_TEST( v1f
< v2f
, tt::tolerance( 1e-2 ) << tt::per_element() ); // not float tolerance
957 // tolerance and custom message
958 BOOST_TEST( v1
== v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 == v2 tolerance<double> ' " << v1
[0] << " != " << v2
[0] );
959 BOOST_TEST( v1f
== v2f
, tt::tolerance( 1e-3 ) << "'custom error message v1f == v2f tolerance<double> ' " << v1f
[0] << " != " << v2f
[0]);
960 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 <= v2 tolerance<double> ' " << v1
[0] << " > " << v2
[0] );
961 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 < v2 tolerance<double> ' " << v1
[0] << " >= " << v2
[0] );
962 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 <= v2 high_tolerance<double> ' " << v1
[0] << " > " << v2
[0] );
963 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-2 ) << "'custom error message v1 < v2 high_tolerance<double> ' " << v1
[0] << " >= " << v2
[0] );
964 BOOST_TEST( v1f
< v2f
, tt::tolerance( 1e-2 ) << "'custom error message v1f < v2f high_tolerance<double> ' " << v1f
[0] << " >= " << v2f
[0]); // not float tolerance
966 // tolerance, custom message and per element
967 BOOST_TEST( v1
== v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 == v2 tolerance<double> ' " << v1
[0] << " != " << v2
[0] << tt::per_element() );
968 BOOST_TEST( v1f
== v2f
, tt::tolerance( 1e-3 ) << "'custom error message v1f == v2f tolerance<double> ' " << v1f
[0] << " != " << v2f
[0] << tt::per_element() );
969 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 <= v2 tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::per_element() );
970 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 < v2 tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::per_element() );
971 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 <= v2 high_tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::per_element() );
972 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-2 ) << "'custom error message v1 < v2 high_tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::per_element() );
973 BOOST_TEST( v1f
< v2f
, tt::tolerance( 1e-2 ) << "'custom error message v1f < v2f high_tolerance<double> ' " << v1f
[0] << " >= " << v2f
[0] << tt::per_element() ); // not float tolerance
975 // swapping tolerance and custom message
976 BOOST_TEST( v1
== v2
, "'custom error message v1 == v2 tolerance<double> ' " << v1
[0] << " != " << v2
[0] << tt::tolerance( 1e-3 ) << tt::per_element() );
977 BOOST_TEST( v1f
== v2f
, "'custom error message v1f == v2f tolerance<double> ' " << v1f
[0] << " != " << v2f
[0] << tt::tolerance( 1e-3 ) << tt::per_element() );
978 BOOST_TEST( v1
<= v2
, "'custom error message v1 <= v2 tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::tolerance( 1e-3 ) << tt::per_element() );
979 BOOST_TEST( v1
< v2
, "'custom error message v1 < v2 tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::tolerance( 1e-3 ) << tt::per_element() );
980 BOOST_TEST( v1
<= v2
, "'custom error message v1 <= v2 high_tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::tolerance( 1e-3 ) << tt::per_element() );
981 BOOST_TEST( v1
< v2
, "'custom error message v1 < v2 high_tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::tolerance( 1e-2 ) << tt::per_element() );
982 BOOST_TEST( v1f
< v2f
, "'custom error message v1f < v2f high_tolerance<double> ' " << v1f
[0] << " >= " << v2f
[0] << tt::tolerance( 1e-2 ) << tt::per_element() ); // not float tolerance
984 // custom message and per_element
985 BOOST_TEST( v1
== v2
, "'custom error message v1 == v2 ' " << v1
[0] << " != " << v2
[0] << tt::per_element() );
986 BOOST_TEST( v1f
== v2f
, "'custom error message v1f == v2f ' " << v1f
[0] << " != " << v2f
[0] << tt::per_element() );
987 BOOST_TEST( v1
<= v2
, "'custom error message v1 <= v2 ' " << v1
[0] << " > " << v2
[0] << tt::per_element() );
988 BOOST_TEST( v1
< v2
, "'custom error message v1 < v2 ' " << v1
[0] << " >= " << v2
[0] << tt::per_element() );
989 BOOST_TEST( v1f
< v2f
, "'custom error message v1f < v2f ' " << v1f
[0] << " >= " << v2f
[0] << tt::per_element() ); // not float tolerance
991 // tolerance and lexicographic
992 // BOOST_TEST( v1 == v2, tt::tolerance( 1e-3 ) << tt::lexicographic());
993 // BOOST_TEST( v1f == v2f, tt::tolerance( 1e-3 ) << tt::lexicographic()); // tolerance is on double, does not apply to vectors.
995 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-3 ) << tt::lexicographic() );
996 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-3 ) << tt::lexicographic() );
997 BOOST_TEST( v2
<= v3
, tt::tolerance( 1e-3 ) << tt::lexicographic() );
998 BOOST_TEST( v2
< v3
, tt::tolerance( 1e-3 ) << tt::lexicographic() );
999 BOOST_TEST( v3
<= v2
, tt::tolerance( 1e-3 ) << tt::lexicographic() );
1000 BOOST_TEST( v3
< v2
, tt::tolerance( 1e-3 ) << tt::lexicographic() );
1001 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-2 ) << tt::lexicographic() ); // high tolerance
1002 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-2 ) << tt::lexicographic() );
1003 BOOST_TEST( v2
<= v3
, tt::tolerance( 1e-2 ) << tt::lexicographic() );
1004 BOOST_TEST( v2
< v3
, tt::tolerance( 1e-2 ) << tt::lexicographic() );
1005 BOOST_TEST( v3
<= v2
, tt::tolerance( 1e-2 ) << tt::lexicographic() );
1006 BOOST_TEST( v3
< v2
, tt::tolerance( 1e-2 ) << tt::lexicographic() );
1008 // custom and lexico
1009 BOOST_TEST( v1
<= v2
, "'custom error message v1 <= v2 ' " << v1
[0] << " != " << v2
[0] << tt::lexicographic() );
1010 BOOST_TEST( v1
< v2
, "'custom error message v1 < v2 ' " << v1f
[0] << " != " << v2f
[0] << tt::lexicographic() );
1011 BOOST_TEST( v2
<= v3
, "'custom error message v2 <= v3 ' " << v1
[0] << " > " << v2
[0] << tt::lexicographic() );
1012 BOOST_TEST( v2
< v3
, "'custom error message v2 < v3 ' " << v1
[0] << " >= " << v2
[0] << tt::lexicographic() );
1013 BOOST_TEST( v3
<= v2
, "'custom error message v3 <= v2 ' " << v1
[0] << " > " << v2
[0] << tt::lexicographic() );
1014 BOOST_TEST( v3
< v2
, "'custom error message v3 < v2 ' " << v1
[0] << " >= " << v2
[0] << tt::lexicographic() );
1016 // custom, tolerance and lexico
1017 BOOST_TEST( v1
<= v2
, "'custom error message v1 <= v2 tolerance<double> ' " << v1
[0] << " != " << v2
[0] << tt::tolerance( 1e-3 ) << tt::lexicographic() );
1018 BOOST_TEST( v1
< v2
, "'custom error message v1 < v2 tolerance<double> ' " << v1f
[0] << " != " << v2f
[0] << tt::tolerance( 1e-3 ) << tt::lexicographic() );
1019 BOOST_TEST( v2
<= v3
, "'custom error message v2 <= v3 tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::tolerance( 1e-3 ) << tt::lexicographic() );
1020 BOOST_TEST( v2
< v3
, "'custom error message v2 < v3 tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::tolerance( 1e-3 ) << tt::lexicographic() );
1021 BOOST_TEST( v3
<= v2
, "'custom error message v3 <= v2 tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::tolerance( 1e-3 ) << tt::lexicographic() );
1022 BOOST_TEST( v3
< v2
, "'custom error message v3 < v2 tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::tolerance( 1e-3 ) << tt::lexicographic() );
1023 BOOST_TEST( v1
<= v2
, "'custom error message v1 <= v2 high_tolerance<double> ' " << v1
[0] << " != " << v2
[0] << tt::tolerance( 1e-2 ) << tt::lexicographic() ); // high tolerance
1024 BOOST_TEST( v1
< v2
, "'custom error message v1 < v2 high_tolerance<double> ' " << v1f
[0] << " != " << v2f
[0] << tt::tolerance( 1e-2 ) << tt::lexicographic() );
1025 BOOST_TEST( v2
<= v3
, "'custom error message v2 <= v3 high_tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::tolerance( 1e-2 ) << tt::lexicographic() );
1026 BOOST_TEST( v2
< v3
, "'custom error message v2 < v3 high_tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::tolerance( 1e-2 ) << tt::lexicographic() );
1027 BOOST_TEST( v3
<= v2
, "'custom error message v3 <= v2 high_tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::tolerance( 1e-2 ) << tt::lexicographic() );
1028 BOOST_TEST( v3
< v2
, "'custom error message v3 < v2 high_tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::tolerance( 1e-2 ) << tt::lexicographic() );
1030 // swapping custom and tolerance
1031 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 <= v2 tolerance<double> ' " << v1
[0] << " != " << v2
[0] << tt::lexicographic() );
1032 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-3 ) << "'custom error message v1 < v2 tolerance<double> ' " << v1f
[0] << " != " << v2f
[0] << tt::lexicographic() );
1033 BOOST_TEST( v2
<= v3
, tt::tolerance( 1e-3 ) << "'custom error message v2 <= v3 tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::lexicographic() );
1034 BOOST_TEST( v2
< v3
, tt::tolerance( 1e-3 ) << "'custom error message v2 < v3 tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::lexicographic() );
1035 BOOST_TEST( v3
<= v2
, tt::tolerance( 1e-3 ) << "'custom error message v3 <= v2 tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::lexicographic() );
1036 BOOST_TEST( v3
< v2
, tt::tolerance( 1e-3 ) << "'custom error message v3 < v2 tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::lexicographic() );
1037 BOOST_TEST( v1
<= v2
, tt::tolerance( 1e-2 ) << "'custom error message v1 <= v2 high_tolerance<double> ' " << v1
[0] << " != " << v2
[0] << tt::lexicographic() ); // high tolerance
1038 BOOST_TEST( v1
< v2
, tt::tolerance( 1e-2 ) << "'custom error message v1 < v2 high_tolerance<double> ' " << v1f
[0] << " != " << v2f
[0] << tt::lexicographic() );
1039 BOOST_TEST( v2
<= v3
, tt::tolerance( 1e-2 ) << "'custom error message v2 <= v3 high_tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::lexicographic() );
1040 BOOST_TEST( v2
< v3
, tt::tolerance( 1e-2 ) << "'custom error message v2 < v3 high_tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::lexicographic() );
1041 BOOST_TEST( v3
<= v2
, tt::tolerance( 1e-2 ) << "'custom error message v3 <= v2 high_tolerance<double> ' " << v1
[0] << " > " << v2
[0] << tt::lexicographic() );
1042 BOOST_TEST( v3
< v2
, tt::tolerance( 1e-2 ) << "'custom error message v3 < v2 high_tolerance<double> ' " << v1
[0] << " >= " << v2
[0] << tt::lexicographic() );