From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/move/test/unique_ptr_assign.cpp | 444 +++++++++++++++++++++++++ 1 file changed, 444 insertions(+) create mode 100644 src/boost/libs/move/test/unique_ptr_assign.cpp (limited to 'src/boost/libs/move/test/unique_ptr_assign.cpp') diff --git a/src/boost/libs/move/test/unique_ptr_assign.cpp b/src/boost/libs/move/test/unique_ptr_assign.cpp new file mode 100644 index 00000000..088b7f4d --- /dev/null +++ b/src/boost/libs/move/test/unique_ptr_assign.cpp @@ -0,0 +1,444 @@ +////////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Howard Hinnant 2009 +// (C) Copyright Ion Gaztanaga 2014-2014. +// +// 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) +// +// See http://www.boost.org/libs/move for documentation. +// +////////////////////////////////////////////////////////////////////////////// +#include +#include +#include +#include + +////////////////////////////////////////////// +// +// The initial implementation of these tests +// was written by Howard Hinnant. +// +// These test were later refactored grouping +// and porting them to Boost.Move. +// +// Many thanks to Howard for releasing his C++03 +// unique_ptr implementation with such detailed +// test cases. +// +////////////////////////////////////////////// + +#include "unique_ptr_test_utils_beg.hpp" + +namespace bml = ::boost::movelib; + +//////////////////////////////// +// unique_ptr_asgn_move_convert_defdel +//////////////////////////////// +namespace unique_ptr_asgn_move_convert_defdel { + +void test() +{ + //Single unique_ptr + reset_counters(); + { + bml::unique_ptr s(new B); + A* p = s.get(); + bml::unique_ptr s2(new A); + BOOST_TEST(A::count == 2); + s2 = boost::move(s); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 1); + BOOST_TEST(B::count == 1); + } + BOOST_TEST(A::count == 0); + BOOST_TEST(B::count == 0); + + //Unbounded array unique_ptr + reset_counters(); + { + bml::unique_ptr s(new A[2]); + A* p = s.get(); + bml::unique_ptr s2(new const A[2]); + BOOST_TEST(A::count == 4); + s2 = boost::move(s); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 2); + } + BOOST_TEST(A::count == 0); + //Bounded array unique_ptr + reset_counters(); + { + bml::unique_ptr s(new A[2]); + A* p = s.get(); + bml::unique_ptr s2(new const A[2]); + BOOST_TEST(A::count == 4); + s2 = boost::move(s); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 2); + } + { + BOOST_TEST(A::count == 0); + bml::unique_ptr s(new A[2]); + A* p = s.get(); + bml::unique_ptr s2(new const A[2]); + BOOST_TEST(A::count == 4); + s2 = boost::move(s); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 2); + } + BOOST_TEST(A::count == 0); +} + +} //namespace unique_ptr_asgn_move_convert_defdel{ + +//////////////////////////////// +// unique_ptr_asgn_move_convert_movdel +//////////////////////////////// + +namespace unique_ptr_asgn_move_convert_movedel{ + +void test() +{ + //Single unique_ptr + reset_counters(); + { + bml::unique_ptr > s(new B); + A* p = s.get(); + bml::unique_ptr > s2(new A); + BOOST_TEST(A::count == 2); + s2 = (boost::move(s)); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 1); + BOOST_TEST(B::count == 1); + BOOST_TEST(s2.get_deleter().state() == 5); + BOOST_TEST(s.get_deleter().state() == 0); + } + BOOST_TEST(A::count == 0); + BOOST_TEST(B::count == 0); + + //Unbounded array unique_ptr + reset_counters(); + { + bml::unique_ptr > s(new A[2]); + A* p = s.get(); + bml::unique_ptr > s2(new const A[2]); + BOOST_TEST(A::count == 4); + s2 = (boost::move(s)); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get_deleter().state() == 5); + BOOST_TEST(s.get_deleter().state() == 0); + } + BOOST_TEST(A::count == 0); + + //Bounded array unique_ptr + reset_counters(); + { + bml::unique_ptr > s(new A[3]); + A* p = s.get(); + bml::unique_ptr > s2(new const A[2]); + BOOST_TEST(A::count == 5); + s2 = (boost::move(s)); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 3); + BOOST_TEST(s2.get_deleter().state() == 5); + BOOST_TEST(s.get_deleter().state() == 0); + } + BOOST_TEST(A::count == 0); + reset_counters(); + { + bml::unique_ptr > s(new A[2]); + A* p = s.get(); + bml::unique_ptr > s2(new const A[2]); + BOOST_TEST(A::count == 4); + s2 = (boost::move(s)); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get_deleter().state() == 5); + BOOST_TEST(s.get_deleter().state() == 0); + } + BOOST_TEST(A::count == 0); +} + +} //namespace unique_ptr_asgn_move_convert_movedel{ + +//////////////////////////////// +// unique_ptr_asgn_move_convert_copydelref +//////////////////////////////// + +namespace unique_ptr_asgn_move_convert_copydelref{ + +// test converting move assignment with reference deleters + +void test() +{ + //Single unique_ptr + reset_counters(); + { + copy_constr_deleter db(5); + bml::unique_ptr&> s(new B, db); + A* p = s.get(); + copy_constr_deleter da(6); + bml::unique_ptr&> s2(new A, da); + s2 = boost::move(s); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 1); + BOOST_TEST(B::count == 1); + BOOST_TEST(s2.get_deleter().state() == 5); + } + BOOST_TEST(A::count == 0); + BOOST_TEST(B::count == 0); + + //Unbounded array unique_ptr + reset_counters(); + { + copy_constr_deleter db(5); + bml::unique_ptr&> s(new A[2], db); + A* p = s.get(); + copy_constr_deleter da(6); + bml::unique_ptr&> s2(new const A[2], da); + BOOST_TEST(A::count == 4); + s2 = boost::move(s); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get_deleter().state() == 5); + } + BOOST_TEST(A::count == 0); + + //Bounded array unique_ptr + reset_counters(); + { + copy_constr_deleter db(5); + bml::unique_ptr&> s(new A[2], db); + A* p = s.get(); + copy_constr_deleter da(6); + bml::unique_ptr&> s2(new const A[2], da); + BOOST_TEST(A::count == 4); + s2 = boost::move(s); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get_deleter().state() == 5); + } + BOOST_TEST(A::count == 0); + reset_counters(); + { + copy_constr_deleter db(5); + bml::unique_ptr&> s(new A[2], db); + A* p = s.get(); + copy_constr_deleter da(6); + bml::unique_ptr&> s2(new const A[2], da); + BOOST_TEST(A::count == 4); + s2 = boost::move(s); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get_deleter().state() == 5); + } + BOOST_TEST(A::count == 0); +} + +} //namespace unique_ptr_asgn_move_convert_copydelref{ + +//////////////////////////////// +// unique_ptr_asgn_move_defdel +//////////////////////////////// +namespace unique_ptr_asgn_move_defdel { + +void test() +{ + //Single unique_ptr + reset_counters(); + { + bml::unique_ptr s1(new A); + A* p = s1.get(); + bml::unique_ptr s2(new A); + BOOST_TEST(A::count == 2); + s2 = boost::move(s1); + BOOST_TEST(A::count == 1); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + } + BOOST_TEST(A::count == 0); + + //Unbounded array unique_ptr + reset_counters(); + { + bml::unique_ptr s1(new A[2]); + A* p = s1.get(); + bml::unique_ptr s2(new A[2]); + BOOST_TEST(A::count == 4); + s2 = boost::move(s1); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + } + BOOST_TEST(A::count == 0); + //Bounded array unique_ptr + reset_counters(); + { + bml::unique_ptr s1(new A[2]); + A* p = s1.get(); + bml::unique_ptr s2(new A[2]); + BOOST_TEST(A::count == 4); + s2 = boost::move(s1); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + } + BOOST_TEST(A::count == 0); +} + +} //unique_ptr_asgn_move_defdel + +//////////////////////////////// +// unique_ptr_asgn_move_movedel +//////////////////////////////// +namespace unique_ptr_asgn_move_movedel { + +void test() +{ + //Single unique_ptr + reset_counters(); + { + bml::unique_ptr > s1(new A); + A* p = s1.get(); + bml::unique_ptr > s2(new A); + BOOST_TEST(A::count == 2); + s2 = boost::move(s1); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + BOOST_TEST(A::count == 1); + BOOST_TEST(s2.get_deleter().state() == 5); + BOOST_TEST(s1.get_deleter().state() == 0); + } + BOOST_TEST(A::count == 0); + + //Unbounded array unique_ptr + reset_counters(); + { + bml::unique_ptr > s1(new A[2]); + A* p = s1.get(); + bml::unique_ptr > s2(new A[2]); + BOOST_TEST(A::count == 4); + s2 = boost::move(s1); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get_deleter().state() == 5); + BOOST_TEST(s1.get_deleter().state() == 0); + } + BOOST_TEST(A::count == 0); + + BOOST_TEST(A::count == 0); + //Bounded array unique_ptr + reset_counters(); + { + bml::unique_ptr > s1(new A[2]); + A* p = s1.get(); + bml::unique_ptr > s2(new A[2]); + BOOST_TEST(A::count == 4); + s2 = boost::move(s1); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(s2.get_deleter().state() == 5); + BOOST_TEST(s1.get_deleter().state() == 0); + } + BOOST_TEST(A::count == 0); +} + +} //unique_ptr_asgn_move_movedel + +//////////////////////////////// +// unique_ptr_asgn_move_copydelref +//////////////////////////////// +namespace unique_ptr_asgn_move_copydelref { + +void test() +{ + //Single unique_ptr + reset_counters(); + { + copy_constr_deleter d1(5); + bml::unique_ptr&> s1(new A, d1); + A* p = s1.get(); + copy_constr_deleter d2(6); + bml::unique_ptr&> s2(new A, d2); + s2 = boost::move(s1); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + BOOST_TEST(A::count == 1); + BOOST_TEST(d1.state() == 5); + BOOST_TEST(d2.state() == 5); + } + BOOST_TEST(A::count == 0); + + //Unbounded array unique_ptr + reset_counters(); + { + copy_constr_deleter d1(5); + bml::unique_ptr&> s1(new A[2], d1); + A* p = s1.get(); + copy_constr_deleter d2(6); + bml::unique_ptr&> s2(new A[2], d2); + BOOST_TEST(A::count == 4); + s2 = boost::move(s1); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(d1.state() == 5); + BOOST_TEST(d2.state() == 5); + } + BOOST_TEST(A::count == 0); + //Bounded array unique_ptr + reset_counters(); + { + copy_constr_deleter d1(5); + bml::unique_ptr&> s1(new A[2], d1); + A* p = s1.get(); + copy_constr_deleter d2(6); + bml::unique_ptr&> s2(new A[2], d2); + BOOST_TEST(A::count == 4); + s2 = boost::move(s1); + BOOST_TEST(s2.get() == p); + BOOST_TEST(s1.get() == 0); + BOOST_TEST(A::count == 2); + BOOST_TEST(d1.state() == 5); + BOOST_TEST(d2.state() == 5); + } + BOOST_TEST(A::count == 0); +} + +} //unique_ptr_asgn_move_copydelref + +//////////////////////////////// +// main +//////////////////////////////// +int main() +{ + //Assignment + unique_ptr_asgn_move_convert_defdel::test(); + unique_ptr_asgn_move_convert_movedel::test(); + unique_ptr_asgn_move_convert_copydelref::test(); + unique_ptr_asgn_move_defdel::test(); + unique_ptr_asgn_move_movedel::test(); + unique_ptr_asgn_move_copydelref::test(); + + //Test results + return boost::report_errors(); +} + +#include "unique_ptr_test_utils_end.hpp" -- cgit v1.2.3