core/containers/queue: Add _q_add_head() function

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-30 11:00:12 -05:00
parent 6a653bd35e
commit 1fefbb6b4a
2 changed files with 23 additions and 3 deletions

View File

@ -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)
{

View File

@ -4,6 +4,11 @@
#include <core/containers/queue.h>
#include <tests/test.h>
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); }