forked from boostorg/move
Added support for bounded arrays in unique_ptr. Splitted test in several subtests.
This commit is contained in:
1 parent
583378a946
commit
19d35253cf
23 files changed
+4091
-2045
No files matched your search
-1983
File diff suppressed because it is too large.
Load diff
@@ -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 <boost/move/utility_core.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//
|
||||
// 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<B> s(new B);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A> 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<A[]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const A[]> 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<A[2]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const A[2]> 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<A[2]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const A[]> 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<B, move_constr_deleter<B> > s(new B);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > 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<A[], move_constr_deleter<A[]> > s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const A[], move_constr_deleter<const A[]> > 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<A[2], move_constr_deleter<A[2]> > s(new A[3]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const A[2], move_constr_deleter<const A[2]> > 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<A[2], move_constr_deleter<A[3]> > s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const A[], move_constr_deleter<const A[]> > 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<B> db(5);
|
||||
bml::unique_ptr<B, copy_constr_deleter<B>&> s(new B, db);
|
||||
A* p = s.get();
|
||||
copy_constr_deleter<A> da(6);
|
||||
bml::unique_ptr<A, copy_constr_deleter<A>&> 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<A[]> db(5);
|
||||
bml::unique_ptr<A[], copy_constr_deleter<A[]>&> s(new A[2], db);
|
||||
A* p = s.get();
|
||||
copy_constr_deleter<const A[]> da(6);
|
||||
bml::unique_ptr<const A[], copy_constr_deleter<const A[]>&> 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<A[2]> db(5);
|
||||
bml::unique_ptr<A[2], copy_constr_deleter<A[2]>&> s(new A[2], db);
|
||||
A* p = s.get();
|
||||
copy_constr_deleter<const A[2]> da(6);
|
||||
bml::unique_ptr<const A[2], copy_constr_deleter<const A[2]>&> 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<A[2]> db(5);
|
||||
bml::unique_ptr<A[2], copy_constr_deleter<A[2]>&> s(new A[2], db);
|
||||
A* p = s.get();
|
||||
copy_constr_deleter<const A[]> da(6);
|
||||
bml::unique_ptr<const A[], copy_constr_deleter<const A[]>&> 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<A> s1(new A);
|
||||
A* p = s1.get();
|
||||
bml::unique_ptr<A> 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<A[]> s1(new A[2]);
|
||||
A* p = s1.get();
|
||||
bml::unique_ptr<A[]> 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<A[2]> s1(new A[2]);
|
||||
A* p = s1.get();
|
||||
bml::unique_ptr<A[2]> 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<A, move_constr_deleter<A> > s1(new A);
|
||||
A* p = s1.get();
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > 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<A[], move_constr_deleter<A[]> > s1(new A[2]);
|
||||
A* p = s1.get();
|
||||
bml::unique_ptr<A[], move_constr_deleter<A[]> > 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<A[2], move_constr_deleter<A[2]> > s1(new A[2]);
|
||||
A* p = s1.get();
|
||||
bml::unique_ptr<A[2], move_constr_deleter<A[2]> > 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<A> d1(5);
|
||||
bml::unique_ptr<A, copy_constr_deleter<A>&> s1(new A, d1);
|
||||
A* p = s1.get();
|
||||
copy_constr_deleter<A> d2(6);
|
||||
bml::unique_ptr<A, copy_constr_deleter<A>&> 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<A[]> d1(5);
|
||||
bml::unique_ptr<A[], copy_constr_deleter<A[]>&> s1(new A[2], d1);
|
||||
A* p = s1.get();
|
||||
copy_constr_deleter<A[]> d2(6);
|
||||
bml::unique_ptr<A[], copy_constr_deleter<A[]>&> 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<A[2]> d1(5);
|
||||
bml::unique_ptr<A[2], copy_constr_deleter<A[2]>&> s1(new A[2], d1);
|
||||
A* p = s1.get();
|
||||
copy_constr_deleter<A[2]> d2(6);
|
||||
bml::unique_ptr<A[2], copy_constr_deleter<A[2]>&> 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"
|
||||
@@ -0,0 +1,708 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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 <boost/move/utility_core.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//
|
||||
// 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_dtor_null
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_dtor_null{
|
||||
|
||||
// The deleter is not called if get() == 0
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
def_constr_deleter<int> d;
|
||||
BOOST_TEST(d.state() == 5);
|
||||
{
|
||||
bml::unique_ptr<int, def_constr_deleter<int>&> p(0, d);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(&p.get_deleter() == &d);
|
||||
}
|
||||
BOOST_TEST(d.state() == 5);
|
||||
}
|
||||
{
|
||||
//Unbounded array unique_ptr
|
||||
def_constr_deleter<int[]> d;
|
||||
BOOST_TEST(d.state() == 5);
|
||||
{
|
||||
bml::unique_ptr<int[], def_constr_deleter<int[]>&> p(0, d);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(&p.get_deleter() == &d);
|
||||
}
|
||||
BOOST_TEST(d.state() == 5);
|
||||
}
|
||||
{
|
||||
//Bounded array unique_ptr
|
||||
def_constr_deleter<int[2]> d;
|
||||
BOOST_TEST(d.state() == 5);
|
||||
{
|
||||
bml::unique_ptr<int[2], def_constr_deleter<int[2]>&> p(0, d);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(&p.get_deleter() == &d);
|
||||
}
|
||||
BOOST_TEST(d.state() == 5);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_dtor_null{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_default_delreq
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_default_delreq{
|
||||
|
||||
// default unique_ptr ctor should only require default deleter ctor
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int> p;
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
{
|
||||
bml::unique_ptr<int, def_constr_deleter<int> > p;
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(p.get_deleter().state() == 5);
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[]> p;
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
{
|
||||
bml::unique_ptr<int[], def_constr_deleter<int[]> > p;
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(p.get_deleter().state() == 5);
|
||||
}
|
||||
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[]> p;
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
{
|
||||
bml::unique_ptr<int[], def_constr_deleter<int[]> > p;
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(p.get_deleter().state() == 5);
|
||||
}
|
||||
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[2]> p;
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
{
|
||||
bml::unique_ptr<int[2], def_constr_deleter<int[2]> > p;
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(p.get_deleter().state() == 5);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_default_delreq{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_default_nocomplete
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_default_nocomplete{
|
||||
|
||||
// default unique_ptr ctor shouldn't require complete type
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
J<I> s;
|
||||
BOOST_TEST(s.get() == 0);
|
||||
}
|
||||
check(0);
|
||||
{
|
||||
J<I, def_constr_deleter<I> > s;
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
check(0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
J<I[]> s;
|
||||
BOOST_TEST(s.get() == 0);
|
||||
}
|
||||
check(0);
|
||||
{
|
||||
J<I[], def_constr_deleter<I[]> > s;
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
check(0);
|
||||
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
J<I[2]> s;
|
||||
BOOST_TEST(s.get() == 0);
|
||||
}
|
||||
check(0);
|
||||
{
|
||||
J<I[2], def_constr_deleter<I[2]> > s;
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
check(0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_default_nocomplete{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_delreq
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_delreq{
|
||||
|
||||
// unique_ptr(pointer) ctor should only require default deleter ctor
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A;
|
||||
BOOST_TEST(A::count == 1);
|
||||
bml::unique_ptr<A> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
A* p = new A;
|
||||
BOOST_TEST(A::count == 1);
|
||||
bml::unique_ptr<A, def_constr_deleter<A> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<A[]> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<A[], def_constr_deleter<A[]> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<A[2]> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<A[2], def_constr_deleter<A[2]> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_delreq{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_nocomplete
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_nocomplete{
|
||||
|
||||
// unique_ptr(pointer) ctor shouldn't require complete type
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
I* p = get();
|
||||
check(1);
|
||||
J<I> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
check(0);
|
||||
{
|
||||
I* p = get();
|
||||
check(1);
|
||||
J<I, def_constr_deleter<I> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
check(0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
I* p = get_array(2);
|
||||
check(2);
|
||||
J<I[]> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
check(0);
|
||||
{
|
||||
I* p = get_array(2);
|
||||
check(2);
|
||||
J<I[], def_constr_deleter<I[]> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
check(0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
I* p = get_array(2);
|
||||
check(2);
|
||||
J<I[]> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
check(0);
|
||||
{
|
||||
I* p = get_array(2);
|
||||
check(2);
|
||||
J<I[2], def_constr_deleter<I[2]> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
check(0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_nocomplete{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_convert
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_convert{
|
||||
|
||||
// unique_ptr(pointer) ctor should work with derived pointers
|
||||
// or same types (cv aside) for unique_ptr<arrays>
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
B* p = new B;
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(B::count == 1);
|
||||
bml::unique_ptr<A> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(B::count == 0);
|
||||
{
|
||||
B* p = new B;
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(B::count == 1);
|
||||
bml::unique_ptr<A, def_constr_deleter<A> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(B::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<const A[]> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
const A* p = new const A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<const volatile A[], def_constr_deleter<const volatile A[]> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<const A[2]> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
const A* p = new const A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<const volatile A[2], def_constr_deleter<const volatile A[2]> > s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_convert{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_deleter_movedel
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_deleter_movedel{
|
||||
|
||||
// test move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
// unique_ptr(pointer, deleter()) only requires MoveConstructible deleter
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A;
|
||||
BOOST_TEST(A::count == 1);
|
||||
move_constr_deleter<A> d;
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > s(p, ::boost::move(d));
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > s2(s.release(), move_constr_deleter<A>(6));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s2.get_deleter().state() == 6);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
move_constr_deleter<A[]> d;
|
||||
bml::unique_ptr<A[], move_constr_deleter<A[]> > s(p, ::boost::move(d));
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
bml::unique_ptr<A[], move_constr_deleter<A[]> > s2(s.release(), move_constr_deleter<A[]>(6));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s2.get_deleter().state() == 6);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
move_constr_deleter<A[2]> d;
|
||||
bml::unique_ptr<A[2], move_constr_deleter<A[2]> > s(p, ::boost::move(d));
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
bml::unique_ptr<A[2], move_constr_deleter<A[2]> > s2(s.release(), move_constr_deleter<A[2]>(6));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s2.get_deleter().state() == 6);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_deleter_movedel{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_deleter_copydel
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_deleter_copydel{
|
||||
|
||||
// unique_ptr(pointer, d) requires CopyConstructible deleter
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A;
|
||||
BOOST_TEST(A::count == 1);
|
||||
copy_constr_deleter<A> d;
|
||||
bml::unique_ptr<A, copy_constr_deleter<A> > s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
copy_constr_deleter<A[]> d;
|
||||
bml::unique_ptr<A[], copy_constr_deleter<A[]> > s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
copy_constr_deleter<A[2]> d;
|
||||
bml::unique_ptr<A[2], copy_constr_deleter<A[2]> > s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_deleter_copydel{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_deleter_dfctrdelref
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_deleter_dfctrdelref{
|
||||
|
||||
// unique_ptr<T, D&>(pointer, d) does not requires CopyConstructible deleter
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A;
|
||||
BOOST_TEST(A::count == 1);
|
||||
def_constr_deleter<A> d;
|
||||
bml::unique_ptr<A, def_constr_deleter<A>&> s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s.get_deleter().state() == 6);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
def_constr_deleter<A[]> d;
|
||||
bml::unique_ptr<A[], def_constr_deleter<A[]>&> s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s.get_deleter().state() == 6);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
def_constr_deleter<A[2]> d;
|
||||
bml::unique_ptr<A[2], def_constr_deleter<A[2]>&> s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s.get_deleter().state() == 6);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_deleter_dfctrdelref{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_deleter_dfctrdelconstref
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_deleter_dfctrdelconstref{
|
||||
|
||||
// unique_ptr<T, const D&>(pointer, d) does not requires CopyConstructible deleter
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A;
|
||||
BOOST_TEST(A::count == 1);
|
||||
def_constr_deleter<A> d;
|
||||
bml::unique_ptr<A, const def_constr_deleter<A>&> s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
def_constr_deleter<A[]> d;
|
||||
bml::unique_ptr<A[], const def_constr_deleter<A[]>&> s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
def_constr_deleter<A[2]> d;
|
||||
bml::unique_ptr<A[2], const def_constr_deleter<A[2]>&> s(p, d);
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_deleter_dfctrdelconstref{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_deleter_convert
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_deleter_convert{
|
||||
|
||||
// unique_ptr(pointer, deleter) should work with derived pointers
|
||||
// or same (cv aside) types for array unique_ptrs
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
B* p = new B;
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(B::count == 1);
|
||||
bml::unique_ptr<A, copy_constr_deleter<A> > s(p, copy_constr_deleter<A>());
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(B::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<const A[], copy_constr_deleter<const A[]> > s(p, copy_constr_deleter<const A[]>());
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(B::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p = new A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
bml::unique_ptr<const A[2], copy_constr_deleter<const A[2]> > s(p, copy_constr_deleter<const A[2]>());
|
||||
BOOST_TEST(s.get() == p);
|
||||
BOOST_TEST(s.get_deleter().state() == 5);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(B::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_deleter_convert{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_pointer_deleter_void
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_pointer_deleter_void{
|
||||
|
||||
// unique_ptr(pointer, deleter) should work with function pointers
|
||||
// unique_ptr<void> should work
|
||||
|
||||
bool my_free_called = false;
|
||||
|
||||
void my_free(void*)
|
||||
{
|
||||
my_free_called = true;
|
||||
}
|
||||
|
||||
void test()
|
||||
{
|
||||
{
|
||||
int i = 0;
|
||||
bml::unique_ptr<void, void (*)(void*)> s(&i, my_free);
|
||||
BOOST_TEST(s.get() == &i);
|
||||
BOOST_TEST(s.get_deleter() == my_free);
|
||||
BOOST_TEST(!my_free_called);
|
||||
}
|
||||
BOOST_TEST(my_free_called);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_pointer_deleter_void{
|
||||
|
||||
////////////////////////////////
|
||||
// main
|
||||
////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
//Constructors/Destructor
|
||||
unique_ptr_dtor_null::test();
|
||||
unique_ptr_ctor_default_delreq::test();
|
||||
unique_ptr_ctor_default_nocomplete::test();
|
||||
unique_ptr_ctor_pointer_delreq::test();
|
||||
unique_ptr_ctor_pointer_nocomplete::test();
|
||||
unique_ptr_ctor_pointer_convert::test();
|
||||
unique_ptr_ctor_pointer_deleter_movedel::test();
|
||||
unique_ptr_ctor_pointer_deleter_copydel::test();
|
||||
unique_ptr_ctor_pointer_deleter_dfctrdelref::test();
|
||||
unique_ptr_ctor_pointer_deleter_dfctrdelconstref::test();
|
||||
unique_ptr_ctor_pointer_deleter_convert::test();
|
||||
unique_ptr_ctor_pointer_deleter_void::test();
|
||||
|
||||
//Test results
|
||||
return boost::report_errors();
|
||||
|
||||
}
|
||||
|
||||
#include "unique_ptr_test_utils_end.hpp"
|
||||
@@ -83,10 +83,73 @@ void test()
|
||||
d1(p);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
//Bounded array element deleter
|
||||
{
|
||||
reset_counters();
|
||||
bml::default_delete<A[2]> d2;
|
||||
bml::default_delete<const A[2]> d1 = d2;
|
||||
const A* p = new const A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
d1(p);
|
||||
bml::default_delete<const A[2]> d0 = d1;
|
||||
d0(0);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_dltr_dflt_convert_ctor{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_dltr_dflt_convert_assign
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_dltr_dflt_convert_assign{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single element deleter
|
||||
{
|
||||
reset_counters();
|
||||
bml::default_delete<B> d2;
|
||||
bml::default_delete<A> d1;
|
||||
d1 = d2;
|
||||
A* p = new B;
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(B::count == 1);
|
||||
d1(p);
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(B::count == 0);
|
||||
}
|
||||
//Array element deleter
|
||||
{
|
||||
reset_counters();
|
||||
bml::default_delete<A[]> d2;
|
||||
bml::default_delete<const A[]> d1;
|
||||
d1 = d2;
|
||||
const A* p = new const A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
d1(p);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
//Bounded array element deleter
|
||||
{
|
||||
reset_counters();
|
||||
bml::default_delete<A[2]> d2;
|
||||
bml::default_delete<const A[2]> d1;
|
||||
d1 = d2;
|
||||
const A* p = new const A[2];
|
||||
BOOST_TEST(A::count == 2);
|
||||
d1(p);
|
||||
bml::default_delete<const A[]> d0;
|
||||
d0 = d1;
|
||||
d0(0);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_dltr_dflt_convert_assign{
|
||||
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_dltr_dflt_default
|
||||
////////////////////////////////
|
||||
@@ -113,6 +176,16 @@ void test()
|
||||
d(p);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
{
|
||||
//Bounded Array element deleter
|
||||
reset_counters();
|
||||
bml::default_delete<A[10]> d;
|
||||
A* p = new A[10];
|
||||
BOOST_TEST(A::count == 10);
|
||||
d(p);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_dltr_dflt_default{
|
||||
@@ -123,6 +196,7 @@ void test()
|
||||
int main()
|
||||
{
|
||||
unique_ptr_dltr_dflt_convert_ctor::test();
|
||||
unique_ptr_dltr_dflt_convert_assign::test();
|
||||
unique_ptr_dltr_dflt_default::test();
|
||||
|
||||
//Test results
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
#include <boost/move/make_unique.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <cstring>
|
||||
|
||||
struct A
|
||||
{
|
||||
@@ -40,6 +41,27 @@ int B::count = 0;
|
||||
void reset_counters()
|
||||
{ A::count = B::count = 0; }
|
||||
|
||||
static const unsigned PatternSize = 8;
|
||||
static const unsigned char ff_patternbuf[PatternSize] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
static const unsigned char ee_patternbuf[PatternSize] = { 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE };
|
||||
|
||||
struct default_init
|
||||
{
|
||||
static void* operator new(std::size_t sz)
|
||||
{
|
||||
void *const p = ::operator new(sz);
|
||||
return std::memset(p, 0xFF, sz);
|
||||
}
|
||||
static void* operator new[](std::size_t sz)
|
||||
{
|
||||
void *const p = ::operator new[](sz);
|
||||
return std::memset(p, 0xEE, sz);
|
||||
}
|
||||
unsigned char buf[PatternSize];
|
||||
};
|
||||
|
||||
|
||||
|
||||
namespace bml = ::boost::movelib;
|
||||
|
||||
////////////////////////////////
|
||||
@@ -53,12 +75,18 @@ void test()
|
||||
//Single element deleter
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<default_init> p(bml::make_unique_definit<default_init>());
|
||||
BOOST_TEST(0 == std::memcmp(p.get(), ff_patternbuf, sizeof(ff_patternbuf)));
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A> p(bml::make_unique<A>());
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(p->a == 999);
|
||||
BOOST_TEST(p->b == 1000);
|
||||
BOOST_TEST(p->c == 1001);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A> p(bml::make_unique<A>(0));
|
||||
BOOST_TEST(A::count == 1);
|
||||
@@ -95,7 +123,7 @@ namespace make_unique_array{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single element deleter
|
||||
//Array element
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A[]> p(bml::make_unique<A[]>(10));
|
||||
@@ -107,6 +135,13 @@ void test()
|
||||
}
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<default_init[]> p(bml::make_unique_definit<default_init[]>(10));
|
||||
for(unsigned i = 0; i != 10; ++i){
|
||||
BOOST_TEST(0 == std::memcmp(&p[i], ee_patternbuf, sizeof(ee_patternbuf)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace make_unique_array{
|
||||
@@ -158,7 +193,6 @@ void test()
|
||||
|
||||
} //namespace unique_compare{
|
||||
|
||||
|
||||
////////////////////////////////
|
||||
// unique_compare_zero
|
||||
////////////////////////////////
|
||||
|
||||
@@ -0,0 +1,377 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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 <boost/move/utility_core.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//
|
||||
// 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_modifiers_release
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_modifiers_release{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int> p(new int(3));
|
||||
int* i = p.get();
|
||||
int* j = p.release();
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(i == j);
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[]> p(new int[2]);
|
||||
int* i = p.get();
|
||||
int* j = p.release();
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(i == j);
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[2]> p(new int[2]);
|
||||
int* i = p.get();
|
||||
int* j = p.release();
|
||||
BOOST_TEST(p.get() == 0);
|
||||
BOOST_TEST(i == j);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_modifiers_release{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_modifiers_reset
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_modifiers_reset{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
reset_counters();
|
||||
{ //reset()
|
||||
bml::unique_ptr<A> p(new A);
|
||||
BOOST_TEST(A::count == 1);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset();
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{ //reset(p)
|
||||
bml::unique_ptr<A> p(new A);
|
||||
BOOST_TEST(A::count == 1);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new A);
|
||||
BOOST_TEST(A::count == 1);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{ //reset(0)
|
||||
bml::unique_ptr<A> p(new A);
|
||||
BOOST_TEST(A::count == 1);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(0);
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
reset_counters();
|
||||
{ //reset()
|
||||
bml::unique_ptr<A[]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset();
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{ //reset(p)
|
||||
bml::unique_ptr<A[]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new A[3]);
|
||||
BOOST_TEST(A::count == 3);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{ //reset(0)
|
||||
bml::unique_ptr<A[]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(0);
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
{
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{ //reset()
|
||||
bml::unique_ptr<A[2]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset();
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{ //reset(p)
|
||||
bml::unique_ptr<A[2]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new A[3]);
|
||||
BOOST_TEST(A::count == 3);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{ //reset(0)
|
||||
bml::unique_ptr<A[2]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(0);
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_modifiers_reset{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_modifiers_reset_convert
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_modifiers_reset_convert{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A> p(new A);
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(B::count == 0);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new B);
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(B::count == 1);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(B::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A> p(new B);
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(B::count == 1);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new B);
|
||||
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<const volatile A[2]> p(new const A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
const volatile A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new volatile A[3]);
|
||||
BOOST_TEST(A::count == 3);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<const A[2]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
const A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new const A[3]);
|
||||
BOOST_TEST(A::count == 3);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<const volatile A[2]> p(new const A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
const volatile A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new volatile A[3]);
|
||||
BOOST_TEST(A::count == 3);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<const A[2]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
const A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(new const A[3]);
|
||||
BOOST_TEST(A::count == 3);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //unique_ptr_modifiers_reset_convert
|
||||
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_modifiers
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_modifiers_swap{
|
||||
|
||||
// test swap
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p1 = new A(1);
|
||||
move_constr_deleter<A> d1(1);
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > s1(p1, ::boost::move(d1));
|
||||
A* p2 = new A(2);
|
||||
move_constr_deleter<A> d2(2);
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > s2(p2, ::boost::move(d2));
|
||||
BOOST_TEST(s1.get() == p1);
|
||||
BOOST_TEST(*s1 == A(1));
|
||||
BOOST_TEST(s1.get_deleter().state() == 1);
|
||||
BOOST_TEST(s2.get() == p2);
|
||||
BOOST_TEST(*s2 == A(2));
|
||||
BOOST_TEST(s2.get_deleter().state() == 2);
|
||||
swap(s1, s2);
|
||||
BOOST_TEST(s1.get() == p2);
|
||||
BOOST_TEST(*s1 == A(2));
|
||||
BOOST_TEST(s1.get_deleter().state() == 2);
|
||||
BOOST_TEST(s2.get() == p1);
|
||||
BOOST_TEST(*s2 == A(1));
|
||||
BOOST_TEST(s2.get_deleter().state() == 1);
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p1 = new A[2];
|
||||
p1[0].set(1);
|
||||
p1[1].set(2);
|
||||
move_constr_deleter<A[]> d1(1);
|
||||
bml::unique_ptr<A[], move_constr_deleter<A[]> > s1(p1, ::boost::move(d1));
|
||||
A* p2 = new A[2];
|
||||
p2[0].set(3);
|
||||
p2[1].set(4);
|
||||
move_constr_deleter<A[]> d2(2);
|
||||
bml::unique_ptr<A[], move_constr_deleter<A[]> > s2(p2, ::boost::move(d2));
|
||||
BOOST_TEST(s1.get() == p1);
|
||||
BOOST_TEST(s1[0] == A(1));
|
||||
BOOST_TEST(s1[1] == A(2));
|
||||
BOOST_TEST(s1.get_deleter().state() == 1);
|
||||
BOOST_TEST(s2.get() == p2);
|
||||
BOOST_TEST(s2[0] == A(3));
|
||||
BOOST_TEST(s2[1] == A(4));
|
||||
BOOST_TEST(s2.get_deleter().state() == 2);
|
||||
swap(s1, s2);
|
||||
BOOST_TEST(s1.get() == p2);
|
||||
BOOST_TEST(s1[0] == A(3));
|
||||
BOOST_TEST(s1[1] == A(4));
|
||||
BOOST_TEST(s1.get_deleter().state() == 2);
|
||||
BOOST_TEST(s2.get() == p1);
|
||||
BOOST_TEST(s2[0] == A(1));
|
||||
BOOST_TEST(s2[1] == A(2));
|
||||
BOOST_TEST(s2.get_deleter().state() == 1);
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
A* p1 = new A[2];
|
||||
p1[0].set(1);
|
||||
p1[1].set(2);
|
||||
move_constr_deleter<A[2]> d1(1);
|
||||
bml::unique_ptr<A[2], move_constr_deleter<A[2]> > s1(p1, ::boost::move(d1));
|
||||
A* p2 = new A[2];
|
||||
p2[0].set(3);
|
||||
p2[1].set(4);
|
||||
move_constr_deleter<A[2]> d2(2);
|
||||
bml::unique_ptr<A[2], move_constr_deleter<A[2]> > s2(p2, ::boost::move(d2));
|
||||
BOOST_TEST(s1.get() == p1);
|
||||
BOOST_TEST(s1[0] == A(1));
|
||||
BOOST_TEST(s1[1] == A(2));
|
||||
BOOST_TEST(s1.get_deleter().state() == 1);
|
||||
BOOST_TEST(s2.get() == p2);
|
||||
BOOST_TEST(s2[0] == A(3));
|
||||
BOOST_TEST(s2[1] == A(4));
|
||||
BOOST_TEST(s2.get_deleter().state() == 2);
|
||||
swap(s1, s2);
|
||||
BOOST_TEST(s1.get() == p2);
|
||||
BOOST_TEST(s1[0] == A(3));
|
||||
BOOST_TEST(s1[1] == A(4));
|
||||
BOOST_TEST(s1.get_deleter().state() == 2);
|
||||
BOOST_TEST(s2.get() == p1);
|
||||
BOOST_TEST(s2[0] == A(1));
|
||||
BOOST_TEST(s2[1] == A(2));
|
||||
BOOST_TEST(s2.get_deleter().state() == 1);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_modifiers_swap{
|
||||
|
||||
////////////////////////////////
|
||||
// main
|
||||
////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
//Modifiers
|
||||
unique_ptr_modifiers_release::test();
|
||||
unique_ptr_modifiers_reset::test();
|
||||
unique_ptr_modifiers_reset_convert::test();
|
||||
unique_ptr_modifiers_swap::test();
|
||||
|
||||
//Test results
|
||||
return boost::report_errors();
|
||||
|
||||
}
|
||||
|
||||
#include "unique_ptr_test_utils_end.hpp"
|
||||
@@ -0,0 +1,516 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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 <boost/move/utility_core.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//
|
||||
// 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_ctor_move_defdel
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_move_defdel{
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A> s(new A);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A> s2(boost::move(s));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 1);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A[]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A[]> 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<A[2]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A[2]> 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_ctor_move_defdel{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_move_movedel
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_move_movedel{
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > s(new A);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > s2 = boost::move(s);
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(s2.get_deleter().state() == 5);
|
||||
BOOST_TEST(s.get_deleter().state() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A[], move_constr_deleter<A[]> > s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A[], move_constr_deleter<A[]> > 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<A[2]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A[2]> 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_ctor_move_movedel{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_move_dfctrdelref
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_move_dfctrdelref{
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
def_constr_deleter<A> d;
|
||||
bml::unique_ptr<A, def_constr_deleter<A>&> s(new A, d);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A, def_constr_deleter<A>&> s2 = boost::move(s);
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 1);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s2.get_deleter().state() == d.state());
|
||||
BOOST_TEST(s.get_deleter().state() == d.state());
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
def_constr_deleter<A[]> d;
|
||||
bml::unique_ptr<A[], def_constr_deleter<A[]>&> s(new A[2], d);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A[], def_constr_deleter<A[]>&> s2 = boost::move(s);
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 2);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s2.get_deleter().state() == d.state());
|
||||
BOOST_TEST(s.get_deleter().state() == d.state());
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
def_constr_deleter<A[2]> d;
|
||||
bml::unique_ptr<A[2], def_constr_deleter<A[2]>&> s(new A[2], d);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A[2], def_constr_deleter<A[2]>&> s2(boost::move(s));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 2);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s2.get_deleter().state() == d.state());
|
||||
BOOST_TEST(s.get_deleter().state() == d.state());
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_move_dfctrdelref{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_move_convert_defdel
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_move_convert_defdel{
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<B> s(new B);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A> 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<A[]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const volatile A[]> 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<A[2]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const volatile A[2]> 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<A[2]> s(new A[2]);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<const volatile A[]> 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_ctor_move_convert_defdel{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_move_convert_movedel
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_move_convert_movedel{
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_convertible<B, A>::value));
|
||||
{
|
||||
bml::unique_ptr<B, move_constr_deleter<B> > s(new B);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > 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<const A[], move_constr_deleter<const A[]> > s(new const A[2]);
|
||||
const A* p = s.get();
|
||||
bml::unique_ptr<const volatile A[], move_constr_deleter<const volatile A[]> > 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);
|
||||
BOOST_TEST(B::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<const A[2], move_constr_deleter<const A[2]> > s(new const A[2]);
|
||||
const A* p = s.get();
|
||||
bml::unique_ptr<const volatile A[2], move_constr_deleter<const volatile A[2]> > 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);
|
||||
BOOST_TEST(B::count == 0);
|
||||
{
|
||||
bml::unique_ptr<const A[2], move_constr_deleter<const A[2]> > s(new const A[2]);
|
||||
const A* p = s.get();
|
||||
bml::unique_ptr<const volatile A[], move_constr_deleter<const volatile A[]> > 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);
|
||||
BOOST_TEST(B::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_move_convert_movedel{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_move_convert_dfctrdelref
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_move_convert_dfctrdelref{
|
||||
|
||||
// test converting move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
def_constr_deleter<A> d;
|
||||
bml::unique_ptr<B, def_constr_deleter<A>&> s(new B, d);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<A, def_constr_deleter<A>&> s2(boost::move(s));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 1);
|
||||
BOOST_TEST(B::count == 1);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s2.get_deleter().state() == d.state());
|
||||
BOOST_TEST(s.get_deleter().state() == d.state());
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(B::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
def_constr_deleter<volatile A[]> d;
|
||||
bml::unique_ptr<A[], def_constr_deleter<volatile A[]>&> s(new A[2], d);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<volatile A[], def_constr_deleter<volatile A[]>&> s2(boost::move(s));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 2);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s2.get_deleter().state() == d.state());
|
||||
BOOST_TEST(s.get_deleter().state() == d.state());
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
def_constr_deleter<volatile A[2]> d;
|
||||
bml::unique_ptr<A[2], def_constr_deleter<volatile A[2]>&> s(new A[2], d);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<volatile A[2], def_constr_deleter<volatile A[2]>&> s2(boost::move(s));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 2);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s2.get_deleter().state() == d.state());
|
||||
BOOST_TEST(s.get_deleter().state() == d.state());
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
def_constr_deleter<volatile A[]> d;
|
||||
bml::unique_ptr<A[2], def_constr_deleter<volatile A[]>&> s(new A[2], d);
|
||||
A* p = s.get();
|
||||
bml::unique_ptr<volatile A[], def_constr_deleter<volatile A[]>&> s2(boost::move(s));
|
||||
BOOST_TEST(s2.get() == p);
|
||||
BOOST_TEST(s.get() == 0);
|
||||
BOOST_TEST(A::count == 2);
|
||||
d.set_state(6);
|
||||
BOOST_TEST(s2.get_deleter().state() == d.state());
|
||||
BOOST_TEST(s.get_deleter().state() == d.state());
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_move_convert_dfctrdelref{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_ctor_move_sourcesink
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_ctor_move_sourcesink{
|
||||
|
||||
// test move ctor. Should only require a MoveConstructible deleter, or if
|
||||
// deleter is a reference, not even that.
|
||||
|
||||
bml::unique_ptr<A> source1()
|
||||
{ return bml::unique_ptr<A>(new A); }
|
||||
|
||||
bml::unique_ptr<A[]> source1_unbounded_array()
|
||||
{ return bml::unique_ptr<A[]> (new A[2]); }
|
||||
|
||||
bml::unique_ptr<A[2]> source1_bounded_array()
|
||||
{ return bml::unique_ptr<A[2]> (new A[2]); }
|
||||
|
||||
void sink1(bml::unique_ptr<A>)
|
||||
{}
|
||||
|
||||
void sink1_unbounded_array(bml::unique_ptr<A[]>)
|
||||
{}
|
||||
|
||||
void sink1_bounded_array(bml::unique_ptr<A[2]>)
|
||||
{}
|
||||
|
||||
bml::unique_ptr<A, move_constr_deleter<A> > source2()
|
||||
{ return bml::unique_ptr<A, move_constr_deleter<A> > (new A); }
|
||||
|
||||
bml::unique_ptr<A[], move_constr_deleter<A[]> > source2_unbounded_array()
|
||||
{ return bml::unique_ptr<A[], move_constr_deleter<A[]> >(new A[2]); }
|
||||
|
||||
bml::unique_ptr<A[2], move_constr_deleter<A[2]> > source2_bounded_array()
|
||||
{ return bml::unique_ptr<A[2], move_constr_deleter<A[2]> >(new A[2]); }
|
||||
|
||||
void sink2(bml::unique_ptr<A, move_constr_deleter<A> >)
|
||||
{}
|
||||
|
||||
void sink2_unbounded_array(bml::unique_ptr<A[], move_constr_deleter<A[]> >)
|
||||
{}
|
||||
|
||||
void sink2_bounded_array(bml::unique_ptr<A[2], move_constr_deleter<A[2]> >)
|
||||
{}
|
||||
|
||||
bml::unique_ptr<A, def_constr_deleter<A>&> source3()
|
||||
{
|
||||
static def_constr_deleter<A> d;
|
||||
return bml::unique_ptr<A, def_constr_deleter<A>&>(new A, d);
|
||||
}
|
||||
|
||||
bml::unique_ptr<A[], def_constr_deleter<A[]>&> source3_unbounded_array()
|
||||
{
|
||||
static def_constr_deleter<A[]> d;
|
||||
return bml::unique_ptr<A[], def_constr_deleter<A[]>&>(new A[2], d);
|
||||
}
|
||||
|
||||
bml::unique_ptr<A[2], def_constr_deleter<A[2]>&> source3_bounded_array()
|
||||
{
|
||||
static def_constr_deleter<A[2]> d;
|
||||
return bml::unique_ptr<A[2], def_constr_deleter<A[2]>&>(new A[2], d);
|
||||
}
|
||||
|
||||
void sink3(bml::unique_ptr<A, def_constr_deleter<A>&> )
|
||||
{}
|
||||
|
||||
void sink3_unbounded_array(bml::unique_ptr<A[], def_constr_deleter<A[]>&> )
|
||||
{}
|
||||
|
||||
void sink3_bounded_array(bml::unique_ptr<A[2], def_constr_deleter<A[2]>&> )
|
||||
{}
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
sink1(source1());
|
||||
sink2(source2());
|
||||
sink3(source3());
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
sink1_unbounded_array(source1_unbounded_array());
|
||||
sink2_unbounded_array(source2_unbounded_array());
|
||||
sink3_unbounded_array(source3_unbounded_array());
|
||||
BOOST_TEST(A::count == 0);
|
||||
//Bbounded array unique_ptr
|
||||
reset_counters();
|
||||
sink1_bounded_array(source1_bounded_array());
|
||||
sink2_bounded_array(source2_bounded_array());
|
||||
sink3_bounded_array(source3_bounded_array());
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_ctor_move_sourcesink{
|
||||
|
||||
////////////////////////////////
|
||||
// main
|
||||
////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
//Move Constructor
|
||||
unique_ptr_ctor_move_defdel::test();
|
||||
unique_ptr_ctor_move_movedel::test();
|
||||
unique_ptr_ctor_move_dfctrdelref::test();
|
||||
unique_ptr_ctor_move_convert_defdel::test();
|
||||
unique_ptr_ctor_move_convert_movedel::test();
|
||||
unique_ptr_ctor_move_convert_dfctrdelref::test();
|
||||
unique_ptr_ctor_move_sourcesink::test();
|
||||
|
||||
//Test results
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#include "unique_ptr_test_utils_end.hpp"
|
||||
@@ -0,0 +1,228 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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 <boost/move/utility_core.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//
|
||||
// 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_zero
|
||||
////////////////////////////////
|
||||
namespace unique_ptr_zero {
|
||||
|
||||
// test initialization/assignment from zero
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A> s2(0);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A> s2(new A);
|
||||
BOOST_TEST(A::count == 1);
|
||||
s2 = 0;
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(s2.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<A[]> s2(0);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A[]> s2(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
s2 = 0;
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(s2.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<A[2]> s2(0);
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A[2]> s2(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
s2 = 0;
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(s2.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_zero {
|
||||
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_nullptr
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_nullptr{
|
||||
|
||||
void test()
|
||||
{
|
||||
#if !defined(BOOST_NO_CXX11_NULLPTR)
|
||||
//Single unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A> p(new A);
|
||||
BOOST_TEST(A::count == 1);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(nullptr);
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A> p(new A);
|
||||
BOOST_TEST(A::count == 1);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p = nullptr;
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
|
||||
{
|
||||
bml::unique_ptr<A> pi(nullptr);
|
||||
BOOST_TEST(pi.get() == nullptr);
|
||||
BOOST_TEST(pi.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A> pi(nullptr, bml::unique_ptr<A>::deleter_type());
|
||||
BOOST_TEST(pi.get() == nullptr);
|
||||
BOOST_TEST(pi.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
|
||||
//Unbounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A[]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(nullptr);
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A[]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p = nullptr;
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A[]> pi(nullptr);
|
||||
BOOST_TEST(pi.get() == nullptr);
|
||||
BOOST_TEST(pi.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A[]> pi(nullptr, bml::unique_ptr<A[]>::deleter_type());
|
||||
BOOST_TEST(pi.get() == nullptr);
|
||||
BOOST_TEST(pi.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
|
||||
//Bounded array unique_ptr
|
||||
reset_counters();
|
||||
{
|
||||
bml::unique_ptr<A[2]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p.reset(nullptr);
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A[2]> p(new A[2]);
|
||||
BOOST_TEST(A::count == 2);
|
||||
A* i = p.get();
|
||||
(void)i;
|
||||
p = nullptr;
|
||||
BOOST_TEST(A::count == 0);
|
||||
BOOST_TEST(p.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A[2]> pi(nullptr);
|
||||
BOOST_TEST(pi.get() == nullptr);
|
||||
BOOST_TEST(pi.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
{
|
||||
bml::unique_ptr<A[2]> pi(nullptr, bml::unique_ptr<A[2]>::deleter_type());
|
||||
BOOST_TEST(pi.get() == nullptr);
|
||||
BOOST_TEST(pi.get() == 0);
|
||||
}
|
||||
BOOST_TEST(A::count == 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_nullptr{
|
||||
|
||||
////////////////////////////////
|
||||
// main
|
||||
////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
//nullptr
|
||||
unique_ptr_zero::test();
|
||||
unique_ptr_nullptr::test();
|
||||
|
||||
//Test results
|
||||
return boost::report_errors();
|
||||
|
||||
}
|
||||
|
||||
#include "unique_ptr_test_utils_end.hpp"
|
||||
@@ -0,0 +1,287 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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 <boost/move/utility_core.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//
|
||||
// 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_observers_dereference
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_observers_dereference{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int> p(new int(3));
|
||||
BOOST_TEST(*p == 3);
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
int *pi = new int[2];
|
||||
pi[0] = 3;
|
||||
pi[1] = 4;
|
||||
bml::unique_ptr<int[]> p(pi);
|
||||
BOOST_TEST(p[0] == 3);
|
||||
BOOST_TEST(p[1] == 4);
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
int *pi = new int[2];
|
||||
pi[0] = 3;
|
||||
pi[1] = 4;
|
||||
bml::unique_ptr<int[2]> p(pi);
|
||||
BOOST_TEST(p[0] == 3);
|
||||
BOOST_TEST(p[1] == 4);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_observers_dereference{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_observers_dereference
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_observers_explicit_bool{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int> p(new int(3));
|
||||
if (p)
|
||||
;
|
||||
else
|
||||
BOOST_TEST(false);
|
||||
if (!p)
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
{
|
||||
bml::unique_ptr<int> p;
|
||||
if (!p)
|
||||
;
|
||||
else
|
||||
BOOST_TEST(false);
|
||||
if (p)
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[]> p(new int[2]);
|
||||
if (p)
|
||||
;
|
||||
else
|
||||
BOOST_TEST(false);
|
||||
if (!p)
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
{
|
||||
bml::unique_ptr<int[]> p;
|
||||
if (!p)
|
||||
;
|
||||
else
|
||||
BOOST_TEST(false);
|
||||
if (p)
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[2]> p(new int[2]);
|
||||
if (p)
|
||||
;
|
||||
else
|
||||
BOOST_TEST(false);
|
||||
if (!p)
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
{
|
||||
bml::unique_ptr<int[2]> p;
|
||||
if (!p)
|
||||
;
|
||||
else
|
||||
BOOST_TEST(false);
|
||||
if (p)
|
||||
BOOST_TEST(false);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_observers_explicit_bool{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_observers_get
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_observers_get{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
int* p = new int;
|
||||
bml::unique_ptr<int> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
int* p = new int[2];
|
||||
bml::unique_ptr<int[]> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
{
|
||||
int* p = new int[2];
|
||||
bml::unique_ptr<int[2]> s(p);
|
||||
BOOST_TEST(s.get() == p);
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
int *pi = new int[2];
|
||||
pi[0] = 3;
|
||||
pi[1] = 4;
|
||||
bml::unique_ptr<int[2]> p(pi);
|
||||
BOOST_TEST(p[0] == 3);
|
||||
BOOST_TEST(p[1] == 4);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_observers_get{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_observers_get_deleter
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_observers_get_deleter{
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
void operator()(void*) {}
|
||||
|
||||
int test() {return 5;}
|
||||
int test() const {return 6;}
|
||||
};
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int, Deleter> p;
|
||||
BOOST_TEST(p.get_deleter().test() == 5);
|
||||
}
|
||||
{
|
||||
const bml::unique_ptr<int, Deleter> p;
|
||||
BOOST_TEST(p.get_deleter().test() == 6);
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[], Deleter> p;
|
||||
BOOST_TEST(p.get_deleter().test() == 5);
|
||||
}
|
||||
{
|
||||
const bml::unique_ptr<int[], Deleter> p;
|
||||
BOOST_TEST(p.get_deleter().test() == 6);
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
bml::unique_ptr<int[2], Deleter> p;
|
||||
BOOST_TEST(p.get_deleter().test() == 5);
|
||||
}
|
||||
{
|
||||
const bml::unique_ptr<int[2], Deleter> p;
|
||||
BOOST_TEST(p.get_deleter().test() == 6);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_observers_get_deleter{
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_observers_op_arrow
|
||||
////////////////////////////////
|
||||
|
||||
namespace unique_ptr_observers_op_arrow{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
bml::unique_ptr<A> p(new A);
|
||||
BOOST_TEST(p->state_ == 999);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_observers_op_arrow{
|
||||
|
||||
|
||||
namespace unique_ptr_observers_op_index{
|
||||
|
||||
void test()
|
||||
{
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
A *pa = new A[2];
|
||||
//pa[0] is left default constructed
|
||||
pa[1].set(888);
|
||||
bml::unique_ptr<A[]> p(pa);
|
||||
BOOST_TEST(p[0].state_ == 999);
|
||||
BOOST_TEST(p[1].state_ == 888);
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
A *pa = new A[2];
|
||||
//pa[0] is left default constructed
|
||||
pa[1].set(888);
|
||||
bml::unique_ptr<A[2]> p(pa);
|
||||
BOOST_TEST(p[0].state_ == 999);
|
||||
BOOST_TEST(p[1].state_ == 888);
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_observers_op_index{
|
||||
|
||||
////////////////////////////////
|
||||
// main
|
||||
////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
//Observers
|
||||
unique_ptr_observers_dereference::test();
|
||||
unique_ptr_observers_explicit_bool::test();
|
||||
unique_ptr_observers_get::test();
|
||||
unique_ptr_observers_get_deleter::test();
|
||||
unique_ptr_observers_op_arrow::test();
|
||||
unique_ptr_observers_op_index::test();
|
||||
|
||||
//Test results
|
||||
return boost::report_errors();
|
||||
|
||||
}
|
||||
|
||||
#include "unique_ptr_test_utils_end.hpp"
|
||||
@@ -0,0 +1,209 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef BOOST_MOVE_UNIQUE_PTR_TEST_UTILS_BEG_HPP
|
||||
#define BOOST_MOVE_UNIQUE_PTR_TEST_UTILS_BEG_HPP
|
||||
#include <boost/move/core.hpp>
|
||||
#include <boost/move/detail/meta_utils.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
//////////////////////////////////////////////
|
||||
|
||||
//A deleter that can only default constructed
|
||||
template <class T>
|
||||
class def_constr_deleter
|
||||
{
|
||||
int state_;
|
||||
def_constr_deleter(const def_constr_deleter&);
|
||||
def_constr_deleter& operator=(const def_constr_deleter&);
|
||||
|
||||
public:
|
||||
typedef typename ::boost::move_detail::remove_extent<T>::type element_type;
|
||||
static const bool is_array = ::boost::move_detail::is_array<T>::value;
|
||||
|
||||
def_constr_deleter() : state_(5) {}
|
||||
|
||||
explicit def_constr_deleter(int s) : state_(s) {}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(element_type* p) const
|
||||
{ is_array ? delete []p : delete p; }
|
||||
|
||||
void operator()(element_type* p)
|
||||
{ ++state_; is_array ? delete []p : delete p; }
|
||||
};
|
||||
|
||||
//A deleter that can be copy constructed
|
||||
template <class T>
|
||||
class copy_constr_deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
public:
|
||||
typedef typename ::boost::move_detail::remove_extent<T>::type element_type;
|
||||
static const bool is_array = ::boost::move_detail::is_array<T>::value;
|
||||
|
||||
copy_constr_deleter() : state_(5) {}
|
||||
|
||||
template<class U>
|
||||
copy_constr_deleter(const copy_constr_deleter<U>&
|
||||
, typename boost::move_detail::enable_def_del<U, T>::type* =0)
|
||||
{ state_ = 5; }
|
||||
|
||||
explicit copy_constr_deleter(int s) : state_(s) {}
|
||||
|
||||
template <class U>
|
||||
typename boost::move_detail::enable_def_del<U, T, copy_constr_deleter&>::type
|
||||
operator=(const copy_constr_deleter<U> &d)
|
||||
{
|
||||
state_ = d.state();
|
||||
return *this;
|
||||
}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(element_type* p) const
|
||||
{ is_array ? delete []p : delete p; }
|
||||
|
||||
void operator()(element_type* p)
|
||||
{ ++state_; is_array ? delete []p : delete p; }
|
||||
};
|
||||
|
||||
//A deleter that can be only move constructed
|
||||
template <class T>
|
||||
class move_constr_deleter
|
||||
{
|
||||
int state_;
|
||||
|
||||
BOOST_MOVABLE_BUT_NOT_COPYABLE(move_constr_deleter)
|
||||
|
||||
public:
|
||||
typedef typename ::boost::move_detail::remove_extent<T>::type element_type;
|
||||
static const bool is_array = ::boost::move_detail::is_array<T>::value;
|
||||
|
||||
move_constr_deleter() : state_(5) {}
|
||||
|
||||
move_constr_deleter(BOOST_RV_REF(move_constr_deleter) r)
|
||||
: state_(r.state_)
|
||||
{ r.state_ = 0; }
|
||||
|
||||
explicit move_constr_deleter(int s) : state_(s) {}
|
||||
|
||||
template <class U>
|
||||
move_constr_deleter(BOOST_RV_REF(move_constr_deleter<U>) d
|
||||
, typename boost::move_detail::enable_def_del<U, T>::type* =0)
|
||||
: state_(d.state())
|
||||
{ d.set_state(0); }
|
||||
|
||||
move_constr_deleter& operator=(BOOST_RV_REF(move_constr_deleter) r)
|
||||
{
|
||||
state_ = r.state_;
|
||||
r.state_ = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
typename boost::move_detail::enable_def_del<U, T, move_constr_deleter&>::type
|
||||
operator=(BOOST_RV_REF(move_constr_deleter<U>) d)
|
||||
{
|
||||
state_ = d.state();
|
||||
d.set_state(0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
int state() const {return state_;}
|
||||
|
||||
void set_state(int s) {state_ = s;}
|
||||
|
||||
void operator()(element_type* p) const
|
||||
{ is_array ? delete []p : delete p; }
|
||||
|
||||
void operator()(element_type* p)
|
||||
{ ++state_; is_array ? delete []p : delete p; }
|
||||
|
||||
friend bool operator==(const move_constr_deleter& x, const move_constr_deleter& y)
|
||||
{return x.state_ == y.state_;}
|
||||
};
|
||||
|
||||
//A base class containing state with a static instance counter
|
||||
struct A
|
||||
{
|
||||
int state_;
|
||||
static int count;
|
||||
|
||||
A() : state_(999) {++count;}
|
||||
explicit A(int i) : state_(i) {++count;}
|
||||
A(const A& a) : state_(a.state_) {++count;}
|
||||
A& operator=(const A& a) { state_ = a.state_; return *this; }
|
||||
void set(int i) {state_ = i;}
|
||||
virtual ~A() {--count;}
|
||||
friend bool operator==(const A& x, const A& y) { return x.state_ == y.state_; }
|
||||
};
|
||||
|
||||
int A::count = 0;
|
||||
|
||||
//A class derived from A with a static instance counter
|
||||
struct B
|
||||
: public A
|
||||
{
|
||||
static int count;
|
||||
B() {++count;}
|
||||
B(const B&) {++count;}
|
||||
virtual ~B() {--count;}
|
||||
};
|
||||
|
||||
int B::count = 0;
|
||||
|
||||
void reset_counters();
|
||||
|
||||
BOOST_STATIC_ASSERT((::boost::move_detail::is_convertible<B, A>::value));
|
||||
|
||||
//Incomplete Type function declarations
|
||||
struct I;
|
||||
void check(int i);
|
||||
I* get();
|
||||
I* get_array(int i);
|
||||
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
|
||||
template <class T, class D = ::boost::movelib::default_delete<T> >
|
||||
struct J
|
||||
{
|
||||
typedef boost::movelib::unique_ptr<T, D> unique_ptr_type;
|
||||
typedef typename unique_ptr_type::element_type element_type;
|
||||
boost::movelib::unique_ptr<T, D> a_;
|
||||
J() {}
|
||||
explicit J(element_type*a) : a_(a) {}
|
||||
~J();
|
||||
|
||||
element_type* get() const {return a_.get();}
|
||||
D& get_deleter() {return a_.get_deleter();}
|
||||
};
|
||||
|
||||
#endif //BOOST_MOVE_UNIQUE_PTR_TEST_UTILS_BEG_HPP
|
||||
@@ -0,0 +1,46 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef BOOST_MOVE_UNIQUE_PTR_TEST_UTILS_END_HPP
|
||||
#define BOOST_MOVE_UNIQUE_PTR_TEST_UTILS_END_HPP
|
||||
|
||||
#ifndef BOOST_MOVE_UNIQUE_PTR_TEST_UTILS_BEG_HPP
|
||||
#error "unique_ptr_test_utils_beg.hpp MUST be included before this header"
|
||||
#endif
|
||||
|
||||
//Define the incomplete I type and out of line functions
|
||||
|
||||
struct I
|
||||
{
|
||||
static int count;
|
||||
I() {++count;}
|
||||
I(const A&) {++count;}
|
||||
~I() {--count;}
|
||||
};
|
||||
|
||||
int I::count = 0;
|
||||
|
||||
I* get() {return new I;}
|
||||
I* get_array(int i) {return new I[i];}
|
||||
|
||||
void check(int i)
|
||||
{
|
||||
BOOST_TEST(I::count == i);
|
||||
}
|
||||
|
||||
template <class T, class D>
|
||||
J<T, D>::~J() {}
|
||||
|
||||
void reset_counters()
|
||||
{ A::count = 0; B::count = 0; I::count = 0; }
|
||||
|
||||
#endif //BOOST_MOVE_UNIQUE_PTR_TEST_UTILS_END_HPP
|
||||
@@ -0,0 +1,164 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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 <boost/move/utility_core.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//
|
||||
// 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_pointer_type
|
||||
////////////////////////////////
|
||||
namespace unique_ptr_pointer_type {
|
||||
|
||||
struct Deleter
|
||||
{
|
||||
struct pointer {};
|
||||
};
|
||||
|
||||
// Test unique_ptr::pointer type
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<int> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::pointer, int*>::value));
|
||||
}
|
||||
{
|
||||
typedef bml::unique_ptr<int, Deleter> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::pointer, Deleter::pointer>::value));
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<int[]> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::pointer, int*>::value));
|
||||
}
|
||||
{
|
||||
typedef bml::unique_ptr<int[], Deleter> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::pointer, Deleter::pointer>::value));
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<int[5]> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::pointer, int*>::value));
|
||||
}
|
||||
{
|
||||
typedef bml::unique_ptr<int[5], Deleter> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::pointer, Deleter::pointer>::value));
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_pointer_type {
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_deleter_type
|
||||
////////////////////////////////
|
||||
namespace unique_ptr_deleter_type {
|
||||
|
||||
struct Deleter
|
||||
{};
|
||||
|
||||
// Test unique_ptr::deleter type
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<int> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::deleter_type, bml::default_delete<int> >::value));
|
||||
}
|
||||
{
|
||||
typedef bml::unique_ptr<int, Deleter> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::deleter_type, Deleter >::value));
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<int[]> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::deleter_type, bml::default_delete<int[]> >::value));
|
||||
}
|
||||
{
|
||||
typedef bml::unique_ptr<int[], Deleter> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::deleter_type, Deleter >::value));
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<int[2]> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::deleter_type, bml::default_delete<int[2]> >::value));
|
||||
}
|
||||
{
|
||||
typedef bml::unique_ptr<int[2], Deleter> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::deleter_type, Deleter >::value));
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_deleter_type {
|
||||
|
||||
////////////////////////////////
|
||||
// unique_ptr_element_type
|
||||
////////////////////////////////
|
||||
namespace unique_ptr_element_type {
|
||||
|
||||
// Test unique_ptr::deleter type
|
||||
void test()
|
||||
{
|
||||
//Single unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<const int> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::element_type, const int>::value));
|
||||
}
|
||||
//Unbounded array unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<const int[]> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::element_type, const int>::value));
|
||||
}
|
||||
//Bounded array unique_ptr
|
||||
{
|
||||
typedef bml::unique_ptr<const int[2]> P;
|
||||
BOOST_STATIC_ASSERT((boost::move_detail::is_same<P::element_type, const int>::value));
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace unique_ptr_element_type {
|
||||
|
||||
////////////////////////////////
|
||||
// main
|
||||
////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
//General
|
||||
unique_ptr_pointer_type::test();
|
||||
unique_ptr_deleter_type::test();
|
||||
unique_ptr_element_type::test();
|
||||
|
||||
//Test results
|
||||
return boost::report_errors();
|
||||
|
||||
}
|
||||
|
||||
#include "unique_ptr_test_utils_end.hpp"
|
||||
Reference in new issue
Block a user