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()
88 * - destroy notifies are called on g_private_replace()
96 private2 = g_private_new (private2_destroy);
98 g_private_set (private2, GINT_TO_POINTER(234));
99 g_private_replace (private2, GINT_TO_POINTER(123));
101 for (i = 0; i < 10; i++)
102 thread[i] = g_thread_create (private2_func, GINT_TO_POINTER (i), TRUE, NULL);
104 for (i = 0; i < 10; i++)
105 g_thread_join (thread[i]);
107 g_assert_cmpint (private2_destroy_count, ==, 11);
110 static gboolean private3_freed;
113 private3_free (gpointer data)
115 g_assert (data == (void*) 0x1234);
116 private3_freed = TRUE;
123 static guint __stdcall
129 private3_func (gpointer data)
131 static GPrivate key = G_PRIVATE_INIT (private3_free);
133 g_private_set (&key, (void *) 0x1234);
141 g_assert (!private3_freed);
147 thread = (HANDLE) _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
148 WaitForSingleObject (thread, INFINITE);
149 CloseHandle (thread);
155 pthread_create (&thread, NULL, private3_func, NULL);
156 pthread_join (thread, NULL);
159 g_assert (private3_freed);
163 * - static initialization works
164 * - initial value is NULL
165 * - get/set works repeatedly
167 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
170 test_static_private1 (void)
174 value = g_static_private_get (&sp1);
175 g_assert (value == NULL);
177 g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
178 value = g_static_private_get (&sp1);
179 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
181 g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
182 value = g_static_private_get (&sp1);
183 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
185 g_static_private_free (&sp1);
187 value = g_static_private_get (&sp1);
188 g_assert (value == NULL);
191 static gint sp2_destroy_count;
194 sp2_destroy (gpointer data)
200 sp2_destroy2 (gpointer data)
202 gint value = GPOINTER_TO_INT (data);
204 g_assert_cmpint (value, ==, 2);
207 /* test that destroy notifies are called as expected
208 * and on the right values
211 test_static_private2 (void)
216 g_static_private_init (&sp2);
218 value = g_static_private_get (&sp2);
219 g_assert (value == NULL);
221 g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
222 g_assert_cmpint (sp2_destroy_count, ==, 0);
223 value = g_static_private_get (&sp2);
224 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
226 g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
227 g_assert_cmpint (sp2_destroy_count, ==, 1);
228 value = g_static_private_get (&sp2);
229 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
231 g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
232 g_assert_cmpint (sp2_destroy_count, ==, 1);
233 value = g_static_private_get (&sp2);
234 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
236 g_static_private_free (&sp2);
238 value = g_static_private_get (&sp2);
239 g_assert (value == NULL);
242 /* test that freeing and reinitializing a static private
243 * drops previous value
246 test_static_private3 (void)
251 g_static_private_init (&sp3);
253 value = g_static_private_get (&sp3);
254 g_assert (value == NULL);
256 g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
257 value = g_static_private_get (&sp3);
258 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
260 g_static_private_free (&sp3);
261 g_static_private_init (&sp3);
263 value = g_static_private_get (&sp3);
264 g_assert (value == NULL);
266 g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
267 value = g_static_private_get (&sp3);
268 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
270 g_static_private_free (&sp3);
273 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
276 sp4_func (gpointer data)
278 gint value = GPOINTER_TO_INT (data);
282 for (i = 0; i < 1000; i++)
285 g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
287 v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
288 g_assert_cmpint (v, ==, v2);
292 g_thread_exit (NULL);
297 /* test that threads do not interfere with each other
300 test_static_private4 (void)
305 for (i = 0; i < 10; i++)
306 thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
308 for (i = 0; i < 10; i++)
309 g_thread_join (thread[i]);
311 g_static_private_free (&sp4);
314 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
321 sp5_func (gpointer data)
323 gint v = GPOINTER_TO_INT (data);
326 value = g_static_private_get (&sp5);
327 g_assert (value == NULL);
329 g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
330 value = g_static_private_get (&sp5);
331 g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
333 if (g_test_verbose ())
334 g_print ("thread %d set sp5\n", v);
336 g_atomic_int_inc (&count5);
337 g_cond_signal (&c5a);
338 g_cond_wait (&c5b, &m5);
339 g_mutex_unlock (&m5);
341 if (g_test_verbose ())
342 g_print ("thread %d get sp5\n", v);
343 value = g_static_private_get (&sp5);
344 g_assert (value == NULL);
350 test_static_private5 (void)
355 g_atomic_int_set (&count5, 0);
357 for (i = 0; i < 10; i++)
358 thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
361 while (g_atomic_int_get (&count5) < 10)
362 g_cond_wait (&c5a, &m5);
364 if (g_test_verbose ())
365 g_print ("sp5 gets nuked\n");
367 g_static_private_free (&sp5);
369 g_cond_broadcast (&c5b);
370 g_mutex_unlock (&m5);
372 for (i = 0; i < 10; i++)
373 g_thread_join (thread[i]);
381 main (int argc, char *argv[])
383 g_test_init (&argc, &argv, NULL);
385 g_test_add_func ("/thread/private1", test_private1);
386 g_test_add_func ("/thread/private2", test_private2);
387 g_test_add_func ("/thread/private3", test_private3);
388 g_test_add_func ("/thread/staticprivate1", test_static_private1);
389 g_test_add_func ("/thread/staticprivate2", test_static_private2);
390 g_test_add_func ("/thread/staticprivate3", test_static_private3);
391 g_test_add_func ("/thread/staticprivate4", test_static_private4);
392 g_test_add_func ("/thread/staticprivate5", test_static_private5);
394 return g_test_run ();