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/>.
22 /* test_grefcount: test the behavior of the grefcount API */
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));
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
));
43 if (g_test_verbose ())
44 g_test_message ("a := %d, b := %d\n", (int) a
, (int) b
);
48 if (g_test_verbose ())
49 g_test_message ("inc(a) := %d\n", (int) a
);
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
));
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
));
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
));
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
));
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
82 test_grefcount_saturation (void)
84 if (g_test_subprocess ())
88 /* We're breaking abstraction here for convenience */
92 g_assert_true (a
== G_MININT
);
95 g_assert_true (a
== G_MININT
);
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
107 g_test_trap_assert_failed ();
108 g_test_trap_assert_stderr ("*saturation*");
110 /* With checks disabled we don't get any warning */
111 g_test_trap_assert_passed ();
115 /* test_gatomicrefcount: test the behavior of the gatomicrefcount API */
117 test_gatomicrefcount (void)
119 gatomicrefcount a
, b
;
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));
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
));
136 if (g_test_verbose ())
137 g_test_message ("a := %d, b := %d\n", (int) a
, (int) b
);
140 g_atomic_ref_count_inc (&a
);
141 if (g_test_verbose ())
142 g_test_message ("inc(a) := %d\n", (int) a
);
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
));
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
));
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
));
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
));
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
175 test_gatomicrefcount_saturation (void)
177 if (g_test_subprocess ())
181 /* We're breaking abstraction here for convenience */
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
);
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
200 g_test_trap_assert_failed ();
201 g_test_trap_assert_stderr ("*saturation*");
203 /* With checks disabled we don't get any warning */
204 g_test_trap_assert_passed ();
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 ();