Make all NanoCoat types closeable; Make unrefing closeable the default.
[SquirrelJME.git] / nanocoat / tests / testAllocWeakDelete.c
blob14039182d382bc93e8a822a6cdcc30ded709bc2f
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 "test.h"
11 #include "proto.h"
12 #include "mock.h"
13 #include "unit.h"
15 static sjme_errorCode testEnqueue(
16 sjme_attrInNotNull sjme_alloc_weak weak,
17 sjme_attrInNullable sjme_pointer data,
18 sjme_attrInValue sjme_jboolean isBlockFree)
20 sjme_test* test;
22 test = data;
24 test->global = data;
26 /* Should not be free block free. */
27 sjme_unit_equalZ(test, isBlockFree, SJME_JNI_FALSE,
28 "Was from block free?");
30 return SJME_ERROR_NONE;
33 /**
34 * Tests freeing a weak reference by freeing the weak reference.
36 * @since 2024/07/01
38 SJME_TEST_DECLARE(testAllocWeakDelete)
40 sjme_alloc_weak weak;
41 sjme_alloc_weak wasWeak;
42 sjme_alloc_link* link;
43 sjme_alloc_link* weakLink;
44 sjme_pointer p;
46 /* Allocate weak reference. */
47 p = NULL;
48 weak = NULL;
49 if (sjme_error_is(sjme_alloc_weakNew(test->pool, 512,
50 testEnqueue, test, &p, &weak)))
51 return sjme_unit_fail(test, "Failed to allocate weak?");
53 /* Get the block link. */
54 link = NULL;
55 if (sjme_error_is(sjme_alloc_getLink(p, &link)))
56 return sjme_unit_fail(test, "Could not get block link?");
58 /* Get the weak link. */
59 weakLink = NULL;
60 if (sjme_error_is(sjme_alloc_getLink(weak, &weakLink)))
61 return sjme_unit_fail(test, "Could not get weak link?");
63 /* Delete it. */
64 wasWeak = weak;
65 test->global = NULL;
66 if (sjme_error_is(sjme_alloc_weakDelete(&weak)))
67 return sjme_unit_fail(test, "Could not delete weak?");
69 /* Should be cleared and enqueue called. */
70 sjme_unit_equalP(test, test->global, test,
71 "Enqueue was not called?");
72 sjme_unit_equalP(test, weak, NULL,
73 "Weak reference not set to NULL?");
75 /* Weak reference should be invalid. */
76 sjme_unit_notEqualI(test,
77 sjme_atomic_sjme_jint_get(&wasWeak->valid),
78 SJME_ALLOC_WEAK_VALID,
79 "Weak reference not marked invalid?");
81 /* Block and weak links should be free too. */
82 sjme_unit_notEqualI(test, link->space, SJME_ALLOC_POOL_SPACE_USED,
83 "Block link is used?");
84 sjme_unit_notEqualI(test, weakLink->space, SJME_ALLOC_POOL_SPACE_USED,
85 "Weak link is used?");
87 /* Success! */
88 return SJME_TEST_RESULT_PASS;