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);
103 static gboolean private3_freed;
106 private3_free (gpointer data)
108 g_assert (data == (void*) 0x1234);
109 private3_freed = TRUE;
115 static guint __stdcall
121 private3_func (gpointer data)
123 static GPrivate key = G_PRIVATE_INIT (private3_free);
125 g_private_set (&key, (void *) 0x1234);
133 g_assert (!private3_freed);
139 thread = _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
140 WaitForSingleObject (thread, INFINITE);
141 CloseHandle (thread);
147 pthread_create (&thread, NULL, private3_func, NULL);
148 pthread_join (thread, NULL);
151 g_assert (private3_freed);
155 * - static initialization works
156 * - initial value is NULL
157 * - get/set works repeatedly
159 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
162 test_static_private1 (void)
166 value = g_static_private_get (&sp1);
167 g_assert (value == NULL);
169 g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
170 value = g_static_private_get (&sp1);
171 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
173 g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
174 value = g_static_private_get (&sp1);
175 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
177 g_static_private_free (&sp1);
179 value = g_static_private_get (&sp1);
180 g_assert (value == NULL);
183 static gint sp2_destroy_count;
186 sp2_destroy (gpointer data)
192 sp2_destroy2 (gpointer data)
194 gint value = GPOINTER_TO_INT (data);
196 g_assert_cmpint (value, ==, 2);
199 /* test that destroy notifies are called as expected
200 * and on the right values
203 test_static_private2 (void)
208 g_static_private_init (&sp2);
210 value = g_static_private_get (&sp2);
211 g_assert (value == NULL);
213 g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
214 g_assert_cmpint (sp2_destroy_count, ==, 0);
215 value = g_static_private_get (&sp2);
216 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
218 g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
219 g_assert_cmpint (sp2_destroy_count, ==, 1);
220 value = g_static_private_get (&sp2);
221 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
223 g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
224 g_assert_cmpint (sp2_destroy_count, ==, 1);
225 value = g_static_private_get (&sp2);
226 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
228 g_static_private_free (&sp2);
230 value = g_static_private_get (&sp2);
231 g_assert (value == NULL);
234 /* test that freeing and reinitializing a static private
235 * drops previous value
238 test_static_private3 (void)
243 g_static_private_init (&sp3);
245 value = g_static_private_get (&sp3);
246 g_assert (value == NULL);
248 g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
249 value = g_static_private_get (&sp3);
250 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
252 g_static_private_free (&sp3);
253 g_static_private_init (&sp3);
255 value = g_static_private_get (&sp3);
256 g_assert (value == NULL);
258 g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
259 value = g_static_private_get (&sp3);
260 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
262 g_static_private_free (&sp3);
265 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
268 sp4_func (gpointer data)
270 gint value = GPOINTER_TO_INT (data);
274 for (i = 0; i < 1000; i++)
277 g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
279 v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
280 g_assert_cmpint (v, ==, v2);
284 g_thread_exit (NULL);
289 /* test that threads do not interfere with each other
292 test_static_private4 (void)
297 for (i = 0; i < 10; i++)
298 thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
300 for (i = 0; i < 10; i++)
301 g_thread_join (thread[i]);
303 g_static_private_free (&sp4);
306 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
313 sp5_func (gpointer data)
315 gint v = GPOINTER_TO_INT (data);
318 value = g_static_private_get (&sp5);
319 g_assert (value == NULL);
321 g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
322 value = g_static_private_get (&sp5);
323 g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
325 if (g_test_verbose ())
326 g_print ("thread %d set sp5\n", v);
328 g_atomic_int_inc (&count5);
329 g_cond_signal (&c5a);
330 g_cond_wait (&c5b, &m5);
331 g_mutex_unlock (&m5);
333 if (g_test_verbose ())
334 g_print ("thread %d get sp5\n", v);
335 value = g_static_private_get (&sp5);
336 g_assert (value == NULL);
342 test_static_private5 (void)
347 g_atomic_int_set (&count5, 0);
349 for (i = 0; i < 10; i++)
350 thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
353 while (g_atomic_int_get (&count5) < 10)
354 g_cond_wait (&c5a, &m5);
356 if (g_test_verbose ())
357 g_print ("sp5 gets nuked\n");
359 g_static_private_free (&sp5);
361 g_cond_broadcast (&c5b);
362 g_mutex_unlock (&m5);
364 for (i = 0; i < 10; i++)
365 g_thread_join (thread[i]);
373 main (int argc, char *argv[])
375 g_test_init (&argc, &argv, NULL);
377 g_assert (g_thread_get_initialized ());
379 g_test_add_func ("/thread/private1", test_private1);
380 g_test_add_func ("/thread/private2", test_private2);
381 g_test_add_func ("/thread/private3", test_private3);
382 g_test_add_func ("/thread/staticprivate1", test_static_private1);
383 g_test_add_func ("/thread/staticprivate2", test_static_private2);
384 g_test_add_func ("/thread/staticprivate3", test_static_private3);
385 g_test_add_func ("/thread/staticprivate4", test_static_private4);
386 g_test_add_func ("/thread/staticprivate5", test_static_private5);
388 return g_test_run ();