summaryrefslogtreecommitdiff
path: root/src/test/mock_queue.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/mock_queue.c')
-rw-r--r--src/test/mock_queue.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/test/mock_queue.c b/src/test/mock_queue.c
new file mode 100644
index 0000000..10ce834
--- /dev/null
+++ b/src/test/mock_queue.c
@@ -0,0 +1,69 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "mock_queue.h"
+
+struct mock_queue_node_t;
+
+struct mock_queue_node_t {
+ struct mock_queue_node_t *next;
+ void *data;
+};
+
+static struct mock_queue_node_t mock_queue_head;
+static struct mock_queue_node_t *mock_queue_tail;
+size_t mock_queue_length;
+
+void mock_queue_init()
+{
+ mock_queue_length = 0;
+ mock_queue_head.next = NULL;
+ mock_queue_tail = &mock_queue_head;
+}
+
+
+size_t mock_queue_len() {
+ return mock_queue_length;
+}
+
+
+void mock_queue_data(size_t size, void *data)
+{
+ struct mock_queue_node_t *node = malloc(sizeof(struct mock_queue_node_t));
+ if (node == NULL) {
+ fprintf(stderr, "WARNING: memory allocation for mock queue node failed!\n");
+ return;
+ }
+
+ node->data = data;
+ node->next = NULL;
+ mock_queue_tail->next = node;
+ mock_queue_tail = node;
+
+ mock_queue_length += 1;
+}
+
+
+void * mock_front_data()
+{
+ if (mock_queue_head.next == NULL)
+ return NULL;
+ return mock_queue_head.next->data;
+}
+
+
+void mock_pop()
+{
+ if (mock_queue_head.next == NULL)
+ return;
+
+ if (mock_queue_head.next->data != NULL)
+ free(mock_queue_head.next->data);
+ struct mock_queue_node_t *headnext = mock_queue_head.next->next;
+ free(mock_queue_head.next);
+ mock_queue_head.next = headnext;
+ if (headnext == NULL)
+ // no more nodes, bring tail back to head
+ mock_queue_tail = &mock_queue_head;
+ mock_queue_length -= 1;
+}