Improve test coverage
[platform/upstream/glib.git] / glib / tests / mainloop.c
1 /* Unit tests for GMainLoop
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 #include <glib.h>
24
25 static void
26 test_maincontext_basic (void)
27 {
28   GMainContext *ctx;
29   GSource *source;
30   GSourceFuncs funcs;
31   guint id;
32
33   ctx = g_main_context_new ();
34
35   g_assert (!g_main_context_pending (ctx));
36   g_assert (!g_main_context_iteration (ctx, FALSE));
37
38   source = g_source_new (&funcs, sizeof (GSource));
39   g_assert (g_source_get_priority (source) == G_PRIORITY_DEFAULT);
40
41   g_assert (!g_source_get_can_recurse (source));
42   g_assert (g_source_get_name (source) == NULL);
43
44   g_source_set_can_recurse (source, TRUE);
45   g_source_set_name (source, "d");
46
47   g_assert (g_source_get_can_recurse (source));
48   g_assert_cmpstr (g_source_get_name (source), ==, "d");
49
50   g_assert (g_main_context_find_source_by_user_data (ctx, NULL) == NULL);
51   g_assert (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL) == NULL);
52
53   id = g_source_attach (source, ctx);
54   g_assert_cmpint (g_source_get_id (source), ==, id);
55   g_assert (g_main_context_find_source_by_id (ctx, id) == source);
56
57   g_source_set_priority (source, G_PRIORITY_HIGH);
58   g_assert (g_source_get_priority (source) == G_PRIORITY_HIGH);
59
60   g_main_context_unref (ctx);
61 }
62
63 static void
64 test_mainloop_basic (void)
65 {
66   GMainLoop *loop;
67   GMainContext *ctx;
68
69   loop = g_main_loop_new (NULL, FALSE);
70
71   g_assert (!g_main_loop_is_running (loop));
72
73   g_main_loop_ref (loop);
74
75   ctx = g_main_loop_get_context (loop);
76   g_assert (ctx == g_main_context_default ());
77
78   g_main_loop_unref (loop);
79
80   g_assert (g_main_depth () == 0);
81 }
82
83 int
84 main (int argc, char *argv[])
85 {
86   g_test_init (&argc, &argv, NULL);
87
88   g_test_add_func ("/maincontext/basic", test_maincontext_basic);
89   g_test_add_func ("/mainloop/basic", test_mainloop_basic);
90
91   return g_test_run ();
92 }