Skip to content

Commit

Permalink
test free
Browse files Browse the repository at this point in the history
  • Loading branch information
bertmelis committed Mar 1, 2024
1 parent c836003 commit 621460f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Fixed.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class Fixed {
if (b == ptr) return true;
b = *reinterpret_cast<unsigned char**>(b);
}
std::cout << reinterpret_cast<void*>(b) << " is allocated" << std::endl;
return false;
}
#endif
Expand Down
54 changes: 54 additions & 0 deletions test/test_FixInt/test_FixInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,63 @@ void mallocFull() {
TEST_ASSERT_EQUAL_UINT(0, pool.freeMemory());
}

void freePartial() {
const size_t nrBlocks = 4;
const size_t blocksize = sizeof(int);
size_t adjustedBlocksize = std::max(blocksize, blockHeadersize);
MemoryPool::Fixed<nrBlocks, blocksize> pool;

int* int1 = reinterpret_cast<int*>(pool.malloc());
int* int2 = reinterpret_cast<int*>(pool.malloc());
int* int3 = reinterpret_cast<int*>(pool.malloc());
int* int4 = reinterpret_cast<int*>(pool.malloc());
int* int5 = reinterpret_cast<int*>(pool.malloc());
pool.print();

(void) int1;
pool.free(int2);
pool.print();
TEST_ASSERT_EQUAL_UINT(1 * adjustedBlocksize, pool.freeMemory());
pool.free(int4);
pool.print();
TEST_ASSERT_EQUAL_UINT(2 * adjustedBlocksize, pool.freeMemory());
int* int5 = reinterpret_cast<int*>(pool.malloc(blocksize));
TEST_ASSERT_NOT_NULL(int5);
pool.print();
(void) int5;

TEST_ASSERT_EQUAL_UINT(1 * adjustedBlocksize, pool.freeMemory());
}

void freeEmpty() {
const size_t nrBlocks = 4;
const size_t blocksize = sizeof(int);
size_t adjustedBlocksize = std::max(blocksize, blockHeadersize);
MemoryPool::Fixed<nrBlocks, blocksize> pool;

int* int1 = reinterpret_cast<int*>(pool.malloc());
int* int2 = reinterpret_cast<int*>(pool.malloc());
int* int3 = reinterpret_cast<int*>(pool.malloc());
int* int4 = reinterpret_cast<int*>(pool.malloc());

pool.print();
pool.free(int1);
pool.print();
pool.free(int2);
pool.print();
pool.free(int3);
pool.print();
pool.free(int4);
pool.print();

TEST_ASSERT_EQUAL_UINT(nrBlocks * adjustedBlocksize, pool.freeMemory());
}

int main() {
UNITY_BEGIN();
RUN_TEST(emptyPool);
RUN_TEST(mallocFull);
RUN_TEST(freePartial);
RUN_TEST(freeEmpty);
return UNITY_END();
}

0 comments on commit 621460f

Please sign in to comment.