diff --git a/include/core/containers/queue.h b/include/core/containers/queue.h index 65b5a129..eecbcdc5 100644 --- a/include/core/containers/queue.h +++ b/include/core/containers/queue.h @@ -89,4 +89,11 @@ static inline guint _q_add_tail(struct _queue *queue, gpointer data) /* Called to remove an item by iterator. */ gpointer _q_remove_it(struct _queue *, struct _q_iter *); +/* Called to sort the queue. */ +static inline void _q_sort(struct _queue *queue, GCompareDataFunc func, + gpointer user_data) +{ + g_queue_sort(&queue->_queue, func, user_data); +} + #endif /* OCARINA_CORE_CONTAINERS_QUEUE_H */ diff --git a/tests/core/containers/queue.c b/tests/core/containers/queue.c index bf1a9ec1..0134f075 100644 --- a/tests/core/containers/queue.c +++ b/tests/core/containers/queue.c @@ -101,8 +101,29 @@ static void test_basics() { test_stress(10); } static void test_stress_0() { test_stress(0); } static void test_stress_100K() { test_stress(100000); } +void test_sort() +{ + struct _queue queue = _Q_INIT(); + unsigned int i, N = 10; + struct _q_iter it; + + for (i = 0; i < N; i++) + _q_add_head(&queue, GINT_TO_POINTER(i)); + + /* _q_sort() */ + _q_sort(&queue, test_sort_int, GINT_TO_POINTER(42)); + test_equal(data_val, 42); + + i = 0; + _q_for_each(&queue, &it) { + test_loop_equal(test_q_iter_val(&it), i, i); + i++; + } test_loop_passed(); +} + DECLARE_UNIT_TESTS( UNIT_TEST("Queue Basics", test_basics), UNIT_TEST("Queue Stress (N = 0)", test_stress_0), UNIT_TEST("Queue Stress (N = 100,000)", test_stress_100K), + UNIT_TEST("Queue Sorting", test_sort), );