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.
23 /* We are testing some deprecated APIs here */
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 * - initial value is NULL
30 * - set/get work repeatedly
35 static GPrivate private = G_PRIVATE_INIT (NULL);
38 value = g_private_get (&private);
39 g_assert (value == NULL);
41 g_private_set (&private, GINT_TO_POINTER(1));
42 value = g_private_get (&private);
43 g_assert_cmpint (GPOINTER_TO_INT (value), ==, 1);
45 g_private_set (&private, GINT_TO_POINTER(2));
46 value = g_private_get (&private);
47 g_assert_cmpint (GPOINTER_TO_INT (value), ==, 2);
50 static GPrivate *private2;
51 static gint private2_destroy_count;
54 private2_destroy (gpointer data)
56 g_atomic_int_inc (&private2_destroy_count);
60 private2_func (gpointer data)
62 gint value = GPOINTER_TO_INT (data);
66 for (i = 0; i < 1000; i++)
69 g_private_set (private2, GINT_TO_POINTER(v));
71 v2 = GPOINTER_TO_INT(g_private_get (private2));
72 g_assert_cmpint (v, ==, v2);
82 * - threads do not interfere with each other
83 * - destroy notifies are called for each thread exit
84 * - destroy notifies are called for g_thread_exit() too
85 * - destroy notifies are not called on g_private_set()
86 * - destroy notifies are called on g_private_replace()
94 private2 = g_private_new (private2_destroy);
96 g_private_set (private2, GINT_TO_POINTER(234));
97 g_private_replace (private2, GINT_TO_POINTER(123));
99 for (i = 0; i < 10; i++)
100 thread[i] = g_thread_create (private2_func, GINT_TO_POINTER (i), TRUE, NULL);
102 for (i = 0; i < 10; i++)
103 g_thread_join (thread[i]);
105 g_assert_cmpint (private2_destroy_count, ==, 11);
108 static gboolean private3_freed;
111 private3_free (gpointer data)
113 g_assert (data == (void*) 0x1234);
114 private3_freed = TRUE;
121 static guint __stdcall
127 private3_func (gpointer data)
129 static GPrivate key = G_PRIVATE_INIT (private3_free);
131 g_private_set (&key, (void *) 0x1234);
139 g_assert (!private3_freed);
145 thread = (HANDLE) _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
146 WaitForSingleObject (thread, INFINITE);
147 CloseHandle (thread);
153 pthread_create (&thread, NULL, private3_func, NULL);
154 pthread_join (thread, NULL);
157 g_assert (private3_freed);
161 * - static initialization works
162 * - initial value is NULL
163 * - get/set works repeatedly
165 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
168 test_static_private1 (void)
172 value = g_static_private_get (&sp1);
173 g_assert (value == NULL);
175 g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
176 value = g_static_private_get (&sp1);
177 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
179 g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
180 value = g_static_private_get (&sp1);
181 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
183 g_static_private_free (&sp1);
185 value = g_static_private_get (&sp1);
186 g_assert (value == NULL);
189 static gint sp2_destroy_count;
192 sp2_destroy (gpointer data)
198 sp2_destroy2 (gpointer data)
200 gint value = GPOINTER_TO_INT (data);
202 g_assert_cmpint (value, ==, 2);
205 /* test that destroy notifies are called as expected
206 * and on the right values
209 test_static_private2 (void)
214 g_static_private_init (&sp2);
216 value = g_static_private_get (&sp2);
217 g_assert (value == NULL);
219 g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
220 g_assert_cmpint (sp2_destroy_count, ==, 0);
221 value = g_static_private_get (&sp2);
222 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
224 g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
225 g_assert_cmpint (sp2_destroy_count, ==, 1);
226 value = g_static_private_get (&sp2);
227 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
229 g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
230 g_assert_cmpint (sp2_destroy_count, ==, 1);
231 value = g_static_private_get (&sp2);
232 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
234 g_static_private_free (&sp2);
236 value = g_static_private_get (&sp2);
237 g_assert (value == NULL);
240 /* test that freeing and reinitializing a static private
241 * drops previous value
244 test_static_private3 (void)
249 g_static_private_init (&sp3);
251 value = g_static_private_get (&sp3);
252 g_assert (value == NULL);
254 g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
255 value = g_static_private_get (&sp3);
256 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
258 g_static_private_free (&sp3);
259 g_static_private_init (&sp3);
261 value = g_static_private_get (&sp3);
262 g_assert (value == NULL);
264 g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
265 value = g_static_private_get (&sp3);
266 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
268 g_static_private_free (&sp3);
271 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
274 sp4_func (gpointer data)
276 gint value = GPOINTER_TO_INT (data);
280 for (i = 0; i < 1000; i++)
283 g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
285 v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
286 g_assert_cmpint (v, ==, v2);
290 g_thread_exit (NULL);
295 /* test that threads do not interfere with each other
298 test_static_private4 (void)
303 for (i = 0; i < 10; i++)
304 thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
306 for (i = 0; i < 10; i++)
307 g_thread_join (thread[i]);
309 g_static_private_free (&sp4);
312 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
319 sp5_func (gpointer data)
321 gint v = GPOINTER_TO_INT (data);
324 value = g_static_private_get (&sp5);
325 g_assert (value == NULL);
327 g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
328 value = g_static_private_get (&sp5);
329 g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
331 if (g_test_verbose ())
332 g_print ("thread %d set sp5\n", v);
334 g_atomic_int_inc (&count5);
335 g_cond_signal (&c5a);
336 g_cond_wait (&c5b, &m5);
337 g_mutex_unlock (&m5);
339 if (g_test_verbose ())
340 g_print ("thread %d get sp5\n", v);
341 value = g_static_private_get (&sp5);
342 g_assert (value == NULL);
348 test_static_private5 (void)
353 g_atomic_int_set (&count5, 0);
355 for (i = 0; i < 10; i++)
356 thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
359 while (g_atomic_int_get (&count5) < 10)
360 g_cond_wait (&c5a, &m5);
362 if (g_test_verbose ())
363 g_print ("sp5 gets nuked\n");
365 g_static_private_free (&sp5);
367 g_cond_broadcast (&c5b);
368 g_mutex_unlock (&m5);
370 for (i = 0; i < 10; i++)
371 g_thread_join (thread[i]);
379 main (int argc, char *argv[])
381 g_test_init (&argc, &argv, NULL);
383 g_test_add_func ("/thread/private1", test_private1);
384 g_test_add_func ("/thread/private2", test_private2);
385 g_test_add_func ("/thread/private3", test_private3);
386 g_test_add_func ("/thread/staticprivate1", test_static_private1);
387 g_test_add_func ("/thread/staticprivate2", test_static_private2);
388 g_test_add_func ("/thread/staticprivate3", test_static_private3);
389 g_test_add_func ("/thread/staticprivate4", test_static_private4);
390 g_test_add_func ("/thread/staticprivate5", test_static_private5);
392 return g_test_run ();