universe@782: /* universe@782: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@782: * universe@782: * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. universe@782: * universe@782: * Redistribution and use in source and binary forms, with or without universe@782: * modification, are permitted provided that the following conditions are met: universe@782: * universe@782: * 1. Redistributions of source code must retain the above copyright universe@782: * notice, this list of conditions and the following disclaimer. universe@782: * universe@782: * 2. Redistributions in binary form must reproduce the above copyright universe@782: * notice, this list of conditions and the following disclaimer in the universe@782: * documentation and/or other materials provided with the distribution. universe@782: * universe@782: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@782: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@782: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@782: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@782: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@782: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@782: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@782: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@782: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@782: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@782: * POSSIBILITY OF SUCH DAMAGE. universe@782: */ universe@782: universe@782: #include "cx/test.h" universe@782: universe@782: #include "cx/allocator.h" universe@782: universe@782: CX_TEST(test_allocator_default) { universe@782: CX_TEST_DO { universe@782: CX_TEST_ASSERT(cxDefaultAllocator->cl != NULL); universe@782: } universe@782: } universe@782: universe@782: CX_TEST(test_allocator_default_malloc) { universe@782: void *test = cxMalloc(cxDefaultAllocator, 16); universe@782: CX_TEST_DO { universe@782: CX_TEST_ASSERT(test != NULL); universe@782: // we cannot assert sth. but valgrind will detect an error universe@782: memcpy(test, "0123456789ABCDEF", 16); universe@782: } universe@782: free(test); universe@782: } universe@782: universe@782: CX_TEST(test_allocator_default_calloc) { universe@782: char *test = cxCalloc(cxDefaultAllocator, 8, 2); universe@782: CX_TEST_DO { universe@782: CX_TEST_ASSERT(test != NULL); universe@782: for (int i = 0; i < 16; i++) { universe@782: CX_TEST_ASSERT(test[i] == 0); universe@782: } universe@782: } universe@782: free(test); universe@782: } universe@782: universe@782: CX_TEST(test_allocator_default_realloc) { universe@782: char *test = calloc(8, 1); universe@782: memcpy(test, "Test", 5); universe@782: CX_TEST_DO { universe@782: test = cxRealloc(cxDefaultAllocator, test, 16); universe@782: CX_TEST_ASSERT(test != NULL); universe@782: CX_TEST_ASSERT(0 == strcmp(test, "Test")); universe@782: } universe@782: free(test); universe@782: } universe@782: universe@782: CX_TEST(test_allocator_default_free) { universe@782: void *test = malloc(16); universe@782: CX_TEST_DO { universe@782: // we cannot assert sth. but valgrind will detect an error universe@782: cxFree(cxDefaultAllocator, test); universe@782: CX_TEST_ASSERT(true); universe@782: } universe@782: } universe@782: universe@782: CX_TEST(test_allocator_reallocate) { universe@782: void *test = calloc(8, 1); universe@782: memcpy(test, "Test", 5); universe@782: CX_TEST_DO { universe@782: int ret = cxReallocate(cxDefaultAllocator, &test, 16); universe@782: CX_TEST_ASSERT(ret == 0); universe@782: CX_TEST_ASSERT(test != NULL); universe@782: CX_TEST_ASSERT(0 == strcmp(test, "Test")); universe@782: } universe@782: free(test); universe@782: } universe@782: universe@782: CX_TEST(test_allocator_reallocate_low_level) { universe@782: void *test = calloc(8, 1); universe@782: memcpy(test, "Test", 5); universe@782: CX_TEST_DO { universe@782: int ret = cx_reallocate(&test, 16); universe@782: CX_TEST_ASSERT(ret == 0); universe@782: CX_TEST_ASSERT(test != NULL); universe@782: CX_TEST_ASSERT(0 == strcmp(test, "Test")); universe@782: } universe@782: free(test); universe@782: } universe@782: universe@782: static void *test_allocator_mock_failing_realloc( universe@782: __attribute__((__unused__))void *p, universe@782: __attribute__((__unused__))void *d, universe@782: __attribute__((__unused__))size_t n universe@782: ) { universe@782: return NULL; universe@782: } universe@782: universe@782: CX_TEST(test_allocator_reallocate_fails) { universe@782: // Mock an allocator that always returns NULL on realloc universe@782: cx_allocator_class mock_cl; universe@782: mock_cl.realloc = test_allocator_mock_failing_realloc; universe@782: CxAllocator mock = {&mock_cl, NULL}; universe@782: universe@782: void *test = calloc(8, 1); universe@782: memcpy(test, "Test", 5); universe@782: void *original = test; universe@782: CX_TEST_DO { universe@782: int ret = cxReallocate(&mock, &test, 16); universe@782: // non-zero return code because of the failure universe@782: CX_TEST_ASSERT(ret != 0); universe@782: // the test pointer was not changed and still points to the same memory universe@782: CX_TEST_ASSERT(test == original); universe@782: CX_TEST_ASSERT(0 == strcmp(test, "Test")); universe@782: } universe@782: free(test); universe@782: } universe@782: universe@782: CxTestSuite *cx_test_suite_allocator(void) { universe@782: CxTestSuite *suite = cx_test_suite_new("allocator"); universe@782: universe@782: cx_test_register(suite, test_allocator_default); universe@782: cx_test_register(suite, test_allocator_default_malloc); universe@782: cx_test_register(suite, test_allocator_default_calloc); universe@782: cx_test_register(suite, test_allocator_default_realloc); universe@782: cx_test_register(suite, test_allocator_default_free); universe@782: cx_test_register(suite, test_allocator_reallocate); universe@782: cx_test_register(suite, test_allocator_reallocate_fails); universe@782: cx_test_register(suite, test_allocator_reallocate_low_level); universe@782: universe@782: return suite; universe@782: }