Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / glib / tests / refcount.c
blobdfccc92c74b26abf5a2036212af62ffe25302b51
1 /* refcount.c: Tests for reference counting types
3 * Copyright 2018 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <glib.h>
22 /* test_grefcount: test the behavior of the grefcount API */
23 static void
24 test_grefcount (void)
26 grefcount a, b;
28 /* init(a): 1 */
29 g_ref_count_init (&a);
30 if (g_test_verbose ())
31 g_test_message ("init(a) := %d\n", (int) a);
32 g_assert_true (g_ref_count_compare (&a, 1));
34 /* inc(a): 2 */
35 g_ref_count_inc (&a);
36 if (g_test_verbose ())
37 g_test_message ("inc(a) := %d\n", (int) a);
38 g_assert_false (g_ref_count_compare (&a, 1));
39 g_assert_false (g_ref_count_compare (&a, G_MAXINT));
41 /* b = a = 2 */
42 b = a;
43 if (g_test_verbose ())
44 g_test_message ("a := %d, b := %d\n", (int) a, (int) b);
46 /* inc(a): 3 */
47 g_ref_count_inc (&a);
48 if (g_test_verbose ())
49 g_test_message ("inc(a) := %d\n", (int) a);
51 /* dec(b) = 1 */
52 if (g_test_verbose ())
53 g_test_message ("dec(b) := %d + 1\n", (int) b);
54 g_assert_false (g_ref_count_dec (&b));
56 /* dec(a) = 2 */
57 if (g_test_verbose ())
58 g_test_message ("dec(a) := %d + 1\n", (int) a);
59 g_assert_false (g_ref_count_dec (&a));
61 /* dec(b) = 0 */
62 if (g_test_verbose ())
63 g_test_message ("dec(b) := %d + 1\n", (int) b);
64 g_assert_true (g_ref_count_dec (&b));
66 /* dec(a) = 1 */
67 if (g_test_verbose ())
68 g_test_message ("dec(a) := %d + 1\n", (int) a);
69 g_assert_false (g_ref_count_dec (&a));
71 /* dec(a) = 0 */
72 if (g_test_verbose ())
73 g_test_message ("dec(a) := %d + 1\n", (int) a);
74 g_assert_true (g_ref_count_dec (&a));
77 /* test_grefcount_saturation: Saturating a grefcount counter
78 * does not cause an overflow; additionally, if we're building
79 * with checks enabled, it'll cause a warning
81 static void
82 test_grefcount_saturation (void)
84 if (g_test_subprocess ())
86 grefcount a;
88 /* We're breaking abstraction here for convenience */
89 a = G_MININT + 1;
91 g_ref_count_inc (&a);
92 g_assert_true (a == G_MININT);
94 g_ref_count_inc (&a);
95 g_assert_true (a == G_MININT);
97 exit (0);
100 g_test_trap_subprocess (NULL, 0, 0);
102 #ifndef G_DISABLE_CHECKS
103 /* Ensure that we got a warning when building with checks; the
104 * test will fail because of the critical warning being caught
105 * by GTest
107 g_test_trap_assert_failed ();
108 g_test_trap_assert_stderr ("*saturation*");
109 #else
110 /* With checks disabled we don't get any warning */
111 g_test_trap_assert_passed ();
112 #endif
115 /* test_gatomicrefcount: test the behavior of the gatomicrefcount API */
116 static void
117 test_gatomicrefcount (void)
119 gatomicrefcount a, b;
121 /* init(a): 1 */
122 g_atomic_ref_count_init (&a);
123 if (g_test_verbose ())
124 g_test_message ("init(a) := %d\n", (int) a);
125 g_assert_true (g_atomic_ref_count_compare (&a, 1));
127 /* inc(a): 2 */
128 g_atomic_ref_count_inc (&a);
129 if (g_test_verbose ())
130 g_test_message ("inc(a) := %d\n", (int) a);
131 g_assert_false (g_atomic_ref_count_compare (&a, 1));
132 g_assert_false (g_atomic_ref_count_compare (&a, G_MAXINT));
134 /* b = a = 2 */
135 b = a;
136 if (g_test_verbose ())
137 g_test_message ("a := %d, b := %d\n", (int) a, (int) b);
139 /* inc(a): 3 */
140 g_atomic_ref_count_inc (&a);
141 if (g_test_verbose ())
142 g_test_message ("inc(a) := %d\n", (int) a);
144 /* dec(b) = 1 */
145 if (g_test_verbose ())
146 g_test_message ("dec(b) := %d + 1\n", (int) b);
147 g_assert_false (g_atomic_ref_count_dec (&b));
149 /* dec(a) = 2 */
150 if (g_test_verbose ())
151 g_test_message ("dec(a) := %d + 1\n", (int) a);
152 g_assert_false (g_atomic_ref_count_dec (&a));
154 /* dec(b) = 0 */
155 if (g_test_verbose ())
156 g_test_message ("dec(b) := %d + 1\n", (int) b);
157 g_assert_true (g_atomic_ref_count_dec (&b));
159 /* dec(a) = 1 */
160 if (g_test_verbose ())
161 g_test_message ("dec(a) := %d + 1\n", (int) a);
162 g_assert_false (g_atomic_ref_count_dec (&a));
164 /* dec(a) = 0 */
165 if (g_test_verbose ())
166 g_test_message ("dec(a) := %d + 1\n", (int) a);
167 g_assert_true (g_atomic_ref_count_dec (&a));
170 /* test_grefcount_saturation: Saturating a gatomicrefcount counter
171 * does not cause an overflow; additionally, if we're building
172 * with checks enabled, it'll cause a warning
174 static void
175 test_gatomicrefcount_saturation (void)
177 if (g_test_subprocess ())
179 gatomicrefcount a;
181 /* We're breaking abstraction here for convenience */
182 a = G_MAXINT - 1;
184 g_atomic_ref_count_inc (&a);
185 g_assert_true (a == G_MAXINT);
187 g_atomic_ref_count_inc (&a);
188 g_assert_true (a == G_MAXINT);
190 exit (0);
193 g_test_trap_subprocess (NULL, 0, 0);
195 #ifndef G_DISABLE_CHECKS
196 /* Ensure that we got a warning when building with checks; the
197 * test will fail because of the critical warning being caught
198 * by GTest
200 g_test_trap_assert_failed ();
201 g_test_trap_assert_stderr ("*saturation*");
202 #else
203 /* With checks disabled we don't get any warning */
204 g_test_trap_assert_passed ();
205 #endif
209 main (int argc,
210 char *argv[])
212 g_test_init (&argc, &argv, NULL);
214 g_test_add_func ("/refcount/grefcount", test_grefcount);
215 g_test_add_func ("/refcount/grefcount/saturation", test_grefcount_saturation);
217 g_test_add_func ("/refcount/gatomicrefcount", test_gatomicrefcount);
218 g_test_add_func ("/refcount/gatomicrefcount/saturation", test_gatomicrefcount_saturation);
220 return g_test_run ();