missing_check.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 missing contract check declaration gives run-time error.
  6. struct err {};
  7. #ifndef BOOST_CONTRACT_ON_MISSING_CHECK_DECL
  8. #error "build must define ON_MISSING_CHECK_DECL=`{ throw err(); }`"
  9. #endif
  10. #include <boost/contract/function.hpp>
  11. #include <boost/contract/check.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. int main() {
  14. boost::contract::check c = boost::contract::function() // Test this is OK.
  15. .precondition([] {})
  16. .old([] {})
  17. .postcondition([] {})
  18. ;
  19. try {
  20. boost::contract::function() // Test no `check c = ...` errors.
  21. .precondition([] {})
  22. .old([] {})
  23. .postcondition([] {})
  24. ;
  25. #if !defined(BOOST_CONTRACT_NO_PRECONDITIONS) || \
  26. !defined(BOOST_CONTRACT_NO_POSTCONDITIONS) || \
  27. !defined(BOOST_CONTRACT_NO_INVARIANTS)
  28. BOOST_TEST(false); // Error, must throw.
  29. #endif
  30. } catch(err const&) {
  31. // OK, threw as expected.
  32. } catch(...) {
  33. BOOST_TEST(false); // Error, unexpected throw.
  34. }
  35. return boost::report_errors();
  36. }