70 void *test = malloc(16); |
70 void *test = malloc(16); |
71 EXPECT_NO_FATAL_FAILURE( |
71 EXPECT_NO_FATAL_FAILURE( |
72 cxFree(cxDefaultAllocator, test); |
72 cxFree(cxDefaultAllocator, test); |
73 ); |
73 ); |
74 } |
74 } |
|
75 |
|
76 TEST(Allocator, FailingReallocate) { |
|
77 // Mock an allocator that always returns nullptr on realloc |
|
78 cx_allocator_class mock_cl; |
|
79 mock_cl.realloc = [](void* p, void* d, size_t n) -> void* { return nullptr; }; |
|
80 cx_allocator_s mock{&mock_cl, nullptr}; |
|
81 |
|
82 void *test = calloc(8, 1); |
|
83 memcpy(test, "Test", 5); |
|
84 void *original = test; |
|
85 int ret = cxReallocate(&mock, &test, 16); |
|
86 // non-zero return code because of the failure |
|
87 EXPECT_NE(ret, 0); |
|
88 // the test pointer was not changed and still points to the same memory |
|
89 EXPECT_EQ(test, original); |
|
90 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); |
|
91 free(test); |
|
92 } |