b12ddec13d418a0af0ab3f2f168736dee8579ffb
[mono.git] / eglib / test / queue.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
5
6 RESULT
7 test_queue_push ()
8 {
9         GQueue *queue = g_queue_new ();
10
11         g_queue_push_head (queue, "foo");
12         g_queue_push_head (queue, "bar");
13         g_queue_push_head (queue, "baz");
14
15         if (queue->length != 3)
16                 return FAILED ("push failed");
17
18         if (NULL != queue->head->prev)
19                 return FAILED ("HEAD: prev is wrong");
20         if (strcmp ("baz", queue->head->data))
21                 return FAILED ("HEAD: First element is wrong");
22         if (strcmp ("bar", queue->head->next->data))
23                 return FAILED ("HEAD: Second element is wrong");
24         if (strcmp ("foo", queue->head->next->next->data))
25                 return FAILED ("HEAD: Third element is wrong");
26         if (NULL != queue->head->next->next->next)
27                 return FAILED ("HEAD: End is wrong");
28
29         if (NULL != queue->tail->next)
30                 return FAILED ("TAIL: next is wrong");
31         if (strcmp ("foo", queue->tail->data))
32                 return FAILED ("TAIL: Third element is wrong");
33         if (strcmp ("bar", queue->tail->prev->data))
34                 return FAILED ("TAIL: Second element is wrong");
35         if (strcmp ("baz", queue->tail->prev->prev->data))
36                 return FAILED ("TAIL: First element is wrong");
37         if (NULL != queue->tail->prev->prev->prev)
38                 return FAILED ("TAIL: End is wrong");
39
40         g_queue_free (queue);
41         return OK;
42 }
43
44 RESULT
45 test_queue_push_tail ()
46 {
47         GQueue *queue = g_queue_new ();
48
49         g_queue_push_tail (queue, "baz");
50         g_queue_push_tail (queue, "bar");
51         g_queue_push_tail (queue, "foo");
52
53         if (queue->length != 3)
54                 return FAILED ("push failed");
55
56         if (NULL != queue->head->prev)
57                 return FAILED ("HEAD: prev is wrong");
58         if (strcmp ("baz", queue->head->data))
59                 return FAILED ("HEAD: First element is wrong");
60         if (strcmp ("bar", queue->head->next->data))
61                 return FAILED ("HEAD: Second element is wrong");
62         if (strcmp ("foo", queue->head->next->next->data))
63                 return FAILED ("HEAD: Third element is wrong");
64         if (NULL != queue->head->next->next->next)
65                 return FAILED ("HEAD: End is wrong");
66
67         if (NULL != queue->tail->next)
68                 return FAILED ("TAIL: next is wrong");
69         if (strcmp ("foo", queue->tail->data))
70                 return FAILED ("TAIL: Third element is wrong");
71         if (strcmp ("bar", queue->tail->prev->data))
72                 return FAILED ("TAIL: Second element is wrong");
73         if (strcmp ("baz", queue->tail->prev->prev->data))
74                 return FAILED ("TAIL: First element is wrong");
75         if (NULL != queue->tail->prev->prev->prev)
76                 return FAILED ("TAIL: End is wrong");
77
78         g_queue_free (queue);
79         return OK;
80 }
81
82 RESULT
83 test_queue_pop ()
84 {
85         GQueue *queue = g_queue_new ();
86         gpointer data;
87
88         g_queue_push_head (queue, "foo");
89         g_queue_push_head (queue, "bar");
90         g_queue_push_head (queue, "baz");
91
92         data = g_queue_pop_head (queue);
93         if (strcmp ("baz", data))
94                 return FAILED ("expect baz.");
95
96         data = g_queue_pop_head (queue);
97         if (strcmp ("bar", data))
98                 return FAILED ("expect bar.");  
99
100         data = g_queue_pop_head (queue);
101         if (strcmp ("foo", data))
102                 return FAILED ("expect foo.");
103         
104         if (g_queue_is_empty (queue) == FALSE)
105                 return FAILED ("expect is_empty.");
106
107         if (queue->length != 0)
108                 return FAILED ("expect 0 length .");
109
110         g_queue_push_head (queue, "foo");
111         g_queue_push_head (queue, "bar");
112         g_queue_push_head (queue, "baz");
113
114         g_queue_pop_head (queue);
115
116         if (NULL != queue->head->prev)
117                 return FAILED ("HEAD: prev is wrong");
118         if (strcmp ("bar", queue->head->data))
119                 return FAILED ("HEAD: Second element is wrong");
120         if (strcmp ("foo", queue->head->next->data))
121                 return FAILED ("HEAD: Third element is wrong");
122         if (NULL != queue->head->next->next)
123                 return FAILED ("HEAD: End is wrong");
124
125         if (NULL != queue->tail->next)
126                 return FAILED ("TAIL: next is wrong");
127         if (strcmp ("foo", queue->tail->data))
128                 return FAILED ("TAIL: Second element is wrong");
129         if (strcmp ("bar", queue->tail->prev->data))
130                 return FAILED ("TAIL: First element is wrong");
131         if (NULL != queue->tail->prev->prev)
132                 return FAILED ("TAIL: End is wrong");
133
134         g_queue_free (queue);
135         return OK;
136 }
137
138 RESULT
139 test_queue_new ()
140 {
141         GQueue *queue = g_queue_new ();
142
143         if (queue->length != 0)
144                 return FAILED ("expect length == 0");
145
146         if (queue->head != NULL)
147                 return FAILED ("expect head == NULL");
148
149         if (queue->tail != NULL)
150                 return FAILED ("expect tail == NULL");
151
152         g_queue_free (queue);
153         return OK;
154 }
155
156 RESULT
157 test_queue_is_empty ()
158 {
159         GQueue *queue = g_queue_new ();
160
161         if (g_queue_is_empty (queue) == FALSE)
162                 return FAILED ("new queue should be empty");
163
164         g_queue_push_head (queue, "foo");
165
166         if (g_queue_is_empty (queue) == TRUE)
167                 return FAILED ("expected TRUE");
168
169         g_queue_free (queue);
170
171         return OK;
172 }
173
174 static Test queue_tests [] = {
175         {    "push", test_queue_push},
176         {"push_tail", test_queue_push_tail},
177         {     "pop", test_queue_pop},
178         {     "new", test_queue_new},
179         {"is_empty", test_queue_is_empty},
180         {NULL, NULL}
181 };
182
183 DEFINE_TEST_GROUP_INIT(queue_tests_init, queue_tests)
184