1 | #include <boost/config.hpp> |
2 | |
3 | #if defined(BOOST_MSVC) |
4 | #pragma warning(disable: 4786) // identifier truncated in debug info |
5 | #pragma warning(disable: 4710) // function not inlined |
6 | #pragma warning(disable: 4711) // function selected for automatic inline expansion |
7 | #pragma warning(disable: 4514) // unreferenced inline removed |
8 | #endif |
9 | |
10 | // |
11 | // bind_and_or_test.cpp - &&, || operators |
12 | // |
13 | // Copyright (c) 2008 Peter Dimov |
14 | // |
15 | // Distributed under the Boost Software License, Version 1.0. (See |
16 | // accompanying file LICENSE_1_0.txt or copy at |
17 | // http://www.boost.org/LICENSE_1_0.txt) |
18 | // |
19 | |
20 | #include <boost/bind/bind.hpp> |
21 | #include <boost/core/lightweight_test.hpp> |
22 | |
23 | using namespace boost::placeholders; |
24 | |
25 | // |
26 | |
27 | bool f( bool x ) |
28 | { |
29 | return x; |
30 | } |
31 | |
32 | bool g( bool x ) |
33 | { |
34 | return !x; |
35 | } |
36 | |
37 | bool h() |
38 | { |
39 | BOOST_ERROR( "Short-circuit evaluation failure" ); |
40 | return false; |
41 | } |
42 | |
43 | template< class F, class A1, class A2, class R > void test( F f, A1 a1, A2 a2, R r ) |
44 | { |
45 | BOOST_TEST( f( a1, a2 ) == r ); |
46 | } |
47 | |
48 | int main() |
49 | { |
50 | // && |
51 | |
52 | test( f: boost::bind( f, a1: true ) && boost::bind( f: g, a1: true ), a1: false, a2: false, r: f( x: true ) && g( x: true ) ); |
53 | test( f: boost::bind( f, a1: true ) && boost::bind( f: g, a1: false ), a1: false, a2: false, r: f( x: true ) && g( x: false ) ); |
54 | |
55 | test( f: boost::bind( f, a1: false ) && boost::bind( f: h ), a1: false, a2: false, r: f( x: false ) && h() ); |
56 | |
57 | test( f: boost::bind( f, a1: _1 ) && boost::bind( f: g, a1: _2 ), a1: true, a2: true, r: f( x: true ) && g( x: true ) ); |
58 | test( f: boost::bind( f, a1: _1 ) && boost::bind( f: g, a1: _2 ), a1: true, a2: false, r: f( x: true ) && g( x: false ) ); |
59 | |
60 | test( f: boost::bind( f, a1: _1 ) && boost::bind( f: h ), a1: false, a2: false, r: f( x: false ) && h() ); |
61 | |
62 | // || |
63 | |
64 | test( f: boost::bind( f, a1: false ) || boost::bind( f: g, a1: true ), a1: false, a2: false, r: f( x: false ) || g( x: true ) ); |
65 | test( f: boost::bind( f, a1: false ) || boost::bind( f: g, a1: false ), a1: false, a2: false, r: f( x: false ) || g( x: false ) ); |
66 | |
67 | test( f: boost::bind( f, a1: true ) || boost::bind( f: h ), a1: false, a2: false, r: f( x: true ) || h() ); |
68 | |
69 | test( f: boost::bind( f, a1: _1 ) || boost::bind( f: g, a1: _2 ), a1: false, a2: true, r: f( x: false ) || g( x: true ) ); |
70 | test( f: boost::bind( f, a1: _1 ) || boost::bind( f: g, a1: _2 ), a1: false, a2: false, r: f( x: false ) || g( x: false ) ); |
71 | |
72 | test( f: boost::bind( f, a1: _1 ) || boost::bind( f: h ), a1: true, a2: false, r: f( x: true ) || h() ); |
73 | |
74 | // |
75 | |
76 | return boost::report_errors(); |
77 | } |
78 | |