diff --git a/include/core/containers/queue.h b/include/core/containers/queue.h index d38ca0ba..2d788be5 100644 --- a/include/core/containers/queue.h +++ b/include/core/containers/queue.h @@ -89,6 +89,12 @@ static inline guint _q_add_tail(struct _queue *queue, gpointer data) /* Called to add an item to a sorted queue. */ guint _q_add_sorted(struct _queue *, gpointer, GCompareDataFunc, gpointer); +/* Called to remove all items from a queue */ +static inline void _q_clear(struct _queue *queue) +{ + g_queue_clear(&queue->_queue); +} + /* Called to remove an item by iterator. */ gpointer _q_remove_it(struct _queue *, struct _q_iter *); diff --git a/tests/core/containers/queue.c b/tests/core/containers/queue.c index 1bbeb4ea..77c77fe5 100644 --- a/tests/core/containers/queue.c +++ b/tests/core/containers/queue.c @@ -88,13 +88,21 @@ static void test_stress(unsigned int N) } test_loop_passed(); test_equal(_q_size(&queue), N); - /* _q_remove_it() (remaining items) */ - for (i = 0; i < N; i++) { + /* _q_remove_it() (half of remaining items) */ + for (i = 0; i < (N / 2); i++) { _q_iter_set(&queue, &it, 0); test_loop_not_equal((void *)it.it_iter, NULL, i); test_loop_equal(it.it_pos, 0, i); test_loop_equal(GPOINTER_TO_INT(_q_remove_it(&queue, &it)), N - (i + 1), i); } test_loop_passed(); + test_equal(_q_size(&queue), N / 2); + + /* _q_clear() (remaining items) */ + _q_clear(&queue); + test_equal(_q_size(&queue), 0); + test_equal((void *)queue._queue.head, NULL); + test_equal((void *)queue._queue.tail, NULL); + test_equal(queue._queue.length, 0); } static void test_basics() { test_stress(10); }