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
38 private1 = g_private_new (NULL);
40 value = g_private_get (private1);
41 g_assert (value == NULL);
43 g_private_set (private1, GINT_TO_POINTER(1));
44 value = g_private_get (private1);
45 g_assert_cmpint (GPOINTER_TO_INT (value), ==, 1);
47 g_private_set (private1, GINT_TO_POINTER(2));
48 value = g_private_get (private1);
49 g_assert_cmpint (GPOINTER_TO_INT (value), ==, 2);
52 static GPrivate *private2;
53 static gint private2_destroy_count;
56 private2_destroy (gpointer data)
58 g_atomic_int_inc (&private2_destroy_count);
62 private2_func (gpointer data)
64 gint value = GPOINTER_TO_INT (data);
68 for (i = 0; i < 1000; i++)
71 g_private_set (private2, GINT_TO_POINTER(v));
73 v2 = GPOINTER_TO_INT(g_private_get (private2));
74 g_assert_cmpint (v, ==, v2);
84 * - threads do not interfere with each other
85 * - destroy notifies are called for each thread exit
86 * - destroy notifies are called for g_thread_exit() too
87 * - destroy notifies are not called on g_private_set()
95 private2 = g_private_new (private2_destroy);
97 for (i = 0; i < 10; i++)
98 thread[i] = g_thread_create (private2_func, GINT_TO_POINTER (i), TRUE, NULL);
100 for (i = 0; i < 10; i++)
101 g_thread_join (thread[i]);
103 g_assert_cmpint (private2_destroy_count, ==, 10);
106 static gboolean private3_freed;
109 private3_free (gpointer data)
111 g_assert (data == (void*) 0x1234);
112 private3_freed = TRUE;
119 static guint __stdcall
125 private3_func (gpointer data)
127 static GPrivate key = G_PRIVATE_INIT (private3_free);
129 g_private_set (&key, (void *) 0x1234);
137 g_assert (!private3_freed);
143 thread = (HANDLE) _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
144 WaitForSingleObject (thread, INFINITE);
145 CloseHandle (thread);
151 pthread_create (&thread, NULL, private3_func, NULL);
152 pthread_join (thread, NULL);
155 g_assert (private3_freed);
159 * - static initialization works
160 * - initial value is NULL
161 * - get/set works repeatedly
163 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
166 test_static_private1 (void)
170 value = g_static_private_get (&sp1);
171 g_assert (value == NULL);
173 g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
174 value = g_static_private_get (&sp1);
175 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
177 g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
178 value = g_static_private_get (&sp1);
179 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
181 g_static_private_free (&sp1);
183 value = g_static_private_get (&sp1);
184 g_assert (value == NULL);
187 static gint sp2_destroy_count;
190 sp2_destroy (gpointer data)
196 sp2_destroy2 (gpointer data)
198 gint value = GPOINTER_TO_INT (data);
200 g_assert_cmpint (value, ==, 2);
203 /* test that destroy notifies are called as expected
204 * and on the right values
207 test_static_private2 (void)
212 g_static_private_init (&sp2);
214 value = g_static_private_get (&sp2);
215 g_assert (value == NULL);
217 g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
218 g_assert_cmpint (sp2_destroy_count, ==, 0);
219 value = g_static_private_get (&sp2);
220 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
222 g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
223 g_assert_cmpint (sp2_destroy_count, ==, 1);
224 value = g_static_private_get (&sp2);
225 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
227 g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
228 g_assert_cmpint (sp2_destroy_count, ==, 1);
229 value = g_static_private_get (&sp2);
230 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
232 g_static_private_free (&sp2);
234 value = g_static_private_get (&sp2);
235 g_assert (value == NULL);
238 /* test that freeing and reinitializing a static private
239 * drops previous value
242 test_static_private3 (void)
247 g_static_private_init (&sp3);
249 value = g_static_private_get (&sp3);
250 g_assert (value == NULL);
252 g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
253 value = g_static_private_get (&sp3);
254 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
256 g_static_private_free (&sp3);
257 g_static_private_init (&sp3);
259 value = g_static_private_get (&sp3);
260 g_assert (value == NULL);
262 g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
263 value = g_static_private_get (&sp3);
264 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
266 g_static_private_free (&sp3);
269 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
272 sp4_func (gpointer data)
274 gint value = GPOINTER_TO_INT (data);
278 for (i = 0; i < 1000; i++)
281 g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
283 v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
284 g_assert_cmpint (v, ==, v2);
288 g_thread_exit (NULL);
293 /* test that threads do not interfere with each other
296 test_static_private4 (void)
301 for (i = 0; i < 10; i++)
302 thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
304 for (i = 0; i < 10; i++)
305 g_thread_join (thread[i]);
307 g_static_private_free (&sp4);
310 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
317 sp5_func (gpointer data)
319 gint v = GPOINTER_TO_INT (data);
322 value = g_static_private_get (&sp5);
323 g_assert (value == NULL);
325 g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
326 value = g_static_private_get (&sp5);
327 g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
329 if (g_test_verbose ())
330 g_print ("thread %d set sp5\n", v);
332 g_atomic_int_inc (&count5);
333 g_cond_signal (&c5a);
334 g_cond_wait (&c5b, &m5);
335 g_mutex_unlock (&m5);
337 if (g_test_verbose ())
338 g_print ("thread %d get sp5\n", v);
339 value = g_static_private_get (&sp5);
340 g_assert (value == NULL);
346 test_static_private5 (void)
351 g_atomic_int_set (&count5, 0);
353 for (i = 0; i < 10; i++)
354 thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
357 while (g_atomic_int_get (&count5) < 10)
358 g_cond_wait (&c5a, &m5);
360 if (g_test_verbose ())
361 g_print ("sp5 gets nuked\n");
363 g_static_private_free (&sp5);
365 g_cond_broadcast (&c5b);
366 g_mutex_unlock (&m5);
368 for (i = 0; i < 10; i++)
369 g_thread_join (thread[i]);
377 main (int argc, char *argv[])
379 g_test_init (&argc, &argv, NULL);
381 g_test_add_func ("/thread/private1", test_private1);
382 g_test_add_func ("/thread/private2", test_private2);
383 g_test_add_func ("/thread/private3", test_private3);
384 g_test_add_func ("/thread/staticprivate1", test_static_private1);
385 g_test_add_func ("/thread/staticprivate2", test_static_private2);
386 g_test_add_func ("/thread/staticprivate3", test_static_private3);
387 g_test_add_func ("/thread/staticprivate4", test_static_private4);
388 g_test_add_func ("/thread/staticprivate5", test_static_private5);
390 return g_test_run ();