From 897646ed3eb02636f5372c4d3342d055c6abc764 Mon Sep 17 00:00:00 2001 From: Joe Herdman Date: Wed, 18 Sep 2019 20:44:41 -0400 Subject: [PATCH] Adds basic coverage-test for case where 'malloc' returns zero. --- test/test_pool_alloc.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/test_pool_alloc.cpp b/test/test_pool_alloc.cpp index d504c295..14754ead 100644 --- a/test/test_pool_alloc.cpp +++ b/test/test_pool_alloc.cpp @@ -153,6 +153,23 @@ std::set TrackAlloc::allocated_blocks; typedef TrackAlloc track_alloc; +// This is a simple UserAllocator to allow coverage-testing of the codepath +// where memory allocation fails. +struct always_fails_allocation_alloc +{ + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + static char * malloc(const size_type /*bytes*/) + { + return 0; + } + + static void free(char * const /*block*/) + { + } +}; + void test() { { @@ -229,6 +246,16 @@ void test() } } #endif + + { + // Test the case where memory allocation intentionally fails + boost::object_pool pool; + BOOST_TEST( pool.construct() == 0 ); + BOOST_TEST( pool.construct(1,2) == 0 ); +#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS) + BOOST_TEST( pool.construct(1,2,3,4) == 0 ); +#endif + } } void test_alloc()