sum.cpp 734 B

123456789101112131415161718192021222324252627282930
  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. //[n1962_sum
  6. #include <boost/contract.hpp>
  7. #include <cassert>
  8. int sum(int count, int* array) {
  9. int result;
  10. boost::contract::check c = boost::contract::function()
  11. .precondition([&] {
  12. BOOST_CONTRACT_ASSERT(count % 4 == 0);
  13. })
  14. ;
  15. result = 0;
  16. for(int i = 0; i < count; ++i) result += array[i];
  17. return result;
  18. }
  19. int main() {
  20. int a[4] = {1, 2, 3, 4};
  21. assert(sum(4, a) == 10);
  22. return 0;
  23. }
  24. //]