1 // Copyright (c) 2008 Joseph Gauterin, Niels Dekker
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)
7 // Tests swapping an array of arrays of swap_test_class objects by means of boost::swap.
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
14 //Put test class in the global namespace
15 #include "./swap_test_class.hpp"
17 #include <algorithm> //for std::copy and std::equal
18 #include <cstddef> //for std::size_t
20 //Provide swap function in both the namespace of swap_test_class
21 //(which is the global namespace), and the std namespace.
22 //It's common to provide a swap function for a class in both
23 //namespaces. Scott Meyers recommends doing so: Effective C++,
24 //Third Edition, item 25, "Consider support for a non-throwing swap".
25 void swap(swap_test_class
& left
, swap_test_class
& right
)
33 void swap(swap_test_class
& left
, swap_test_class
& right
)
42 const std::size_t array_size
= 2;
43 const swap_test_class initial_array1
[array_size
] = { swap_test_class(1), swap_test_class(2) };
44 const swap_test_class initial_array2
[array_size
] = { swap_test_class(3), swap_test_class(4) };
46 swap_test_class array1
[array_size
];
47 swap_test_class array2
[array_size
];
49 std::copy(initial_array1
, initial_array1
+ array_size
, array1
);
50 std::copy(initial_array2
, initial_array2
+ array_size
, array2
);
52 swap_test_class::reset();
53 boost::swap(array1
, array2
);
55 BOOST_CHECK(std::equal(array1
, array1
+ array_size
, initial_array2
));
56 BOOST_CHECK(std::equal(array2
, array2
+ array_size
, initial_array1
));
58 BOOST_CHECK_EQUAL(swap_test_class::swap_count(), array_size
);
59 BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
61 return boost::report_errors();