Sat, 16 Apr 2022 09:10:10 +0200
test uncovered branch in cxReallocate()
test/test_allocator.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/test/test_allocator.cpp Sat Apr 16 08:59:51 2022 +0200 1.2 +++ b/test/test_allocator.cpp Sat Apr 16 09:10:10 2022 +0200 1.3 @@ -72,3 +72,21 @@ 1.4 cxFree(cxDefaultAllocator, test); 1.5 ); 1.6 } 1.7 + 1.8 +TEST(Allocator, FailingReallocate) { 1.9 + // Mock an allocator that always returns nullptr on realloc 1.10 + cx_allocator_class mock_cl; 1.11 + mock_cl.realloc = [](void* p, void* d, size_t n) -> void* { return nullptr; }; 1.12 + cx_allocator_s mock{&mock_cl, nullptr}; 1.13 + 1.14 + void *test = calloc(8, 1); 1.15 + memcpy(test, "Test", 5); 1.16 + void *original = test; 1.17 + int ret = cxReallocate(&mock, &test, 16); 1.18 + // non-zero return code because of the failure 1.19 + EXPECT_NE(ret, 0); 1.20 + // the test pointer was not changed and still points to the same memory 1.21 + EXPECT_EQ(test, original); 1.22 + EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); 1.23 + free(test); 1.24 +}