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