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 "sjme/atomic.h"
16 #define P(x) ((sjme_pointer)x)
19 * Tests atomic values.
23 SJME_TEST_DECLARE(testAtomic
)
25 sjme_atomic_sjme_jint atomInt
;
26 sjme_atomic_sjme_pointer atomPtr
;
28 /* Seed to some invalid value. */
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?");
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
,
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
,
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?");
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
,
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
,
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?");
105 return SJME_TEST_RESULT_PASS
;