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 #include <cassert> 9 10 //[volatile 11 class u { 12 public: 13 static void static_invariant(); // Static invariants. 14 void invariant() const volatile; // Volatile invariants. 15 void invariant() const; // Const invariants. 16 u()17 u() { // Check static, volatile, and const invariants. 18 boost::contract::check c= boost::contract::constructor(this); 19 } 20 ~u()21 ~u() { // Check static, volatile, and const invariants. 22 boost::contract::check c = boost::contract::destructor(this); 23 } 24 nc()25 void nc() { // Check static and const invariants. 26 boost::contract::check c = boost::contract::public_function(this); 27 } 28 c() const29 void c() const { // Check static and const invariants. 30 boost::contract::check c = boost::contract::public_function(this); 31 } 32 v()33 void v() volatile { // Check static and volatile invariants. 34 boost::contract::check c = boost::contract::public_function(this); 35 } 36 cv() const37 void cv() const volatile { // Check static and volatile invariants. 38 boost::contract::check c = boost::contract::public_function(this); 39 } 40 s()41 static void s() { // Check static invariants only. 42 boost::contract::check c = boost::contract::public_function<u>(); 43 } 44 }; 45 //] 46 47 bool static_inv_checked, cv_inv_checked, const_inv_checked; static_invariant()48void u::static_invariant() { static_inv_checked = true; } invariant() const49void u::invariant() const volatile { cv_inv_checked = true; } invariant() const50void u::invariant() const { const_inv_checked = true; } 51 main()52int main() { 53 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 54 { 55 static_inv_checked = cv_inv_checked = const_inv_checked = false; 56 u x; 57 assert(static_inv_checked); 58 assert(cv_inv_checked); 59 assert(const_inv_checked); 60 61 static_inv_checked = cv_inv_checked = const_inv_checked = false; 62 x.nc(); 63 assert(static_inv_checked); 64 assert(!cv_inv_checked); 65 assert(const_inv_checked); 66 67 static_inv_checked = cv_inv_checked = const_inv_checked = false; 68 x.c(); 69 assert(static_inv_checked); 70 assert(!cv_inv_checked); 71 assert(const_inv_checked); 72 73 static_inv_checked = cv_inv_checked = const_inv_checked = false; 74 x.v(); 75 assert(static_inv_checked); 76 assert(cv_inv_checked); 77 assert(!const_inv_checked); 78 79 static_inv_checked = cv_inv_checked = const_inv_checked = false; 80 x.cv(); 81 assert(static_inv_checked); 82 assert(cv_inv_checked); 83 assert(!const_inv_checked); 84 85 static_inv_checked = cv_inv_checked = const_inv_checked = false; 86 x.s(); 87 assert(static_inv_checked); 88 assert(!cv_inv_checked); 89 assert(!const_inv_checked); 90 91 static_inv_checked = cv_inv_checked = const_inv_checked = false; 92 } // Call destructor. 93 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 94 assert(static_inv_checked); 95 assert(cv_inv_checked); 96 assert(const_inv_checked); 97 #endif 98 #endif 99 100 return 0; 101 } 102 103