1/* Unit tests for hook lists
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
7 *
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
20 *
21 * Author: Matthias Clasen
22 */
23
24#include "glib.h"
25
26static void
27hook_func (gpointer data)
28{
29}
30
31static void
32destroy (gpointer data)
33{
34}
35
36static void
37test_hook1 (void)
38{
39 GHookList *hl;
40 GHook *hook;
41 gulong id;
42 GHook *h;
43
44 hl = g_new (GHookList, 1);
45 g_hook_list_init (hook_list: hl, hook_size: sizeof (GHook));
46
47 hook = g_hook_alloc (hook_list: hl);
48 hook->data = GINT_TO_POINTER(1);
49 hook->func = hook_func;
50 hook->flags = G_HOOK_FLAG_ACTIVE;
51 hook->destroy = destroy;
52 g_hook_append (hl, hook);
53 id = hook->hook_id;
54
55 h = g_hook_get (hook_list: hl, hook_id: id);
56 g_assert (h == hook);
57
58 h = hook = g_hook_alloc (hook_list: hl);
59 hook->data = GINT_TO_POINTER(2);
60 hook->func = hook_func;
61 hook->flags = G_HOOK_FLAG_ACTIVE;
62 hook->destroy = destroy;
63 g_hook_prepend (hook_list: hl, hook);
64
65 g_hook_destroy (hook_list: hl, hook_id: id);
66
67 hook = g_hook_alloc (hook_list: hl);
68 hook->data = GINT_TO_POINTER(3);
69 hook->func = hook_func;
70 hook->flags = G_HOOK_FLAG_ACTIVE;
71 hook->destroy = destroy;
72 g_hook_insert_sorted (hook_list: hl, hook, func: g_hook_compare_ids);
73
74 hook = g_hook_alloc (hook_list: hl);
75 hook->data = GINT_TO_POINTER(4);
76 hook->func = hook_func;
77 hook->flags = G_HOOK_FLAG_ACTIVE;
78 hook->destroy = destroy;
79 g_hook_insert_before (hook_list: hl, sibling: h, hook);
80
81 g_hook_list_invoke (hook_list: hl, TRUE);
82
83 g_hook_list_clear (hook_list: hl);
84 g_free (mem: hl);
85}
86
87int main (int argc, char *argv[])
88{
89 g_test_init (argc: &argc, argv: &argv, NULL);
90
91 g_test_add_func (testpath: "/hook/test1", test_func: test_hook1);
92
93 return g_test_run ();
94}
95

source code of gtk/subprojects/glib/glib/tests/hook.c