no_equal_condition_if.cpp 1.8 KB

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