Add some more cases to the app-id unit tests
[glib.git] / glib / tests / once.c
blobb11e577f6fb4dd27bcc14121fb0969624338dda4
2 /* Unit tests for GOnce and friends
3 * Copyright (C) 2011 Red Hat, Inc
4 * Author: Matthias Clasen
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
10 * This work is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
24 #include <glib.h>
26 static gpointer
27 do_once (gpointer data)
29 static gint i = 0;
31 i++;
33 return GINT_TO_POINTER (i);
36 static void
37 test_once1 (void)
39 GOnce once = G_ONCE_INIT;
40 gpointer res;
42 g_assert (once.status == G_ONCE_STATUS_NOTCALLED);
44 res = g_once (&once, do_once, NULL);
45 g_assert_cmpint (GPOINTER_TO_INT (res), ==, 1);
47 g_assert (once.status == G_ONCE_STATUS_READY);
49 res = g_once (&once, do_once, NULL);
50 g_assert_cmpint (GPOINTER_TO_INT (res), ==, 1);
53 static void
54 test_once2 (void)
56 static gsize init = 0;
58 if (g_once_init_enter (&init))
60 g_assert (TRUE);
61 g_once_init_leave (&init, 1);
64 g_assert_cmpint (init, ==, 1);
65 if (g_once_init_enter (&init))
67 g_assert_not_reached ();
68 g_once_init_leave (&init, 2);
70 g_assert_cmpint (init, ==, 1);
73 #define THREADS 100
75 static gint64 shared;
77 static void
78 init_shared (void)
80 static gsize init = 0;
82 if (g_once_init_enter (&init))
84 shared += 42;
86 g_once_init_leave (&init, 1);
90 static gpointer
91 thread_func (gpointer data)
93 init_shared ();
95 return NULL;
98 static void
99 test_once3 (void)
101 gint i;
102 GThread *threads[THREADS];
104 shared = 0;
106 for (i = 0; i < THREADS; i++)
107 threads[i] = g_thread_new ("once3", thread_func, NULL);
109 for (i = 0; i < THREADS; i++)
110 g_thread_join (threads[i]);
112 g_assert_cmpint (shared, ==, 42);
115 static void
116 test_once4 (void)
118 static const gchar *val;
120 if (g_once_init_enter (&val))
121 g_once_init_leave (&val, "foo");
123 g_assert_cmpstr (val, ==, "foo");
127 main (int argc, char *argv[])
129 g_test_init (&argc, &argv, NULL);
131 g_test_add_func ("/thread/once1", test_once1);
132 g_test_add_func ("/thread/once2", test_once2);
133 g_test_add_func ("/thread/once3", test_once3);
134 g_test_add_func ("/thread/once4", test_once4);
136 return g_test_run ();