| @@ -0,0 +1,120 @@ |
| 1 | +#include "Test.h" |
| 2 | + |
| 3 | +#include <MacMemory.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +enum |
| 8 | +{ |
| 9 | + initialSize = 64 * 1024, |
| 10 | + grownSize = 128 * 1024, |
| 11 | + blockerSize = 8 * 1024, |
| 12 | + blockerCount = 96, |
| 13 | + releasedBlockers = (grownSize / blockerSize) + 4, |
| 14 | + freeMemoryTolerance = 1024 |
| 15 | +}; |
| 16 | + |
| 17 | +int main(void) |
| 18 | +{ |
| 19 | + Size freeBefore = FreeMem(); |
| 20 | + unsigned char *allocation = malloc(initialSize); |
| 21 | + void *blockers[blockerCount]; |
| 22 | + unsigned char *grown; |
| 23 | + Size freeAfter; |
| 24 | + int failure = 0; |
| 25 | + int numBlockers = 0; |
| 26 | + int ok = allocation != NULL; |
| 27 | + int i; |
| 28 | + |
| 29 | + memset(blockers, 0, sizeof(blockers)); |
| 30 | + |
| 31 | + if(!ok) |
| 32 | + failure = 1; |
| 33 | + |
| 34 | + if(ok) |
| 35 | + { |
| 36 | + for(i = 0; i < initialSize; ++i) |
| 37 | + allocation[i] = (unsigned char)(i * 37 + 11); |
| 38 | + |
| 39 | + while(numBlockers < blockerCount && |
| 40 | + (blockers[numBlockers] = malloc(blockerSize)) != NULL) |
| 41 | + { |
| 42 | + memset(blockers[numBlockers], 0x5a, blockerSize); |
| 43 | + ++numBlockers; |
| 44 | + } |
| 45 | + |
| 46 | + if(numBlockers <= releasedBlockers) |
| 47 | + { |
| 48 | + ok = 0; |
| 49 | + failure = 6; |
| 50 | + } |
| 51 | + |
| 52 | + for(i = numBlockers - releasedBlockers; i < numBlockers; ++i) |
| 53 | + { |
| 54 | + if(i >= 0) |
| 55 | + { |
| 56 | + free(blockers[i]); |
| 57 | + blockers[i] = NULL; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + grown = realloc(allocation, grownSize); |
| 62 | + if(!grown) |
| 63 | + { |
| 64 | + ok = 0; |
| 65 | + failure = 2; |
| 66 | + } |
| 67 | + else if(grown == allocation) |
| 68 | + { |
| 69 | + ok = 0; |
| 70 | + failure = 3; |
| 71 | + } |
| 72 | + |
| 73 | + if(grown) |
| 74 | + { |
| 75 | + for(i = 0; ok && i < initialSize; ++i) |
| 76 | + { |
| 77 | + if(grown[i] != (unsigned char)(i * 37 + 11)) |
| 78 | + { |
| 79 | + ok = 0; |
| 80 | + failure = 4; |
| 81 | + } |
| 82 | + } |
| 83 | + free(grown); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + free(allocation); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + free(allocation); |
| 94 | + } |
| 95 | + |
| 96 | + for(i = 0; i < numBlockers; ++i) |
| 97 | + free(blockers[i]); |
| 98 | + |
| 99 | + freeAfter = FreeMem(); |
| 100 | + if(ok && freeAfter + freeMemoryTolerance < freeBefore) |
| 101 | + { |
| 102 | + ok = 0; |
| 103 | + failure = 5; |
| 104 | + } |
| 105 | + |
| 106 | + if(ok) |
| 107 | + { |
| 108 | + TEST_LOG_OK(); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + char result[3]; |
| 113 | + result[0] = 'N'; |
| 114 | + result[1] = (char)('0' + failure); |
| 115 | + result[2] = '\0'; |
| 116 | + TEST_LOG_SIZED(result, 2); |
| 117 | + } |
| 118 | + |
| 119 | + return ok ? 0 : 1; |
| 120 | +} |