Adjust sourcize.c to use _snprintf if using MSVC.
[SquirrelJME.git] / nanocoat / tests / testAtomic.c
blobf35512ea12b5f916fbbafd68a45838cb288e813b
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 "mock.h"
11 #include "proto.h"
12 #include "test.h"
13 #include "unit.h"
14 #include "sjme/atomic.h"
16 #define P(x) ((sjme_pointer)x)
18 /**
19 * Tests atomic values.
21 * @since 2024/01/09
23 SJME_TEST_DECLARE(testAtomic)
25 sjme_atomic_sjme_jint atomInt;
26 sjme_atomic_sjme_pointer atomPtr;
28 /* Seed to some invalid value. */
29 atomInt.value = 666;
31 /* Set value, implicit with no check. */
32 sjme_atomic_sjme_jint_set(&atomInt, 123);
34 /* Set, should have old value. */
35 sjme_unit_equalI(test,
36 123, sjme_atomic_sjme_jint_set(&atomInt, 456),
37 "Int old value was not set first?");
39 /* Add to it. */
40 sjme_unit_equalI(test,
41 456, sjme_atomic_sjme_jint_getAdd(&atomInt, 2),
42 "Int old value was not set second?");
44 /* Should have two added to it. */
45 sjme_unit_equalI(test,
46 458, sjme_atomic_sjme_jint_get(&atomInt),
47 "Atomic add did not add?");
49 /* Try changing it but failing. */
50 sjme_unit_equalZ(test, SJME_JNI_FALSE,
51 sjme_atomic_sjme_jint_compareSet(&atomInt,
52 666, 123),
53 "Int compare mismatch failed?");
55 /* Should still be old value. */
56 sjme_unit_equalI(test,
57 458, sjme_atomic_sjme_jint_get(&atomInt),
58 "Atomic was a different value?");
60 /* Then change it but pass. */
61 sjme_unit_equalZ(test, SJME_JNI_TRUE,
62 sjme_atomic_sjme_jint_compareSet(&atomInt,
63 458, 123),
64 "Int compare matched failed?");
66 /* Should be the new value. */
67 sjme_unit_equalI(test,
68 123, sjme_atomic_sjme_jint_get(&atomInt),
69 "Int compared value not set?");
71 /* Seed to some invalid value. */
72 atomPtr.value = P(666);
74 /* Set value, implicit with no check. */
75 sjme_atomic_sjme_pointer_set(&atomPtr, P(123));
77 /* Set, should have old value. */
78 sjme_unit_equalP(test,
79 P(123), sjme_atomic_sjme_pointer_set(&atomPtr, P(456)),
80 "Pointer old value was not set first?");
82 /* Add to it. */
83 sjme_unit_equalP(test,
84 P(456), sjme_atomic_sjme_pointer_getAdd(&atomPtr, 2),
85 "Pointer old value was not set second?");
87 /* Try changing it but failing. */
88 sjme_unit_equalZ(test, SJME_JNI_FALSE,
89 sjme_atomic_sjme_pointer_compareSet(&atomPtr,
90 P(666), P(123)),
91 "Pointer compare mismatch failed?");
93 /* Then change it but pass. */
94 sjme_unit_equalZ(test, SJME_JNI_TRUE,
95 sjme_atomic_sjme_pointer_compareSet(&atomPtr,
96 P(458), P(123)),
97 "Pointer compare matched failed?");
99 /* Should be the new value. */
100 sjme_unit_equalP(test,
101 P(123), sjme_atomic_sjme_pointer_get(&atomPtr),
102 "Pointer compared value not set?");
104 /* Success! */
105 return SJME_TEST_RESULT_PASS;