101b75af307bdc1d6086a6055c2534a83c03add3
[platform/upstream/glib.git] / tests / queue-test.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #ifdef HAVE_CONFIG_H
5 #  include <config.h>
6 #endif
7 #include <glib.h>
8
9 int main()
10 {
11   GQueue *q;
12   GList *node;
13   gpointer data;
14
15   q = g_queue_new ();
16
17   g_assert (g_queue_is_empty (q) == TRUE);
18
19   g_queue_push_head (q, GINT_TO_POINTER (2));
20   g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (2));
21   g_assert (g_queue_is_empty (q) == FALSE);
22   g_assert (g_list_length (q->head) == 1);
23   g_assert (q->head == q->tail);
24   g_queue_push_head (q, GINT_TO_POINTER (1));
25   g_assert (q->head->next == q->tail);
26   g_assert (q->tail->prev == q->head);
27   g_assert (g_list_length (q->head) == 2);
28   g_assert (q->tail->data == GINT_TO_POINTER (2));
29   g_assert (q->head->data == GINT_TO_POINTER (1));
30   g_queue_push_tail (q, GINT_TO_POINTER (3));
31   g_assert (g_list_length (q->head) == 3);
32   g_assert (q->head->data == GINT_TO_POINTER (1));
33   g_assert (q->head->next->data == GINT_TO_POINTER (2));
34   g_assert (q->head->next->next == q->tail);
35   g_assert (q->head->next == q->tail->prev);
36   g_assert (q->tail->data == GINT_TO_POINTER (3));
37   g_queue_push_tail (q, GINT_TO_POINTER (4));
38   g_assert (g_list_length (q->head) == 4);
39   g_assert (q->head->data == GINT_TO_POINTER (1));
40   g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (4));
41   g_queue_push_tail (q, GINT_TO_POINTER (5));
42   g_assert (g_list_length (q->head) == 5);
43
44   g_assert (g_queue_is_empty (q) == FALSE);
45
46   g_assert (q->length == 5);
47   g_assert (q->head->prev == NULL);
48   g_assert (q->head->data == GINT_TO_POINTER (1));
49   g_assert (q->head->next->data == GINT_TO_POINTER (2));
50   g_assert (q->head->next->next->data == GINT_TO_POINTER (3));
51   g_assert (q->head->next->next->next->data == GINT_TO_POINTER (4));
52   g_assert (q->head->next->next->next->next->data == GINT_TO_POINTER (5));
53   g_assert (q->head->next->next->next->next->next == NULL);
54   g_assert (q->head->next->next->next->next == q->tail);
55   g_assert (q->tail->data == GINT_TO_POINTER (5));
56   g_assert (q->tail->prev->data == GINT_TO_POINTER (4));
57   g_assert (q->tail->prev->prev->data == GINT_TO_POINTER (3));
58   g_assert (q->tail->prev->prev->prev->data == GINT_TO_POINTER (2));
59   g_assert (q->tail->prev->prev->prev->prev->data == GINT_TO_POINTER (1));
60   g_assert (q->tail->prev->prev->prev->prev->prev == NULL);
61   g_assert (q->tail->prev->prev->prev->prev == q->head);
62   g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (5));
63   g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (1));
64
65   g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (1));
66   g_assert (g_list_length (q->head) == 4 && q->length == 4);
67   g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (5));
68   g_assert (g_list_length (q->head) == 3);
69   g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (2));
70   g_assert (g_list_length (q->head) == 2);
71   g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (4));
72   g_assert (g_list_length (q->head) == 1);
73   g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (3));
74   g_assert (g_list_length (q->head) == 0);
75   g_assert (g_queue_pop_tail (q) == NULL);
76   g_assert (g_list_length (q->head) == 0);
77   g_assert (g_queue_pop_head (q) == NULL);
78   g_assert (g_list_length (q->head) == 0);
79
80   g_assert (g_queue_is_empty (q) == TRUE);
81
82   /************************/
83
84   g_queue_push_head (q, GINT_TO_POINTER (1));
85   g_assert (g_list_length (q->head) == 1 && 1 == q->length);
86   g_queue_push_head (q, GINT_TO_POINTER (2));
87   g_assert (g_list_length (q->head) == 2 && 2 == q->length);
88   g_queue_push_head (q, GINT_TO_POINTER (3));
89   g_assert (g_list_length (q->head) == 3 && 3 == q->length);
90   g_queue_push_head (q, GINT_TO_POINTER (4));
91   g_assert (g_list_length (q->head) == 4 && 4 == q->length);
92   g_queue_push_head (q, GINT_TO_POINTER (5));
93   g_assert (g_list_length (q->head) == 5 && 5 == q->length);
94
95   g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (5));
96   g_assert (g_list_length (q->head) == 4);
97   node = q->tail;
98   g_assert (node == g_queue_pop_tail_link (q));
99   g_assert (g_list_length (q->head) == 3);
100   data = q->head->data;
101   g_assert (data == g_queue_pop_head (q));
102   g_assert (g_list_length (q->head) == 2);
103   g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (2));
104   g_assert (g_list_length (q->head) == 1);
105   g_assert (q->head == q->tail);
106   g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (3));
107   g_assert (g_list_length (q->head) == 0);
108   g_assert (g_queue_pop_head (q) == NULL);
109   g_assert (g_queue_pop_head_link (q) == NULL);
110   g_assert (g_list_length (q->head) == 0);
111   g_assert (g_queue_pop_tail_link (q) == NULL);
112   g_assert (g_list_length (q->head) == 0);
113
114   g_queue_free (q);
115
116   return 0;
117 }
118