Make all NanoCoat types closeable; Make unrefing closeable the default.
[SquirrelJME.git] / nanocoat / tests / testAllocWeakRef.c
blob44ed73fcf791e11df7ea5808b93bde9f29f49ed2
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 /**
16 * Tests general creation of a weak reference.
18 * @since 2024/07/01
20 SJME_TEST_DECLARE(testAllocWeakRef)
22 sjme_alloc_weak weak;
23 sjme_alloc_weak weakSecond;
24 sjme_alloc_link* link;
25 sjme_pointer p;
27 /* Allocate weak reference. */
28 p = NULL;
29 weak = NULL;
30 if (sjme_error_is(sjme_alloc_weakNew(test->pool, 512,
31 NULL, NULL, &p, &weak)))
32 return sjme_unit_fail(test, "Failed to allocate weak?");
34 /* These should be set. */
35 sjme_unit_notEqualP(test, p, NULL,
36 "Did not set p?");
37 sjme_unit_notEqualP(test, weak, NULL,
38 "Did not set weak?");
40 /* This should be marked valid. */
41 sjme_unit_equalI(test,
42 sjme_atomic_sjme_jint_get(&weak->valid), SJME_ALLOC_WEAK_VALID,
43 "Weak reference not marked valid?");
45 /* Get the block link. */
46 link = NULL;
47 if (sjme_error_is(sjme_alloc_getLink(p, &link)))
48 return sjme_unit_fail(test, "Could not get block link?");
50 /* Should be this pointer. */
51 sjme_unit_equalP(test, weak->pointer, p,
52 "Weak pointer not allocated pointer?");
53 sjme_unit_equalP(test, weak->link, link,
54 "Weak pointer not allocated link?");
55 sjme_unit_equalP(test, link->weak, weak,
56 "Link weak not allocated weak?");
58 /* These should not be set. */
59 sjme_unit_equalP(test, weak->enqueue, NULL,
60 "Enqueue was set?");
61 sjme_unit_equalP(test, weak->enqueueData, NULL,
62 "Enqueue data was set?");
64 /* Reference count should be zero. */
65 sjme_unit_equalI(test, sjme_atomic_sjme_jint_get(&weak->count), 0,
66 "Reference count not one?");
68 /* Count up. */
69 weakSecond = NULL;
70 if (sjme_error_is(sjme_alloc_weakRef(p, &weakSecond)))
71 return sjme_unit_fail(test, "Could not weak ref count up?");
73 /* Weak reference should be returned. */
74 sjme_unit_equalP(test, weak, weakSecond,
75 "Different or un-passed weak pointer?");
77 /* Reference count should be one. */
78 sjme_unit_equalI(test, sjme_atomic_sjme_jint_get(&weak->count), 1,
79 "Reference count not one?");
81 /* Success! */
82 return SJME_TEST_RESULT_PASS;