]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 swap_test_template<int> objects 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 | //Put test class in the global namespace | |
15 | #include "./swap_test_class.hpp" | |
16 | ||
17 | #include <algorithm> //for std::copy and std::equal | |
18 | #include <cstddef> //for std::size_t | |
19 | ||
20 | template <class T> | |
21 | class swap_test_template | |
22 | { | |
23 | public: | |
24 | typedef T template_argument; | |
25 | swap_test_class swap_test_object; | |
26 | }; | |
27 | ||
28 | template <class T> | |
29 | inline bool operator==(const swap_test_template<T> & lhs, const swap_test_template<T> & rhs) | |
30 | { | |
31 | return lhs.swap_test_object == rhs.swap_test_object; | |
32 | } | |
33 | ||
34 | template <class T> | |
35 | inline bool operator!=(const swap_test_template<T> & lhs, const swap_test_template<T> & rhs) | |
36 | { | |
37 | return !(lhs == rhs); | |
38 | } | |
39 | ||
40 | //Provide swap function in the namespace of swap_test_template | |
41 | //(which is the global namespace). Note that it isn't allowed to put | |
42 | //an overload of this function within the std namespace. | |
43 | template <class T> | |
44 | void swap(swap_test_template<T>& left, swap_test_template<T>& right) | |
45 | { | |
46 | left.swap_test_object.swap(right.swap_test_object); | |
47 | } | |
48 | ||
49 | ||
50 | int main() | |
51 | { | |
52 | const std::size_t array_size = 2; | |
1e59de90 TL |
53 | const swap_test_template<int> initial_array1[array_size] = { { swap_test_class(1) }, { swap_test_class(2) } }; |
54 | const swap_test_template<int> initial_array2[array_size] = { { swap_test_class(3) }, { swap_test_class(4) } }; | |
7c673cae FG |
55 | |
56 | swap_test_template<int> array1[array_size]; | |
57 | swap_test_template<int> array2[array_size]; | |
58 | ||
59 | std::copy(initial_array1, initial_array1 + array_size, array1); | |
60 | std::copy(initial_array2, initial_array2 + array_size, array2); | |
61 | ||
62 | swap_test_class::reset(); | |
63 | boost::swap(array1, array2); | |
64 | ||
65 | BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2)); | |
66 | BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1)); | |
67 | ||
68 | BOOST_CHECK_EQUAL(swap_test_class::swap_count(), array_size); | |
69 | BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); | |
70 | ||
71 | return boost::report_errors(); | |
72 | } |