diff --git a/swap/test/Jamfile.v2 b/swap/test/Jamfile.v2 new file mode 100644 index 0000000..7071837 --- /dev/null +++ b/swap/test/Jamfile.v2 @@ -0,0 +1,33 @@ +# Copyright (c) 2007, 2008 Joseph Gauterin +# +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +# bring in rules for testing +import testing ; + +test-suite utility/swap + : + [ compile root_header_1.cpp ] + [ compile root_header_2.cpp ] + [ compile lib_header_1.cpp ] + [ compile lib_header_2.cpp ] + [ compile mixed_headers_1.cpp ] + [ compile mixed_headers_2.cpp ] + [ run primitive.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_global.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_other.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_std.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run specialized_in_boost_and_other.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_bitset.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_dateorder.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_string.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_typeinfo_ptr.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_vector_of_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_vector_of_global.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run std_vector_of_other.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run no_ambiguity_in_boost.cpp ../../../test/build//boost_test_exec_monitor/static ] + [ run swap_arrays.cpp ../../../test/build//boost_test_exec_monitor/static ] + ; diff --git a/swap/test/lib_header_1.cpp b/swap/test/lib_header_1.cpp new file mode 100644 index 0000000..923dea6 --- /dev/null +++ b/swap/test/lib_header_1.cpp @@ -0,0 +1,10 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests that the swap header compiles as a standalone translation unit + +#include + diff --git a/swap/test/lib_header_2.cpp b/swap/test/lib_header_2.cpp new file mode 100644 index 0000000..e88909d --- /dev/null +++ b/swap/test/lib_header_2.cpp @@ -0,0 +1,11 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests that the swap header include guards work correctly + +#include +#include + diff --git a/swap/test/mixed_headers_1.cpp b/swap/test/mixed_headers_1.cpp new file mode 100644 index 0000000..cdb9fe5 --- /dev/null +++ b/swap/test/mixed_headers_1.cpp @@ -0,0 +1,11 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests that the swap headers work when both are included + +#include +#include + diff --git a/swap/test/mixed_headers_2.cpp b/swap/test/mixed_headers_2.cpp new file mode 100644 index 0000000..94e9d87 --- /dev/null +++ b/swap/test/mixed_headers_2.cpp @@ -0,0 +1,12 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests that the swap headers work when both are included + +#include +#include + + diff --git a/swap/test/no_ambiguity_in_boost.cpp b/swap/test/no_ambiguity_in_boost.cpp new file mode 100644 index 0000000..e269252 --- /dev/null +++ b/swap/test/no_ambiguity_in_boost.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// boost::swap internally does an unqualified function call to swap. +// This could have led to ambiguity or infinite recursion, when the +// objects to be swapped would themselves be from the boost namespace. +// If so, boost::swap itself might be found by argument dependent lookup. +// The implementation of boost::swap resolves this issue by giving +// boost::swap two template argumetns, thereby making it less specialized +// than std::swap. + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace boost +namespace boost +{ + #include "./swap_test_class.hpp" +} + + +int test_main(int, char*[]) +{ + const boost::swap_test_class initial_value1(1); + const boost::swap_test_class initial_value2(2); + + boost::swap_test_class object1 = initial_value1; + boost::swap_test_class object2 = initial_value2; + + boost::swap_test_class::reset(); + boost::swap(object1,object2); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),0); + BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),3); + + return 0; +} + diff --git a/swap/test/primitive.cpp b/swap/test/primitive.cpp new file mode 100644 index 0000000..380edb3 --- /dev/null +++ b/swap/test/primitive.cpp @@ -0,0 +1,23 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#define BOOST_INCLUDE_MAIN +#include + +int test_main(int, char*[]) +{ + int object1 = 1; + int object2 = 2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,2); + BOOST_CHECK_EQUAL(object2,1); + + return 0; +} + diff --git a/swap/test/root_header_1.cpp b/swap/test/root_header_1.cpp new file mode 100644 index 0000000..575d2cb --- /dev/null +++ b/swap/test/root_header_1.cpp @@ -0,0 +1,10 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests that the swap header compiles as a standalone translation unit + +#include + diff --git a/swap/test/root_header_2.cpp b/swap/test/root_header_2.cpp new file mode 100644 index 0000000..d26b3a6 --- /dev/null +++ b/swap/test/root_header_2.cpp @@ -0,0 +1,11 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests that the swap header include guards work correctly + +#include +#include + diff --git a/swap/test/specialized_in_boost.cpp b/swap/test/specialized_in_boost.cpp new file mode 100644 index 0000000..7b3e12e --- /dev/null +++ b/swap/test/specialized_in_boost.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace boost +namespace boost +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace boost +namespace boost +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + const boost::swap_test_class initial_value1(1); + const boost::swap_test_class initial_value2(2); + + boost::swap_test_class object1 = initial_value1; + boost::swap_test_class object2 = initial_value2; + + boost::swap_test_class::reset(); + boost::swap(object1,object2); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + + BOOST_CHECK_EQUAL(boost::swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(boost::swap_test_class::copy_count(),0); + + return 0; +} + diff --git a/swap/test/specialized_in_boost_and_other.cpp b/swap/test/specialized_in_boost_and_other.cpp new file mode 100644 index 0000000..ba6247f --- /dev/null +++ b/swap/test/specialized_in_boost_and_other.cpp @@ -0,0 +1,64 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests whether instances of a class from a namespace other than boost are +// properly swapped, when both boost and the other namespace have a custom +// swap function for that class. Note that it shouldn't be necessary for a class +// in an other namespace to have a custom swap function in boost, because the +// boost::swap utility should find the swap function in the other namespace, by +// argument dependent lookup (ADL). Unfortunately ADL isn't fully implemented +// by some specific compiler versions, including Intel C++ 8.1, MSVC 7.1, and +// Borland 5.9.3. Users of those compilers might consider adding a swap overload +// to the boost namespace. + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace other +namespace other +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace boost +namespace boost +{ + void swap(::other::swap_test_class& left, ::other::swap_test_class& right) + { + left.swap(right); + } +} + +//Provide swap function in namespace other +namespace other +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + const other::swap_test_class initial_value1(1); + const other::swap_test_class initial_value2(2); + + other::swap_test_class object1 = initial_value1; + other::swap_test_class object2 = initial_value2; + + other::swap_test_class::reset(); + boost::swap(object1,object2); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + + BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); + + return 0; +} + diff --git a/swap/test/specialized_in_global.cpp b/swap/test/specialized_in_global.cpp new file mode 100644 index 0000000..0a0a029 --- /dev/null +++ b/swap/test/specialized_in_global.cpp @@ -0,0 +1,39 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + +//Provide swap function in gloabl namespace +void swap(swap_test_class& left, swap_test_class& right) +{ + left.swap(right); +} + +int test_main(int, char*[]) +{ + const swap_test_class initial_value1(1); + const swap_test_class initial_value2(2); + + swap_test_class object1 = initial_value1; + swap_test_class object2 = initial_value2; + + swap_test_class::reset(); + boost::swap(object1,object2); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); + + return 0; +} + diff --git a/swap/test/specialized_in_other.cpp b/swap/test/specialized_in_other.cpp new file mode 100644 index 0000000..a39896f --- /dev/null +++ b/swap/test/specialized_in_other.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in namespace other +namespace other +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace other +namespace other +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + const other::swap_test_class initial_value1(1); + const other::swap_test_class initial_value2(2); + + other::swap_test_class object1 = initial_value1; + other::swap_test_class object2 = initial_value2; + + other::swap_test_class::reset(); + boost::swap(object1,object2); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + + BOOST_CHECK_EQUAL(other::swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(other::swap_test_class::copy_count(),0); + + return 0; +} + diff --git a/swap/test/specialized_in_std.cpp b/swap/test/specialized_in_std.cpp new file mode 100644 index 0000000..e31e850 --- /dev/null +++ b/swap/test/specialized_in_std.cpp @@ -0,0 +1,44 @@ +// Copyright (c) 2007 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + + +//Provide swap function in namespace std +namespace std +{ + template <> + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + const swap_test_class initial_value1(1); + const swap_test_class initial_value2(2); + + swap_test_class object1 = initial_value1; + swap_test_class object2 = initial_value2; + + swap_test_class::reset(); + boost::swap(object1,object2); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),1); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); + + return 0; +} + diff --git a/swap/test/std_bitset.cpp b/swap/test/std_bitset.cpp new file mode 100644 index 0000000..4b9fbae --- /dev/null +++ b/swap/test/std_bitset.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests swapping std::bitset objects by means of boost::swap. +// Unlike most other Standard C++ Library template classes, +// std::bitset does not have its own std::swap overload. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +int test_main(int, char*[]) +{ + typedef std::bitset<8> bitset_type; + const bitset_type initial_value1 = 1ul; + const bitset_type initial_value2 = 2ul; + + bitset_type object1 = initial_value1; + bitset_type object2 = initial_value2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,initial_value2); + BOOST_CHECK_EQUAL(object2,initial_value1); + + return 0; +} + diff --git a/swap/test/std_dateorder.cpp b/swap/test/std_dateorder.cpp new file mode 100644 index 0000000..b593f6f --- /dev/null +++ b/swap/test/std_dateorder.cpp @@ -0,0 +1,32 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests swapping std::time_base::dateorder objects by means of boost::swap. +// std::time_base::dateorder is an enumerated type. It does not have an +// std::swap overload or template specialization. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +int test_main(int, char*[]) +{ + const std::time_base::dateorder initial_value1 = std::time_base::dmy; + const std::time_base::dateorder initial_value2 = std::time_base::mdy; + + std::time_base::dateorder object1 = initial_value1; + std::time_base::dateorder object2 = initial_value2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,initial_value2); + BOOST_CHECK_EQUAL(object2,initial_value1); + + return 0; +} + diff --git a/swap/test/std_string.cpp b/swap/test/std_string.cpp new file mode 100644 index 0000000..b7d3d4d --- /dev/null +++ b/swap/test/std_string.cpp @@ -0,0 +1,31 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests swapping std::string objects by means of boost::swap. +// std::string has its own std::swap overload. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +int test_main(int, char*[]) +{ + const std::string initial_value1 = "one"; + const std::string initial_value2 = "two"; + + std::string object1 = initial_value1; + std::string object2 = initial_value2; + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1,initial_value2); + BOOST_CHECK_EQUAL(object2,initial_value1); + + return 0; +} + diff --git a/swap/test/std_typeinfo_ptr.cpp b/swap/test/std_typeinfo_ptr.cpp new file mode 100644 index 0000000..38e293a --- /dev/null +++ b/swap/test/std_typeinfo_ptr.cpp @@ -0,0 +1,32 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests swapping std::type_info pointers by means of boost::swap. +// There is no std::swap overload or template specialization +// for std::type_info pointers. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +int test_main(int, char*[]) +{ + const std::type_info * const initial_value1 = 0; + const std::type_info * const initial_value2 = &typeid(double); + + const std::type_info * ptr1 = initial_value1; + const std::type_info * ptr2 = initial_value2; + + boost::swap(ptr1,ptr2); + + BOOST_CHECK_EQUAL(ptr1,initial_value2); + BOOST_CHECK_EQUAL(ptr2,initial_value1); + + return 0; +} + diff --git a/swap/test/std_vector_of_boost.cpp b/swap/test/std_vector_of_boost.cpp new file mode 100644 index 0000000..b0c6355 --- /dev/null +++ b/swap/test/std_vector_of_boost.cpp @@ -0,0 +1,60 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests swapping std::vector objects by means of boost::swap, +// having boost::swap_test_class as vector element type. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +//Put test class in namespace boost +namespace boost +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace boost +namespace boost +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + typedef boost::swap_test_class swap_test_class_type; + typedef std::vector vector_type; + + const vector_type::size_type initial_size1 = 1; + const vector_type::size_type initial_size2 = 2; + + const vector_type initial_value1(initial_size1, swap_test_class_type(1)); + const vector_type initial_value2(initial_size2, swap_test_class_type(2)); + + vector_type object1 = initial_value1; + vector_type object2 = initial_value2; + + swap_test_class_type::reset(); + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1.size(),initial_size2); + BOOST_CHECK_EQUAL(object2.size(),initial_size1); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + + BOOST_CHECK_EQUAL(swap_test_class_type::swap_count(),0); + BOOST_CHECK_EQUAL(swap_test_class_type::copy_count(),0); + + return 0; +} + diff --git a/swap/test/std_vector_of_global.cpp b/swap/test/std_vector_of_global.cpp new file mode 100644 index 0000000..96a9b6a --- /dev/null +++ b/swap/test/std_vector_of_global.cpp @@ -0,0 +1,53 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests swapping std::vector objects by means of boost::swap, +// having ::swap_test_class as vector element type. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + +//Provide swap function in the global namespace +void swap(swap_test_class& left, swap_test_class& right) +{ + left.swap(right); +} + +int test_main(int, char*[]) +{ + typedef std::vector vector_type; + + const vector_type::size_type initial_size1 = 1; + const vector_type::size_type initial_size2 = 2; + + const vector_type initial_value1(initial_size1, swap_test_class(1)); + const vector_type initial_value2(initial_size2, swap_test_class(2)); + + vector_type object1 = initial_value1; + vector_type object2 = initial_value2; + + swap_test_class::reset(); + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1.size(),initial_size2); + BOOST_CHECK_EQUAL(object2.size(),initial_size1); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(),0); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(),0); + + return 0; +} + diff --git a/swap/test/std_vector_of_other.cpp b/swap/test/std_vector_of_other.cpp new file mode 100644 index 0000000..2176f6e --- /dev/null +++ b/swap/test/std_vector_of_other.cpp @@ -0,0 +1,60 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests swapping std::vector objects by means of boost::swap, +// having other::swap_test_class as vector element type. + +#include +#define BOOST_INCLUDE_MAIN +#include + +#include + +//Put test class in namespace other +namespace other +{ + #include "./swap_test_class.hpp" +} + +//Provide swap function in namespace other +namespace other +{ + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +int test_main(int, char*[]) +{ + typedef other::swap_test_class swap_test_class_type; + typedef std::vector vector_type; + + const vector_type::size_type initial_size1 = 1; + const vector_type::size_type initial_size2 = 2; + + const vector_type initial_value1(initial_size1, swap_test_class_type(1)); + const vector_type initial_value2(initial_size2, swap_test_class_type(2)); + + vector_type object1 = initial_value1; + vector_type object2 = initial_value2; + + swap_test_class_type::reset(); + + boost::swap(object1,object2); + + BOOST_CHECK_EQUAL(object1.size(),initial_size2); + BOOST_CHECK_EQUAL(object2.size(),initial_size1); + + BOOST_CHECK(object1 == initial_value2); + BOOST_CHECK(object2 == initial_value1); + + BOOST_CHECK_EQUAL(swap_test_class_type::swap_count(),0); + BOOST_CHECK_EQUAL(swap_test_class_type::copy_count(),0); + + return 0; +} + diff --git a/swap/test/swap_arrays.cpp b/swap/test/swap_arrays.cpp new file mode 100644 index 0000000..eeb64bc --- /dev/null +++ b/swap/test/swap_arrays.cpp @@ -0,0 +1,100 @@ +// Copyright (c) 2008 Joseph Gauterin, Niels Dekker +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#define BOOST_INCLUDE_MAIN +#include + +//Put test class in the global namespace +#include "./swap_test_class.hpp" + +#include //for std::copy and std::equal +#include //for std::size_t + +//Provide swap function in both the namespace of swap_test_class +//(which is the global namespace), and the std namespace. +//It's common to provide a swap function for a class in both +//namespaces. Scott Meyers recommends doing so: Effective C++, +//Third Edition, item 25, "Consider support for a non-throwing swap". +void swap(swap_test_class& left, swap_test_class& right) +{ + left.swap(right); +} + +namespace std +{ + template <> + void swap(swap_test_class& left, swap_test_class& right) + { + left.swap(right); + } +} + +// Tests swapping 1-dimensional arrays. +void test_swapping_1D_arrays() +{ + const std::size_t dimension = 2; + const swap_test_class initial_array1[dimension] = { swap_test_class(1), swap_test_class(2) }; + const swap_test_class initial_array2[dimension] = { swap_test_class(3), swap_test_class(4) }; + + swap_test_class array1[dimension]; + swap_test_class array2[dimension]; + + std::copy(initial_array1, initial_array1 + dimension, array1); + std::copy(initial_array2, initial_array2 + dimension, array2); + + swap_test_class::reset(); + boost::swap(array1, array2); + + BOOST_CHECK(std::equal(array1, array1 + dimension, initial_array2)); + BOOST_CHECK(std::equal(array2, array2 + dimension, initial_array1)); + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), dimension); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); +} + + +// Tests swapping 2-dimensional arrays. +void test_swapping_2D_arrays() +{ + const std::size_t first_dimension = 3; + const std::size_t second_dimension = 4; + const std::size_t number_of_elements = first_dimension * second_dimension; + + swap_test_class array1[first_dimension][second_dimension]; + swap_test_class array2[first_dimension][second_dimension]; + + swap_test_class* const ptr1 = array1[0]; + swap_test_class* const ptr2 = array2[0]; + + for (std::size_t i = 0; i < number_of_elements; ++i) + { + ptr1[i].set_data( static_cast(i) ); + ptr2[i].set_data( static_cast(i + number_of_elements) ); + } + + swap_test_class::reset(); + boost::swap(array1, array2); + + for (std::size_t i = 0; i < number_of_elements; ++i) + { + BOOST_CHECK_EQUAL(ptr1[i].get_data(), static_cast(i + number_of_elements) ); + BOOST_CHECK_EQUAL(ptr2[i].get_data(), static_cast(i) ); + } + + BOOST_CHECK_EQUAL(swap_test_class::swap_count(), number_of_elements); + BOOST_CHECK_EQUAL(swap_test_class::copy_count(), 0); +} + + +int test_main(int, char*[]) +{ + test_swapping_1D_arrays(); + test_swapping_2D_arrays(); + + return 0; +} + diff --git a/swap/test/swap_test_class.hpp b/swap/test/swap_test_class.hpp new file mode 100644 index 0000000..8cf1fe9 --- /dev/null +++ b/swap/test/swap_test_class.hpp @@ -0,0 +1,114 @@ +// Copyright (c) 2007-2008 Joseph Gauterin +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Tests class used by the Boost.Swap tests + +#ifndef BOOST_UTILITY_SWAP_TEST_CLASS_HPP +#define BOOST_UTILITY_SWAP_TEST_CLASS_HPP + + +class swap_test_class +{ + int m_data; +public: + explicit swap_test_class(int arg = 0) + : + m_data(arg) + { + ++constructCount(); + } + + ~swap_test_class() + { + ++destructCount(); + } + + swap_test_class(const swap_test_class& arg) + : + m_data(arg.m_data) + { + ++copyCount(); + ++destructCount(); + } + + swap_test_class& operator=(const swap_test_class& arg) + { + m_data = arg.m_data; + ++copyCount(); + return *this; + } + + void swap(swap_test_class& other) + { + const int temp = m_data; + m_data = other.m_data; + other.m_data = temp; + + ++swapCount(); + } + + int get_data() const + { + return m_data; + } + + void set_data(int arg) + { + m_data = arg; + } + + static unsigned int swap_count(){ return swapCount(); } + static unsigned int copy_count(){ return copyCount(); } + static unsigned int construct_count(){ return constructCount(); } + static unsigned int destruct_count(){ return destructCount(); } + + static void reset() + { + swapCount() = 0; + copyCount() = 0; + constructCount() = 0; + destructCount() = 0; + } + +private: + static unsigned int& swapCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& copyCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& constructCount() + { + static unsigned int value = 0; + return value; + } + + static unsigned int& destructCount() + { + static unsigned int value = 0; + return value; + } + +}; + + +inline bool operator==(const swap_test_class & lhs, const swap_test_class & rhs) +{ + return lhs.get_data() == rhs.get_data(); +} + +inline bool operator!=(const swap_test_class & lhs, const swap_test_class & rhs) +{ + return !(lhs == rhs); +} + +#endif