Skip to content

Commit 311661f

Browse files
author
Joe Herdman
committed
Adds basic coverage-test for case where 'malloc' returns zero.
1 parent 7f9a0ba commit 311661f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/test_pool_alloc.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,23 @@ std::set<char *> TrackAlloc<UserAllocator>::allocated_blocks;
153153

154154
typedef TrackAlloc<boost::default_user_allocator_new_delete> track_alloc;
155155

156+
// This is a simple UserAllocator to allow coverage-testing of the codepath
157+
// where memory allocation fails.
158+
struct always_fails_allocation_alloc
159+
{
160+
typedef std::size_t size_type;
161+
typedef std::ptrdiff_t difference_type;
162+
163+
static char * malloc(const size_type /*bytes*/)
164+
{
165+
return 0;
166+
}
167+
168+
static void free(char * const /*block*/)
169+
{
170+
}
171+
};
172+
156173
void test()
157174
{
158175
{
@@ -229,6 +246,16 @@ void test()
229246
}
230247
}
231248
#endif
249+
250+
{
251+
// Test the case where memory allocation intentionally fails
252+
boost::object_pool<tester, always_fails_allocation_alloc> pool;
253+
BOOST_TEST( pool.construct() == 0 );
254+
BOOST_TEST( pool.construct(1,2) == 0 );
255+
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
256+
BOOST_TEST( pool.construct(1,2,3,4) == 0 );
257+
#endif
258+
}
232259
}
233260

234261
void test_alloc()

0 commit comments

Comments
 (0)