Compile fixes.
[SquirrelJME.git] / nanocoat / tests / testAllocWeakCounting.c
blob8fd984e551a8f05b7a5eb0fb8299bf165356c951
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 counting of weak references.
18 * @since 2024/07/01
20 SJME_TEST_DECLARE(testAllocWeakCounting)
22 sjme_alloc_weak weak;
23 sjme_alloc_weak weakB;
24 sjme_pointer p;
26 /* Allocate weak reference. */
27 p = NULL;
28 weak = NULL;
29 if (sjme_error_is(sjme_alloc_weakNew(test->pool, 512,
30 NULL, NULL, &p, &weak)))
31 return sjme_unit_fail(test, "Failed to allocate weak?");
33 /* Reference count should be one. */
34 sjme_unit_equalI(test, sjme_atomic_sjme_jint_get(&weak->count), 1,
35 "Reference count not one?");
37 /* Reference again. */
38 weakB = NULL;
39 if (sjme_error_is(sjme_alloc_weakRef(p, &weakB)))
40 return sjme_unit_fail(test, "Could not re-ref weak?");
42 /* Reference count should be two. */
43 sjme_unit_equalI(test, sjme_atomic_sjme_jint_get(&weak->count), 2,
44 "Reference count not two?");
46 /* Delete weak reference. */
47 if (sjme_error_is(sjme_alloc_weakDelete(&weakB)))
48 return sjme_unit_fail(test, "Could not delete weak?");
50 /* Since it was two, it should still be there. */
51 sjme_unit_equalP(test, weakB, weak,
52 "Second weak pointer changed?");
54 /* Reference count should be one. */
55 sjme_unit_equalI(test, sjme_atomic_sjme_jint_get(&weak->count), 1,
56 "Reference count not one?");
58 /* Delete weak reference again. */
59 if (sjme_error_is(sjme_alloc_weakDelete(&weakB)))
60 return sjme_unit_fail(test, "Could not delete weak?");
62 /* Since it was one, it should be gone now. */
63 sjme_unit_equalP(test, weakB, NULL,
64 "Weak pointer on last delete not cleared?");
66 /* This should be marked invalid. */
67 sjme_unit_notEqualI(test,
68 sjme_atomic_sjme_jint_get(&weak->valid), SJME_ALLOC_WEAK_VALID,
69 "Weak reference not marked invalid?");
71 /* Success! */
72 return SJME_TEST_RESULT_PASS;