core/containers/queue: Add _q_sort() function

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-12-01 08:04:20 -05:00
parent b7e4a1de23
commit b36869ff93
2 changed files with 28 additions and 0 deletions

View File

@ -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 */

View File

@ -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),
);