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