summaryrefslogtreecommitdiff
path: root/tests/queue-test.c
blob: a519e3dc21a60a415954d078d280d160f3ce74ef (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
#include <glib.h>

int main()
{
  GQueue *q;
  GList *node;
  gpointer data;

  q = g_queue_new ();

  g_assert (g_queue_is_empty (q) == TRUE);

  g_queue_push_head (q, GINT_TO_POINTER (2));
  g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (2));
  g_assert (g_queue_is_empty (q) == FALSE);
  g_assert (g_list_length (q->head) == 1);
  g_assert (q->head == q->tail);
  g_queue_push_head (q, GINT_TO_POINTER (1));
  g_assert (q->head->next == q->tail);
  g_assert (q->tail->prev == q->head);
  g_assert (g_list_length (q->head) == 2);
  g_assert (q->tail->data == GINT_TO_POINTER (2));
  g_assert (q->head->data == GINT_TO_POINTER (1));
  g_queue_push_tail (q, GINT_TO_POINTER (3));
  g_assert (g_list_length (q->head) == 3);
  g_assert (q->head->data == GINT_TO_POINTER (1));
  g_assert (q->head->next->data == GINT_TO_POINTER (2));
  g_assert (q->head->next->next == q->tail);
  g_assert (q->head->next == q->tail->prev);
  g_assert (q->tail->data == GINT_TO_POINTER (3));
  g_queue_push_tail (q, GINT_TO_POINTER (4));
  g_assert (g_list_length (q->head) == 4);
  g_assert (q->head->data == GINT_TO_POINTER (1));
  g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (4));
  g_queue_push_tail (q, GINT_TO_POINTER (5));
  g_assert (g_list_length (q->head) == 5);

  g_assert (g_queue_is_empty (q) == FALSE);

  g_assert (q->length == 5);
  g_assert (q->head->prev == NULL);
  g_assert (q->head->data == GINT_TO_POINTER (1));
  g_assert (q->head->next->data == GINT_TO_POINTER (2));
  g_assert (q->head->next->next->data == GINT_TO_POINTER (3));
  g_assert (q->head->next->next->next->data == GINT_TO_POINTER (4));
  g_assert (q->head->next->next->next->next->data == GINT_TO_POINTER (5));
  g_assert (q->head->next->next->next->next->next == NULL);
  g_assert (q->head->next->next->next->next == q->tail);
  g_assert (q->tail->data == GINT_TO_POINTER (5));
  g_assert (q->tail->prev->data == GINT_TO_POINTER (4));
  g_assert (q->tail->prev->prev->data == GINT_TO_POINTER (3));
  g_assert (q->tail->prev->prev->prev->data == GINT_TO_POINTER (2));
  g_assert (q->tail->prev->prev->prev->prev->data == GINT_TO_POINTER (1));
  g_assert (q->tail->prev->prev->prev->prev->prev == NULL);
  g_assert (q->tail->prev->prev->prev->prev == q->head);
  g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (5));
  g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (1));

  g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (1));
  g_assert (g_list_length (q->head) == 4 && q->length == 4);
  g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (5));
  g_assert (g_list_length (q->head) == 3);
  g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (2));
  g_assert (g_list_length (q->head) == 2);
  g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (4));
  g_assert (g_list_length (q->head) == 1);
  g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (3));
  g_assert (g_list_length (q->head) == 0);
  g_assert (g_queue_pop_tail (q) == NULL);
  g_assert (g_list_length (q->head) == 0);
  g_assert (g_queue_pop_head (q) == NULL);
  g_assert (g_list_length (q->head) == 0);

  g_assert (g_queue_is_empty (q) == TRUE);

  /************************/

  g_queue_push_head (q, GINT_TO_POINTER (1));
  g_assert (g_list_length (q->head) == 1 && 1 == q->length);
  g_queue_push_head (q, GINT_TO_POINTER (2));
  g_assert (g_list_length (q->head) == 2 && 2 == q->length);
  g_queue_push_head (q, GINT_TO_POINTER (3));
  g_assert (g_list_length (q->head) == 3 && 3 == q->length);
  g_queue_push_head (q, GINT_TO_POINTER (4));
  g_assert (g_list_length (q->head) == 4 && 4 == q->length);
  g_queue_push_head (q, GINT_TO_POINTER (5));
  g_assert (g_list_length (q->head) == 5 && 5 == q->length);

  g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (5));
  g_assert (g_list_length (q->head) == 4);
  node = q->tail;
  g_assert (node == g_queue_pop_tail_link (q));
  g_assert (g_list_length (q->head) == 3);
  data = q->head->data;
  g_assert (data == g_queue_pop_head (q));
  g_assert (g_list_length (q->head) == 2);
  g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (2));
  g_assert (g_list_length (q->head) == 1);
  g_assert (q->head == q->tail);
  g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (3));
  g_assert (g_list_length (q->head) == 0);
  g_assert (g_queue_pop_head (q) == NULL);
  g_assert (g_queue_pop_head_link (q) == NULL);
  g_assert (g_list_length (q->head) == 0);
  g_assert (g_queue_pop_tail_link (q) == NULL);
  g_assert (g_list_length (q->head) == 0);

  g_queue_free (q);

  return 0;
}