| 1 | /* |
| 2 | * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the <organization> nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY |
| 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "CppUTest/TestHarness.h" |
| 29 | #include "CppUTest/SimpleStringInternalCache.h" |
| 30 | #include "CppUTest/TestTestingFixture.h" |
| 31 | |
| 32 | class TestFunctionWithCache : public ExecFunction |
| 33 | { |
| 34 | public: |
| 35 | void (*testFunction)(SimpleStringInternalCache*, size_t); |
| 36 | SimpleStringInternalCache* parameter; |
| 37 | size_t allocationSize; |
| 38 | |
| 39 | void exec() _override |
| 40 | { |
| 41 | testFunction(parameter, allocationSize); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | TEST_GROUP(SimpleStringInternalCache) |
| 46 | { |
| 47 | SimpleStringInternalCache cache; |
| 48 | MemoryAccountant accountant; |
| 49 | MemoryLeakAllocator* defaultAllocator; |
| 50 | AccountingTestMemoryAllocator* allocator; |
| 51 | |
| 52 | TestFunctionWithCache testFunction; |
| 53 | TestTestingFixture fixture; |
| 54 | |
| 55 | void setup() |
| 56 | { |
| 57 | fixture.setTestFunction(&testFunction); |
| 58 | testFunction.parameter = &cache; |
| 59 | |
| 60 | defaultAllocator = new MemoryLeakAllocator(defaultMallocAllocator()); |
| 61 | allocator = new AccountingTestMemoryAllocator(accountant, defaultAllocator); |
| 62 | cache.setAllocator(defaultAllocator); |
| 63 | } |
| 64 | |
| 65 | void teardown() |
| 66 | { |
| 67 | cache.clearAllIncludingCurrentlyUsedMemory(); |
| 68 | accountant.clear(); |
| 69 | delete allocator; |
| 70 | delete defaultAllocator; |
| 71 | } |
| 72 | |
| 73 | void createCacheForSize(size_t size, size_t amount) |
| 74 | { |
| 75 | for (size_t i = 0; i < amount; i++) { |
| 76 | char* memory = cache.alloc(size); |
| 77 | cache.dealloc(memory, size); |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | TEST(SimpleStringInternalCache, cacheHitWithOneEntry) |
| 83 | { |
| 84 | createCacheForSize(10, 1); |
| 85 | cache.setAllocator(allocator); |
| 86 | |
| 87 | char* mem = cache.alloc(10); |
| 88 | mem[0] = 'B'; |
| 89 | mem[3] = 'A'; |
| 90 | mem[9] = 'S'; |
| 91 | |
| 92 | cache.setAllocator(allocator->originalAllocator()); |
| 93 | |
| 94 | LONGS_EQUAL(0, accountant.totalAllocationsOfSize(10)); |
| 95 | CHECK(!cache.hasFreeBlocksOfSize(10)); |
| 96 | |
| 97 | cache.setAllocator(allocator); |
| 98 | } |
| 99 | |
| 100 | TEST(SimpleStringInternalCache, cacheHitWithTwoEntries) |
| 101 | { |
| 102 | createCacheForSize(10, 2); |
| 103 | cache.setAllocator(allocator); |
| 104 | |
| 105 | cache.alloc(10); |
| 106 | cache.alloc(10); |
| 107 | |
| 108 | cache.setAllocator(allocator->originalAllocator()); |
| 109 | |
| 110 | LONGS_EQUAL(0, accountant.totalAllocationsOfSize(10)); |
| 111 | CHECK(!cache.hasFreeBlocksOfSize(10)); |
| 112 | |
| 113 | cache.setAllocator(allocator); |
| 114 | } |
| 115 | |
| 116 | TEST(SimpleStringInternalCache, allocatingMoreThanCacheAvailable) |
| 117 | { |
| 118 | createCacheForSize(10, 1); |
| 119 | cache.setAllocator(allocator); |
| 120 | |
| 121 | cache.alloc(10); |
| 122 | cache.alloc(10); |
| 123 | |
| 124 | cache.setAllocator(allocator->originalAllocator()); |
| 125 | |
| 126 | LONGS_EQUAL(1, accountant.totalAllocationsOfSize(32)); |
| 127 | CHECK(!cache.hasFreeBlocksOfSize(10)); |
| 128 | |
| 129 | cache.setAllocator(allocator); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | TEST(SimpleStringInternalCache, allocationWillReuseTheAllocatedBlocks) |
| 134 | { |
| 135 | cache.setAllocator(allocator); |
| 136 | |
| 137 | char* mem = cache.alloc(10); |
| 138 | cache.dealloc(mem, 10); |
| 139 | mem = cache.alloc(10); |
| 140 | cache.dealloc(mem, 10); |
| 141 | |
| 142 | LONGS_EQUAL(1, accountant.totalAllocationsOfSize(32)); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | TEST(SimpleStringInternalCache, multipleDifferentSizeAllocationsAndDeallocations) |
| 147 | { |
| 148 | cache.setAllocator(allocator); |
| 149 | |
| 150 | char* mem10 = cache.alloc(10); |
| 151 | char* mem11 = cache.alloc(11); |
| 152 | |
| 153 | char* mem100 = cache.alloc(100); |
| 154 | cache.dealloc(mem100, 100); |
| 155 | |
| 156 | char* mem101 = cache.alloc(101); |
| 157 | char* mem102 = cache.alloc(102); |
| 158 | char* mem103 = cache.alloc(103); |
| 159 | cache.dealloc(mem101, 102); |
| 160 | cache.dealloc(mem102, 103); |
| 161 | cache.dealloc(mem103, 104); |
| 162 | |
| 163 | cache.alloc(105); |
| 164 | cache.alloc(106); |
| 165 | cache.alloc(107); |
| 166 | |
| 167 | cache.dealloc(mem10, 10); |
| 168 | cache.dealloc(mem11, 11); |
| 169 | |
| 170 | LONGS_EQUAL(2, accountant.totalAllocationsOfSize(32)); |
| 171 | LONGS_EQUAL(3, accountant.totalAllocationsOfSize(128)); |
| 172 | } |
| 173 | |
| 174 | TEST(SimpleStringInternalCache, deallocOfCachedMemoryWillNotDealloc) |
| 175 | { |
| 176 | cache.setAllocator(allocator); |
| 177 | |
| 178 | char* mem = cache.alloc(10); |
| 179 | cache.dealloc(mem, 10); |
| 180 | |
| 181 | LONGS_EQUAL(0, accountant.totalDeallocationsOfSize(32)); |
| 182 | } |
| 183 | |
| 184 | TEST(SimpleStringInternalCache, clearCacheWillRemoveAllCachedMemoryButNotAllUsedMemory) |
| 185 | { |
| 186 | cache.setAllocator(allocator); |
| 187 | |
| 188 | char* mem = cache.alloc(10); |
| 189 | cache.dealloc(mem, 10); |
| 190 | |
| 191 | mem = cache.alloc(60); |
| 192 | |
| 193 | cache.clearCache(); |
| 194 | |
| 195 | LONGS_EQUAL(1, accountant.totalDeallocationsOfSize(32)); |
| 196 | LONGS_EQUAL(0, accountant.totalDeallocationsOfSize(64)); |
| 197 | } |
| 198 | |
| 199 | TEST(SimpleStringInternalCache, clearAllIncludingCurrentlyUsedMemory) |
| 200 | { |
| 201 | cache.setAllocator(allocator); |
| 202 | |
| 203 | cache.alloc(60); |
| 204 | |
| 205 | cache.clearAllIncludingCurrentlyUsedMemory(); |
| 206 | |
| 207 | LONGS_EQUAL(1, accountant.totalDeallocationsOfSize(64)); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | TEST(SimpleStringInternalCache, allocatingLargerStringThanCached) |
| 213 | { |
| 214 | cache.setAllocator(allocator); |
| 215 | |
| 216 | char* mem = cache.alloc(1234); |
| 217 | cache.dealloc(mem, 1234); |
| 218 | |
| 219 | LONGS_EQUAL(1, accountant.totalAllocationsOfSize(1234)); |
| 220 | LONGS_EQUAL(1, accountant.totalDeallocationsOfSize(1234)); |
| 221 | } |
| 222 | |
| 223 | TEST(SimpleStringInternalCache, allocatingMultipleLargerStringThanCached) |
| 224 | { |
| 225 | cache.setAllocator(allocator); |
| 226 | |
| 227 | char* mem = cache.alloc(1234); |
| 228 | char* mem2 = cache.alloc(1234); |
| 229 | char* mem3 = cache.alloc(1234); |
| 230 | |
| 231 | cache.dealloc(mem2, 1234); |
| 232 | cache.dealloc(mem, 1234); |
| 233 | cache.dealloc(mem3, 1234); |
| 234 | |
| 235 | LONGS_EQUAL(3, accountant.totalAllocationsOfSize(1234)); |
| 236 | LONGS_EQUAL(3, accountant.totalDeallocationsOfSize(1234)); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | TEST(SimpleStringInternalCache, clearAllIncludingCurrentlyUsedMemoryAlsoReleasesLargeNonCachesMemory) |
| 241 | { |
| 242 | cache.setAllocator(allocator); |
| 243 | |
| 244 | cache.alloc(1234); |
| 245 | cache.alloc(1234); |
| 246 | cache.alloc(1234); |
| 247 | |
| 248 | cache.clearAllIncludingCurrentlyUsedMemory(); |
| 249 | |
| 250 | LONGS_EQUAL(3, accountant.totalAllocationsOfSize(1234)); |
| 251 | LONGS_EQUAL(3, accountant.totalDeallocationsOfSize(1234)); |
| 252 | } |
| 253 | |
| 254 | static void _deallocatingStringMemoryThatWasntAllocatedWithCache(SimpleStringInternalCache* cache, size_t allocationSize) |
| 255 | { |
| 256 | char* mem = defaultMallocAllocator()->alloc_memory(allocationSize, __FILE__, __LINE__); |
| 257 | mem[0] = 'B'; |
| 258 | mem[1] = 'a'; |
| 259 | mem[2] = 's'; |
| 260 | mem[3] = '\0'; |
| 261 | cache->dealloc(mem, allocationSize); |
| 262 | defaultMallocAllocator()->free_memory(mem, allocationSize, __FILE__, __LINE__); |
| 263 | } |
| 264 | |
| 265 | TEST(SimpleStringInternalCache, deallocatingMemoryThatWasntAllocatedWhileCacheWasInPlaceProducesWarning) |
| 266 | { |
| 267 | testFunction.testFunction = _deallocatingStringMemoryThatWasntAllocatedWithCache; |
| 268 | testFunction.allocationSize = 123; |
| 269 | |
| 270 | cache.setAllocator(allocator); |
| 271 | fixture.runAllTests(); |
| 272 | |
| 273 | fixture.assertPrintContains("\nWARNING: Attempting to deallocate a String buffer that was allocated while not caching. Ignoring it!\n" |
| 274 | "This is likely due statics and will cause problems.\n" |
| 275 | "Only warning once to avoid recursive warnings.\n" |
| 276 | "String we are deallocating: \"Bas\"\n"); |
| 277 | |
| 278 | } |
| 279 | |
| 280 | static void _deallocatingStringMemoryTwiceThatWasntAllocatedWithCache(SimpleStringInternalCache* cache, size_t allocationSize) |
| 281 | { |
| 282 | char* mem = defaultMallocAllocator()->alloc_memory(allocationSize, __FILE__, __LINE__); |
| 283 | mem[0] = '\0'; |
| 284 | cache->dealloc(mem, allocationSize); |
| 285 | cache->dealloc(mem, allocationSize); |
| 286 | defaultMallocAllocator()->free_memory(mem, allocationSize, __FILE__, __LINE__); |
| 287 | } |
| 288 | |
| 289 | TEST(SimpleStringInternalCache, deallocatingMemoryThatWasntAllocatedWhileCacheWasInPlaceProducesWarningButOnlyOnce) |
| 290 | { |
| 291 | testFunction.testFunction = _deallocatingStringMemoryTwiceThatWasntAllocatedWithCache; |
| 292 | testFunction.allocationSize = 123; |
| 293 | |
| 294 | cache.setAllocator(allocator); |
| 295 | fixture.runAllTests(); |
| 296 | |
| 297 | LONGS_EQUAL(1, fixture.getOutput().count("WARNING")); |
| 298 | } |
| 299 | |
| 300 | TEST(SimpleStringInternalCache, deallocatingLargeMemoryThatWasntAllocatedWhileCacheWasInPlaceProducesWarning) |
| 301 | { |
| 302 | testFunction.testFunction = _deallocatingStringMemoryThatWasntAllocatedWithCache; |
| 303 | testFunction.allocationSize = 12345; |
| 304 | |
| 305 | cache.setAllocator(allocator); |
| 306 | fixture.runAllTests(); |
| 307 | |
| 308 | fixture.assertPrintContains("\nWARNING: Attempting to deallocate a String buffer that was allocated while not caching. Ignoring it!\n" |
| 309 | "This is likely due statics and will cause problems.\n" |
| 310 | "Only warning once to avoid recursive warnings.\n" |
| 311 | "String we are deallocating: \"Bas\"\n"); |
| 312 | |
| 313 | } |
| 314 | |
| 315 | TEST(SimpleStringInternalCache, deallocatingLargeMemoryThatWasntAllocatedWhileCacheWasInPlaceProducesWarningButOnlyOnce) |
| 316 | { |
| 317 | testFunction.testFunction = _deallocatingStringMemoryTwiceThatWasntAllocatedWithCache; |
| 318 | testFunction.allocationSize = 12345; |
| 319 | |
| 320 | cache.setAllocator(allocator); |
| 321 | fixture.runAllTests(); |
| 322 | |
| 323 | LONGS_EQUAL(1, fixture.getOutput().count("WARNING")); |
| 324 | } |
| 325 | |
| 326 | TEST_GROUP(SimpleStringCacheAllocator) |
| 327 | { |
| 328 | SimpleStringCacheAllocator* allocator; |
| 329 | SimpleStringInternalCache cache; |
| 330 | MemoryAccountant accountant; |
| 331 | AccountingTestMemoryAllocator* accountingAllocator; |
| 332 | |
| 333 | void setup() |
| 334 | { |
| 335 | accountingAllocator = new AccountingTestMemoryAllocator(accountant, defaultMallocAllocator()); |
| 336 | allocator = new SimpleStringCacheAllocator(cache, accountingAllocator); |
| 337 | } |
| 338 | |
| 339 | void teardown() |
| 340 | { |
| 341 | cache.clearCache(); |
| 342 | delete allocator; |
| 343 | delete accountingAllocator; |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | TEST(SimpleStringCacheAllocator, allocationIsCached) |
| 348 | { |
| 349 | char* mem = allocator->alloc_memory(10, __FILE__, __LINE__); |
| 350 | allocator->free_memory(mem, 10, __FILE__, __LINE__); |
| 351 | |
| 352 | size_t totalAllocations = accountant.totalAllocations(); |
| 353 | size_t totalDeallocations = accountant.totalDeallocations(); |
| 354 | |
| 355 | mem = allocator->alloc_memory(10, __FILE__, __LINE__); |
| 356 | allocator->free_memory(mem, 10, __FILE__, __LINE__); |
| 357 | |
| 358 | LONGS_EQUAL(totalAllocations, accountant.totalAllocations()); |
| 359 | LONGS_EQUAL(totalDeallocations, accountant.totalDeallocations()); |
| 360 | } |
| 361 | |
| 362 | TEST(SimpleStringCacheAllocator, originalAllocator) |
| 363 | { |
| 364 | POINTERS_EQUAL(defaultMallocAllocator(), allocator->actualAllocator()); |
| 365 | STRCMP_EQUAL(defaultMallocAllocator()->alloc_name(), allocator->alloc_name()); |
| 366 | STRCMP_EQUAL(defaultMallocAllocator()->free_name(), allocator->free_name()); |
| 367 | } |
| 368 | |
| 369 | TEST(SimpleStringCacheAllocator, name) |
| 370 | { |
| 371 | STRCMP_EQUAL("SimpleStringCacheAllocator", allocator->name()); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | |
| 376 | TEST_GROUP(GlobalSimpleStringCache) |
| 377 | { |
| 378 | }; |
| 379 | |
| 380 | TEST(GlobalSimpleStringCache, installsAndRemovedCache) |
| 381 | { |
| 382 | TestMemoryAllocator* originalStringAllocator = SimpleString::getStringAllocator(); |
| 383 | { |
| 384 | GlobalSimpleStringCache cache; |
| 385 | STRCMP_EQUAL("SimpleStringCacheAllocator", SimpleString::getStringAllocator()->name()); |
| 386 | POINTERS_EQUAL(cache.getAllocator(), SimpleString::getStringAllocator()); |
| 387 | } |
| 388 | POINTERS_EQUAL(originalStringAllocator, SimpleString::getStringAllocator()); |
| 389 | } |
| 390 |