]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/test/writing-test-ts/test_tools-test.cpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / libs / test / test / writing-test-ts / test_tools-test.cpp
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)
5
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 // File : $RCSfile$
9 //
10 // Version : $Revision$
11 //
12 // Description : tests all Test Tools but output_test_stream
13 // ***************************************************************************
14
15 // Boost.Test
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>
26
27 // Boost
28 #include <boost/bind/bind.hpp>
29 #include <boost/noncopyable.hpp>
30
31 // STL
32 #include <iostream>
33 #include <iomanip>
34
35 #ifdef BOOST_MSVC
36 # pragma warning(push)
37 # pragma warning(disable: 4702) // unreachable code
38 #endif
39
40 #include "../framework-ts/logger-for-tests.hpp"
41
42 namespace ut=boost::unit_test;
43 namespace tt=boost::test_tools;
44
45 // GH-179 char can be unsigned on some archs
46 namespace boost{ namespace test_tools{ namespace tt_detail{
47 template<>
48 struct print_log_value<signed char> {
49 void operator()( std::ostream& ostr, signed char t )
50 {
51 ostr << std::hex
52 #if BOOST_TEST_USE_STD_LOCALE
53 << std::showbase
54 #else
55 << "0x"
56 #endif
57 << static_cast<int>(t);
58 }
59 };
60 }}}
61
62 //____________________________________________________________________________//
63
64 #define CHECK_CRITICAL_TOOL_USAGE( tool_usage ) \
65 { \
66 bool throw_ = false; \
67 try { \
68 tool_usage; \
69 } catch( boost::execution_aborted const& ) { \
70 throw_ = true; \
71 } \
72 \
73 BOOST_CHECK_MESSAGE( throw_, "not aborted" ); \
74 } \
75 /**/
76
77 //____________________________________________________________________________//
78
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)) {}
83
84 ~ScientificNotationExponentOutputNormalizer() { _set_output_format(m_old_format); }
85 private:
86 unsigned m_old_format;
87 };
88 #else
89 struct ScientificNotationExponentOutputNormalizer {};
90 #endif
91
92 //____________________________________________________________________________//
93
94 class bool_convertible
95 {
96 struct Tester {};
97 public:
98 operator Tester*() const { return static_cast<Tester*>( 0 ) + 1; }
99 };
100
101 //____________________________________________________________________________//
102
103 struct shorten_lf : public boost::unit_test::output::compiler_log_formatter
104 {
105 void print_prefix( std::ostream& output, boost::unit_test::const_string, std::size_t line )
106 {
107 }
108 };
109
110 //____________________________________________________________________________//
111
112 std::string match_file_name( "./baseline-outputs/test_tools-test.pattern" );
113 std::string save_file_name( "test_tools-test.pattern" );
114
115 static tt::output_test_stream&
116 ots()
117 {
118 static boost::shared_ptr<tt::output_test_stream> inst;
119
120 if( !inst ) {
121 inst.reset(
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() ) );
125 }
126
127 return *inst;
128 }
129
130 //____________________________________________________________________________//
131
132 #define TEST_CASE( name ) \
133 static void name ## _impl(); \
134 static void name ## _impl_defer(); \
135 \
136 BOOST_AUTO_TEST_CASE( name ) \
137 { \
138 ut::test_case* impl = ut::make_test_case(&name ## _impl, \
139 #name, \
140 __FILE__, \
141 __LINE__ ); \
142 impl->p_default_status.value = ut::test_unit::RS_ENABLED; \
143 \
144 /* to detect for issues concerning the file pattern, we take
145 the stream now */ \
146 tt::output_test_stream& stream = ots(); \
147 \
148 { \
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, \
153 OF_CUSTOM_LOGGER, \
154 ut::log_nothing, \
155 ll != ut::invalid_log_level ? \
156 ll \
157 : ut::log_all_errors ); \
158 \
159 ut::framework::finalize_setup_phase( impl->p_id ); \
160 ut::framework::run( impl ); \
161 \
162 } \
163 \
164 BOOST_CHECK( stream.match_pattern() ); \
165 } \
166 \
167 void name ## _impl() \
168 { \
169 ut::unit_test_log.set_threshold_level( ut::log_all_errors ); \
170 \
171 name ## _impl_defer(); \
172 \
173 ut::unit_test_log.set_threshold_level( ut::log_nothing ); \
174 } \
175 \
176 void name ## _impl_defer() \
177 /**/
178
179 #line 162
180 // should be line 162 _______________________________________________________//
181
182 TEST_CASE( test_BOOST_WARN )
183 {
184 ut::unit_test_log.set_threshold_level( ut::log_warnings );
185 BOOST_WARN( sizeof(int) == sizeof(short) );
186
187 ut::unit_test_log.set_threshold_level( ut::log_successful_tests );
188 BOOST_WARN( sizeof(unsigned char) == sizeof(char) );
189 }
190
191 //____________________________________________________________________________//
192
193 TEST_CASE( test_BOOST_CHECK )
194 {
195 // check correct behavior in if clause
196 if( true )
197 BOOST_CHECK( true );
198
199 // check correct behavior in else clause
200 if( false )
201 {}
202 else
203 BOOST_CHECK( true );
204
205 bool_convertible bc;
206 BOOST_CHECK( bc );
207
208 int i=2;
209
210 BOOST_CHECK( false );
211 BOOST_CHECK( 1==2 );
212 BOOST_CHECK( i==1 );
213
214 ut::unit_test_log.set_threshold_level( ut::log_successful_tests );
215 BOOST_CHECK( i==2 );
216 }
217
218 //____________________________________________________________________________//
219
220 TEST_CASE( test_BOOST_REQUIRE )
221 {
222 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE( true ) );
223
224 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE( false ) );
225
226 int j = 3;
227
228 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE( j > 5 ) );
229
230 ut::unit_test_log.set_threshold_level( ut::log_successful_tests );
231 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE( j < 5 ) );
232 }
233
234 //____________________________________________________________________________//
235
236 TEST_CASE( test_BOOST_WARN_MESSAGE )
237 {
238 BOOST_WARN_MESSAGE( sizeof(int) == sizeof(short), "memory won't be used efficiently" );
239 int obj_size = 33;
240
241 BOOST_WARN_MESSAGE( obj_size <= 8,
242 "object size " << obj_size << " is too big to be efficiently passed by value" );
243
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" );
246 }
247
248 //____________________________________________________________________________//
249
250 boost::test_tools::predicate_result
251 test_pred1()
252 {
253 boost::test_tools::predicate_result res( false );
254
255 res.message() << "Some explanation";
256
257 return res;
258 }
259
260 TEST_CASE( test_BOOST_CHECK_MESSAGE )
261 {
262 BOOST_CHECK_MESSAGE( 2+2 == 5, "Well, may be that what I believe in" );
263
264 BOOST_CHECK_MESSAGE( test_pred1(), "Checking predicate failed" );
265
266 ut::unit_test_log.set_threshold_level( ut::log_successful_tests );
267 BOOST_CHECK_MESSAGE( 2+2 == 4, "Could it fail?" );
268
269 int i = 1;
270 int j = 2;
271 std::string msg = "some explanation";
272 BOOST_CHECK_MESSAGE( i > j, "Comparing " << i << " and " << j << ": " << msg );
273 }
274
275 //____________________________________________________________________________//
276
277 TEST_CASE( test_BOOST_REQUIRE_MESSAGE )
278 {
279 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_MESSAGE( false, "Here we should stop" ) );
280
281 ut::unit_test_log.set_threshold_level( ut::log_successful_tests );
282 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_MESSAGE( true, "That's OK" ) );
283 }
284
285 //____________________________________________________________________________//
286
287 TEST_CASE( test_BOOST_ERROR )
288 {
289 BOOST_ERROR( "Fail to miss an error" );
290 }
291
292 //____________________________________________________________________________//
293
294 TEST_CASE( test_BOOST_FAIL )
295 {
296 CHECK_CRITICAL_TOOL_USAGE( BOOST_FAIL( "No! No! Show must go on." ) );
297 }
298
299 //____________________________________________________________________________//
300
301 struct my_exception {
302 explicit my_exception( int ec = 0 ) : m_error_code( ec ) {}
303
304 int m_error_code;
305 };
306
307 bool is_critical( my_exception const& ex ) { return ex.m_error_code < 0; }
308
309 TEST_CASE( test_BOOST_CHECK_THROW )
310 {
311 int i=0;
312 BOOST_CHECK_THROW( i++, my_exception );
313
314 ut::unit_test_log.set_threshold_level( ut::log_warnings );
315 BOOST_WARN_THROW( i++, my_exception );
316
317 ut::unit_test_log.set_threshold_level( ut::log_all_errors );
318 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_THROW( i++, my_exception ) );
319
320 ut::unit_test_log.set_threshold_level( ut::log_successful_tests );
321 if( i/10 > 10 )
322 {}
323 else
324 BOOST_CHECK_THROW( throw my_exception(), my_exception ); // unreachable code warning is expected
325 }
326
327 //____________________________________________________________________________//
328
329 TEST_CASE( test_BOOST_CHECK_EXCEPTION )
330 {
331 BOOST_CHECK_EXCEPTION( throw my_exception( 1 ), my_exception, is_critical ); // unreachable code warning is expected
332
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
335 }
336
337 //____________________________________________________________________________//
338
339 TEST_CASE( test_BOOST_CHECK_NO_THROW )
340 {
341 int i=0;
342 if( i*10 == 0 )
343 BOOST_CHECK_NO_THROW( i++ );
344 else {}
345
346 BOOST_CHECK_NO_THROW( throw my_exception() ); // unreachable code warning is expected
347 }
348
349 //____________________________________________________________________________//
350
351 struct B {
352 B( int i ) : m_i( i ) {}
353
354 int m_i;
355 };
356
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 << ")"; }
359
360 //____________________________________________________________________________//
361
362 struct C {
363 C( int i, int id ) : m_i( i ), m_id( id ) {}
364
365 int m_i;
366 int m_id;
367 };
368
369 boost::test_tools::predicate_result
370 operator==( C const& c1, C const& c2 )
371 {
372 boost::test_tools::predicate_result res( c1.m_i == c2.m_i && c1.m_id == c2.m_id );
373
374 if( !res ) {
375 if( c1.m_i != c2.m_i )
376 res.message() << "Index mismatch";
377 else
378 res.message() << "Id mismatch";
379 }
380
381 return res;
382 }
383
384 std::ostream& operator<<( std::ostream& str, C const& c ) { return str << "C(" << c.m_i << ',' << c.m_id << ")"; }
385
386 //____________________________________________________________________________//
387
388 TEST_CASE( test_BOOST_CHECK_EQUAL )
389 {
390 int i=1;
391 int j=2;
392 BOOST_CHECK_EQUAL( i, j );
393 BOOST_CHECK_EQUAL( ++i, j );
394 BOOST_CHECK_EQUAL( i++, j );
395
396 char const* str1 = "test1";
397 char const* str2 = "test12";
398 BOOST_CHECK_EQUAL( str1, str2 );
399
400 ut::unit_test_log.set_threshold_level( ut::log_successful_tests );
401 BOOST_CHECK_EQUAL( i+1, j );
402
403 char const* str3 = "1test1";
404 BOOST_CHECK_EQUAL( str1, str3+1 );
405
406 ut::unit_test_log.set_threshold_level( ut::log_all_errors );
407 str1 = NULL;
408 str2 = NULL;
409 BOOST_CHECK_EQUAL( str1, str2 );
410
411 str1 = "test";
412 str2 = NULL;
413 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_EQUAL( str1, str2 ) );
414
415 B b1(1);
416 B b2(2);
417
418 ut::unit_test_log.set_threshold_level( ut::log_warnings );
419 BOOST_WARN_EQUAL( b1, b2 );
420
421 ut::unit_test_log.set_threshold_level( ( ut::log_all_errors ) );
422 C c1( 0, 100 );
423 C c2( 0, 101 );
424 C c3( 1, 102 );
425 BOOST_CHECK_EQUAL( c1, c3 );
426 BOOST_CHECK_EQUAL( c1, c2 );
427
428 signed char ch1 = -2; // char is unsigned on some archs GH-179
429 signed char ch2 = -3;
430 BOOST_CHECK_EQUAL(ch1, ch2);
431 }
432
433 //____________________________________________________________________________//
434
435 TEST_CASE( test_BOOST_CHECK_LOGICAL_EXPR )
436 {
437 int i=1;
438 int j=2;
439 BOOST_CHECK_NE( i, j );
440
441 BOOST_CHECK_NE( ++i, j );
442
443 BOOST_CHECK_LT( i, j );
444 BOOST_CHECK_GT( i, j );
445
446 BOOST_CHECK_LE( i, j );
447 BOOST_CHECK_GE( i, j );
448
449 ++i;
450
451 BOOST_CHECK_LE( i, j );
452 BOOST_CHECK_GE( j, i );
453
454 char const* str1 = "test1";
455 char const* str2 = "test1";
456
457 BOOST_CHECK_NE( str1, str2 );
458 }
459
460 //____________________________________________________________________________//
461
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; }
465
466 BOOST_TEST_DONT_PRINT_LOG_VALUE( std::list<int> )
467
468 boost::test_tools::assertion_result
469 compare_lists( std::list<int> const& l1, std::list<int> const& l2 )
470 {
471 if( l1.size() != l2.size() ) {
472 boost::test_tools::predicate_result res( false );
473
474 res.message() << "Different sizes [" << l1.size() << "!=" << l2.size() << "]";
475
476 return res;
477 }
478
479 return true;
480 }
481
482 TEST_CASE( test_BOOST_CHECK_PREDICATE )
483 {
484 BOOST_CHECK_PREDICATE( is_even, (14) );
485
486 int i = 17;
487 BOOST_CHECK_PREDICATE( is_even, (i) );
488
489 using std::not_equal_to;
490 BOOST_CHECK_PREDICATE( not_equal_to<int>(), (i)(17) );
491
492 using namespace boost::placeholders;
493 int j=15;
494 BOOST_CHECK_PREDICATE( boost::bind( is_even, boost::bind( &foo, _1, _2 ) ), (i)(j) );
495
496 ut::unit_test_log.set_threshold_level( ut::log_warnings );
497 BOOST_WARN_PREDICATE( moo, (12)(i)(j) );
498
499 ut::unit_test_log.set_threshold_level( ( ut::log_all_errors ) );
500 std::list<int> l1, l2, l3;
501 l1.push_back( 1 );
502 l3.push_back( 1 );
503 l1.push_back( 2 );
504 l3.push_back( 3 );
505 BOOST_CHECK_PREDICATE( compare_lists, (l1)(l2) );
506 }
507
508 //____________________________________________________________________________//
509
510 TEST_CASE( test_BOOST_REQUIRE_PREDICATE )
511 {
512 int arg1 = 1;
513 int arg2 = 2;
514
515 using std::less_equal;
516 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_PREDICATE( less_equal<int>(), (arg1)(arg2) ) );
517
518 CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_PREDICATE( less_equal<int>(), (arg2)(arg1) ) );
519 }
520
521 //____________________________________________________________________________//
522
523 TEST_CASE( test_BOOST_CHECK_EQUAL_COLLECTIONS )
524 {
525 ut::unit_test_log.set_threshold_level( ( ut::log_all_errors ) );
526
527 int pattern [] = { 1, 2, 3, 4, 5, 6, 7 };
528
529 std::list<int> testlist;
530
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 );
538
539 BOOST_CHECK_EQUAL_COLLECTIONS( testlist.begin(), testlist.end(), pattern, pattern+7 );
540 BOOST_CHECK_EQUAL_COLLECTIONS( testlist.begin(), testlist.end(), pattern, pattern+2 );
541 }
542
543 //____________________________________________________________________________//
544
545 TEST_CASE( test_BOOST_CHECK_BITWISE_EQUAL )
546 {
547 BOOST_CHECK_BITWISE_EQUAL( 0x16, 0x16 );
548
549 BOOST_CHECK_BITWISE_EQUAL( (char)0x06, (char)0x16 );
550
551 ut::unit_test_log.set_threshold_level( ut::log_warnings );
552 BOOST_WARN_BITWISE_EQUAL( (char)0x26, (char)0x04 );
553
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 ) );
556 }
557
558 //____________________________________________________________________________//
559
560 struct A {
561 friend std::ostream& operator<<( std::ostream& str, A const& ) { str << "struct A"; return str;}
562 };
563
564 TEST_CASE( test_BOOST_TEST_MESSAGE )
565 {
566 ut::unit_test_log.set_threshold_level( ut::log_messages );
567
568 BOOST_TEST_MESSAGE( "still testing" );
569 BOOST_TEST_MESSAGE( "1+1=" << 2 );
570
571 int i = 2;
572 BOOST_TEST_MESSAGE( i << "+" << i << "=" << (i+i) );
573
574 A a = A();
575 BOOST_TEST_MESSAGE( a );
576
577 #if BOOST_TEST_USE_STD_LOCALE
578 BOOST_TEST_MESSAGE( std::hex << std::showbase << 20 );
579 #else
580 BOOST_TEST_MESSAGE( "0x14" );
581 #endif
582
583 BOOST_TEST_MESSAGE( std::setw( 4 ) << 20 );
584 }
585
586 //____________________________________________________________________________//
587
588 TEST_CASE( test_BOOST_TEST_CHECKPOINT )
589 {
590 BOOST_TEST_CHECKPOINT( "Going to do a silly things" );
591
592 throw "some error";
593 }
594
595 //____________________________________________________________________________//
596
597 bool foo() { throw 1; return true; }
598
599 TEST_CASE( test_BOOST_TEST_PASSPOINT )
600 {
601 BOOST_CHECK( foo() );
602 }
603
604 //____________________________________________________________________________//
605
606 BOOST_AUTO_TEST_CASE( test_BOOST_IS_DEFINED )
607 {
608 #define SYMBOL1
609 #define SYMBOL2 std::cout
610 #define ONE_ARG( arg ) arg
611 #define TWO_ARG( arg1, arg2 ) BOOST_JOIN( arg1, arg2 )
612
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) ) );
620 }
621
622 //____________________________________________________________________________//
623
624 TEST_CASE( test_context_logging )
625 {
626 BOOST_TEST_INFO( "some context" );
627 BOOST_CHECK( false );
628
629 int i = 12;
630 BOOST_TEST_INFO( "some more context: " << i );
631 BOOST_CHECK( false );
632
633 BOOST_TEST_INFO( "info 1" );
634 BOOST_TEST_INFO( "info 2" );
635 BOOST_TEST_INFO( "info 3" );
636 BOOST_CHECK( false );
637
638 BOOST_TEST_CONTEXT( "some sticky context" ) {
639 BOOST_CHECK( false );
640
641 BOOST_TEST_INFO( "more context" );
642 BOOST_CHECK( false );
643
644 BOOST_TEST_INFO( "different subcontext" );
645 BOOST_CHECK( false );
646 }
647
648 BOOST_TEST_CONTEXT( "outer context" ) {
649 BOOST_CHECK( false );
650
651 BOOST_TEST_CONTEXT( "inner context" ) {
652 BOOST_CHECK( false );
653 }
654
655 BOOST_CHECK( false );
656 }
657 }
658
659 //____________________________________________________________________________//
660
661 class FooType {
662 public:
663 FooType& operator*() { return *this; }
664 operator bool() const { return false; }
665 int operator&() { return 10; }
666 };
667
668 TEST_CASE( test_BOOST_TEST_basic_arithmetic_op )
669 {
670 ut::unit_test_log.set_threshold_level( ut::log_successful_tests );
671
672 BOOST_TEST( true );
673 BOOST_TEST( false );
674
675 bool_convertible bc;
676 BOOST_TEST( bc );
677
678 int i = 1;
679
680 BOOST_TEST( i == 2 );
681 BOOST_TEST( i != 1 );
682 BOOST_TEST( i > 2 );
683 BOOST_TEST( i < 1 );
684 BOOST_TEST( i <= 0 );
685 BOOST_TEST( i >= 5 );
686
687 int j = 2;
688 BOOST_TEST( i+j >= 5 );
689 BOOST_TEST( j-i == 2 );
690
691 int* p = &i;
692 BOOST_TEST( *p == 2 );
693
694 BOOST_TEST( j-*p == 0 );
695
696 FooType F;
697
698 BOOST_TEST( FooType() );
699 BOOST_TEST( *F );
700 BOOST_TEST( **F );
701 BOOST_TEST( ***F );
702 BOOST_TEST( &F > 100 );
703 BOOST_TEST( &*F > 100 );
704
705 BOOST_TEST( (i == 1) & (j == 1) );
706 BOOST_TEST( (i == 2) | (j == 1) );
707
708 BOOST_TEST(( i == 1 && j == 1 ));
709 BOOST_TEST(( i == 2 || j == 1 ));
710
711 // check correct behavior in if clause
712 if( true )
713 BOOST_TEST( true );
714
715 // check correct behavior in else clause
716 if( false )
717 {}
718 else
719 BOOST_TEST( true );
720
721 BOOST_TEST( i+j==15, "This message reported instead including " << i << " and " << j );
722
723 // Does not work
724 // BOOST_TEST( i == 1 && j == 1 );
725 // BOOST_TEST( i == 2 || j == 1 );
726 // BOOST_TEST( i > 5 ? false : true );
727 }
728
729 BOOST_TEST_SPECIALIZED_COLLECTION_COMPARE(std::vector<int>)
730
731 TEST_CASE( test_BOOST_TEST_collection_comp )
732 {
733 std::vector<int> v;
734 v.push_back( 1 );
735 v.push_back( 2 );
736 v.push_back( 3 );
737
738 std::vector<int> v2 = v;
739
740 std::vector<int> l;
741 l.push_back( 1 );
742 l.push_back( 3 );
743 l.push_back( 2 );
744
745 BOOST_TEST( v == l );
746 BOOST_TEST( v != v2 );
747 BOOST_TEST( v < l );
748 BOOST_TEST( v > l );
749
750 BOOST_TEST( v <= l );
751 BOOST_TEST( v >= l );
752 }
753
754 //____________________________________________________________________________//
755
756 namespace boost{ namespace test_tools{ namespace tt_detail{
757 template<>
758 struct print_log_value<double> {
759 void operator()( std::ostream& os, double d )
760 {
761 std::streamsize curr_prec = os.precision();
762 os << std::setprecision(3) << d << std::setprecision( curr_prec );
763 }
764 };
765 }}}
766
767 TEST_CASE( test_BOOST_TEST_fpv_comp )
768 {
769 ScientificNotationExponentOutputNormalizer norm;
770 boost::ignore_unused( norm );
771
772 double d1 = 1.1e-5;
773 double d2 = 1.101e-5;
774 float f1 = 1.1e-5f;
775
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 );
784
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 );
790
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 ) );
795
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() );
801
802 BOOST_TEST( 0.0 == 0.0);
803 }
804
805 //____________________________________________________________________________//
806
807 TEST_CASE( test_BOOST_TEST_cstring_comp )
808 {
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";
815
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 );
822
823 BOOST_TEST( str1 != (str6+1) );
824 BOOST_TEST( str5 != str5 );
825
826 BOOST_TEST( str3 < str1 );
827 BOOST_TEST( str1 >= str2 );
828 }
829
830 //____________________________________________________________________________//
831
832 TEST_CASE( string_comparison_per_element )
833 {
834 using namespace boost::test_tools;
835
836 std::string s1 = "asdfhjk";
837 std::string s2 = "asdfgjk";
838
839 BOOST_TEST( s1 == s2, tt::per_element() );
840
841 std::string s3 = "hello world";
842 std::string s4 = "helko worlt";
843
844 BOOST_TEST( s3 == s4, tt::per_element() );
845 }
846
847 //____________________________________________________________________________//
848
849 TEST_CASE( test_BOOST_TEST_bitwise )
850 {
851 int a = 0xAB;
852 int b = 0x88;
853 short c = 0x8A;
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() );
857 }
858
859 //____________________________________________________________________________//
860
861 int goo()
862 {
863 static int i = 0;
864 return i++;
865 }
866
867 struct copy_counter : boost::noncopyable {
868 static int s_value;
869
870 copy_counter() {}
871 copy_counter( copy_counter const& ) { s_value++; }
872 copy_counter( copy_counter&& ) {}
873 };
874
875 int copy_counter::s_value = 0;
876
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"; }
879
880 BOOST_AUTO_TEST_CASE( test_argument_handling )
881 {
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 );
889 }
890
891 //____________________________________________________________________________//
892
893 BOOST_AUTO_TEST_CASE( test_precision_mutation, * ut::expected_failures( 1 ) )
894 {
895 std::streamsize initial_precition = std::cout.precision();
896 std::cout.precision(initial_precition);
897
898 BOOST_TEST( 1.2 == 2.3, 10.% tt::tolerance() );
899
900 BOOST_TEST( initial_precition == std::cout.precision() );
901 }
902
903 //____________________________________________________________________________//
904
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)
911
912 struct rv_erasure_test {
913 rv_erasure_test() : value( 1 ) {}
914 ~rv_erasure_test() { value = 0; }
915
916 int value;
917 };
918
919 BOOST_AUTO_TEST_CASE( test_rvalue_erasure )
920 {
921 auto erase_rv = []( rv_erasure_test const& arg ) -> rv_erasure_test const& { return arg; };
922
923 BOOST_TEST( 1 == erase_rv( rv_erasure_test{} ).value );
924 }
925
926 #endif
927
928
929 TEST_CASE( test_BOOST_TEST_fpv_comp_on_collections )
930 {
931 ScientificNotationExponentOutputNormalizer norm;
932 boost::ignore_unused( norm );
933
934 using namespace boost::test_tools;
935 ut::unit_test_log.set_threshold_level( ut::log_successful_tests ); // we log everything
936
937 double d1 = 1.1e-5;
938 double d2 = 1.11e-5;
939 float f1 = 1.1e-5f;
940
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));
943
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 ) );
947
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
956
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
965
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
974
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
983
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
990
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.
994 v3[1] = d1;
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() );
1007
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() );
1015
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() );
1029
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() );
1043 }
1044
1045
1046 // EOF