A blank base for heavier AWT components that would be needed by applets in the future...
[SquirrelJME.git] / nanocoat / tests / testAllocSplit.c
blobb7cd3f96d416c441b9f9419ed2e87cae9b798a1e
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include "proto.h"
11 #include "test.h"
12 #include "unit.h"
13 #include "sjme/alloc.h"
15 #define TEST_BLOCK_SIZE 64
17 /**
18 * Tests subsequent splitting of blocks when allocating.
20 * @since 2023/12/10
22 SJME_TEST_DECLARE(testAllocSplit)
24 void* chunk;
25 sjme_jint chunkLen;
26 sjme_alloc_pool* pool;
27 void* block;
28 sjme_alloc_link* initLink;
29 sjme_alloc_link* link;
30 sjme_alloc_link* next;
31 sjme_jint oldInitLinkBlockSize, initTotal, newTotal;
32 sjme_jint initReserved, newReserved;
34 /* Allocate data on the stack so it gets cleared. */
35 chunkLen = 32768;
36 chunk = sjme_alloca(chunkLen);
37 if (chunk == NULL)
38 return sjme_unit_skip(test, "Could not alloca(%d).",
39 (int)chunkLen);
41 /* Initialize the pool. */
42 pool = NULL;
43 if (sjme_error_is(sjme_alloc_poolInitStatic(&pool, chunk,
44 chunkLen)) || pool == NULL)
45 return sjme_unit_fail(test, "Could not initialize static pool?");
47 /* There should be a front and back link. */
48 sjme_unit_notEqualP(test, pool->frontLink, NULL,
49 "There is no front link?");
50 sjme_unit_notEqualP(test, pool->backLink, NULL,
51 "There is no back link?");
53 /* There should be a next to the front link, and it should not be the */
54 /* back link. Vice versa as well... */
55 sjme_unit_notEqualP(test, pool->frontLink->next, NULL,
56 "There is no next after the front link?");
57 sjme_unit_notEqualP(test, pool->frontLink->next, pool->backLink,
58 "Front link next is the back link?");
59 sjme_unit_notEqualP(test, pool->backLink->prev, NULL,
60 "There is no prev before the back link?");
61 sjme_unit_notEqualP(test, pool->backLink->prev, pool->frontLink,
62 "Back link prev is the front link?");
64 /* The front and back should point to the same link. */
65 sjme_unit_equalP(test, pool->frontLink->next, pool->backLink->prev,
66 "Different link in the middle?");
68 /* Get the main starting link. */
69 initLink = pool->frontLink->next;
70 oldInitLinkBlockSize = initLink->blockSize;
72 /* Determine the old initial total space. */
73 initTotal = 0;
74 initReserved = 0;
75 sjme_alloc_poolSpaceTotalSize(pool,
76 &initTotal, &initReserved, NULL);
77 sjme_unit_notEqualI(test, initTotal, 0,
78 "Pool indicates that it has zero space usage?");
80 /* Debug. */
81 sjme_message("Initial size is: %d (reserved %d)",
82 (int)initTotal, (int)initReserved);
84 /* Allocate some memory in the pool. */
85 block = NULL;
86 if (sjme_error_is(sjme_alloc(pool, TEST_BLOCK_SIZE,
87 &block)) || block == NULL)
88 return sjme_unit_fail(test, "Could not allocate %d bytes.",
89 TEST_BLOCK_SIZE);
91 /* Obtain the block link. */
92 link = NULL;
93 if (sjme_error_is(sjme_alloc_getLink(block, &link)) ||
94 link == NULL)
95 return sjme_unit_fail(test, "Could not obtain block link?");
97 /* The initial link should be this one. */
98 sjme_unit_equalP(test, link, initLink,
99 "Used different link from the first?");
101 /* The two links should be linked together. */
102 sjme_unit_equalP(test, link, link->next->prev,
103 "Link not linked back?");
105 /* The back link's prev should be the new link's next. */
106 sjme_unit_equalP(test, link->next, pool->backLink->prev,
107 "Back link is not link's next?");
108 sjme_unit_equalP(test, link->next->next, pool->backLink,
109 "Link's next is not the back link?");
111 /* The sizes of both links should be equal. */
112 next = link->next;
113 sjme_unit_equalI(test, oldInitLinkBlockSize,
114 link->blockSize + next->blockSize + SJME_SIZEOF_ALLOC_LINK(0),
115 "Block sizes do not add up?");
117 /* The next link should be free. */
118 sjme_unit_equalI(test, next->space, SJME_ALLOC_POOL_SPACE_FREE,
119 "Next link is not in the free space pool?");
121 /* Determine the new total space. */
122 newTotal = 0;
123 newReserved = 0;
124 sjme_alloc_poolSpaceTotalSize(pool,
125 &newTotal, &newReserved, NULL);
127 /* Debug. */
128 sjme_message("New size is: %d (reserved %d)",
129 (int)newTotal, (int)newReserved);
131 /* Since a new link was created, the reserved size should differ. */
132 sjme_unit_notEqualI(test, initReserved, newReserved,
133 "Reserved space did not change at all?");
135 /* These total sizes should add up the same. */
136 sjme_unit_equalI(test, initTotal, newTotal,
137 "Total sizes are different?");
139 /* Success! */
140 return SJME_TEST_RESULT_PASS;