check_macro.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. #include <boost/contract.hpp>
  6. int gcd(int const a, int const b) {
  7. int result;
  8. boost::contract::check c = boost::contract::function()
  9. .precondition([&] {
  10. BOOST_CONTRACT_ASSERT(a > 0);
  11. BOOST_CONTRACT_ASSERT(b > 0);
  12. })
  13. .postcondition([&] {
  14. BOOST_CONTRACT_ASSERT(result <= a);
  15. BOOST_CONTRACT_ASSERT(result <= b);
  16. })
  17. ;
  18. int x = a, y = b;
  19. while(x != y) {
  20. if(x > y) x = x - y;
  21. else y = y - x;
  22. }
  23. return result = x;
  24. }
  25. //[check_macro
  26. int main() {
  27. // Implementation checks (via macro, disable run-/compile-time overhead).
  28. BOOST_CONTRACT_CHECK(gcd(12, 28) == 4);
  29. BOOST_CONTRACT_CHECK(gcd(4, 14) == 2);
  30. return 0;
  31. }
  32. //]