tests/util_allocator.cpp

changeset 654
c9d008861178
parent 653
e081643aae2a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/util_allocator.cpp	Wed Feb 08 20:26:26 2023 +0100
@@ -0,0 +1,167 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "util_allocator.h"
+
+void *cx_malloc_testing(void *d, size_t n) {
+    auto data = reinterpret_cast<CxTestingAllocator *>(d);
+    void *ptr = malloc(n);
+    data->alloc_total++;
+    if (ptr == nullptr) {
+        data->alloc_failed++;
+    } else {
+        data->tracked.insert(ptr);
+    }
+    return ptr;
+}
+
+void *cx_realloc_testing(void *d, void *mem, size_t n) {
+    auto data = reinterpret_cast<CxTestingAllocator *>(d);
+    void *ptr = realloc(mem, n);
+    if (ptr == mem) {
+        return ptr;
+    } else {
+        data->alloc_total++;
+        if (ptr == nullptr) {
+            data->alloc_failed++;
+        } else {
+            data->free_total++;
+            if (data->tracked.erase(mem) == 0) {
+                data->free_failed++;
+            }
+            data->tracked.insert(ptr);
+        }
+        return ptr;
+    }
+}
+
+void *cx_calloc_testing(void *d, size_t nelem, size_t n) {
+    auto data = reinterpret_cast<CxTestingAllocator *>(d);
+    void *ptr = calloc(nelem, n);
+    data->alloc_total++;
+    if (ptr == nullptr) {
+        data->alloc_failed++;
+    } else {
+        data->tracked.insert(ptr);
+    }
+    return ptr;
+}
+
+void cx_free_testing(void *d, void *mem) {
+    auto data = reinterpret_cast<CxTestingAllocator *>(d);
+    data->free_total++;
+    if (data->tracked.erase(mem) == 0) {
+        data->free_failed++;
+        // do not even attempt to free mem, because it is likely to segfault
+    } else {
+        free(mem);
+    }
+}
+
+cx_allocator_class cx_testing_allocator_class = {
+        cx_malloc_testing,
+        cx_realloc_testing,
+        cx_calloc_testing,
+        cx_free_testing
+};
+
+CxTestingAllocator::CxTestingAllocator() : CxAllocator() {
+    cl = &cx_testing_allocator_class;
+    data = this;
+}
+
+bool CxTestingAllocator::used() const {
+    return alloc_total > 0;
+}
+
+bool CxTestingAllocator::verify() const {
+    return tracked.empty() && alloc_failed == 0 && free_failed == 0 && alloc_total == free_total;
+}
+
+// SELF-TEST
+
+#include <gtest/gtest.h>
+
+TEST(TestingAllocator, ExpectFree) {
+    CxTestingAllocator allocator;
+
+    ASSERT_TRUE(allocator.verify());
+    EXPECT_FALSE(allocator.used());
+    auto ptr = cxMalloc(&allocator, 16);
+    EXPECT_TRUE(allocator.used());
+    ASSERT_NE(ptr, nullptr);
+    EXPECT_FALSE(allocator.verify());
+
+    cxFree(&allocator, ptr);
+    EXPECT_TRUE(allocator.verify());
+}
+
+TEST(TestingAllocator, DetectDoubleFree) {
+    CxTestingAllocator allocator;
+
+    ASSERT_TRUE(allocator.verify());
+    auto ptr = cxMalloc(&allocator, 16);
+    ASSERT_NE(ptr, nullptr);
+
+    cxFree(&allocator, ptr);
+    EXPECT_TRUE(allocator.verify());
+    ASSERT_NO_FATAL_FAILURE(cxFree(&allocator, ptr));
+    EXPECT_FALSE(allocator.verify());
+}
+
+TEST(TestingAllocator, FreeUntracked) {
+    CxTestingAllocator allocator;
+
+    auto ptr = malloc(16);
+    ASSERT_TRUE(allocator.verify());
+    ASSERT_NO_FATAL_FAILURE(cxFree(&allocator, ptr));
+    EXPECT_FALSE(allocator.verify());
+    ASSERT_NO_FATAL_FAILURE(free(ptr));
+}
+
+TEST(TestingAllocator, FullLifecycleWithRealloc) {
+    CxTestingAllocator allocator;
+    ASSERT_TRUE(allocator.verify());
+    auto ptr = cxMalloc(&allocator, 16);
+    ASSERT_NE(ptr, nullptr);
+    EXPECT_EQ(allocator.tracked.size(), 1);
+    ptr = cxRealloc(&allocator, ptr, 256);
+    ASSERT_NE(ptr, nullptr);
+    EXPECT_EQ(allocator.tracked.size(), 1);
+    cxFree(&allocator, ptr);
+    EXPECT_TRUE(allocator.verify());
+}
+
+TEST(TestingAllocator, CallocInitializes) {
+    CxTestingAllocator allocator;
+    const char zeros[16] = {0};
+    auto ptr = cxCalloc(&allocator, 16, 1);
+    EXPECT_EQ(memcmp(ptr, zeros, 16), 0);
+    cxFree(&allocator, ptr);
+    EXPECT_TRUE(allocator.verify());
+}

mercurial