| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include "gtest/gtest.h" |
| 36 |
|
| 37 |
namespace |
| 38 |
{ |
| 39 |
|
| 40 |
TEST(UnitTestMallocUsed, Malloc_1_8) |
| 41 |
{ |
| 42 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 43 |
static const size_t bytes_to_allocate = 8; |
| 44 |
|
| 45 |
size_t start = malloc_used(); |
| 46 |
|
| 47 |
size_t used = 0; |
| 48 |
|
| 49 |
char *x = static_cast<char *>(malloc(bytes_to_allocate)); |
| 50 |
|
| 51 |
used += malloc_used(); |
| 52 |
|
| 53 |
free(x); |
| 54 |
|
| 55 |
size_t end = malloc_used(); |
| 56 |
|
| 57 |
EXPECT_LE(bytes_to_allocate, used - start); |
| 58 |
EXPECT_LE(start, end); |
| 59 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 60 |
#endif |
| 61 |
} |
| 62 |
|
| 63 |
TEST(UnitTestMallocUsed, Malloc_1_16) |
| 64 |
{ |
| 65 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 66 |
static const size_t bytes_to_allocate = 16; |
| 67 |
|
| 68 |
size_t start = malloc_used(); |
| 69 |
|
| 70 |
size_t used = 0; |
| 71 |
|
| 72 |
char *x = static_cast<char *>(malloc(bytes_to_allocate)); |
| 73 |
|
| 74 |
used += malloc_used(); |
| 75 |
|
| 76 |
free(x); |
| 77 |
|
| 78 |
size_t end = malloc_used(); |
| 79 |
|
| 80 |
EXPECT_LE(bytes_to_allocate, used - start); |
| 81 |
EXPECT_LE(start, end); |
| 82 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 83 |
#endif |
| 84 |
} |
| 85 |
|
| 86 |
TEST(UnitTestMallocUsed, Malloc_1_32) |
| 87 |
{ |
| 88 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 89 |
static const size_t bytes_to_allocate = 32; |
| 90 |
|
| 91 |
size_t start = malloc_used(); |
| 92 |
|
| 93 |
size_t used = 0; |
| 94 |
|
| 95 |
char *x = static_cast<char *>(malloc(bytes_to_allocate)); |
| 96 |
|
| 97 |
used += malloc_used(); |
| 98 |
|
| 99 |
free(x); |
| 100 |
|
| 101 |
size_t end = malloc_used(); |
| 102 |
|
| 103 |
EXPECT_LE(bytes_to_allocate, used - start); |
| 104 |
EXPECT_LE(start, end); |
| 105 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 106 |
#endif |
| 107 |
} |
| 108 |
|
| 109 |
TEST(UnitTestMallocUsed, Malloc_1_1024) |
| 110 |
{ |
| 111 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 112 |
static const size_t bytes_to_allocate = 1024; |
| 113 |
|
| 114 |
size_t start = malloc_used(); |
| 115 |
|
| 116 |
size_t used = 0; |
| 117 |
|
| 118 |
char *x = static_cast<char *>(malloc(bytes_to_allocate)); |
| 119 |
|
| 120 |
used += malloc_used(); |
| 121 |
|
| 122 |
free(x); |
| 123 |
|
| 124 |
size_t end = malloc_used(); |
| 125 |
|
| 126 |
EXPECT_LE(bytes_to_allocate, used - start); |
| 127 |
EXPECT_LE(start, end); |
| 128 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 129 |
#endif |
| 130 |
} |
| 131 |
|
| 132 |
TEST(UnitTestMallocUsed, Malloc_100x1024) |
| 133 |
{ |
| 134 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 135 |
static const size_t bytes_to_allocate = 1024; |
| 136 |
|
| 137 |
for (size_t i = 0; i != 100; ++i) { |
| 138 |
|
| 139 |
size_t start = malloc_used(); |
| 140 |
|
| 141 |
size_t used = 0; |
| 142 |
|
| 143 |
char *x = static_cast<char *>(malloc(bytes_to_allocate)); |
| 144 |
|
| 145 |
used += malloc_used(); |
| 146 |
|
| 147 |
free(x); |
| 148 |
|
| 149 |
size_t end = malloc_used(); |
| 150 |
|
| 151 |
EXPECT_LE(bytes_to_allocate, used - start); |
| 152 |
EXPECT_LE(start, end); |
| 153 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 154 |
} |
| 155 |
#endif |
| 156 |
} |
| 157 |
|
| 158 |
TEST(UnitTestMallocUsed, Malloc_1_1M) |
| 159 |
{ |
| 160 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 161 |
static const size_t bytes_to_allocate = 1024*1024; |
| 162 |
|
| 163 |
size_t start = malloc_used(); |
| 164 |
|
| 165 |
size_t used = 0; |
| 166 |
|
| 167 |
char *x = static_cast<char *>(malloc(bytes_to_allocate)); |
| 168 |
|
| 169 |
used += malloc_used(); |
| 170 |
|
| 171 |
free(x); |
| 172 |
|
| 173 |
size_t end = malloc_used(); |
| 174 |
|
| 175 |
EXPECT_LE(bytes_to_allocate, used - start); |
| 176 |
EXPECT_LE(start, end); |
| 177 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 178 |
#endif |
| 179 |
} |
| 180 |
|
| 181 |
TEST(UnitTestMallocUsed, Malloc_1_100M) |
| 182 |
{ |
| 183 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 184 |
static const size_t bytes_to_allocate = 100*1024*1024; |
| 185 |
|
| 186 |
size_t start = malloc_used(); |
| 187 |
|
| 188 |
size_t used = 0; |
| 189 |
|
| 190 |
char *x = static_cast<char *>(malloc(bytes_to_allocate)); |
| 191 |
|
| 192 |
used += malloc_used(); |
| 193 |
|
| 194 |
free(x); |
| 195 |
|
| 196 |
size_t end = malloc_used(); |
| 197 |
|
| 198 |
EXPECT_LE(bytes_to_allocate, used - start); |
| 199 |
EXPECT_LE(start, end); |
| 200 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 201 |
#endif |
| 202 |
} |
| 203 |
|
| 204 |
TEST(UnitTestMallocUsed, Malloc_100_32) |
| 205 |
{ |
| 206 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 207 |
static const size_t bytes_to_allocate = 32; |
| 208 |
|
| 209 |
size_t start = malloc_used(); |
| 210 |
|
| 211 |
char *x[100]; |
| 212 |
size_t used = 0; |
| 213 |
|
| 214 |
for (size_t i = 0; i < 100; ++i) |
| 215 |
x[i] = static_cast<char *>(malloc(bytes_to_allocate)); |
| 216 |
|
| 217 |
used += malloc_used(); |
| 218 |
|
| 219 |
for (size_t i = 0; i < 100; ++i) |
| 220 |
free(x[i]); |
| 221 |
|
| 222 |
size_t end = malloc_used(); |
| 223 |
|
| 224 |
EXPECT_LE(bytes_to_allocate*100, used - start); |
| 225 |
EXPECT_LE(start, end); |
| 226 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 227 |
#endif |
| 228 |
} |
| 229 |
|
| 230 |
TEST(UnitTestMallocUsed, Malloc_100_1024) |
| 231 |
{ |
| 232 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 233 |
static const size_t bytes_to_allocate = 1024; |
| 234 |
|
| 235 |
size_t start = malloc_used(); |
| 236 |
|
| 237 |
char *x[100]; |
| 238 |
size_t used = 0; |
| 239 |
|
| 240 |
for (size_t i = 0; i < 100; ++i) |
| 241 |
x[i] = static_cast<char *>(malloc(bytes_to_allocate)); |
| 242 |
|
| 243 |
used += malloc_used(); |
| 244 |
|
| 245 |
for (size_t i = 0; i < 100; ++i) |
| 246 |
free(x[i]); |
| 247 |
|
| 248 |
size_t end = malloc_used(); |
| 249 |
|
| 250 |
EXPECT_LE(bytes_to_allocate*100, used - start); |
| 251 |
EXPECT_LE(start, end); |
| 252 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 253 |
#endif |
| 254 |
} |
| 255 |
|
| 256 |
TEST(UnitTestMallocUsed, Malloc_100_1M) |
| 257 |
{ |
| 258 |
#if defined SIERRA_PTMALLOC3_ALLOCATOR || defined SIERRA_PTMALLOC2_ALLOCATOR |
| 259 |
static const size_t bytes_to_allocate = 1024*1024; |
| 260 |
|
| 261 |
size_t start = malloc_used(); |
| 262 |
|
| 263 |
char *x[100]; |
| 264 |
size_t used = 0; |
| 265 |
|
| 266 |
for (size_t i = 0; i < 100; ++i) |
| 267 |
x[i] = static_cast<char *>(malloc(bytes_to_allocate)); |
| 268 |
|
| 269 |
used += malloc_used(); |
| 270 |
|
| 271 |
for (size_t i = 0; i < 100; ++i) |
| 272 |
free(x[i]); |
| 273 |
|
| 274 |
size_t end = malloc_used(); |
| 275 |
|
| 276 |
EXPECT_LE(bytes_to_allocate*100, used - start); |
| 277 |
EXPECT_LE(start, end); |
| 278 |
std::cout << "start " << start << ", end " << end << ", used " << used - start << std::endl; |
| 279 |
#endif |
| 280 |
} |
| 281 |
|
| 282 |
} |