1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (C) 2019 - 2021 Intel Corporation. */
#include "memkind.h"
#include "allocator_perf_tool/TimerSysTime.hpp"
#include "common.h"
class MemkindNullKindTests: public ::testing::Test
{
protected:
void SetUp()
{}
void TearDown()
{}
};
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_DefaultKindFreeNullPtr)
{
const double test_time = 5;
TimerSysTime timer;
timer.start();
do {
memkind_free(MEMKIND_DEFAULT, nullptr);
} while (timer.getElapsedTime() < test_time);
}
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_NullKindFreeNullPtr)
{
const double test_time = 5;
TimerSysTime timer;
timer.start();
do {
memkind_free(nullptr, nullptr);
} while (timer.getElapsedTime() < test_time);
}
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_NullKindUsableSizeNullPtr)
{
const double test_time = 5;
TimerSysTime timer;
timer.start();
do {
memkind_malloc_usable_size(nullptr, nullptr);
} while (timer.getElapsedTime() < test_time);
}
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_DefaultRegularKindFreeNullPtr)
{
const size_t size_1 = 1 * KB;
const size_t size_2 = 1 * MB;
void *ptr_default = nullptr;
void *ptr_regular = nullptr;
for (unsigned int i = 0; i < MEMKIND_MAX_KIND; ++i) {
ptr_default = memkind_malloc(MEMKIND_DEFAULT, size_2);
ASSERT_NE(nullptr, ptr_default);
memkind_free(nullptr, ptr_default);
ptr_default = memkind_malloc(MEMKIND_DEFAULT, size_1);
ASSERT_NE(nullptr, ptr_default);
memkind_free(nullptr, ptr_default);
ptr_regular = memkind_malloc(MEMKIND_REGULAR, size_2);
ASSERT_NE(nullptr, ptr_regular);
memkind_free(nullptr, ptr_regular);
ptr_regular = memkind_malloc(MEMKIND_REGULAR, size_1);
ASSERT_NE(nullptr, ptr_regular);
memkind_free(nullptr, ptr_regular);
}
}
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_DefaultReallocNullptrSizeZero)
{
const double test_time = 5;
void *test_nullptr = nullptr;
void *test_ptr_malloc = nullptr;
void *test_ptr_realloc = nullptr;
TimerSysTime timer;
timer.start();
do {
test_ptr_malloc = memkind_malloc(MEMKIND_DEFAULT, 5 * MB);
errno = 0;
test_ptr_realloc = memkind_realloc(MEMKIND_DEFAULT, test_ptr_malloc, 0);
ASSERT_EQ(test_ptr_realloc, nullptr);
ASSERT_EQ(errno, 0);
errno = 0;
// equivalent to memkind_malloc(MEMKIND_DEFAULT,0)
test_nullptr = memkind_realloc(MEMKIND_DEFAULT, nullptr, 0);
ASSERT_EQ(test_nullptr, nullptr);
ASSERT_EQ(errno, 0);
} while (timer.getElapsedTime() < test_time);
}
TEST_F(MemkindNullKindTests,
test_TC_MEMKIND_DefaultKindReallocNullKindSizeZeroImplicitFree)
{
const double test_time = 5;
size_t size = 1 * KB;
void *test_ptr_malloc = nullptr;
void *test_ptr_realloc = nullptr;
TimerSysTime timer;
timer.start();
do {
errno = 0;
test_ptr_malloc = memkind_malloc(MEMKIND_DEFAULT, size);
ASSERT_NE(test_ptr_malloc, nullptr);
ASSERT_EQ(errno, 0);
errno = 0;
test_ptr_realloc = memkind_realloc(nullptr, test_ptr_malloc, 0);
ASSERT_EQ(test_ptr_realloc, nullptr);
ASSERT_EQ(errno, 0);
} while (timer.getElapsedTime() < test_time);
}
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_DefaultKindReallocNullKindSizeMax)
{
size_t size = 1 * KB;
void *test_ptr_malloc = nullptr;
void *test_ptr_realloc = nullptr;
test_ptr_malloc = memkind_malloc(MEMKIND_DEFAULT, size);
ASSERT_NE(test_ptr_malloc, nullptr);
errno = 0;
test_ptr_realloc = memkind_realloc(nullptr, test_ptr_malloc, SIZE_MAX);
ASSERT_EQ(test_ptr_realloc, nullptr);
ASSERT_EQ(errno, ENOMEM);
memkind_free(nullptr, test_ptr_malloc);
}
TEST_F(MemkindNullKindTests,
test_TC_MEMKIND_DefaultReallocIncreaseSizeNullKindVariant)
{
size_t size = 1 * KB;
char *test1 = nullptr;
char *test2 = nullptr;
const char val[] =
"test_TC_MEMKIND_DefaultReallocIncreaseSizeNullKindVariant";
int status;
test1 = (char *)memkind_malloc(MEMKIND_DEFAULT, size);
ASSERT_NE(test1, nullptr);
sprintf(test1, "%s", val);
size *= 2;
test2 = (char *)memkind_realloc(nullptr, test1, size);
ASSERT_NE(test2, nullptr);
status = memcmp(val, test2, sizeof(val));
ASSERT_EQ(status, 0);
memkind_free(MEMKIND_DEFAULT, test2);
}
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_ReallocNullptrNullKind)
{
size_t size = 1 * KB;
errno = 0;
void *test = memkind_realloc(nullptr, nullptr, size);
ASSERT_EQ(test, nullptr);
ASSERT_EQ(errno, EINVAL);
}
TEST_F(MemkindNullKindTests,
test_TC_MEMKIND_DefaultReallocDecreaseSizeNullKindVariant)
{
size_t size = 1 * KB;
char *test1 = nullptr;
char *test2 = nullptr;
const char val[] =
"test_TC_MEMKIND_DefaultReallocDecreaseSizeNullKindVariant";
int status;
test1 = (char *)memkind_malloc(MEMKIND_DEFAULT, size);
ASSERT_NE(test1, nullptr);
sprintf(test1, "%s", val);
size = 4;
test2 = (char *)memkind_realloc(nullptr, test1, size);
ASSERT_NE(test2, nullptr);
status = memcmp(val, test2, size);
ASSERT_EQ(status, 0);
memkind_free(MEMKIND_DEFAULT, test2);
}
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_ReallocNullptrNullKindSizeMax)
{
errno = 0;
void *test = memkind_realloc(nullptr, nullptr, SIZE_MAX);
ASSERT_EQ(test, nullptr);
ASSERT_EQ(errno, EINVAL);
}
TEST_F(MemkindNullKindTests, test_TC_MEMKIND_ReallocNullptrNullKindSizeZero)
{
errno = 0;
void *test = memkind_realloc(nullptr, nullptr, 0);
ASSERT_EQ(test, nullptr);
ASSERT_EQ(errno, EINVAL);
}
|