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;
118 static guint __stdcall
124 private3_func (gpointer data)
126 static GPrivate key = G_PRIVATE_INIT (private3_free);
128 g_private_set (&key, (void *) 0x1234);
136 g_assert (!private3_freed);
142 thread = _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
143 WaitForSingleObject (thread, INFINITE);
144 CloseHandle (thread);
150 pthread_create (&thread, NULL, private3_func, NULL);
151 pthread_join (thread, NULL);
154 g_assert (private3_freed);
158 * - static initialization works
159 * - initial value is NULL
160 * - get/set works repeatedly
162 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
165 test_static_private1 (void)
169 value = g_static_private_get (&sp1);
170 g_assert (value == NULL);
172 g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
173 value = g_static_private_get (&sp1);
174 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
176 g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
177 value = g_static_private_get (&sp1);
178 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
180 g_static_private_free (&sp1);
182 value = g_static_private_get (&sp1);
183 g_assert (value == NULL);
186 static gint sp2_destroy_count;
189 sp2_destroy (gpointer data)
195 sp2_destroy2 (gpointer data)
197 gint value = GPOINTER_TO_INT (data);
199 g_assert_cmpint (value, ==, 2);
202 /* test that destroy notifies are called as expected
203 * and on the right values
206 test_static_private2 (void)
211 g_static_private_init (&sp2);
213 value = g_static_private_get (&sp2);
214 g_assert (value == NULL);
216 g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
217 g_assert_cmpint (sp2_destroy_count, ==, 0);
218 value = g_static_private_get (&sp2);
219 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
221 g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
222 g_assert_cmpint (sp2_destroy_count, ==, 1);
223 value = g_static_private_get (&sp2);
224 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
226 g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
227 g_assert_cmpint (sp2_destroy_count, ==, 1);
228 value = g_static_private_get (&sp2);
229 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
231 g_static_private_free (&sp2);
233 value = g_static_private_get (&sp2);
234 g_assert (value == NULL);
237 /* test that freeing and reinitializing a static private
238 * drops previous value
241 test_static_private3 (void)
246 g_static_private_init (&sp3);
248 value = g_static_private_get (&sp3);
249 g_assert (value == NULL);
251 g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
252 value = g_static_private_get (&sp3);
253 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
255 g_static_private_free (&sp3);
256 g_static_private_init (&sp3);
258 value = g_static_private_get (&sp3);
259 g_assert (value == NULL);
261 g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
262 value = g_static_private_get (&sp3);
263 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
265 g_static_private_free (&sp3);
268 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
271 sp4_func (gpointer data)
273 gint value = GPOINTER_TO_INT (data);
277 for (i = 0; i < 1000; i++)
280 g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
282 v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
283 g_assert_cmpint (v, ==, v2);
287 g_thread_exit (NULL);
292 /* test that threads do not interfere with each other
295 test_static_private4 (void)
300 for (i = 0; i < 10; i++)
301 thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
303 for (i = 0; i < 10; i++)
304 g_thread_join (thread[i]);
306 g_static_private_free (&sp4);
309 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
316 sp5_func (gpointer data)
318 gint v = GPOINTER_TO_INT (data);
321 value = g_static_private_get (&sp5);
322 g_assert (value == NULL);
324 g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
325 value = g_static_private_get (&sp5);
326 g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
328 if (g_test_verbose ())
329 g_print ("thread %d set sp5\n", v);
331 g_atomic_int_inc (&count5);
332 g_cond_signal (&c5a);
333 g_cond_wait (&c5b, &m5);
334 g_mutex_unlock (&m5);
336 if (g_test_verbose ())
337 g_print ("thread %d get sp5\n", v);
338 value = g_static_private_get (&sp5);
339 g_assert (value == NULL);
345 test_static_private5 (void)
350 g_atomic_int_set (&count5, 0);
352 for (i = 0; i < 10; i++)
353 thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
356 while (g_atomic_int_get (&count5) < 10)
357 g_cond_wait (&c5a, &m5);
359 if (g_test_verbose ())
360 g_print ("sp5 gets nuked\n");
362 g_static_private_free (&sp5);
364 g_cond_broadcast (&c5b);
365 g_mutex_unlock (&m5);
367 for (i = 0; i < 10; i++)
368 g_thread_join (thread[i]);
376 main (int argc, char *argv[])
378 g_test_init (&argc, &argv, NULL);
380 g_test_add_func ("/thread/private1", test_private1);
381 g_test_add_func ("/thread/private2", test_private2);
382 g_test_add_func ("/thread/private3", test_private3);
383 g_test_add_func ("/thread/staticprivate1", test_static_private1);
384 g_test_add_func ("/thread/staticprivate2", test_static_private2);
385 g_test_add_func ("/thread/staticprivate3", test_static_private3);
386 g_test_add_func ("/thread/staticprivate4", test_static_private4);
387 g_test_add_func ("/thread/staticprivate5", test_static_private5);
389 return g_test_run ();