From bb2a453ff6c9dc0dbf1a3bfa1406e8f90077cded Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Fri, 30 Aug 2019 08:16:11 -0400 Subject: [PATCH] Add additional test cases --- test/Jamfile | 2 + test/allocate_unique_aggregate_test.cpp | 84 +++++++++++++++++++ test/allocate_unique_value_test.cpp | 103 ++++++++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 test/allocate_unique_aggregate_test.cpp create mode 100644 test/allocate_unique_value_test.cpp diff --git a/test/Jamfile b/test/Jamfile index 61b375e..4e9e62e 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -320,6 +320,7 @@ run shared_from_test.cpp ; run weak_from_test.cpp ; run weak_from_test2.cpp ; +run allocate_unique_aggregate_test.cpp ; run allocate_unique_args_test.cpp ; run allocate_unique_array_construct_test.cpp ; run allocate_unique_array_noinit_test.cpp ; @@ -331,3 +332,4 @@ run allocate_unique_construct_test.cpp ; run allocate_unique_noinit_test.cpp ; run allocate_unique_test.cpp ; run allocate_unique_throws_test.cpp ; +run allocate_unique_value_test.cpp ; diff --git a/test/allocate_unique_aggregate_test.cpp b/test/allocate_unique_aggregate_test.cpp new file mode 100644 index 0000000..8861981 --- /dev/null +++ b/test/allocate_unique_aggregate_test.cpp @@ -0,0 +1,84 @@ +/* +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include +#if (!defined(BOOST_LIBSTDCXX_VERSION) || \ + BOOST_LIBSTDCXX_VERSION >= 46000) && \ + !defined(BOOST_NO_CXX11_SMART_PTR) && \ + !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) +#include +#include + +template +struct creator { + typedef T value_type; + typedef T* pointer; + + template + struct rebind { + typedef creator other; + }; + + creator() { } + + template + creator(const creator&) { } + + T* allocate(std::size_t size) { + return static_cast(::operator new(sizeof(T) * size)); + } + + void deallocate(T* ptr, std::size_t) { + ::operator delete(ptr); + } +}; + +template +inline bool +operator==(const creator&, const creator&) +{ + return true; +} + +template +inline bool +operator!=(const creator&, const creator&) +{ + return false; +} + +struct type { + int x; + int y; +}; + +int main() +{ + { + std::unique_ptr > > result = + boost::allocate_unique(creator(), { 1, 2 }); + BOOST_TEST(result.get() != 0); + BOOST_TEST(result->x == 1); + BOOST_TEST(result->y == 2); + } + { + std::unique_ptr > > result = + boost::allocate_unique(creator<>(), { 1, 2 }); + BOOST_TEST(result.get() != 0); + BOOST_TEST(result->x == 1); + BOOST_TEST(result->y == 2); + } + return boost::report_errors(); +} +#else +int main() +{ + return 0; +} +#endif diff --git a/test/allocate_unique_value_test.cpp b/test/allocate_unique_value_test.cpp new file mode 100644 index 0000000..3b909ff --- /dev/null +++ b/test/allocate_unique_value_test.cpp @@ -0,0 +1,103 @@ +/* +Copyright 2019 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ +#include +#if (!defined(BOOST_LIBSTDCXX_VERSION) || \ + BOOST_LIBSTDCXX_VERSION >= 46000) && \ + !defined(BOOST_NO_CXX11_SMART_PTR) +#include +#include + +template +struct creator { + typedef T value_type; + typedef T* pointer; + + template + struct rebind { + typedef creator other; + }; + + creator() { } + + template + creator(const creator&) { } + + T* allocate(std::size_t size) { + return static_cast(::operator new(sizeof(T) * size)); + } + + void deallocate(T* ptr, std::size_t) { + ::operator delete(ptr); + } +}; + +template +inline bool +operator==(const creator&, const creator&) +{ + return true; +} + +template +inline bool +operator!=(const creator&, const creator&) +{ + return false; +} + +class type { +public: + type(int x, int y) + : value_(x + y) { } + + int sum() const { + return value_; + } + +private: + int value_; +}; + +int main() +{ + { + std::unique_ptr > > result = + boost::allocate_unique(creator(), 1); + BOOST_TEST(result.get() != 0); + BOOST_TEST(*result == 1); + } + { + std::unique_ptr > > result = + boost::allocate_unique(creator<>(), 1); + BOOST_TEST(result.get() != 0); + BOOST_TEST(*result == 1); + } + { + std::unique_ptr > > result = + boost::allocate_unique(creator(), type(1, 2)); + BOOST_TEST(result.get() != 0); + BOOST_TEST(result->sum() == 3); + } + { + std::unique_ptr > > result = + boost::allocate_unique(creator<>(), type(1, 2)); + BOOST_TEST(result.get() != 0); + BOOST_TEST(result->sum() == 3); + } + return boost::report_errors(); +} +#else +int main() +{ + return 0; +} +#endif