1 /* Unit tests for hook lists
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LicenseRef-old-glib-tests
5 *
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.
9 *
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.
13 *
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.
22 *
23 * Author: Matthias Clasen
24 */
25
26 #include "glib.h"
27
28 static void
29 hook_func (gpointer data)
30 {
31 }
32
33 static void
34 hook_destroy (gpointer data)
35 {
36 }
37
38 static gboolean
39 hook_find_false (GHook *hook, gpointer data)
40 {
41 return FALSE;
42 }
43
44 static gboolean
45 hook_find_true (GHook *hook, gpointer data)
46 {
47 return TRUE;
48 }
49
50 static void
51 hook_marshaller (GHook *hook, gpointer marshal_data)
52 {
53 }
54
55 static gboolean
56 hook_marshaller_check (GHook *hook, gpointer marshal_data)
57 {
58 return TRUE;
59 }
60
61 static gint
62 hook_compare (GHook *new_hook, GHook *sibling)
63 {
64 return 1;
65 }
66
67 static void
68 test_hook_corner_cases (void)
69 {
70 GHookList *hl;
71 GHook *hook;
72
73 /* Check if hl->finalize_hook is NULL */
74 hl = g_new (GHookList, 1);
75 g_hook_list_init (hl, sizeof (GHook));
76 hl->finalize_hook = NULL;
77 hl->is_setup = FALSE;
78 g_hook_list_clear (hl);
79 g_free (hl);
80
81 /* Check if hook->destroy is NULL */
82 hl = g_new (GHookList, 1);
83 g_hook_list_init (hl, sizeof (GHook));
84
85 hook = g_hook_alloc (hl);
86 g_assert_nonnull (hook);
87 hook->data = GINT_TO_POINTER (1);
88 hook->func = hook_func;
89 hook->flags = G_HOOK_FLAG_ACTIVE;
90 hook->destroy = NULL;
91 g_hook_append (hl, hook);
92
93 g_assert_false (g_hook_destroy (hl, 10));
94
95 g_hook_list_clear (hl);
96 g_free (hl);
97 }
98
99 static void
100 test_hook_basics (void)
101 {
102 GHookList *hl;
103 GHook *hook;
104 gulong id;
105 GHook *h;
106
107 hl = g_new (GHookList, 1);
108 g_hook_list_init (hl, sizeof (GHook));
109 g_assert_nonnull (hl);
110 g_assert_cmpint (hl->seq_id, ==, 1);
111 g_assert_cmpint (hl->hook_size, ==, sizeof (GHook));
112 g_assert_true (hl->is_setup);
113 g_assert_null (hl->hooks);
114 g_assert_null (hl->dummy3);
115 g_assert_nonnull (hl->finalize_hook);
116 g_assert_null (hl->dummy[0]);
117 g_assert_null (hl->dummy[1]);
118
119 hook = g_hook_alloc (hl);
120 g_assert_null (hook->data);
121 g_assert_null (hook->next);
122 g_assert_null (hook->prev);
123 g_assert_cmpint (hook->flags, ==, G_HOOK_FLAG_ACTIVE);
124 g_assert_cmpint (hook->ref_count, ==, 0);
125 g_assert_cmpint (hook->hook_id, ==, 0);
126 g_assert_null (hook->func);
127 g_assert_null (hook->destroy);
128
129 hook->data = GINT_TO_POINTER(1);
130 hook->func = hook_func;
131 hook->flags = G_HOOK_FLAG_ACTIVE;
132 hook->destroy = hook_destroy;
133 g_hook_append (hl, hook);
134 id = hook->hook_id;
135
136 h = g_hook_get (hl, id);
137 g_assert_cmpmem (h, sizeof (GHook), hook, sizeof (GHook));
138
139 g_assert_cmpint (g_hook_compare_ids (h, hook), ==, 0);
140
141 h = hook = g_hook_alloc (hl);
142 hook->data = GINT_TO_POINTER(2);
143 hook->func = hook_func;
144 hook->flags = G_HOOK_FLAG_ACTIVE;
145 hook->destroy = hook_destroy;
146 g_hook_prepend (hl, hook);
147
148 g_hook_destroy (hl, id);
149
150 hook = g_hook_alloc (hl);
151 hook->data = GINT_TO_POINTER(3);
152 hook->func = hook_func;
153 hook->flags = G_HOOK_FLAG_ACTIVE;
154 hook->destroy = hook_destroy;
155 g_hook_insert_sorted (hl, hook, g_hook_compare_ids);
156
157 g_assert_cmpint (g_hook_compare_ids (h, hook), ==, -1);
158
159 hook = g_hook_alloc (hl);
160 hook->data = GINT_TO_POINTER(4);
161 hook->func = hook_func;
162 hook->flags = G_HOOK_FLAG_ACTIVE;
163 hook->destroy = hook_destroy;
164 g_hook_insert_sorted (hl, hook, hook_compare);
165
166 hook = g_hook_alloc (hl);
167 hook->data = GINT_TO_POINTER(5);
168 hook->func = hook_func;
169 hook->flags = G_HOOK_FLAG_ACTIVE;
170 hook->destroy = hook_destroy;
171 g_hook_insert_before (hl, h, hook);
172
173 hook = g_hook_alloc (hl);
174 hook->data = GINT_TO_POINTER (6);
175 hook->func = hook_func;
176 hook->flags = G_HOOK_FLAG_ACTIVE;
177 hook->destroy = hook_destroy;
178 g_hook_insert_before (hl, NULL, hook);
179
180 /* Hook list is built, let's dig into it now */
181 g_hook_list_invoke (hl, TRUE);
182 g_hook_list_invoke_check (hl, TRUE);
183
184 g_assert_null (g_hook_find (hl, FALSE, hook_find_false, NULL));
185 g_assert_nonnull (g_hook_find (hl, TRUE, hook_find_true, NULL));
186
187 g_assert_null (g_hook_find_data (hl, TRUE, &id));
188 g_assert_nonnull (g_hook_find_data (hl, TRUE, GINT_TO_POINTER(2)));
189 g_assert_null (g_hook_find_data (hl, FALSE, &id));
190
191 g_assert_nonnull (g_hook_find_func (hl, TRUE, hook_func));
192 g_assert_nonnull (g_hook_find_func (hl, FALSE, hook_func));
193 g_assert_null (g_hook_find_func (hl, FALSE, hook_destroy));
194
195 g_assert_nonnull (g_hook_find_func_data (hl, TRUE, hook_func, GINT_TO_POINTER(2)));
196 g_assert_null (g_hook_find_func_data (hl, FALSE, hook_func, GINT_TO_POINTER(20)));
197 g_assert_null (g_hook_find_func_data (hl, FALSE, hook_destroy, GINT_TO_POINTER(20)));
198
199 g_hook_list_marshal (hl, TRUE, hook_marshaller, NULL);
200 g_hook_list_marshal (hl, TRUE, hook_marshaller, GINT_TO_POINTER(2));
201 g_hook_list_marshal (hl, FALSE, hook_marshaller, NULL);
202
203 g_hook_list_marshal_check (hl, TRUE, hook_marshaller_check, NULL);
204 g_hook_list_marshal_check (hl, TRUE, hook_marshaller_check, GINT_TO_POINTER(2));
205 g_hook_list_marshal_check (hl, FALSE, hook_marshaller_check, NULL);
206
207 g_hook_list_clear (hl);
208 g_free (hl);
209 }
210
211 int main (int argc, char *argv[])
212 {
213 g_test_init (&argc, &argv, NULL);
214
215 g_test_add_func ("/hook/basics", test_hook_basics);
216 g_test_add_func ("/hook/corner-cases", test_hook_corner_cases);
217
218 return g_test_run ();
219 }