Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / contract / example / features / check.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 #include <boost/contract.hpp>
8
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
31 int main() {
32     // Implementation checks (via nullary functor).
33     boost::contract::check c = [] {
34         BOOST_CONTRACT_ASSERT(gcd(12, 28) == 4);
35         BOOST_CONTRACT_ASSERT(gcd(4, 14) == 2);
36     };
37
38     return 0;
39 }
40 //]
41