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 // -------------------------------------------------------------------------*/
16 * Tests counting of weak references.
20 SJME_TEST_DECLARE(testAllocWeakCounting
)
23 sjme_alloc_weak weakB
;
26 /* Allocate weak reference. */
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. */
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?");
72 return SJME_TEST_RESULT_PASS
;