From f27b780724e2af140dae213968ab1c5c9f8f310b Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Tue, 4 Feb 2014 13:16:06 -0800 Subject: [PATCH] Add unit test for allocate_shared construct case --- test/Jamfile.v2 | 1 + test/allocate_shared_array_construct_test.cpp | 160 ++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 test/allocate_shared_array_construct_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 09263d3..8a74194 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -148,6 +148,7 @@ import testing ; [ run allocate_shared_array_esft_test.cpp ] [ run allocate_shared_array_noinit_test.cpp ] [ run allocate_shared_array_value_test.cpp ] + [ run allocate_shared_array_construct_test.cpp ] [ run make_unique_test.cpp ] [ run make_unique_args_test.cpp ] diff --git a/test/allocate_shared_array_construct_test.cpp b/test/allocate_shared_array_construct_test.cpp new file mode 100644 index 0000000..623aa65 --- /dev/null +++ b/test/allocate_shared_array_construct_test.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2012-2014 Glen Joseph Fernandes + * glenfe at live dot com + * + * Distributed under the Boost Software License, + * Version 1.0. (See accompanying file LICENSE_1_0.txt + * or copy at http://boost.org/LICENSE_1_0.txt) + */ +#include +#if !defined(BOOST_NO_CXX11_ALLOCATOR) +#include + +template +class creator { +public: + typedef T value_type; + + creator() { + } + + template + creator(const creator&) { + } + + T* allocate(std::size_t size) { + void* p1 = ::operator new(size * sizeof(T)); + return static_cast(p1); + } + + void deallocate(T* memory, std::size_t) { + void* p1 = memory; + ::operator delete(p1); + } + + template + void construct(U* memory, Args&&... args) { + void* p1 = memory; + ::new(p1) U(std::forward(args)...); + } + + template + void destroy(U* memory) { + memory->~U(); + } +}; + +class type { + friend class creator; + +public: + static unsigned int instances; + static type object; + +protected: + explicit type() { + instances++; + } + + type(const type&) { + instances++; + } + + ~type() { + instances--; + } +}; + +unsigned int type::instances; +type type::object; + +int main() { + BOOST_TEST(type::instances == 1); + { + boost::shared_ptr a1 = boost::allocate_shared(creator(), 3); + BOOST_TEST(a1.use_count() == 1); + BOOST_TEST(a1.get() != 0); + BOOST_TEST(type::instances == 4); + a1.reset(); + BOOST_TEST(type::instances == 1); + } + + BOOST_TEST(type::instances == 1); + { + boost::shared_ptr a1 = boost::allocate_shared(creator()); + BOOST_TEST(a1.use_count() == 1); + BOOST_TEST(a1.get() != 0); + BOOST_TEST(type::instances == 4); + a1.reset(); + BOOST_TEST(type::instances == 1); + } + + BOOST_TEST(type::instances == 1); + { + boost::shared_ptr a1 = boost::allocate_shared(creator(), 2); + BOOST_TEST(a1.get() != 0); + BOOST_TEST(a1.use_count() == 1); + BOOST_TEST(type::instances == 5); + a1.reset(); + BOOST_TEST(type::instances == 1); + } + + BOOST_TEST(type::instances == 1); + { + boost::shared_ptr a1 = boost::allocate_shared(creator()); + BOOST_TEST(a1.get() != 0); + BOOST_TEST(a1.use_count() == 1); + BOOST_TEST(type::instances == 5); + a1.reset(); + BOOST_TEST(type::instances == 1); + } + + BOOST_TEST(type::instances == 1); + { + boost::shared_ptr a1 = boost::allocate_shared(creator(), 3); + BOOST_TEST(a1.get() != 0); + BOOST_TEST(a1.use_count() == 1); + BOOST_TEST(type::instances == 4); + a1.reset(); + BOOST_TEST(type::instances == 1); + } + + BOOST_TEST(type::instances == 1); + { + boost::shared_ptr a1 = boost::allocate_shared(creator()); + BOOST_TEST(a1.get() != 0); + BOOST_TEST(a1.use_count() == 1); + BOOST_TEST(type::instances == 4); + a1.reset(); + BOOST_TEST(type::instances == 1); + } + + BOOST_TEST(type::instances == 1); + { + boost::shared_ptr a1 = boost::allocate_shared(creator(), 2); + BOOST_TEST(a1.get() != 0); + BOOST_TEST(a1.use_count() == 1); + BOOST_TEST(type::instances == 5); + a1.reset(); + BOOST_TEST(type::instances == 1); + } + + BOOST_TEST(type::instances == 1); + { + boost::shared_ptr a1 = boost::allocate_shared(creator()); + BOOST_TEST(a1.get() != 0); + BOOST_TEST(a1.use_count() == 1); + BOOST_TEST(type::instances == 5); + a1.reset(); + BOOST_TEST(type::instances == 1); + } + + return boost::report_errors(); +} +#else + +int main() { + return 0; +} + +#endif