test uncovered branch in cxReallocate()

Sat, 16 Apr 2022 09:10:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 Apr 2022 09:10:10 +0200
changeset 514
6f9d97a53d67
parent 513
b66532b5d8db
child 515
6d3909bf1609

test uncovered branch in cxReallocate()

test/test_allocator.cpp file | annotate | diff | comparison | revisions
--- a/test/test_allocator.cpp	Sat Apr 16 08:59:51 2022 +0200
+++ b/test/test_allocator.cpp	Sat Apr 16 09:10:10 2022 +0200
@@ -72,3 +72,21 @@
             cxFree(cxDefaultAllocator, test);
     );
 }
+
+TEST(Allocator, FailingReallocate) {
+    // Mock an allocator that always returns nullptr on realloc
+    cx_allocator_class mock_cl;
+    mock_cl.realloc = [](void* p, void* d, size_t n) -> void* { return nullptr; };
+    cx_allocator_s mock{&mock_cl, nullptr};
+
+    void *test = calloc(8, 1);
+    memcpy(test, "Test", 5);
+    void *original = test;
+    int ret = cxReallocate(&mock, &test, 16);
+    // non-zero return code because of the failure
+    EXPECT_NE(ret, 0);
+    // the test pointer was not changed and still points to the same memory
+    EXPECT_EQ(test, original);
+    EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
+    free(test);
+}

mercurial