1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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 // -------------------------------------------------------------------------*/
15 static sjme_errorCode
testEnqueue(
16 sjme_attrInNotNull sjme_alloc_weak weak
,
17 sjme_attrInNullable sjme_pointer data
,
18 sjme_attrInValue sjme_jboolean isBlockFree
)
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
;
34 * Tests freeing a weak reference by freeing the weak reference.
38 SJME_TEST_DECLARE(testAllocWeakDelete
)
41 sjme_alloc_weak wasWeak
;
42 sjme_alloc_link
* link
;
43 sjme_alloc_link
* weakLink
;
46 /* Allocate weak reference. */
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. */
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. */
60 if (sjme_error_is(sjme_alloc_getLink(weak
, &weakLink
)))
61 return sjme_unit_fail(test
, "Could not get weak link?");
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?");
88 return SJME_TEST_RESULT_PASS
;