Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / utility / swap / test / array_of_array_of_class.cpp
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 swap_test_class objects by means of boost::swap.
8
9 #include <boost/utility/swap.hpp>
10 #define BOOST_INCLUDE_MAIN
11 #include <boost/test/test_tools.hpp>
12
13 //Put test class in the global namespace
14 #include "./swap_test_class.hpp"
15
16 #include <algorithm> //for std::copy and std::equal
17 #include <cstddef> //for std::size_t
18
19 //Provide swap function in both the namespace of swap_test_class
20 //(which is the global namespace), and the std namespace.
21 //It's common to provide a swap function for a class in both
22 //namespaces. Scott Meyers recommends doing so: Effective C++,
23 //Third Edition, item 25, "Consider support for a non-throwing swap".
24 void swap(swap_test_class& left, swap_test_class& right)
25 {
26   left.swap(right);
27 }
28
29 namespace std
30 {
31   template <>
32   void swap(swap_test_class& left, swap_test_class& right)
33   {
34     left.swap(right);
35   }
36 }
37
38
39 int test_main(int, char*[])
40 {
41   const std::size_t first_dimension = 3;
42   const std::size_t second_dimension = 4;
43   const std::size_t number_of_elements = first_dimension * second_dimension;
44
45   swap_test_class array1[first_dimension][second_dimension];
46   swap_test_class array2[first_dimension][second_dimension];
47
48   swap_test_class* const ptr1 = array1[0];
49   swap_test_class* const ptr2 = array2[0];
50
51   for (std::size_t i = 0; i < number_of_elements; ++i)
52   {
53     ptr1[i].set_data( static_cast<int>(i) );
54     ptr2[i].set_data( static_cast<int>(i + number_of_elements) );
55   }
56
57   boost::swap(array1, array2);
58
59   for (std::size_t i = 0; i < number_of_elements; ++i)
60   {
61     BOOST_CHECK_EQUAL(ptr1[i].get_data(), static_cast<int>(i + number_of_elements) );
62     BOOST_CHECK_EQUAL(ptr2[i].get_data(), static_cast<int>(i) );
63   }
64
65   BOOST_CHECK_EQUAL(swap_test_class::swap_count(), number_of_elements);
66   BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
67
68   return 0;
69 }