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.
24 #include <sys/types.h>
28 static gboolean cb (gpointer data)
34 test_maincontext_basic (void)
40 gpointer data = &funcs;
42 ctx = g_main_context_new ();
44 g_assert (!g_main_context_pending (ctx));
45 g_assert (!g_main_context_iteration (ctx, FALSE));
47 source = g_source_new (&funcs, sizeof (GSource));
48 g_assert (g_source_get_priority (source) == G_PRIORITY_DEFAULT);
50 g_assert (!g_source_get_can_recurse (source));
51 g_assert (g_source_get_name (source) == NULL);
53 g_source_set_can_recurse (source, TRUE);
54 g_source_set_name (source, "d");
56 g_assert (g_source_get_can_recurse (source));
57 g_assert_cmpstr (g_source_get_name (source), ==, "d");
59 g_assert (g_main_context_find_source_by_user_data (ctx, NULL) == NULL);
60 g_assert (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL) == NULL);
62 id = g_source_attach (source, ctx);
63 g_assert_cmpint (g_source_get_id (source), ==, id);
64 g_assert (g_main_context_find_source_by_id (ctx, id) == source);
66 g_source_set_priority (source, G_PRIORITY_HIGH);
67 g_assert (g_source_get_priority (source) == G_PRIORITY_HIGH);
69 g_main_context_unref (ctx);
71 ctx = g_main_context_default ();
72 source = g_source_new (&funcs, sizeof (GSource));
73 g_source_set_funcs (source, &funcs);
74 g_source_set_callback (source, cb, data, NULL);
75 id = g_source_attach (source, ctx);
76 g_source_set_name_by_id (id, "e");
77 g_assert_cmpstr (g_source_get_name (source), ==, "e");
78 g_assert (g_source_remove_by_funcs_user_data (&funcs, data));
82 test_mainloop_basic (void)
87 loop = g_main_loop_new (NULL, FALSE);
89 g_assert (!g_main_loop_is_running (loop));
91 g_main_loop_ref (loop);
93 ctx = g_main_loop_get_context (loop);
94 g_assert (ctx == g_main_context_default ());
96 g_main_loop_unref (loop);
98 g_assert (g_main_depth () == 0);
102 main (int argc, char *argv[])
104 g_test_init (&argc, &argv, NULL);
106 g_test_add_func ("/maincontext/basic", test_maincontext_basic);
107 g_test_add_func ("/mainloop/basic", test_mainloop_basic);
109 return g_test_run ();