1 /* Unit tests for GPrivate and friends
2 * Copyright (C) 2011 Red Hat, Inc
3 * Author: Matthias Clasen
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
26 * - initial value is NULL
27 * - set/get work repeatedly
35 private1 = g_private_new (NULL);
37 value = g_private_get (private1);
38 g_assert (value == NULL);
40 g_private_set (private1, GINT_TO_POINTER(1));
41 value = g_private_get (private1);
42 g_assert_cmpint (GPOINTER_TO_INT (value), ==, 1);
44 g_private_set (private1, GINT_TO_POINTER(2));
45 value = g_private_get (private1);
46 g_assert_cmpint (GPOINTER_TO_INT (value), ==, 2);
49 static GPrivate *private2;
50 static gint private2_destroy_count;
53 private2_destroy (gpointer data)
55 g_atomic_int_inc (&private2_destroy_count);
59 private2_func (gpointer data)
61 gint value = GPOINTER_TO_INT (data);
65 for (i = 0; i < 1000; i++)
68 g_private_set (private2, GINT_TO_POINTER(v));
70 v2 = GPOINTER_TO_INT(g_private_get (private2));
71 g_assert_cmpint (v, ==, v2);
81 * - threads do not interfere with each other
82 * - destroy notifies are called for each thread exit
83 * - destroy notifies are called for g_thread_exit() too
84 * - destroy notifies are not called on g_private_set()
92 private2 = g_private_new (private2_destroy);
94 for (i = 0; i < 10; i++)
95 thread[i] = g_thread_create (private2_func, GINT_TO_POINTER (i), TRUE, NULL);
97 for (i = 0; i < 10; i++)
98 g_thread_join (thread[i]);
100 g_assert_cmpint (private2_destroy_count, ==, 10);
104 * - static initialization works
105 * - initial value is NULL
106 * - get/set works repeatedly
108 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
111 test_static_private1 (void)
115 value = g_static_private_get (&sp1);
116 g_assert (value == NULL);
118 g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
119 value = g_static_private_get (&sp1);
120 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
122 g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
123 value = g_static_private_get (&sp1);
124 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
126 g_static_private_free (&sp1);
128 value = g_static_private_get (&sp1);
129 g_assert (value == NULL);
132 static gint sp2_destroy_count;
135 sp2_destroy (gpointer data)
141 sp2_destroy2 (gpointer data)
143 gint value = GPOINTER_TO_INT (data);
145 g_assert_cmpint (value, ==, 2);
148 /* test that destroy notifies are called as expected
149 * and on the right values
152 test_static_private2 (void)
157 g_static_private_init (&sp2);
159 value = g_static_private_get (&sp2);
160 g_assert (value == NULL);
162 g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
163 g_assert_cmpint (sp2_destroy_count, ==, 0);
164 value = g_static_private_get (&sp2);
165 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
167 g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
168 g_assert_cmpint (sp2_destroy_count, ==, 1);
169 value = g_static_private_get (&sp2);
170 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
172 g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
173 g_assert_cmpint (sp2_destroy_count, ==, 1);
174 value = g_static_private_get (&sp2);
175 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
177 g_static_private_free (&sp2);
179 value = g_static_private_get (&sp2);
180 g_assert (value == NULL);
183 /* test that freeing and reinitializing a static private
184 * drops previous value
187 test_static_private3 (void)
192 g_static_private_init (&sp3);
194 value = g_static_private_get (&sp3);
195 g_assert (value == NULL);
197 g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
198 value = g_static_private_get (&sp3);
199 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
201 g_static_private_free (&sp3);
202 g_static_private_init (&sp3);
204 value = g_static_private_get (&sp3);
205 g_assert (value == NULL);
207 g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
208 value = g_static_private_get (&sp3);
209 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
211 g_static_private_free (&sp3);
214 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
217 sp4_func (gpointer data)
219 gint value = GPOINTER_TO_INT (data);
223 for (i = 0; i < 1000; i++)
226 g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
228 v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
229 g_assert_cmpint (v, ==, v2);
233 g_thread_exit (NULL);
238 /* test that threads do not interfere with each other
241 test_static_private4 (void)
246 for (i = 0; i < 10; i++)
247 thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
249 for (i = 0; i < 10; i++)
250 g_thread_join (thread[i]);
252 g_static_private_free (&sp4);
255 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
256 static GMutex m5 = G_MUTEX_INIT;
257 static GCond c5a = G_COND_INIT;
258 static GCond c5b = G_COND_INIT;
262 sp5_func (gpointer data)
264 gint v = GPOINTER_TO_INT (data);
267 value = g_static_private_get (&sp5);
268 g_assert (value == NULL);
270 g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
271 value = g_static_private_get (&sp5);
272 g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
274 if (g_test_verbose ())
275 g_print ("thread %d set sp5\n", v);
277 g_atomic_int_inc (&count5);
278 g_cond_signal (&c5a);
279 g_cond_wait (&c5b, &m5);
280 g_mutex_unlock (&m5);
282 if (g_test_verbose ())
283 g_print ("thread %d get sp5\n", v);
284 value = g_static_private_get (&sp5);
285 g_assert (value == NULL);
291 test_static_private5 (void)
296 g_atomic_int_set (&count5, 0);
298 for (i = 0; i < 10; i++)
299 thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
302 while (g_atomic_int_get (&count5) < 10)
303 g_cond_wait (&c5a, &m5);
305 if (g_test_verbose ())
306 g_print ("sp5 gets nuked\n");
308 g_static_private_free (&sp5);
310 g_cond_broadcast (&c5b);
311 g_mutex_unlock (&m5);
313 for (i = 0; i < 10; i++)
314 g_thread_join (thread[i]);
322 main (int argc, char *argv[])
324 g_test_init (&argc, &argv, NULL);
326 g_assert (g_thread_get_initialized ());
328 g_test_add_func ("/thread/private1", test_private1);
329 g_test_add_func ("/thread/private2", test_private2);
330 g_test_add_func ("/thread/staticprivate1", test_static_private1);
331 g_test_add_func ("/thread/staticprivate2", test_static_private2);
332 g_test_add_func ("/thread/staticprivate3", test_static_private3);
333 g_test_add_func ("/thread/staticprivate4", test_static_private4);
334 g_test_add_func ("/thread/staticprivate5", test_static_private5);
336 return g_test_run ();