2014-02-04 13:16:06 -08:00
|
|
|
/*
|
2015-11-09 22:35:34 -05:00
|
|
|
(c) 2012-2015 Glen Joseph Fernandes
|
|
|
|
<glenjofe -at- gmail.com>
|
|
|
|
|
|
|
|
Distributed under the Boost Software
|
|
|
|
License, Version 1.0.
|
|
|
|
http://boost.org/LICENSE_1_0.txt
|
|
|
|
*/
|
|
|
|
#include <boost/core/lightweight_test.hpp>
|
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
|
|
|
|
|
|
|
struct tag { };
|
2014-02-04 13:16:06 -08:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class creator {
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
|
|
|
creator() {
|
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
creator(const creator<U>&) {
|
|
|
|
}
|
|
|
|
T* allocate(std::size_t size) {
|
2015-11-09 22:35:34 -05:00
|
|
|
void* p = ::operator new(size * sizeof(T));
|
|
|
|
return static_cast<T*>(p);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
void deallocate(T* memory, std::size_t) {
|
2015-11-09 22:35:34 -05:00
|
|
|
void* p = memory;
|
|
|
|
::operator delete(p);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
2014-02-06 17:09:27 -08:00
|
|
|
template<typename U>
|
|
|
|
void construct(U* memory) {
|
2015-11-09 22:35:34 -05:00
|
|
|
void* p = memory;
|
|
|
|
::new(p) U(tag());
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
void destroy(U* memory) {
|
|
|
|
memory->~U();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-05 09:10:45 -08:00
|
|
|
class type {
|
2014-02-04 13:16:06 -08:00
|
|
|
public:
|
|
|
|
static unsigned int instances;
|
2015-11-09 22:35:34 -05:00
|
|
|
explicit type(tag) {
|
2014-02-04 13:16:06 -08:00
|
|
|
instances++;
|
|
|
|
}
|
2014-02-05 09:10:45 -08:00
|
|
|
type(const type&) {
|
2014-02-04 13:16:06 -08:00
|
|
|
instances++;
|
|
|
|
}
|
2014-02-05 09:10:45 -08:00
|
|
|
~type() {
|
2014-02-04 13:16:06 -08:00
|
|
|
instances--;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-09 22:35:34 -05:00
|
|
|
unsigned int type::instances = 0;
|
2014-02-04 13:16:06 -08:00
|
|
|
|
2015-11-09 22:35:34 -05:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
2014-02-04 13:16:06 -08:00
|
|
|
{
|
2014-02-05 09:10:45 -08:00
|
|
|
boost::shared_ptr<type[]> a1 = boost::allocate_shared<type[]>(creator<void>(), 3);
|
2014-02-04 13:16:06 -08:00
|
|
|
BOOST_TEST(a1.use_count() == 1);
|
|
|
|
BOOST_TEST(a1.get() != 0);
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 3);
|
2014-02-04 13:16:06 -08:00
|
|
|
a1.reset();
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 0);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
{
|
2014-02-05 09:10:45 -08:00
|
|
|
boost::shared_ptr<type[3]> a1 = boost::allocate_shared<type[3]>(creator<void>());
|
2014-02-04 13:16:06 -08:00
|
|
|
BOOST_TEST(a1.use_count() == 1);
|
|
|
|
BOOST_TEST(a1.get() != 0);
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 3);
|
2014-02-04 13:16:06 -08:00
|
|
|
a1.reset();
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 0);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
{
|
2014-02-05 09:10:45 -08:00
|
|
|
boost::shared_ptr<type[][2]> a1 = boost::allocate_shared<type[][2]>(creator<void>(), 2);
|
2014-02-04 13:16:06 -08:00
|
|
|
BOOST_TEST(a1.get() != 0);
|
|
|
|
BOOST_TEST(a1.use_count() == 1);
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 4);
|
2014-02-04 13:16:06 -08:00
|
|
|
a1.reset();
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 0);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
{
|
2014-02-05 09:10:45 -08:00
|
|
|
boost::shared_ptr<type[2][2]> a1 = boost::allocate_shared<type[2][2]>(creator<void>());
|
2014-02-04 13:16:06 -08:00
|
|
|
BOOST_TEST(a1.get() != 0);
|
|
|
|
BOOST_TEST(a1.use_count() == 1);
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 4);
|
2014-02-04 13:16:06 -08:00
|
|
|
a1.reset();
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 0);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
{
|
2014-02-05 09:10:45 -08:00
|
|
|
boost::shared_ptr<const type[]> a1 = boost::allocate_shared<const type[]>(creator<void>(), 3);
|
2014-02-04 13:16:06 -08:00
|
|
|
BOOST_TEST(a1.get() != 0);
|
|
|
|
BOOST_TEST(a1.use_count() == 1);
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 3);
|
2014-02-04 13:16:06 -08:00
|
|
|
a1.reset();
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 0);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
{
|
2014-02-05 09:10:45 -08:00
|
|
|
boost::shared_ptr<const type[3]> a1 = boost::allocate_shared<const type[3]>(creator<void>());
|
2014-02-04 13:16:06 -08:00
|
|
|
BOOST_TEST(a1.get() != 0);
|
|
|
|
BOOST_TEST(a1.use_count() == 1);
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 3);
|
2014-02-04 13:16:06 -08:00
|
|
|
a1.reset();
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 0);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
{
|
2014-02-05 09:10:45 -08:00
|
|
|
boost::shared_ptr<const type[][2]> a1 = boost::allocate_shared<const type[][2]>(creator<void>(), 2);
|
2014-02-04 13:16:06 -08:00
|
|
|
BOOST_TEST(a1.get() != 0);
|
|
|
|
BOOST_TEST(a1.use_count() == 1);
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 4);
|
2014-02-04 13:16:06 -08:00
|
|
|
a1.reset();
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 0);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
|
|
|
{
|
2014-02-05 09:10:45 -08:00
|
|
|
boost::shared_ptr<const type[2][2]> a1 = boost::allocate_shared<const type[2][2]>(creator<void>());
|
2014-02-04 13:16:06 -08:00
|
|
|
BOOST_TEST(a1.get() != 0);
|
|
|
|
BOOST_TEST(a1.use_count() == 1);
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 4);
|
2014-02-04 13:16:06 -08:00
|
|
|
a1.reset();
|
2015-11-09 22:35:34 -05:00
|
|
|
BOOST_TEST(type::instances == 0);
|
2014-02-04 13:16:06 -08:00
|
|
|
}
|
2015-11-09 22:35:34 -05:00
|
|
|
#endif
|
2014-02-04 13:16:06 -08:00
|
|
|
return boost::report_errors();
|
|
|
|
}
|