• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 // Tests swapping an array of arrays of integers by means of boost::swap.
8 
9 #include <boost/utility/swap.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #define BOOST_CHECK BOOST_TEST
12 #define BOOST_CHECK_EQUAL BOOST_TEST_EQ
13 
14 #include <algorithm> //for std::copy and std::equal
15 #include <cstddef> //for std::size_t
16 
main()17 int main()
18 {
19   const std::size_t first_dimension = 3;
20   const std::size_t second_dimension = 4;
21   const std::size_t number_of_elements = first_dimension * second_dimension;
22 
23   int array1[first_dimension][second_dimension];
24   int array2[first_dimension][second_dimension];
25 
26   int* const ptr1 = array1[0];
27   int* const ptr2 = array2[0];
28 
29   for (std::size_t i = 0; i < number_of_elements; ++i)
30   {
31     ptr1[i] = static_cast<int>(i);
32     ptr2[i] = static_cast<int>(i + number_of_elements);
33   }
34 
35   boost::swap(array1, array2);
36 
37   for (std::size_t i = 0; i < number_of_elements; ++i)
38   {
39     BOOST_CHECK_EQUAL(ptr1[i], static_cast<int>(i + number_of_elements) );
40     BOOST_CHECK_EQUAL(ptr2[i], static_cast<int>(i) );
41   }
42 
43   return boost::report_errors();
44 }
45