Adjust sourcize.c to use _snprintf if using MSVC.
[SquirrelJME.git] / nanocoat / tests / testCloseableRefTwo.c
blob7de20fa6daad7c645a1a4ef6235ea83f0f38678c
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"
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;
24 /* Bump up count. */
25 closeCount++;
27 return SJME_ERROR_NONE;
30 /**
31 * Tests closeable with two references.
33 * @since 2024/09/28
35 SJME_TEST_DECLARE(testCloseableRefTwo)
37 sjme_closeable closing;
38 sjme_alloc_weak weak;
40 /* Make closeable. */
41 if (sjme_error_is(test->error = testCloseable_new(test->pool,
42 &closing, testHandler,
43 SJME_JNI_TRUE)))
44 return sjme_unit_fail(test, "Could not make closeable?");
46 /* Count up. */
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,
54 "Was closed 0???");
56 /* Close it. */
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,
62 "Was closed 1???");
64 /* Weak ref should still be valid */
65 weak = NULL;
66 if (sjme_error_is(test->error = sjme_alloc_weakRefGet(closing,
67 &weak)))
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),
72 "Wrong count?");
74 /* Close it. */
75 if (sjme_error_is(test->error = sjme_closeable_close(closing)))
76 return sjme_unit_fail(test, "Failed closing?");
78 /* Only once. */
79 sjme_unit_equalI(test, 1, closeCount,
80 "Not closed 2?");
82 /* Should not be a weak reference, as it is now gone! */
83 weak = NULL;
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?");
90 /* Success! */
91 return SJME_TEST_RESULT_PASS;