Sun, 20 Nov 2022 17:48:42 +0100
fix cx_array_copy() unintentionally shrinking the array
/* * 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 "cx/basic_mempool.h" #include "util_allocator.h" #include <gtest/gtest.h> class CxBasicMempool : public ::testing::Test { protected: CxMempool *pool = nullptr; void TearDown() override { if (pool != nullptr) { cxMempoolDestroy(pool); } } }; TEST_F(CxBasicMempool, Create) { pool = cxBasicMempoolCreate(16); ASSERT_NE(pool->allocator, nullptr); ASSERT_NE(pool->cl, nullptr); EXPECT_NE(pool->cl->destroy, nullptr); ASSERT_NE(pool->allocator->cl, nullptr); EXPECT_EQ(pool->allocator->data, pool); EXPECT_NE(pool->allocator->cl->malloc, nullptr); EXPECT_NE(pool->allocator->cl->calloc, nullptr); EXPECT_NE(pool->allocator->cl->realloc, nullptr); EXPECT_NE(pool->allocator->cl->free, nullptr); auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool); EXPECT_EQ(basic_pool->size, 16); EXPECT_EQ(basic_pool->ndata, 0); EXPECT_NE(basic_pool->data, nullptr); } TEST_F(CxBasicMempool, malloc) { pool = cxBasicMempoolCreate(4); auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_EQ(basic_pool->ndata, 2); EXPECT_EQ(basic_pool->size, 4); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_EQ(basic_pool->ndata, 4); EXPECT_EQ(basic_pool->size, 4); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_EQ(basic_pool->ndata, 6); EXPECT_GE(basic_pool->size, 6); } TEST_F(CxBasicMempool, calloc) { pool = cxBasicMempoolCreate(4); auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int)); ASSERT_NE(test, nullptr); EXPECT_EQ(test[0], 0); EXPECT_EQ(test[1], 0); } static unsigned test_destructor_called = 0; static void test_destructor([[maybe_unused]] void *mem) { test_destructor_called++; } TEST_F(CxBasicMempool, destructor) { pool = cxBasicMempoolCreate(4); auto data = cxMalloc(pool->allocator, sizeof(int)); *((int *) data) = 13; cxMempoolSetDestructor(pool, data, test_destructor); EXPECT_EQ(*((int *) data), 13); test_destructor_called = 0; cxFree(pool->allocator, data); EXPECT_EQ(test_destructor_called, 1); data = cxMalloc(pool->allocator, sizeof(int)); cxMempoolSetDestructor(pool, data, test_destructor); cxMempoolDestroy(pool); pool = nullptr; EXPECT_EQ(test_destructor_called, 2); } TEST_F(CxBasicMempool, realloc) { pool = cxBasicMempoolCreate(4); auto data = cxMalloc(pool->allocator, sizeof(int)); *((int *) data) = 13; cxMempoolSetDestructor(pool, data, test_destructor); void *rdata = data; unsigned n = 1; while (rdata == data) { n <<= 1; ASSERT_LT(n, 65536); // eventually the memory should be moved elsewhere rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); } EXPECT_EQ(*((int *) rdata), 13); // test if destructor is still intact test_destructor_called = 0; cxFree(pool->allocator, rdata); EXPECT_EQ(test_destructor_called, 1); } TEST_F(CxBasicMempool, free) { pool = cxBasicMempoolCreate(4); auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool); void *mem1; void *mem2; mem1 = cxMalloc(pool->allocator, 16); cxFree(pool->allocator, mem1); EXPECT_EQ(basic_pool->ndata, 0); cxMalloc(pool->allocator, 16); cxMalloc(pool->allocator, 16); mem1 = cxMalloc(pool->allocator, 16); cxMalloc(pool->allocator, 16); mem2 = cxMalloc(pool->allocator, 16); EXPECT_EQ(basic_pool->ndata, 5); cxFree(pool->allocator, mem1); EXPECT_EQ(basic_pool->ndata, 4); cxFree(pool->allocator, mem2); EXPECT_EQ(basic_pool->ndata, 3); }