test/test_allocator.cpp

Sat, 16 Apr 2022 08:49:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 Apr 2022 08:49:31 +0200
changeset 511
a32e6a6b1ca7
parent 443
test/test_allocator.c@d6d8712e15bc
child 513
b66532b5d8db
permissions
-rw-r--r--

migrate allocator tests to gtest

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@511 32 #define SUITE_NAME Allocator
universe@511 33
universe@511 34 TEST(SUITE_NAME, DefaultAllocator) {
universe@397 35 cx_allocator_class *clazz = cxDefaultAllocator->cl;
universe@511 36 ASSERT_NE(clazz, nullptr);
universe@391 37 }
universe@391 38
universe@511 39 TEST(SUITE_NAME, DefaultMalloc) {
universe@397 40 void *test = cxMalloc(cxDefaultAllocator, 16);
universe@511 41 ASSERT_NE(test, nullptr);
universe@391 42 free(test);
universe@391 43 }
universe@391 44
universe@511 45 TEST(SUITE_NAME, DefaultRealloc) {
universe@397 46 void *test = calloc(8, 1);
universe@421 47 memcpy(test, "Test", 5);
universe@391 48 test = cxRealloc(cxDefaultAllocator, test, 16);
universe@511 49 ASSERT_NE(test, nullptr);
universe@511 50 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
universe@391 51 free(test);
universe@391 52 }
universe@391 53
universe@511 54 TEST(SUITE_NAME, Reallocate) {
universe@414 55 void *test = calloc(8, 1);
universe@421 56 memcpy(test, "Test", 5);
universe@511 57 int ret = cxReallocate(cxDefaultAllocator, &test, 16);
universe@511 58 EXPECT_EQ(ret, 0);
universe@511 59 ASSERT_NE(test, nullptr);
universe@511 60 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
universe@414 61 free(test);
universe@414 62 }
universe@414 63
universe@511 64 TEST(SUITE_NAME, DefaultCalloc) {
universe@511 65 char *test = reinterpret_cast<char *>(cxCalloc(cxDefaultAllocator, 8, 2));
universe@511 66 ASSERT_NE(test, nullptr);
universe@511 67 for (int i = 0; i < 16; i++) ASSERT_EQ(test[i], 0);
universe@391 68 free(test);
universe@391 69 }
universe@391 70
universe@511 71 TEST(SUITE_NAME, DefaultFree) {
universe@397 72 void *test = malloc(16);
universe@511 73 EXPECT_NO_FATAL_FAILURE(
universe@511 74 cxFree(cxDefaultAllocator, test);
universe@511 75 );
universe@391 76 }

mercurial