From 805380156ff8d442a18f5b8d4d127a05574ea575 Mon Sep 17 00:00:00 2001 From: Stephanie Gawroriski Date: Mon, 11 Dec 2023 15:25:27 +0000 Subject: [PATCH] Implement test for block splitting. --- .../.idea/inspectionProfiles/Project_Default.xml | 1 + nanocoat/tests/testAllocSplit.c | 77 +++++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/nanocoat/.idea/inspectionProfiles/Project_Default.xml b/nanocoat/.idea/inspectionProfiles/Project_Default.xml index 6facc11697..7f8290b055 100644 --- a/nanocoat/.idea/inspectionProfiles/Project_Default.xml +++ b/nanocoat/.idea/inspectionProfiles/Project_Default.xml @@ -2,5 +2,6 @@ \ No newline at end of file diff --git a/nanocoat/tests/testAllocSplit.c b/nanocoat/tests/testAllocSplit.c index 0d782f3185..8443b6351e 100644 --- a/nanocoat/tests/testAllocSplit.c +++ b/nanocoat/tests/testAllocSplit.c @@ -25,7 +25,11 @@ SJME_TEST_DECLARE(testAllocSplit) sjme_jint chunkLen; sjme_alloc_pool* pool; void* block; + sjme_alloc_link* initLink; sjme_alloc_link* link; + sjme_alloc_link* next; + sjme_alloc_link* prev; + sjme_jint oldInitLinkBlockSize, oldInitTotal, i, newTotal; /* Allocate data on the stack so it gets cleared. */ chunkLen = 32768; @@ -40,6 +44,39 @@ SJME_TEST_DECLARE(testAllocSplit) chunkLen)) || pool == NULL) return sjme_unitFail(test, "Could not initialize static pool?"); + /* There should be a front and back link. */ + sjme_unitNotEqualP(test, pool->frontLink, NULL, + "There is no front link?"); + sjme_unitNotEqualP(test, pool->backLink, NULL, + "There is no back link?"); + + /* There should be a next to the front link, and it should not be the */ + /* back link. Vice versa as well... */ + sjme_unitNotEqualP(test, pool->frontLink->next, NULL, + "There is no next after the front link?"); + sjme_unitNotEqualP(test, pool->frontLink->next, pool->backLink, + "Front link next is the back link?"); + sjme_unitNotEqualP(test, pool->backLink->prev, NULL, + "There is no prev before the back link?"); + sjme_unitNotEqualP(test, pool->backLink->prev, pool->frontLink, + "Back link prev is the front link?"); + + /* The front and back should point to the same link. */ + sjme_unitEqualP(test, pool->frontLink->next, pool->backLink->prev, + "Different link in the middle?"); + + /* Get the main starting link. */ + initLink = pool->frontLink->next; + oldInitLinkBlockSize = initLink->blockSize; + + /* Determine the old initial total space. */ + oldInitTotal = 0; + for (i = 0; i < SJME_NUM_ALLOC_POOL_SPACE; i++) + { + oldInitTotal += pool->space[i].usable; + oldInitTotal += pool->space[i].reserved; + } + /* Allocate some memory in the pool. */ block = NULL; if (SJME_IS_ERROR(sjme_alloc(pool, TEST_BLOCK_SIZE, @@ -53,6 +90,42 @@ SJME_TEST_DECLARE(testAllocSplit) link == NULL) return sjme_unitFail(test, "Could not obtain block link?"); - sjme_todo("Implement %s", __func__); - return SJME_TEST_RESULT_FAIL; + /* The initial link should be this one. */ + sjme_unitEqualP(test, link, initLink, + "Used different link from the first?"); + + /* The two links should be linked together. */ + sjme_unitEqualP(test, link, link->next->prev, + "Link not linked back?"); + + /* The back link's prev should be the new link's next. */ + sjme_unitEqualP(test, link->next, pool->backLink->prev, + "Back link is not link's next?"); + sjme_unitEqualP(test, link->next->next, pool->backLink, + "Link's next is not the back link?"); + + /* The sizes of both links should be equal. */ + next = link->next; + sjme_unitEqualI(test, oldInitLinkBlockSize, + link->blockSize + next->blockSize + SJME_SIZEOF_ALLOC_LINK(0), + "Block sizes do not add up?"); + + /* The next link should be free. */ + sjme_unitEqualI(test, next->space, SJME_ALLOC_POOL_SPACE_FREE, + "Next link is not in the free space pool?"); + + /* Determine the new total space. */ + newTotal = 0; + for (i = 0; i < SJME_NUM_ALLOC_POOL_SPACE; i++) + { + newTotal += pool->space[i].usable; + newTotal += pool->space[i].reserved; + } + + /* These total sizes should add up the same. */ + sjme_unitEqualI(test, oldInitTotal, newTotal, + "Total sizes are different?"); + + /* Success! */ + return SJME_TEST_RESULT_PASS; } -- 2.11.4.GIT