Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / contract / test / call_if / no_equal_call_if.cpp
1
2 // Copyright (C) 2008-2018 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0 (see accompanying
4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
6
7 // Test assertions skipped when operations to check them missing (e.g., `==`).
8
9 // C++17 warning from Boost.Bind.
10 #define _SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING
11
12 #include <boost/contract/function.hpp>
13 #include <boost/contract/check.hpp>
14 #include <boost/contract/assert.hpp>
15 #include <boost/contract/call_if.hpp>
16 #include <boost/bind.hpp>
17 #include <boost/type_traits/has_equal_to.hpp>
18 #include <boost/detail/lightweight_test.hpp>
19 #include <functional>
20 #include <vector>
21
22 unsigned equal_skips;
23
24 template<typename T>
25 void push_back(std::vector<T>& vect, T const& value) {
26     boost::contract::check c = boost::contract::function()
27         .postcondition([&] {
28             BOOST_CONTRACT_ASSERT(
29                 boost::contract::call_if<boost::has_equal_to<T> >(
30                     boost::bind(std::equal_to<T>(), boost::cref(vect.back()),
31                             boost::cref(value))
32                 // Explicit bool return and [&] needed for MSVC10 lambdas.
33                 ).else_([&] () -> bool { ++equal_skips; return true; })
34             );
35         })
36     ;
37     vect.push_back(value);
38 }
39
40 struct j { // Type without operator==.
41     explicit j(int /* i */) {}
42 };
43
44 int main() {
45     std::vector<int> vi;
46     equal_skips = 0;
47     push_back(vi, 123);
48     BOOST_TEST_EQ(equal_skips, 0);
49         
50     unsigned const cnt =
51         #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
52             1
53         #else
54             0
55         #endif
56     ;
57
58     j jj(456);
59     std::vector<j> vj;
60     equal_skips = 0;
61     push_back(vj, jj);
62     BOOST_TEST_EQ(equal_skips, cnt);
63
64     return boost::report_errors();
65 }
66