smoke.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // Test free function contracts.
  6. #include "../detail/oteststream.hpp"
  7. #include "../detail/counter.hpp"
  8. #include <boost/contract/function.hpp>
  9. #include <boost/contract/old.hpp>
  10. #include <boost/contract/assert.hpp>
  11. #include <boost/contract/check.hpp>
  12. #include <boost/preprocessor/control/iif.hpp>
  13. #include <boost/detail/lightweight_test.hpp>
  14. #include <sstream>
  15. boost::contract::test::detail::oteststream out;
  16. struct x_tag; typedef boost::contract::test::detail::counter<x_tag, int> x_type;
  17. struct y_tag; typedef boost::contract::test::detail::counter<y_tag, int> y_type;
  18. bool swap(x_type& x, y_type& y) {
  19. bool result;
  20. boost::contract::old_ptr<x_type> old_x =
  21. BOOST_CONTRACT_OLDOF(x_type::eval(x));
  22. boost::contract::old_ptr<y_type> old_y;
  23. boost::contract::check c = boost::contract::function()
  24. .precondition([&] {
  25. out << "swap::pre" << std::endl;
  26. BOOST_CONTRACT_ASSERT(x.value != y.value);
  27. })
  28. .old([&] {
  29. out << "swap::old" << std::endl;
  30. old_y = BOOST_CONTRACT_OLDOF(y_type::eval(y));
  31. })
  32. .postcondition([&] {
  33. out << "swap::post" << std::endl;
  34. BOOST_CONTRACT_ASSERT(x.value == old_y->value);
  35. BOOST_CONTRACT_ASSERT(y.value == old_x->value);
  36. BOOST_CONTRACT_ASSERT(result == (old_x->value != old_y->value));
  37. })
  38. ;
  39. out << "swap::body" << std::endl;
  40. if(x.value == y.value) return result = false;
  41. int save_x = x.value;
  42. x.value = y.value;
  43. y.value = save_x;
  44. return result = true;
  45. }
  46. int main() {
  47. std::ostringstream ok;
  48. {
  49. x_type x; x.value = 123;
  50. y_type y; y.value = 456;
  51. out.str("");
  52. bool r = swap(x, y);
  53. ok.str(""); ok
  54. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  55. << "swap::pre" << std::endl
  56. #endif
  57. #ifndef BOOST_CONTRACT_NO_OLDS
  58. << "swap::old" << std::endl
  59. #endif
  60. << "swap::body" << std::endl
  61. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  62. << "swap::post" << std::endl
  63. #endif
  64. ;
  65. BOOST_TEST(out.eq(ok.str()));
  66. BOOST_TEST(r);
  67. BOOST_TEST_EQ(x.value, 456);
  68. BOOST_TEST_EQ(y.value, 123);
  69. }
  70. #ifndef BOOST_CONTRACT_NO_OLDS
  71. #define BOOST_CONTRACT_TEST_old 1u
  72. #else
  73. #define BOOST_CONTRACT_TEST_old 0u
  74. #endif
  75. BOOST_TEST_EQ(x_type::copies(), BOOST_CONTRACT_TEST_old);
  76. BOOST_TEST_EQ(x_type::evals(), BOOST_CONTRACT_TEST_old);
  77. BOOST_TEST_EQ(x_type::ctors(), x_type::dtors()); // No leak.
  78. BOOST_TEST_EQ(y_type::copies(), BOOST_CONTRACT_TEST_old);
  79. BOOST_TEST_EQ(y_type::evals(), BOOST_CONTRACT_TEST_old);
  80. BOOST_TEST_EQ(y_type::ctors(), y_type::dtors()); // No leak.
  81. #undef BOOST_CONTRACT_TEST_old
  82. return boost::report_errors();
  83. }