summaryrefslogtreecommitdiff
path: root/src/test/mock_queue.c
blob: 10ce8346e85a76e2e0b621ef277bd33edcf92dd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
}