1 /* Unit tests for GAsyncQueue
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.
23 /* We are testing some deprecated APIs here */
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 compare_func (gconstpointer d1
, gconstpointer d2
, gpointer data
)
33 i1
= GPOINTER_TO_INT (d1
);
34 i2
= GPOINTER_TO_INT (d2
);
40 void test_async_queue_sort (void)
44 q
= g_async_queue_new ();
46 g_async_queue_push (q
, GINT_TO_POINTER (10));
47 g_async_queue_push (q
, GINT_TO_POINTER (2));
48 g_async_queue_push (q
, GINT_TO_POINTER (7));
50 g_async_queue_sort (q
, compare_func
, NULL
);
52 g_async_queue_push_sorted (q
, GINT_TO_POINTER (1), compare_func
, NULL
);
53 g_async_queue_push_sorted (q
, GINT_TO_POINTER (8), compare_func
, NULL
);
55 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 1);
56 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 2);
57 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 7);
58 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 8);
59 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 10);
61 g_assert (g_async_queue_try_pop (q
) == NULL
);
63 g_async_queue_unref (q
);
66 static gint destroy_count
;
69 destroy_notify (gpointer item
)
75 test_async_queue_destroy (void)
79 q
= g_async_queue_new_full (destroy_notify
);
81 g_assert (destroy_count
== 0);
83 g_async_queue_push (q
, GINT_TO_POINTER (1));
84 g_async_queue_push (q
, GINT_TO_POINTER (1));
85 g_async_queue_push (q
, GINT_TO_POINTER (1));
86 g_async_queue_push (q
, GINT_TO_POINTER (1));
88 g_assert (g_async_queue_length (q
) == 4);
90 g_async_queue_unref (q
);
92 g_assert (destroy_count
== 4);
95 static GAsyncQueue
*q
;
97 static GThread
*threads
[10];
98 static gint counts
[10];
103 thread_func (gpointer data
)
105 gint pos
= GPOINTER_TO_INT (data
);
110 value
= GPOINTER_TO_INT (g_async_queue_pop (q
));
125 test_async_queue_threads (void)
131 q
= g_async_queue_new ();
133 for (i
= 0; i
< 10; i
++)
134 threads
[i
] = g_thread_new ("test", thread_func
, GINT_TO_POINTER (i
));
136 for (i
= 0; i
< 100; i
++)
138 g_async_queue_lock (q
);
139 for (j
= 0; j
< 10; j
++)
141 value
= g_random_int_range (1, 100);
143 g_async_queue_push_unlocked (q
, GINT_TO_POINTER (value
));
145 g_async_queue_unlock (q
);
150 for (i
= 0; i
< 10; i
++)
151 g_async_queue_push (q
, GINT_TO_POINTER(-1));
153 for (i
= 0; i
< 10; i
++)
154 g_thread_join (threads
[i
]);
156 g_assert_cmpint (g_async_queue_length (q
), ==, 0);
160 for (i
= 0; i
< 10; i
++)
162 g_assert_cmpint (sums
[i
], >, 0);
163 g_assert_cmpint (counts
[i
], >, 0);
168 g_assert_cmpint (s
, ==, total
);
169 g_assert_cmpint (c
, ==, 1000);
171 g_async_queue_unref (q
);
175 test_async_queue_timed (void)
179 gint64 start
, end
, diff
;
182 q
= g_async_queue_new ();
184 start
= g_get_monotonic_time ();
185 val
= g_async_queue_timeout_pop (q
, G_USEC_PER_SEC
/ 10);
186 g_assert (val
== NULL
);
188 end
= g_get_monotonic_time ();
190 g_assert_cmpint (diff
, >=, G_USEC_PER_SEC
/ 10);
191 /* diff should be only a little bit more than G_USEC_PER_SEC/10, but
192 * we have to leave some wiggle room for heavily-loaded machines...
194 g_assert_cmpint (diff
, <, G_USEC_PER_SEC
);
197 g_get_current_time (&tv
);
198 g_time_val_add (&tv
, G_USEC_PER_SEC
/ 10);
199 val
= g_async_queue_timed_pop (q
, &tv
);
200 g_assert (val
== NULL
);
202 end
= g_get_monotonic_time ();
204 g_assert_cmpint (diff
, >=, G_USEC_PER_SEC
/ 10);
205 g_assert_cmpint (diff
, <, G_USEC_PER_SEC
);
208 g_get_current_time (&tv
);
209 g_time_val_add (&tv
, G_USEC_PER_SEC
/ 10);
210 g_async_queue_lock (q
);
211 val
= g_async_queue_timed_pop_unlocked (q
, &tv
);
212 g_async_queue_unlock (q
);
213 g_assert (val
== NULL
);
215 end
= g_get_monotonic_time ();
217 g_assert_cmpint (diff
, >=, G_USEC_PER_SEC
/ 10);
218 g_assert_cmpint (diff
, <, G_USEC_PER_SEC
);
220 g_async_queue_unref (q
);
224 test_async_queue_remove (void)
228 q
= g_async_queue_new ();
230 g_async_queue_push (q
, GINT_TO_POINTER (10));
231 g_async_queue_push (q
, GINT_TO_POINTER (2));
232 g_async_queue_push (q
, GINT_TO_POINTER (7));
233 g_async_queue_push (q
, GINT_TO_POINTER (1));
235 g_async_queue_remove (q
, GINT_TO_POINTER (7));
237 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 10);
238 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 2);
239 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 1);
241 g_assert (g_async_queue_try_pop (q
) == NULL
);
243 g_async_queue_unref (q
);
247 test_async_queue_push_front (void)
251 q
= g_async_queue_new ();
253 g_async_queue_push (q
, GINT_TO_POINTER (10));
254 g_async_queue_push (q
, GINT_TO_POINTER (2));
255 g_async_queue_push (q
, GINT_TO_POINTER (7));
257 g_async_queue_push_front (q
, GINT_TO_POINTER (1));
259 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 1);
260 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 10);
261 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 2);
262 g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q
)), ==, 7);
264 g_assert (g_async_queue_try_pop (q
) == NULL
);
266 g_async_queue_unref (q
);
270 main (int argc
, char *argv
[])
272 g_test_init (&argc
, &argv
, NULL
);
274 g_test_add_func ("/asyncqueue/sort", test_async_queue_sort
);
275 g_test_add_func ("/asyncqueue/destroy", test_async_queue_destroy
);
276 g_test_add_func ("/asyncqueue/threads", test_async_queue_threads
);
277 g_test_add_func ("/asyncqueue/timed", test_async_queue_timed
);
278 g_test_add_func ("/asyncqueue/remove", test_async_queue_remove
);
279 g_test_add_func ("/asyncqueue/push_front", test_async_queue_push_front
);
281 return g_test_run ();