~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to libs/utility/swap/test/array_of_template.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#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
template <class T>
 
20
class swap_test_template
 
21
{
 
22
public:
 
23
  typedef T template_argument;
 
24
  swap_test_class swap_test_object;
 
25
};
 
26
 
 
27
template <class T>
 
28
inline bool operator==(const swap_test_template<T> & lhs, const swap_test_template<T> & rhs)
 
29
{
 
30
  return lhs.swap_test_object == rhs.swap_test_object;
 
31
}
 
32
 
 
33
template <class T>
 
34
inline bool operator!=(const swap_test_template<T> & lhs, const swap_test_template<T> & rhs)
 
35
{
 
36
  return !(lhs == rhs);
 
37
}
 
38
 
 
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.
 
42
template <class T>
 
43
void swap(swap_test_template<T>& left, swap_test_template<T>& right)
 
44
{
 
45
  left.swap_test_object.swap(right.swap_test_object);
 
46
}
 
47
 
 
48
 
 
49
int test_main(int, char*[])
 
50
{
 
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) };
 
54
  
 
55
  swap_test_template<int> array1[array_size];
 
56
  swap_test_template<int> array2[array_size];
 
57
 
 
58
  std::copy(initial_array1, initial_array1 + array_size, array1);
 
59
  std::copy(initial_array2, initial_array2 + array_size, array2);
 
60
  
 
61
  swap_test_class::reset();
 
62
  boost::swap(array1, array2);
 
63
 
 
64
  BOOST_CHECK(std::equal(array1, array1 + array_size, initial_array2));
 
65
  BOOST_CHECK(std::equal(array2, array2 + array_size, initial_array1));
 
66
 
 
67
  BOOST_CHECK_EQUAL(swap_test_class::swap_count(), array_size);
 
68
  BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0);
 
69
 
 
70
  return 0;
 
71
}