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 // Test public static member function contracts. 8 9 #include "../detail/oteststream.hpp" 10 #include <boost/contract/base_types.hpp> 11 #include <boost/contract/public_function.hpp> 12 #include <boost/contract/check.hpp> 13 #include <boost/detail/lightweight_test.hpp> 14 #include <sstream> 15 16 boost::contract::test::detail::oteststream out; 17 18 struct b { static_invariantb19 static void static_invariant() { out << "b::static_inv" << std::endl; } invariantb20 void invariant() const { out << "b::inv" << std::endl; } 21 fb22 static void f() { 23 boost::contract::check c = boost::contract::public_function<b>() 24 .precondition([] { out << "b::f::pre" << std::endl; }) 25 .old([] { out << "b::f::old" << std::endl; }) 26 .postcondition([] { out << "b::f::post" << std::endl; }) 27 ; 28 out << "b::f::body" << std::endl; 29 } 30 }; 31 32 struct a 33 #define BASES public b 34 : BASES 35 { 36 typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; 37 #undef BASES 38 static_invarianta39 static void static_invariant() { out << "a::static_inv" << std::endl; } invarianta40 void invariant() const { out << "a::inv" << std::endl; } 41 fa42 static void f() { 43 boost::contract::check c = boost::contract::public_function<a>() 44 .precondition([] { out << "a::f::pre" << std::endl; }) 45 .old([] { out << "a::f::old" << std::endl; }) 46 .postcondition([] { out << "a::f::post" << std::endl; }) 47 ; 48 out << "a::f::body" << std::endl; 49 } 50 }; 51 main()52int main() { 53 std::ostringstream ok; 54 55 out.str(""); 56 a::f(); 57 ok.str(""); ok 58 // Static so no object thus only static inv, plus never virtual so subst 59 // principle does not apply and no subcontracting. 60 #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS 61 << "a::static_inv" << std::endl 62 #endif 63 #ifndef BOOST_CONTRACT_NO_PRECONDITIONS 64 << "a::f::pre" << std::endl 65 #endif 66 #ifndef BOOST_CONTRACT_NO_OLDS 67 << "a::f::old" << std::endl 68 #endif 69 << "a::f::body" << std::endl 70 #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS 71 << "a::static_inv" << std::endl 72 #endif 73 #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS 74 // No old call here because not base object. 75 << "a::f::post" << std::endl 76 #endif 77 ; 78 BOOST_TEST(out.eq(ok.str())); 79 80 return boost::report_errors(); 81 } 82 83