1 /* Unit tests for GMainLoop
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.
25 static gboolean cb (gpointer data)
31 test_maincontext_basic (void)
37 gpointer data = &funcs;
39 ctx = g_main_context_new ();
41 g_assert (!g_main_context_pending (ctx));
42 g_assert (!g_main_context_iteration (ctx, FALSE));
44 source = g_source_new (&funcs, sizeof (GSource));
45 g_assert (g_source_get_priority (source) == G_PRIORITY_DEFAULT);
47 g_assert (!g_source_get_can_recurse (source));
48 g_assert (g_source_get_name (source) == NULL);
50 g_source_set_can_recurse (source, TRUE);
51 g_source_set_name (source, "d");
53 g_assert (g_source_get_can_recurse (source));
54 g_assert_cmpstr (g_source_get_name (source), ==, "d");
56 g_assert (g_main_context_find_source_by_user_data (ctx, NULL) == NULL);
57 g_assert (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL) == NULL);
59 id = g_source_attach (source, ctx);
60 g_assert_cmpint (g_source_get_id (source), ==, id);
61 g_assert (g_main_context_find_source_by_id (ctx, id) == source);
63 g_source_set_priority (source, G_PRIORITY_HIGH);
64 g_assert (g_source_get_priority (source) == G_PRIORITY_HIGH);
66 g_main_context_unref (ctx);
68 ctx = g_main_context_default ();
69 source = g_source_new (&funcs, sizeof (GSource));
70 g_source_set_funcs (source, &funcs);
71 g_source_set_callback (source, cb, data, NULL);
72 id = g_source_attach (source, ctx);
73 g_source_set_name_by_id (id, "e");
74 g_assert_cmpstr (g_source_get_name (source), ==, "e");
75 g_assert (g_source_remove_by_funcs_user_data (&funcs, data));
79 test_mainloop_basic (void)
84 loop = g_main_loop_new (NULL, FALSE);
86 g_assert (!g_main_loop_is_running (loop));
88 g_main_loop_ref (loop);
90 ctx = g_main_loop_get_context (loop);
91 g_assert (ctx == g_main_context_default ());
93 g_main_loop_unref (loop);
95 g_assert (g_main_depth () == 0);
99 main (int argc, char *argv[])
101 g_test_init (&argc, &argv, NULL);
103 g_test_add_func ("/maincontext/basic", test_maincontext_basic);
104 g_test_add_func ("/mainloop/basic", test_mainloop_basic);
106 return g_test_run ();