From 1fefbb6b4a05ee3c89063969ed02be9c67a1d544 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 30 Nov 2015 11:00:12 -0500 Subject: [PATCH] core/containers/queue: Add _q_add_head() function Signed-off-by: Anna Schumaker --- include/core/containers/queue.h | 7 +++++++ tests/core/containers/queue.c | 19 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/core/containers/queue.h b/include/core/containers/queue.h index 041579e8..dbaf0d48 100644 --- a/include/core/containers/queue.h +++ b/include/core/containers/queue.h @@ -27,6 +27,13 @@ static inline guint _q_size(struct _queue *queue) return g_queue_get_length(&queue->_queue); } +/* Called to add an item to the head of a queue. */ +static inline guint _q_add_head(struct _queue *queue, gpointer data) +{ + g_queue_push_head(&queue->_queue, data); + return 0; +} + /* Called to add an item to the tail of a queue. */ static inline guint _q_add_tail(struct _queue *queue, gpointer data) { diff --git a/tests/core/containers/queue.c b/tests/core/containers/queue.c index f8d41bfb..d9e4111b 100644 --- a/tests/core/containers/queue.c +++ b/tests/core/containers/queue.c @@ -4,6 +4,11 @@ #include #include +static inline unsigned int test_q_first(struct _queue *queue) +{ + return GPOINTER_TO_INT(g_queue_peek_head(&queue->_queue)); +} + static inline unsigned int test_q_last(struct _queue *queue) { return GPOINTER_TO_INT(g_queue_peek_tail(&queue->_queue)); @@ -19,13 +24,21 @@ static void test_stress(unsigned int N) test_equal((void *)queue._queue.tail, NULL); test_equal(_q_size(&queue), 0); - /* _q_add_tail() */ + /* _q_add_head() */ for (i = 0; i < N; i++) { - test_loop_equal(_q_add_tail(&queue, GINT_TO_POINTER(i)), i, i); + test_loop_equal(_q_add_head(&queue, GINT_TO_POINTER(i)), 0, i); test_loop_equal(_q_size(&queue), i + 1, i); - test_loop_equal(test_q_last(&queue), i, i); + test_loop_equal(test_q_first(&queue), i, i); } test_loop_passed(); test_equal(_q_size(&queue), N); + + /* _q_add_tail() */ + for (i = 0; i < N; i++) { + test_loop_equal(_q_add_tail(&queue, GINT_TO_POINTER(i)), N + i, i); + test_loop_equal(_q_size(&queue), N + i + 1, i); + test_loop_equal(test_q_last(&queue), i, i); + } test_loop_passed(); + test_equal(_q_size(&queue), 2 * N); } static void test_basics() { test_stress(10); }