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 // -------------------------------------------------------------------------*/
14 #include "testCloseable.h"
16 static sjme_jint closeCount
= 0;
18 static sjme_errorCode
testHandler(
19 sjme_attrInNotNull sjme_closeable closeable
)
21 if (closeable
== NULL
)
22 return SJME_ERROR_NULL_ARGUMENTS
;
27 return SJME_ERROR_NONE
;
31 * Tests closeable with two references.
35 SJME_TEST_DECLARE(testCloseableRefTwo
)
37 sjme_closeable closing
;
41 if (sjme_error_is(test
->error
= testCloseable_new(test
->pool
,
42 &closing
, testHandler
,
44 return sjme_unit_fail(test
, "Could not make closeable?");
47 if (sjme_error_is(test
->error
= sjme_alloc_weakRef(closing
, NULL
)))
48 return sjme_unit_fail(test
, "Could not count 1?");
49 if (sjme_error_is(test
->error
= sjme_alloc_weakRef(closing
, NULL
)))
50 return sjme_unit_fail(test
, "Could not count 2?");
52 /* Should not be closed. */
53 sjme_unit_equalI(test
, 0, closeCount
,
57 if (sjme_error_is(test
->error
= sjme_closeable_close(closing
)))
58 return sjme_unit_fail(test
, "Failed closing?");
60 /* Should not be closed. */
61 sjme_unit_equalI(test
, 0, closeCount
,
64 /* Weak ref should still be valid */
66 if (sjme_error_is(test
->error
= sjme_alloc_weakRefGet(closing
,
68 return sjme_unit_fail(test
, "Could not get weak ref");
69 sjme_unit_notEqualP(test
, NULL
, weak
,
70 "No weak reference returned?");
71 sjme_unit_equalI(test
, 1, sjme_atomic_sjme_jint_get(&weak
->count
),
75 if (sjme_error_is(test
->error
= sjme_closeable_close(closing
)))
76 return sjme_unit_fail(test
, "Failed closing?");
79 sjme_unit_equalI(test
, 1, closeCount
,
82 /* Should not be a weak reference, as it is now gone! */
84 test
->error
= sjme_alloc_weakRefGet(closing
, &weak
);
85 sjme_unit_equalI(test
, SJME_ERROR_NOT_WEAK_REFERENCE
, test
->error
,
86 "Get of weak did not fail?");
87 sjme_unit_equalP(test
, NULL
, weak
,
88 "There was a weak pointer returned?");
91 return SJME_TEST_RESULT_PASS
;