test/test_allocator.cpp

Mon, 08 Aug 2022 17:12:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 08 Aug 2022 17:12:00 +0200
changeset 572
f0f99dd06d9f
parent 522
b91c50d023f4
permissions
-rw-r--r--

#201 - remove dangerous allocator config

There is no plausible use case, except using the testing
allocator in the test case, and having the possibility to
specify any allocator (including another mempool) causes
more harm than good.

universe@391 1 /*
universe@391 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@391 3 *
universe@391 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@391 5 *
universe@391 6 * Redistribution and use in source and binary forms, with or without
universe@391 7 * modification, are permitted provided that the following conditions are met:
universe@391 8 *
universe@391 9 * 1. Redistributions of source code must retain the above copyright
universe@391 10 * notice, this list of conditions and the following disclaimer.
universe@391 11 *
universe@391 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@391 13 * notice, this list of conditions and the following disclaimer in the
universe@391 14 * documentation and/or other materials provided with the distribution.
universe@391 15 *
universe@391 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@391 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@391 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@391 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@391 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@391 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@391 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@391 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@391 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@391 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@391 26 * POSSIBILITY OF SUCH DAMAGE.
universe@391 27 */
universe@391 28
universe@391 29 #include "cx/allocator.h"
universe@511 30 #include <gtest/gtest.h>
universe@391 31
universe@513 32 TEST(Allocator, DefaultAllocator) {
universe@397 33 cx_allocator_class *clazz = cxDefaultAllocator->cl;
universe@511 34 ASSERT_NE(clazz, nullptr);
universe@391 35 }
universe@391 36
universe@513 37 TEST(Allocator, DefaultMalloc) {
universe@397 38 void *test = cxMalloc(cxDefaultAllocator, 16);
universe@511 39 ASSERT_NE(test, nullptr);
universe@391 40 free(test);
universe@391 41 }
universe@391 42
universe@513 43 TEST(Allocator, DefaultRealloc) {
universe@397 44 void *test = calloc(8, 1);
universe@421 45 memcpy(test, "Test", 5);
universe@391 46 test = cxRealloc(cxDefaultAllocator, test, 16);
universe@511 47 ASSERT_NE(test, nullptr);
universe@511 48 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
universe@391 49 free(test);
universe@391 50 }
universe@391 51
universe@513 52 TEST(Allocator, Reallocate) {
universe@414 53 void *test = calloc(8, 1);
universe@421 54 memcpy(test, "Test", 5);
universe@511 55 int ret = cxReallocate(cxDefaultAllocator, &test, 16);
universe@511 56 EXPECT_EQ(ret, 0);
universe@511 57 ASSERT_NE(test, nullptr);
universe@511 58 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
universe@414 59 free(test);
universe@414 60 }
universe@414 61
universe@513 62 TEST(Allocator, DefaultCalloc) {
universe@511 63 char *test = reinterpret_cast<char *>(cxCalloc(cxDefaultAllocator, 8, 2));
universe@511 64 ASSERT_NE(test, nullptr);
universe@511 65 for (int i = 0; i < 16; i++) ASSERT_EQ(test[i], 0);
universe@391 66 free(test);
universe@391 67 }
universe@391 68
universe@513 69 TEST(Allocator, DefaultFree) {
universe@397 70 void *test = malloc(16);
universe@511 71 EXPECT_NO_FATAL_FAILURE(
universe@511 72 cxFree(cxDefaultAllocator, test);
universe@511 73 );
universe@391 74 }
universe@514 75
universe@514 76 TEST(Allocator, FailingReallocate) {
universe@514 77 // Mock an allocator that always returns nullptr on realloc
universe@514 78 cx_allocator_class mock_cl;
universe@522 79 mock_cl.realloc = [](
universe@522 80 [[maybe_unused]]void *p,
universe@522 81 [[maybe_unused]]void *d,
universe@522 82 [[maybe_unused]]size_t n
universe@522 83 ) -> void * { return nullptr; };
universe@514 84 cx_allocator_s mock{&mock_cl, nullptr};
universe@514 85
universe@514 86 void *test = calloc(8, 1);
universe@514 87 memcpy(test, "Test", 5);
universe@514 88 void *original = test;
universe@514 89 int ret = cxReallocate(&mock, &test, 16);
universe@514 90 // non-zero return code because of the failure
universe@514 91 EXPECT_NE(ret, 0);
universe@514 92 // the test pointer was not changed and still points to the same memory
universe@514 93 EXPECT_EQ(test, original);
universe@514 94 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
universe@514 95 free(test);
universe@514 96 }

mercurial