if_copyable.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 old values of non-copyable types.
  6. #include "if_copyable.hpp"
  7. #include <boost/contract/public_function.hpp>
  8. #include <boost/contract/function.hpp>
  9. #include <boost/contract/base_types.hpp>
  10. #include <boost/contract/override.hpp>
  11. #include <boost/contract/old.hpp>
  12. #include <boost/contract/check.hpp>
  13. #include <boost/contract/assert.hpp>
  14. #include <boost/noncopyable.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. unsigned old_checks;
  17. template<typename T>
  18. struct b {
  19. virtual void next(T& x, boost::contract::virtual_* v = 0) {
  20. boost::contract::old_ptr_if_copyable<T> old_x =
  21. BOOST_CONTRACT_OLDOF(v, x);
  22. boost::contract::check c = boost::contract::public_function(v, this)
  23. .postcondition([&] {
  24. if(old_x) {
  25. BOOST_CONTRACT_ASSERT(x == *old_x + T(1));
  26. ++old_checks;
  27. }
  28. })
  29. ;
  30. ++x;
  31. }
  32. BOOST_CONTRACT_OVERRIDE(next)
  33. };
  34. template<typename T>
  35. struct a
  36. #define BASES public b<T>
  37. : BASES
  38. {
  39. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  40. #undef BASES
  41. virtual void next(T& x, boost::contract::virtual_* v = 0) /* override */ {
  42. boost::contract::old_ptr_if_copyable<T> old_x =
  43. BOOST_CONTRACT_OLDOF(v, x);
  44. boost::contract::check c = boost::contract::public_function<
  45. override_next>(v, &a::next, this, x)
  46. .postcondition([&] {
  47. if(old_x) {
  48. BOOST_CONTRACT_ASSERT(x == *old_x + T(1));
  49. ++old_checks;
  50. }
  51. })
  52. ;
  53. ++x;
  54. }
  55. BOOST_CONTRACT_OVERRIDE(next)
  56. };
  57. template<typename T>
  58. void next(T& x) {
  59. boost::contract::old_ptr_if_copyable<T> old_x = BOOST_CONTRACT_OLDOF(x);
  60. boost::contract::check c = boost::contract::function()
  61. .postcondition([&] {
  62. if(old_x) {
  63. BOOST_CONTRACT_ASSERT(x == *old_x + T(1));
  64. ++old_checks;
  65. }
  66. })
  67. ;
  68. ++x;
  69. }
  70. int main() {
  71. int i = 1; // Test built-in copyable type.
  72. cp c(1); // Test custom copyable type.
  73. ncp n(1); // Test non-copyable type.
  74. // Test free functions (old values without `v`).
  75. unsigned cnt =
  76. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  77. 1
  78. #else
  79. 0
  80. #endif
  81. ;
  82. old_checks = 0;
  83. next(i);
  84. BOOST_TEST_EQ(old_checks, cnt);
  85. old_checks = 0;
  86. next(c);
  87. BOOST_TEST_EQ(old_checks, cnt);
  88. old_checks = 0;
  89. next(n);
  90. BOOST_TEST_EQ(old_checks, 0u);
  91. // Test virtual functions (old values with `v`).
  92. cnt =
  93. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  94. 2
  95. #else
  96. 0
  97. #endif
  98. ;
  99. a<int> ai;
  100. old_checks = 0;
  101. ai.next(i);
  102. BOOST_TEST_EQ(old_checks, cnt);
  103. a<cp> ac;
  104. old_checks = 0;
  105. ac.next(c);
  106. BOOST_TEST_EQ(old_checks, cnt);
  107. a<ncp> an;
  108. old_checks = 0;
  109. an.next(n);
  110. BOOST_TEST_EQ(old_checks, 0u);
  111. return boost::report_errors();
  112. }