std_vector_of_other.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // Tests swapping std::vector objects by means of boost::swap,
  7. // having other::swap_test_class as vector element type.
  8. #include <boost/utility/swap.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #define BOOST_CHECK BOOST_TEST
  11. #define BOOST_CHECK_EQUAL BOOST_TEST_EQ
  12. #include <vector>
  13. //Put test class in namespace other
  14. namespace other
  15. {
  16. #include "./swap_test_class.hpp"
  17. }
  18. //Provide swap function in namespace other
  19. namespace other
  20. {
  21. void swap(swap_test_class& left, swap_test_class& right)
  22. {
  23. left.swap(right);
  24. }
  25. }
  26. int main()
  27. {
  28. typedef other::swap_test_class swap_test_class_type;
  29. typedef std::vector<swap_test_class_type> vector_type;
  30. const vector_type::size_type initial_size1 = 1;
  31. const vector_type::size_type initial_size2 = 2;
  32. const vector_type initial_value1(initial_size1, swap_test_class_type(1));
  33. const vector_type initial_value2(initial_size2, swap_test_class_type(2));
  34. vector_type object1 = initial_value1;
  35. vector_type object2 = initial_value2;
  36. swap_test_class_type::reset();
  37. boost::swap(object1,object2);
  38. BOOST_CHECK_EQUAL(object1.size(),initial_size2);
  39. BOOST_CHECK_EQUAL(object2.size(),initial_size1);
  40. BOOST_CHECK(object1 == initial_value2);
  41. BOOST_CHECK(object2 == initial_value1);
  42. BOOST_CHECK_EQUAL(swap_test_class_type::swap_count(),0);
  43. BOOST_CHECK_EQUAL(swap_test_class_type::copy_count(),0);
  44. return boost::report_errors();
  45. }