Fri, 29 Dec 2023 17:17:43 +0100
revert the insanely stupid idea to automatically run the test program on default build
universe@571 | 1 | /* |
universe@571 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@571 | 3 | * |
universe@571 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@571 | 5 | * |
universe@571 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@571 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@571 | 8 | * |
universe@571 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@571 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@571 | 11 | * |
universe@571 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@571 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@571 | 14 | * documentation and/or other materials provided with the distribution. |
universe@571 | 15 | * |
universe@571 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@571 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@571 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@571 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@571 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@571 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@571 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@571 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@571 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@571 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@571 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@571 | 27 | */ |
universe@571 | 28 | |
universe@727 | 29 | #include "cx/mempool.h" |
universe@571 | 30 | #include "util_allocator.h" |
universe@571 | 31 | #include <gtest/gtest.h> |
universe@571 | 32 | |
universe@727 | 33 | TEST(Mempool, Create) { |
universe@727 | 34 | auto pool = cxBasicMempoolCreate(16); |
universe@727 | 35 | ASSERT_EQ(pool->auto_destr, nullptr); |
universe@571 | 36 | ASSERT_NE(pool->allocator, nullptr); |
universe@571 | 37 | ASSERT_NE(pool->allocator->cl, nullptr); |
universe@571 | 38 | EXPECT_EQ(pool->allocator->data, pool); |
universe@571 | 39 | EXPECT_NE(pool->allocator->cl->malloc, nullptr); |
universe@571 | 40 | EXPECT_NE(pool->allocator->cl->calloc, nullptr); |
universe@571 | 41 | EXPECT_NE(pool->allocator->cl->realloc, nullptr); |
universe@571 | 42 | EXPECT_NE(pool->allocator->cl->free, nullptr); |
universe@727 | 43 | EXPECT_EQ(pool->capacity, 16); |
universe@727 | 44 | EXPECT_EQ(pool->size, 0); |
universe@727 | 45 | EXPECT_NE(pool->data, nullptr); |
universe@727 | 46 | cxMempoolDestroy(pool); |
universe@571 | 47 | } |
universe@571 | 48 | |
universe@727 | 49 | TEST(Mempool, malloc) { |
universe@727 | 50 | auto pool = cxBasicMempoolCreate(4); |
universe@571 | 51 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
universe@571 | 52 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
universe@727 | 53 | EXPECT_EQ(pool->size, 2); |
universe@727 | 54 | EXPECT_EQ(pool->capacity, 4); |
universe@571 | 55 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
universe@571 | 56 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
universe@727 | 57 | EXPECT_EQ(pool->size, 4); |
universe@727 | 58 | EXPECT_EQ(pool->capacity, 4); |
universe@571 | 59 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
universe@571 | 60 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
universe@727 | 61 | EXPECT_EQ(pool->size, 6); |
universe@727 | 62 | EXPECT_GE(pool->capacity, 6); |
universe@727 | 63 | cxMempoolDestroy(pool); |
universe@571 | 64 | } |
universe@571 | 65 | |
universe@727 | 66 | TEST(Mempool, calloc) { |
universe@727 | 67 | auto pool = cxBasicMempoolCreate(4); |
universe@571 | 68 | |
universe@571 | 69 | auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int)); |
universe@571 | 70 | ASSERT_NE(test, nullptr); |
universe@571 | 71 | EXPECT_EQ(test[0], 0); |
universe@571 | 72 | EXPECT_EQ(test[1], 0); |
universe@727 | 73 | cxMempoolDestroy(pool); |
universe@571 | 74 | } |
universe@571 | 75 | |
universe@727 | 76 | static unsigned test_destructor_called; |
universe@571 | 77 | |
universe@571 | 78 | static void test_destructor([[maybe_unused]] void *mem) { |
universe@571 | 79 | test_destructor_called++; |
universe@571 | 80 | } |
universe@571 | 81 | |
universe@727 | 82 | TEST(Mempool, realloc) { |
universe@727 | 83 | auto pool = cxMempoolCreate(4, test_destructor); |
universe@727 | 84 | ASSERT_EQ(pool->auto_destr, test_destructor); |
universe@571 | 85 | auto data = cxMalloc(pool->allocator, sizeof(int)); |
universe@571 | 86 | *((int *) data) = 13; |
universe@571 | 87 | |
universe@571 | 88 | void *rdata = data; |
universe@571 | 89 | unsigned n = 1; |
universe@571 | 90 | while (rdata == data) { |
universe@571 | 91 | n <<= 1; |
universe@571 | 92 | ASSERT_LT(n, 65536); // eventually the memory should be moved elsewhere |
universe@571 | 93 | rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); |
universe@571 | 94 | } |
universe@571 | 95 | |
universe@571 | 96 | EXPECT_EQ(*((int *) rdata), 13); |
universe@571 | 97 | // test if destructor is still intact |
universe@571 | 98 | test_destructor_called = 0; |
universe@571 | 99 | cxFree(pool->allocator, rdata); |
universe@571 | 100 | EXPECT_EQ(test_destructor_called, 1); |
universe@727 | 101 | cxMempoolDestroy(pool); |
universe@571 | 102 | } |
universe@571 | 103 | |
universe@571 | 104 | |
universe@727 | 105 | TEST(Mempool, free) { |
universe@727 | 106 | auto pool = cxBasicMempoolCreate(4); |
universe@571 | 107 | |
universe@571 | 108 | void *mem1; |
universe@571 | 109 | void *mem2; |
universe@571 | 110 | |
universe@571 | 111 | mem1 = cxMalloc(pool->allocator, 16); |
universe@571 | 112 | cxFree(pool->allocator, mem1); |
universe@727 | 113 | EXPECT_EQ(pool->size, 0); |
universe@571 | 114 | |
universe@571 | 115 | cxMalloc(pool->allocator, 16); |
universe@571 | 116 | cxMalloc(pool->allocator, 16); |
universe@571 | 117 | mem1 = cxMalloc(pool->allocator, 16); |
universe@571 | 118 | cxMalloc(pool->allocator, 16); |
universe@571 | 119 | mem2 = cxMalloc(pool->allocator, 16); |
universe@571 | 120 | |
universe@727 | 121 | EXPECT_EQ(pool->size, 5); |
universe@571 | 122 | cxFree(pool->allocator, mem1); |
universe@727 | 123 | EXPECT_EQ(pool->size, 4); |
universe@571 | 124 | cxFree(pool->allocator, mem2); |
universe@727 | 125 | EXPECT_EQ(pool->size, 3); |
universe@727 | 126 | cxMempoolDestroy(pool); |
universe@727 | 127 | } |
universe@727 | 128 | |
universe@727 | 129 | TEST(Mempool, Destroy) { |
universe@727 | 130 | auto pool = cxBasicMempoolCreate(4); |
universe@727 | 131 | auto data = cxMalloc(pool->allocator, sizeof(int)); |
universe@727 | 132 | *((int *) data) = 13; |
universe@727 | 133 | cxMempoolSetDestructor(data, test_destructor); |
universe@727 | 134 | EXPECT_EQ(*((int *) data), 13); |
universe@727 | 135 | test_destructor_called = 0; |
universe@727 | 136 | cxFree(pool->allocator, data); |
universe@727 | 137 | EXPECT_EQ(test_destructor_called, 1); |
universe@727 | 138 | data = cxMalloc(pool->allocator, sizeof(int)); |
universe@727 | 139 | cxMempoolSetDestructor(data, test_destructor); |
universe@727 | 140 | cxMempoolDestroy(pool); |
universe@727 | 141 | EXPECT_EQ(test_destructor_called, 2); |
universe@727 | 142 | } |
universe@727 | 143 | |
universe@727 | 144 | TEST(Mempool, Register) { |
universe@727 | 145 | auto pool = cxBasicMempoolCreate(4); |
universe@727 | 146 | auto data = cxMalloc(pool->allocator, sizeof(int)); |
universe@727 | 147 | test_destructor_called = 0; |
universe@727 | 148 | cxMempoolSetDestructor(data, test_destructor); |
universe@727 | 149 | int donotfree = 0; |
universe@727 | 150 | cxMempoolRegister(pool, &donotfree, test_destructor); |
universe@727 | 151 | cxMempoolDestroy(pool); |
universe@727 | 152 | EXPECT_EQ(test_destructor_called, 2); |
universe@727 | 153 | } |