universe@391: /* universe@391: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@391: * universe@391: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@391: * universe@391: * Redistribution and use in source and binary forms, with or without universe@391: * modification, are permitted provided that the following conditions are met: universe@391: * universe@391: * 1. Redistributions of source code must retain the above copyright universe@391: * notice, this list of conditions and the following disclaimer. universe@391: * universe@391: * 2. Redistributions in binary form must reproduce the above copyright universe@391: * notice, this list of conditions and the following disclaimer in the universe@391: * documentation and/or other materials provided with the distribution. universe@391: * universe@391: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@391: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@391: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@391: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@391: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@391: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@391: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@391: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@391: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@391: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@391: * POSSIBILITY OF SUCH DAMAGE. universe@391: */ universe@391: universe@391: #include "cx/allocator.h" universe@511: #include universe@391: universe@513: TEST(Allocator, DefaultAllocator) { universe@397: cx_allocator_class *clazz = cxDefaultAllocator->cl; universe@511: ASSERT_NE(clazz, nullptr); universe@391: } universe@391: universe@513: TEST(Allocator, DefaultMalloc) { universe@397: void *test = cxMalloc(cxDefaultAllocator, 16); universe@511: ASSERT_NE(test, nullptr); universe@391: free(test); universe@391: } universe@391: universe@513: TEST(Allocator, DefaultRealloc) { universe@397: void *test = calloc(8, 1); universe@421: memcpy(test, "Test", 5); universe@391: test = cxRealloc(cxDefaultAllocator, test, 16); universe@511: ASSERT_NE(test, nullptr); universe@511: EXPECT_STREQ(reinterpret_cast(test), "Test"); universe@391: free(test); universe@391: } universe@391: universe@513: TEST(Allocator, Reallocate) { universe@414: void *test = calloc(8, 1); universe@421: memcpy(test, "Test", 5); universe@511: int ret = cxReallocate(cxDefaultAllocator, &test, 16); universe@511: EXPECT_EQ(ret, 0); universe@511: ASSERT_NE(test, nullptr); universe@511: EXPECT_STREQ(reinterpret_cast(test), "Test"); universe@414: free(test); universe@414: } universe@414: universe@513: TEST(Allocator, DefaultCalloc) { universe@511: char *test = reinterpret_cast(cxCalloc(cxDefaultAllocator, 8, 2)); universe@511: ASSERT_NE(test, nullptr); universe@511: for (int i = 0; i < 16; i++) ASSERT_EQ(test[i], 0); universe@391: free(test); universe@391: } universe@391: universe@513: TEST(Allocator, DefaultFree) { universe@397: void *test = malloc(16); universe@511: EXPECT_NO_FATAL_FAILURE( universe@511: cxFree(cxDefaultAllocator, test); universe@511: ); universe@391: } universe@514: universe@514: TEST(Allocator, FailingReallocate) { universe@514: // Mock an allocator that always returns nullptr on realloc universe@514: cx_allocator_class mock_cl; universe@522: mock_cl.realloc = []( universe@522: [[maybe_unused]]void *p, universe@522: [[maybe_unused]]void *d, universe@522: [[maybe_unused]]size_t n universe@522: ) -> void * { return nullptr; }; universe@514: cx_allocator_s mock{&mock_cl, nullptr}; universe@514: universe@514: void *test = calloc(8, 1); universe@514: memcpy(test, "Test", 5); universe@514: void *original = test; universe@514: int ret = cxReallocate(&mock, &test, 16); universe@514: // non-zero return code because of the failure universe@514: EXPECT_NE(ret, 0); universe@514: // the test pointer was not changed and still points to the same memory universe@514: EXPECT_EQ(test, original); universe@514: EXPECT_STREQ(reinterpret_cast(test), "Test"); universe@514: free(test); universe@514: }