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 swap_test_template<int> objects by means of boost::swap.
9
#include <boost/utility/swap.hpp>
10
#define BOOST_INCLUDE_MAIN
11
#include <boost/test/test_tools.hpp>
13
//Put test class in the global namespace
14
#include "./swap_test_class.hpp"
16
#include <algorithm> //for std::copy and std::equal
17
#include <cstddef> //for std::size_t
20
class swap_test_template
23
typedef T template_argument;
24
swap_test_class swap_test_object;
28
inline bool operator==(const swap_test_template<T> & lhs, const swap_test_template<T> & rhs)
30
return lhs.swap_test_object == rhs.swap_test_object;
34
inline bool operator!=(const swap_test_template<T> & lhs, const swap_test_template<T> & rhs)
39
//Provide swap function in the namespace of swap_test_template
40
//(which is the global namespace). Note that it isn't allowed to put
41
//an overload of this function within the std namespace.
43
void swap(swap_test_template<T>& left, swap_test_template<T>& right)
45
left.swap_test_object.swap(right.swap_test_object);
49
int test_main(int, char*[])
51
const std::size_t array_size = 2;
52
const swap_test_template<int> initial_array1[array_size] = { swap_test_class(1), swap_test_class(2) };
53
const swap_test_template<int> initial_array2[array_size] = { swap_test_class(3), swap_test_class(4) };
55
swap_test_template<int> array1[array_size];
56
swap_test_template<int> array2[array_size];
58
std::copy(initial_array1, initial_array1 + array_size, array1);
59
std::copy(initial_array2, initial_array2 + array_size, array2);
61
swap_test_class::reset();
62
boost::swap(array1, array2);
64
BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2));
65
BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1));
67
BOOST_CHECK_EQUAL(swap_test_class::swap_count(), array_size);
68
BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);