private: Fix memory leak in tests
[platform/upstream/glib.git] / glib / tests / private.c
1 /* Unit tests for GPrivate and friends
2  * Copyright (C) 2011 Red Hat, Inc
3  * Author: Matthias Clasen
4  *
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.
8  *
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.
12  *
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.
21  */
22
23 /* We are testing some deprecated APIs here */
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
25
26 #include <glib.h>
27
28 /* test basics:
29  * - initial value is NULL
30  * - set/get work repeatedly
31  */
32 static void
33 test_private1 (void)
34 {
35   static GPrivate private = G_PRIVATE_INIT (NULL);
36   gpointer value;
37
38   value = g_private_get (&private);
39   g_assert (value == NULL);
40
41   g_private_set (&private, GINT_TO_POINTER(1));
42   value = g_private_get (&private);
43   g_assert_cmpint (GPOINTER_TO_INT (value), ==, 1);
44
45   g_private_set (&private, GINT_TO_POINTER(2));
46   value = g_private_get (&private);
47   g_assert_cmpint (GPOINTER_TO_INT (value), ==, 2);
48 }
49
50 static GPrivate *private2;
51 static gint private2_destroy_count;
52
53 static void
54 private2_destroy (gpointer data)
55 {
56   g_atomic_int_inc (&private2_destroy_count);
57 }
58
59 static gpointer
60 private2_func (gpointer data)
61 {
62   gint value = GPOINTER_TO_INT (data);
63   gint i;
64   gint v, v2;
65
66   for (i = 0; i < 1000; i++)
67     {
68       v = value + (i % 5);
69       g_private_set (private2, GINT_TO_POINTER(v));
70       g_usleep (1000);
71       v2 = GPOINTER_TO_INT(g_private_get (private2));
72       g_assert_cmpint (v, ==, v2);
73     }
74
75   if (value % 2 == 0)
76     g_thread_exit (NULL);
77
78   return NULL;
79 }
80
81 /* test that
82  * - threads do not interfere with each other
83  * - destroy notifies are called for each thread exit
84  * - destroy notifies are called for g_thread_exit() too
85  * - destroy notifies are not called on g_private_set()
86  * - destroy notifies are called on g_private_replace()
87  */
88 static void
89 test_private2 (void)
90 {
91   GThread *thread[10];
92   gint i;
93
94   private2 = g_private_new (private2_destroy);
95
96   g_private_set (private2, GINT_TO_POINTER(234));
97   g_private_replace (private2, GINT_TO_POINTER(123));
98
99   for (i = 0; i < 10; i++)
100     thread[i] = g_thread_create (private2_func, GINT_TO_POINTER (i), TRUE, NULL);
101
102   for (i = 0; i < 10; i++)
103     g_thread_join (thread[i]);
104
105   g_assert_cmpint (private2_destroy_count, ==, 11);
106 }
107
108 static gboolean private3_freed;
109
110 static void
111 private3_free (gpointer data)
112 {
113   g_assert (data == (void*) 0x1234);
114   private3_freed = TRUE;
115 }
116
117 #ifdef G_OS_WIN32
118 #include <windows.h>
119 #include <process.h>
120
121 static guint __stdcall
122 #else
123 #include <pthread.h>
124
125 static gpointer
126 #endif
127 private3_func (gpointer data)
128 {
129   static GPrivate key = G_PRIVATE_INIT (private3_free);
130
131   g_private_set (&key, (void *) 0x1234);
132
133   return 0;
134 }
135
136 static void
137 test_private3 (void)
138 {
139   g_assert (!private3_freed);
140
141 #ifdef G_OS_WIN32
142   {
143     HANDLE thread;
144     guint ignore;
145     thread = (HANDLE) _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
146     WaitForSingleObject (thread, INFINITE);
147     CloseHandle (thread);
148   }
149 #else
150   {
151     pthread_t thread;
152
153     pthread_create (&thread, NULL, private3_func, NULL);
154     pthread_join (thread, NULL);
155   }
156 #endif
157   g_assert (private3_freed);
158 }
159
160 /* test basics:
161  * - static initialization works
162  * - initial value is NULL
163  * - get/set works repeatedly
164  */
165 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
166
167 static void
168 test_static_private1 (void)
169 {
170   gpointer value;
171
172   value = g_static_private_get (&sp1);
173   g_assert (value == NULL);
174
175   g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
176   value = g_static_private_get (&sp1);
177   g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
178
179   g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
180   value = g_static_private_get (&sp1);
181   g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
182
183   g_static_private_free (&sp1);
184
185   value = g_static_private_get (&sp1);
186   g_assert (value == NULL);
187 }
188
189 static gint sp2_destroy_count;
190
191 static void
192 sp2_destroy (gpointer data)
193 {
194   sp2_destroy_count++;
195 }
196
197 static void
198 sp2_destroy2 (gpointer data)
199 {
200   gint value = GPOINTER_TO_INT (data);
201
202   g_assert_cmpint (value, ==, 2);
203 }
204
205 /* test that destroy notifies are called as expected
206  * and on the right values
207  */
208 static void
209 test_static_private2 (void)
210 {
211   GStaticPrivate sp2;
212   gpointer value;
213
214   g_static_private_init (&sp2);
215
216   value = g_static_private_get (&sp2);
217   g_assert (value == NULL);
218
219   g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
220   g_assert_cmpint (sp2_destroy_count, ==, 0);
221   value = g_static_private_get (&sp2);
222   g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
223
224   g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
225   g_assert_cmpint (sp2_destroy_count, ==, 1);
226   value = g_static_private_get (&sp2);
227   g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
228
229   g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
230   g_assert_cmpint (sp2_destroy_count, ==, 1);
231   value = g_static_private_get (&sp2);
232   g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
233
234   g_static_private_free (&sp2);
235
236   value = g_static_private_get (&sp2);
237   g_assert (value == NULL);
238 }
239
240 /* test that freeing and reinitializing a static private
241  * drops previous value
242  */
243 static void
244 test_static_private3 (void)
245 {
246   GStaticPrivate sp3;
247   gpointer value;
248
249   g_static_private_init (&sp3);
250
251   value = g_static_private_get (&sp3);
252   g_assert (value == NULL);
253
254   g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
255   value = g_static_private_get (&sp3);
256   g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
257
258   g_static_private_free (&sp3);
259   g_static_private_init (&sp3);
260
261   value = g_static_private_get (&sp3);
262   g_assert (value == NULL);
263
264   g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
265   value = g_static_private_get (&sp3);
266   g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
267
268   g_static_private_free (&sp3);
269 }
270
271 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
272
273 static gpointer
274 sp4_func (gpointer data)
275 {
276   gint value = GPOINTER_TO_INT (data);
277   gint i;
278   gint v, v2;
279
280   for (i = 0; i < 1000; i++)
281     {
282       v = value + (i % 5);
283       g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
284       g_usleep (1000);
285       v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
286       g_assert_cmpint (v, ==, v2);
287     }
288
289   if (value % 2 == 0)
290     g_thread_exit (NULL);
291
292   return NULL;
293 }
294
295 /* test that threads do not interfere with each other
296  */
297 static void
298 test_static_private4 (void)
299 {
300   GThread *thread[10];
301   gint i;
302
303   for (i = 0; i < 10; i++)
304     thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
305
306   for (i = 0; i < 10; i++)
307     g_thread_join (thread[i]);
308
309   g_static_private_free (&sp4);
310 }
311
312 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
313 static GMutex m5;
314 static GCond c5a;
315 static GCond c5b;
316 static gint count5;
317
318 static gpointer
319 sp5_func (gpointer data)
320 {
321   gint v = GPOINTER_TO_INT (data);
322   gpointer value;
323
324   value = g_static_private_get (&sp5);
325   g_assert (value == NULL);
326
327   g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
328   value = g_static_private_get (&sp5);
329   g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
330
331   if (g_test_verbose ())
332     g_print ("thread %d set sp5\n", v);
333   g_mutex_lock (&m5);
334   g_atomic_int_inc (&count5);
335   g_cond_signal (&c5a);
336   g_cond_wait (&c5b, &m5);
337   g_mutex_unlock (&m5);
338
339   if (g_test_verbose ())
340     g_print ("thread %d get sp5\n", v);
341   value = g_static_private_get (&sp5);
342   g_assert (value == NULL);
343
344   return NULL;
345 }
346
347 static void
348 test_static_private5 (void)
349 {
350   GThread *thread[10];
351   gint i;
352
353   g_atomic_int_set (&count5, 0);
354
355   for (i = 0; i < 10; i++)
356     thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
357
358   g_mutex_lock (&m5);
359   while (g_atomic_int_get (&count5) < 10)
360     g_cond_wait (&c5a, &m5);
361
362   if (g_test_verbose ())
363     g_print ("sp5 gets nuked\n");
364
365   g_static_private_free (&sp5);
366
367   g_cond_broadcast (&c5b);
368   g_mutex_unlock (&m5);
369
370   for (i = 0; i < 10; i++)
371     g_thread_join (thread[i]);
372
373   g_mutex_clear (&m5);
374   g_cond_clear (&c5a);
375   g_cond_clear (&c5b);
376 }
377
378 int
379 main (int argc, char *argv[])
380 {
381   g_test_init (&argc, &argv, NULL);
382
383   g_test_add_func ("/thread/private1", test_private1);
384   g_test_add_func ("/thread/private2", test_private2);
385   g_test_add_func ("/thread/private3", test_private3);
386   g_test_add_func ("/thread/staticprivate1", test_static_private1);
387   g_test_add_func ("/thread/staticprivate2", test_static_private2);
388   g_test_add_func ("/thread/staticprivate3", test_static_private3);
389   g_test_add_func ("/thread/staticprivate4", test_static_private4);
390   g_test_add_func ("/thread/staticprivate5", test_static_private5);
391
392   return g_test_run ();
393 }