gtestutils: Fix a typo in a documentation comment
[glib.git] / gobject / tests / signal-handler.c
blob7a236eed9af33f45e7b8e4de43ff5055bd6cccc9
1 #include <glib-object.h>
3 typedef struct {
4 GObject instance;
5 } MyObj;
7 typedef struct {
8 GObjectClass parent_class;
9 } MyObjClass;
11 enum {
12 SIGNAL1,
13 SIGNAL2,
14 LAST_SIGNAL
17 guint signals[LAST_SIGNAL];
19 GType my_obj_get_type (void);
21 G_DEFINE_TYPE (MyObj, my_obj, G_TYPE_OBJECT)
23 static void
24 my_obj_init (MyObj *o)
28 static void
29 my_obj_class_init (MyObjClass *class)
31 signals[SIGNAL1] =
32 g_signal_new ("signal1",
33 G_TYPE_FROM_CLASS (class),
34 G_SIGNAL_RUN_LAST,
35 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
36 signals[SIGNAL2] =
37 g_signal_new ("signal2",
38 G_TYPE_FROM_CLASS (class),
39 G_SIGNAL_RUN_LAST,
40 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
43 static void
44 nop (void)
48 #define HANDLERS 500000
50 static void
51 test_connect_many (void)
53 MyObj *o;
54 gdouble time_elapsed;
55 gint i;
57 o = g_object_new (my_obj_get_type (), NULL);
59 g_test_timer_start ();
61 for (i = 0; i < HANDLERS; i++)
62 g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
64 time_elapsed = g_test_timer_elapsed ();
66 g_object_unref (o);
68 g_test_minimized_result (time_elapsed, "connected %u handlers in %6.3f seconds", HANDLERS, time_elapsed);
71 static void
72 test_disconnect_many_ordered (void)
74 MyObj *o;
75 gulong handlers[HANDLERS];
76 gdouble time_elapsed;
77 gint i;
79 o = g_object_new (my_obj_get_type (), NULL);
81 for (i = 0; i < HANDLERS; i++)
82 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
84 g_test_timer_start ();
86 for (i = 0; i < HANDLERS; i++)
87 g_signal_handler_disconnect (o, handlers[i]);
89 time_elapsed = g_test_timer_elapsed ();
91 g_object_unref (o);
93 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed);
96 static void
97 test_disconnect_many_inverse (void)
99 MyObj *o;
100 gulong handlers[HANDLERS];
101 gdouble time_elapsed;
102 gint i;
104 o = g_object_new (my_obj_get_type (), NULL);
106 for (i = 0; i < HANDLERS; i++)
107 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
109 g_test_timer_start ();
111 for (i = HANDLERS - 1; i >= 0; i--)
112 g_signal_handler_disconnect (o, handlers[i]);
114 time_elapsed = g_test_timer_elapsed ();
116 g_object_unref (o);
118 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed);
121 static void
122 test_disconnect_many_random (void)
124 MyObj *o;
125 gulong handlers[HANDLERS];
126 gulong id;
127 gdouble time_elapsed;
128 gint i, j;
130 o = g_object_new (my_obj_get_type (), NULL);
132 for (i = 0; i < HANDLERS; i++)
133 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
135 for (i = 0; i < HANDLERS; i++)
137 j = g_test_rand_int_range (0, HANDLERS);
138 id = handlers[i];
139 handlers[i] = handlers[j];
140 handlers[j] = id;
143 g_test_timer_start ();
145 for (i = 0; i < HANDLERS; i++)
146 g_signal_handler_disconnect (o, handlers[i]);
148 time_elapsed = g_test_timer_elapsed ();
150 g_object_unref (o);
152 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed);
155 static void
156 test_disconnect_2_signals (void)
158 MyObj *o;
159 gulong handlers[HANDLERS];
160 gulong id;
161 gdouble time_elapsed;
162 gint i, j;
164 o = g_object_new (my_obj_get_type (), NULL);
166 for (i = 0; i < HANDLERS; i++)
168 if (i % 2 == 0)
169 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
170 else
171 handlers[i] = g_signal_connect (o, "signal2", G_CALLBACK (nop), NULL);
174 for (i = 0; i < HANDLERS; i++)
176 j = g_test_rand_int_range (0, HANDLERS);
177 id = handlers[i];
178 handlers[i] = handlers[j];
179 handlers[j] = id;
182 g_test_timer_start ();
184 for (i = 0; i < HANDLERS; i++)
185 g_signal_handler_disconnect (o, handlers[i]);
187 time_elapsed = g_test_timer_elapsed ();
189 g_object_unref (o);
191 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed);
194 static void
195 test_disconnect_2_objects (void)
197 MyObj *o1, *o2, *o;
198 gulong handlers[HANDLERS];
199 MyObj *objects[HANDLERS];
200 gulong id;
201 gdouble time_elapsed;
202 gint i, j;
204 o1 = g_object_new (my_obj_get_type (), NULL);
205 o2 = g_object_new (my_obj_get_type (), NULL);
207 for (i = 0; i < HANDLERS; i++)
209 if (i % 2 == 0)
211 handlers[i] = g_signal_connect (o1, "signal1", G_CALLBACK (nop), NULL);
212 objects[i] = o1;
214 else
216 handlers[i] = g_signal_connect (o2, "signal1", G_CALLBACK (nop), NULL);
217 objects[i] = o2;
221 for (i = 0; i < HANDLERS; i++)
223 j = g_test_rand_int_range (0, HANDLERS);
224 id = handlers[i];
225 handlers[i] = handlers[j];
226 handlers[j] = id;
227 o = objects[i];
228 objects[i] = objects[j];
229 objects[j] = o;
232 g_test_timer_start ();
234 for (i = 0; i < HANDLERS; i++)
235 g_signal_handler_disconnect (objects[i], handlers[i]);
237 time_elapsed = g_test_timer_elapsed ();
239 g_object_unref (o1);
240 g_object_unref (o2);
242 g_test_minimized_result (time_elapsed, "disconnected %u handlers in %6.3f seconds", HANDLERS, time_elapsed);
245 static void
246 test_block_many (void)
248 MyObj *o;
249 gulong handlers[HANDLERS];
250 gulong id;
251 gdouble time_elapsed;
252 gint i, j;
254 o = g_object_new (my_obj_get_type (), NULL);
256 for (i = 0; i < HANDLERS; i++)
257 handlers[i] = g_signal_connect (o, "signal1", G_CALLBACK (nop), NULL);
259 for (i = 0; i < HANDLERS; i++)
261 j = g_test_rand_int_range (0, HANDLERS);
262 id = handlers[i];
263 handlers[i] = handlers[j];
264 handlers[j] = id;
267 g_test_timer_start ();
269 for (i = 0; i < HANDLERS; i++)
270 g_signal_handler_block (o, handlers[i]);
272 for (i = HANDLERS - 1; i >= 0; i--)
273 g_signal_handler_unblock (o, handlers[i]);
275 time_elapsed = g_test_timer_elapsed ();
277 g_object_unref (o);
279 g_test_minimized_result (time_elapsed, "blocked and unblocked %u handlers in %6.3f seconds", HANDLERS, time_elapsed);
283 main (int argc, char *argv[])
285 g_test_init (&argc, &argv, NULL);
287 if (g_test_perf ())
289 g_test_add_func ("/signal/handler/connect-many", test_connect_many);
290 g_test_add_func ("/signal/handler/disconnect-many-ordered", test_disconnect_many_ordered);
291 g_test_add_func ("/signal/handler/disconnect-many-inverse", test_disconnect_many_inverse);
292 g_test_add_func ("/signal/handler/disconnect-many-random", test_disconnect_many_random);
293 g_test_add_func ("/signal/handler/disconnect-2-signals", test_disconnect_2_signals);
294 g_test_add_func ("/signal/handler/disconnect-2-objects", test_disconnect_2_objects);
295 g_test_add_func ("/signal/handler/block-many", test_block_many);
298 return g_test_run ();