array_of_array_of_int.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) 2008 Joseph Gauterin, Niels Dekker
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Tests swapping an array of arrays of integers by means of boost::swap.
  7. #include <boost/utility/swap.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. #define BOOST_CHECK BOOST_TEST
  10. #define BOOST_CHECK_EQUAL BOOST_TEST_EQ
  11. #include <algorithm> //for std::copy and std::equal
  12. #include <cstddef> //for std::size_t
  13. int main()
  14. {
  15. const std::size_t first_dimension = 3;
  16. const std::size_t second_dimension = 4;
  17. const std::size_t number_of_elements = first_dimension * second_dimension;
  18. int array1[first_dimension][second_dimension];
  19. int array2[first_dimension][second_dimension];
  20. int* const ptr1 = array1[0];
  21. int* const ptr2 = array2[0];
  22. for (std::size_t i = 0; i < number_of_elements; ++i)
  23. {
  24. ptr1[i] = static_cast<int>(i);
  25. ptr2[i] = static_cast<int>(i + number_of_elements);
  26. }
  27. boost::swap(array1, array2);
  28. for (std::size_t i = 0; i < number_of_elements; ++i)
  29. {
  30. BOOST_CHECK_EQUAL(ptr1[i], static_cast<int>(i + number_of_elements) );
  31. BOOST_CHECK_EQUAL(ptr2[i], static_cast<int>(i) );
  32. }
  33. return boost::report_errors();
  34. }