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 gint private2_destroy_count;
53 private2_destroy (gpointer data)
55 g_atomic_int_inc (&private2_destroy_count);
58 static GPrivate private2 = G_PRIVATE_INIT (private2_destroy);
61 private2_func (gpointer data)
63 gint value = GPOINTER_TO_INT (data);
67 for (i = 0; i < 1000; i++)
70 g_private_set (&private2, GINT_TO_POINTER (v));
72 v2 = GPOINTER_TO_INT (g_private_get (&private2));
73 g_assert_cmpint (v, ==, v2);
83 * - threads do not interfere with each other
84 * - destroy notifies are called for each thread exit
85 * - destroy notifies are called for g_thread_exit() too
86 * - destroy notifies are not called on g_private_set()
87 * - destroy notifies are called on g_private_replace()
95 g_private_set (&private2, GINT_TO_POINTER (234));
96 g_private_replace (&private2, GINT_TO_POINTER (123));
98 for (i = 0; i < 10; i++)
99 thread[i] = g_thread_create (private2_func, GINT_TO_POINTER (i), TRUE, NULL);
101 for (i = 0; i < 10; i++)
102 g_thread_join (thread[i]);
104 g_assert_cmpint (private2_destroy_count, ==, 11);
107 static gboolean private3_freed;
110 private3_free (gpointer data)
112 g_assert (data == (void*) 0x1234);
113 private3_freed = TRUE;
120 static guint __stdcall
126 private3_func (gpointer data)
128 static GPrivate key = G_PRIVATE_INIT (private3_free);
130 g_private_set (&key, (void *) 0x1234);
138 g_assert (!private3_freed);
144 thread = (HANDLE) _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
145 WaitForSingleObject (thread, INFINITE);
146 CloseHandle (thread);
152 pthread_create (&thread, NULL, private3_func, NULL);
153 pthread_join (thread, NULL);
156 g_assert (private3_freed);
160 * - static initialization works
161 * - initial value is NULL
162 * - get/set works repeatedly
164 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
167 test_static_private1 (void)
171 value = g_static_private_get (&sp1);
172 g_assert (value == NULL);
174 g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
175 value = g_static_private_get (&sp1);
176 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
178 g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
179 value = g_static_private_get (&sp1);
180 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
182 g_static_private_free (&sp1);
184 value = g_static_private_get (&sp1);
185 g_assert (value == NULL);
188 static gint sp2_destroy_count;
191 sp2_destroy (gpointer data)
197 sp2_destroy2 (gpointer data)
199 gint value = GPOINTER_TO_INT (data);
201 g_assert_cmpint (value, ==, 2);
204 /* test that destroy notifies are called as expected
205 * and on the right values
208 test_static_private2 (void)
213 g_static_private_init (&sp2);
215 value = g_static_private_get (&sp2);
216 g_assert (value == NULL);
218 g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
219 g_assert_cmpint (sp2_destroy_count, ==, 0);
220 value = g_static_private_get (&sp2);
221 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
223 g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
224 g_assert_cmpint (sp2_destroy_count, ==, 1);
225 value = g_static_private_get (&sp2);
226 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
228 g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
229 g_assert_cmpint (sp2_destroy_count, ==, 1);
230 value = g_static_private_get (&sp2);
231 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
233 g_static_private_free (&sp2);
235 value = g_static_private_get (&sp2);
236 g_assert (value == NULL);
239 /* test that freeing and reinitializing a static private
240 * drops previous value
243 test_static_private3 (void)
248 g_static_private_init (&sp3);
250 value = g_static_private_get (&sp3);
251 g_assert (value == NULL);
253 g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
254 value = g_static_private_get (&sp3);
255 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
257 g_static_private_free (&sp3);
258 g_static_private_init (&sp3);
260 value = g_static_private_get (&sp3);
261 g_assert (value == NULL);
263 g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
264 value = g_static_private_get (&sp3);
265 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
267 g_static_private_free (&sp3);
270 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
273 sp4_func (gpointer data)
275 gint value = GPOINTER_TO_INT (data);
279 for (i = 0; i < 1000; i++)
282 g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
284 v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
285 g_assert_cmpint (v, ==, v2);
289 g_thread_exit (NULL);
294 /* test that threads do not interfere with each other
297 test_static_private4 (void)
302 for (i = 0; i < 10; i++)
303 thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
305 for (i = 0; i < 10; i++)
306 g_thread_join (thread[i]);
308 g_static_private_free (&sp4);
311 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
318 sp5_func (gpointer data)
320 gint v = GPOINTER_TO_INT (data);
323 value = g_static_private_get (&sp5);
324 g_assert (value == NULL);
326 g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
327 value = g_static_private_get (&sp5);
328 g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
330 if (g_test_verbose ())
331 g_print ("thread %d set sp5\n", v);
333 g_atomic_int_inc (&count5);
334 g_cond_signal (&c5a);
335 g_cond_wait (&c5b, &m5);
336 g_mutex_unlock (&m5);
338 if (g_test_verbose ())
339 g_print ("thread %d get sp5\n", v);
340 value = g_static_private_get (&sp5);
341 g_assert (value == NULL);
347 test_static_private5 (void)
352 g_atomic_int_set (&count5, 0);
354 for (i = 0; i < 10; i++)
355 thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
358 while (g_atomic_int_get (&count5) < 10)
359 g_cond_wait (&c5a, &m5);
361 if (g_test_verbose ())
362 g_print ("sp5 gets nuked\n");
364 g_static_private_free (&sp5);
366 g_cond_broadcast (&c5b);
367 g_mutex_unlock (&m5);
369 for (i = 0; i < 10; i++)
370 g_thread_join (thread[i]);
374 main (int argc, char *argv[])
376 g_test_init (&argc, &argv, NULL);
378 g_test_add_func ("/thread/private1", test_private1);
379 g_test_add_func ("/thread/private2", test_private2);
380 g_test_add_func ("/thread/private3", test_private3);
381 g_test_add_func ("/thread/staticprivate1", test_static_private1);
382 g_test_add_func ("/thread/staticprivate2", test_static_private2);
383 g_test_add_func ("/thread/staticprivate3", test_static_private3);
384 g_test_add_func ("/thread/staticprivate4", test_static_private4);
385 g_test_add_func ("/thread/staticprivate5", test_static_private5);
387 return g_test_run ();