sl@0
|
1 |
/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
|
sl@0
|
2 |
#undef G_DISABLE_ASSERT
|
sl@0
|
3 |
#undef G_LOG_DOMAIN
|
sl@0
|
4 |
|
sl@0
|
5 |
#include <time.h>
|
sl@0
|
6 |
#include <stdlib.h>
|
sl@0
|
7 |
|
sl@0
|
8 |
#include <glib.h>
|
sl@0
|
9 |
|
sl@0
|
10 |
#ifdef __SYMBIAN32__
|
sl@0
|
11 |
#include "mrt2_glib2_test.h"
|
sl@0
|
12 |
#endif /*__SYMBIAN32__*/
|
sl@0
|
13 |
|
sl@0
|
14 |
static gboolean verbose = FALSE;
|
sl@0
|
15 |
|
sl@0
|
16 |
|
sl@0
|
17 |
static void
|
sl@0
|
18 |
check_integrity (GQueue *queue)
|
sl@0
|
19 |
{
|
sl@0
|
20 |
GList *list;
|
sl@0
|
21 |
GList *last;
|
sl@0
|
22 |
GList *links;
|
sl@0
|
23 |
GList *link;
|
sl@0
|
24 |
gint n;
|
sl@0
|
25 |
|
sl@0
|
26 |
g_assert (queue->length < 4000000000u);
|
sl@0
|
27 |
|
sl@0
|
28 |
g_assert (g_queue_get_length (queue) == queue->length);
|
sl@0
|
29 |
|
sl@0
|
30 |
if (!queue->head)
|
sl@0
|
31 |
g_assert (!queue->tail);
|
sl@0
|
32 |
if (!queue->tail)
|
sl@0
|
33 |
g_assert (!queue->head);
|
sl@0
|
34 |
|
sl@0
|
35 |
n = 0;
|
sl@0
|
36 |
last = NULL;
|
sl@0
|
37 |
for (list = queue->head; list != NULL; list = list->next)
|
sl@0
|
38 |
{
|
sl@0
|
39 |
if (!list->next)
|
sl@0
|
40 |
last = list;
|
sl@0
|
41 |
++n;
|
sl@0
|
42 |
}
|
sl@0
|
43 |
g_assert (n == queue->length);
|
sl@0
|
44 |
g_assert (last == queue->tail);
|
sl@0
|
45 |
|
sl@0
|
46 |
n = 0;
|
sl@0
|
47 |
last = NULL;
|
sl@0
|
48 |
for (list = queue->tail; list != NULL; list = list->prev)
|
sl@0
|
49 |
{
|
sl@0
|
50 |
if (!list->prev)
|
sl@0
|
51 |
last = list;
|
sl@0
|
52 |
++n;
|
sl@0
|
53 |
}
|
sl@0
|
54 |
g_assert (n == queue->length);
|
sl@0
|
55 |
g_assert (last == queue->head);
|
sl@0
|
56 |
|
sl@0
|
57 |
links = NULL;
|
sl@0
|
58 |
for (list = queue->head; list != NULL; list = list->next)
|
sl@0
|
59 |
links = g_list_prepend (links, list);
|
sl@0
|
60 |
|
sl@0
|
61 |
link = links;
|
sl@0
|
62 |
for (list = queue->tail; list != NULL; list = list->prev)
|
sl@0
|
63 |
{
|
sl@0
|
64 |
g_assert (list == link->data);
|
sl@0
|
65 |
link = link->next;
|
sl@0
|
66 |
}
|
sl@0
|
67 |
g_list_free (links);
|
sl@0
|
68 |
|
sl@0
|
69 |
links = NULL;
|
sl@0
|
70 |
for (list = queue->tail; list != NULL; list = list->prev)
|
sl@0
|
71 |
links = g_list_prepend (links, list);
|
sl@0
|
72 |
|
sl@0
|
73 |
link = links;
|
sl@0
|
74 |
for (list = queue->head; list != NULL; list = list->next)
|
sl@0
|
75 |
{
|
sl@0
|
76 |
g_assert (list == link->data);
|
sl@0
|
77 |
link = link->next;
|
sl@0
|
78 |
}
|
sl@0
|
79 |
g_list_free (links);
|
sl@0
|
80 |
}
|
sl@0
|
81 |
|
sl@0
|
82 |
static gboolean
|
sl@0
|
83 |
rnd_bool (void)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
return g_random_int_range (0, 2);
|
sl@0
|
86 |
}
|
sl@0
|
87 |
|
sl@0
|
88 |
static void
|
sl@0
|
89 |
check_max (gpointer elm, gpointer user_data)
|
sl@0
|
90 |
{
|
sl@0
|
91 |
gint *best = user_data;
|
sl@0
|
92 |
gint element = GPOINTER_TO_INT (elm);
|
sl@0
|
93 |
|
sl@0
|
94 |
if (element > *best)
|
sl@0
|
95 |
*best = element;
|
sl@0
|
96 |
}
|
sl@0
|
97 |
|
sl@0
|
98 |
static void
|
sl@0
|
99 |
check_min (gpointer elm, gpointer user_data)
|
sl@0
|
100 |
{
|
sl@0
|
101 |
gint *best = user_data;
|
sl@0
|
102 |
gint element = GPOINTER_TO_INT (elm);
|
sl@0
|
103 |
|
sl@0
|
104 |
if (element < *best)
|
sl@0
|
105 |
*best = element;
|
sl@0
|
106 |
}
|
sl@0
|
107 |
|
sl@0
|
108 |
static gint
|
sl@0
|
109 |
find_min (GQueue *queue)
|
sl@0
|
110 |
{
|
sl@0
|
111 |
gint min = G_MAXINT;
|
sl@0
|
112 |
|
sl@0
|
113 |
g_queue_foreach (queue, check_min, &min);
|
sl@0
|
114 |
|
sl@0
|
115 |
return min;
|
sl@0
|
116 |
}
|
sl@0
|
117 |
|
sl@0
|
118 |
static gint
|
sl@0
|
119 |
find_max (GQueue *queue)
|
sl@0
|
120 |
{
|
sl@0
|
121 |
gint max = G_MININT;
|
sl@0
|
122 |
|
sl@0
|
123 |
g_queue_foreach (queue, check_max, &max);
|
sl@0
|
124 |
|
sl@0
|
125 |
return max;
|
sl@0
|
126 |
}
|
sl@0
|
127 |
|
sl@0
|
128 |
static void
|
sl@0
|
129 |
delete_elm (gpointer elm, gpointer user_data)
|
sl@0
|
130 |
{
|
sl@0
|
131 |
g_queue_remove ((GQueue *)user_data, elm);
|
sl@0
|
132 |
check_integrity ((GQueue *)user_data);
|
sl@0
|
133 |
}
|
sl@0
|
134 |
|
sl@0
|
135 |
static void
|
sl@0
|
136 |
delete_all (GQueue *queue)
|
sl@0
|
137 |
{
|
sl@0
|
138 |
g_queue_foreach (queue, delete_elm, queue);
|
sl@0
|
139 |
}
|
sl@0
|
140 |
|
sl@0
|
141 |
static int
|
sl@0
|
142 |
compare_int (gconstpointer a, gconstpointer b, gpointer data)
|
sl@0
|
143 |
{
|
sl@0
|
144 |
int ai = GPOINTER_TO_INT (a);
|
sl@0
|
145 |
int bi = GPOINTER_TO_INT (b);
|
sl@0
|
146 |
|
sl@0
|
147 |
if (ai > bi)
|
sl@0
|
148 |
return 1;
|
sl@0
|
149 |
else if (ai == bi)
|
sl@0
|
150 |
return 0;
|
sl@0
|
151 |
else
|
sl@0
|
152 |
return -1;
|
sl@0
|
153 |
}
|
sl@0
|
154 |
|
sl@0
|
155 |
static gint
|
sl@0
|
156 |
get_random_position (GQueue *queue, gboolean allow_offlist)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
int n;
|
sl@0
|
159 |
enum { OFF_QUEUE, HEAD, TAIL, MIDDLE, LAST } where;
|
sl@0
|
160 |
|
sl@0
|
161 |
if (allow_offlist)
|
sl@0
|
162 |
where = g_random_int_range (OFF_QUEUE, LAST);
|
sl@0
|
163 |
else
|
sl@0
|
164 |
where = g_random_int_range (HEAD, LAST);
|
sl@0
|
165 |
|
sl@0
|
166 |
switch (where)
|
sl@0
|
167 |
{
|
sl@0
|
168 |
case OFF_QUEUE:
|
sl@0
|
169 |
n = g_random_int ();
|
sl@0
|
170 |
break;
|
sl@0
|
171 |
|
sl@0
|
172 |
case HEAD:
|
sl@0
|
173 |
n = 0;
|
sl@0
|
174 |
break;
|
sl@0
|
175 |
|
sl@0
|
176 |
case TAIL:
|
sl@0
|
177 |
if (allow_offlist)
|
sl@0
|
178 |
n = queue->length;
|
sl@0
|
179 |
else
|
sl@0
|
180 |
n = queue->length - 1;
|
sl@0
|
181 |
break;
|
sl@0
|
182 |
|
sl@0
|
183 |
case MIDDLE:
|
sl@0
|
184 |
if (queue->length == 0)
|
sl@0
|
185 |
n = 0;
|
sl@0
|
186 |
else
|
sl@0
|
187 |
n = g_random_int_range (0, queue->length);
|
sl@0
|
188 |
break;
|
sl@0
|
189 |
|
sl@0
|
190 |
default:
|
sl@0
|
191 |
g_assert_not_reached();
|
sl@0
|
192 |
n = 100;
|
sl@0
|
193 |
break;
|
sl@0
|
194 |
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
return n;
|
sl@0
|
198 |
}
|
sl@0
|
199 |
|
sl@0
|
200 |
static void
|
sl@0
|
201 |
random_test (int seed)
|
sl@0
|
202 |
{
|
sl@0
|
203 |
typedef enum {
|
sl@0
|
204 |
IS_EMPTY, GET_LENGTH, REVERSE, COPY,
|
sl@0
|
205 |
FOREACH, FIND, FIND_CUSTOM, SORT,
|
sl@0
|
206 |
PUSH_HEAD, PUSH_TAIL, PUSH_NTH, POP_HEAD,
|
sl@0
|
207 |
POP_TAIL, POP_NTH, PEEK_HEAD, PEEK_TAIL,
|
sl@0
|
208 |
PEEK_NTH, INDEX, REMOVE, REMOVE_ALL,
|
sl@0
|
209 |
INSERT_BEFORE, INSERT_AFTER, INSERT_SORTED, PUSH_HEAD_LINK,
|
sl@0
|
210 |
PUSH_TAIL_LINK, PUSH_NTH_LINK, POP_HEAD_LINK, POP_TAIL_LINK,
|
sl@0
|
211 |
POP_NTH_LINK, PEEK_HEAD_LINK, PEEK_TAIL_LINK, PEEK_NTH_LINK,
|
sl@0
|
212 |
LINK_INDEX, UNLINK, DELETE_LINK, LAST_OP
|
sl@0
|
213 |
} QueueOp;
|
sl@0
|
214 |
|
sl@0
|
215 |
#ifdef __SYMBIAN32__
|
sl@0
|
216 |
#define N_ITERATIONS 50000
|
sl@0
|
217 |
#else
|
sl@0
|
218 |
#define N_ITERATIONS 500000
|
sl@0
|
219 |
#endif//__SYMBIAN32__
|
sl@0
|
220 |
#define N_QUEUES 3
|
sl@0
|
221 |
|
sl@0
|
222 |
#define RANDOM_QUEUE() &(queues[g_random_int_range(0, N_QUEUES)])
|
sl@0
|
223 |
|
sl@0
|
224 |
typedef struct QueueInfo QueueInfo;
|
sl@0
|
225 |
struct QueueInfo
|
sl@0
|
226 |
{
|
sl@0
|
227 |
GQueue *queue;
|
sl@0
|
228 |
GList *tail;
|
sl@0
|
229 |
GList *head;
|
sl@0
|
230 |
guint length;
|
sl@0
|
231 |
};
|
sl@0
|
232 |
|
sl@0
|
233 |
gint i;
|
sl@0
|
234 |
QueueOp op;
|
sl@0
|
235 |
QueueInfo queues[N_QUEUES];
|
sl@0
|
236 |
|
sl@0
|
237 |
if (verbose)
|
sl@0
|
238 |
g_print ("seed: %d\n", seed);
|
sl@0
|
239 |
|
sl@0
|
240 |
g_random_set_seed (seed);
|
sl@0
|
241 |
|
sl@0
|
242 |
for (i = 0; i < N_QUEUES; ++i)
|
sl@0
|
243 |
{
|
sl@0
|
244 |
queues[i].queue = g_queue_new ();
|
sl@0
|
245 |
queues[i].head = NULL;
|
sl@0
|
246 |
queues[i].tail = NULL;
|
sl@0
|
247 |
queues[i].length = 0;
|
sl@0
|
248 |
}
|
sl@0
|
249 |
|
sl@0
|
250 |
for (i = 0; i < N_ITERATIONS; ++i)
|
sl@0
|
251 |
{
|
sl@0
|
252 |
int j;
|
sl@0
|
253 |
QueueInfo *qinf = RANDOM_QUEUE();
|
sl@0
|
254 |
GQueue *q = qinf->queue;
|
sl@0
|
255 |
op = g_random_int_range (IS_EMPTY, LAST_OP);
|
sl@0
|
256 |
|
sl@0
|
257 |
g_assert (qinf->head == q->head);
|
sl@0
|
258 |
g_assert (qinf->tail == q->tail);
|
sl@0
|
259 |
g_assert (qinf->length == q->length);
|
sl@0
|
260 |
|
sl@0
|
261 |
switch (op)
|
sl@0
|
262 |
{
|
sl@0
|
263 |
case IS_EMPTY:
|
sl@0
|
264 |
{
|
sl@0
|
265 |
if (g_queue_is_empty (qinf->queue))
|
sl@0
|
266 |
{
|
sl@0
|
267 |
g_assert (q->head == NULL);
|
sl@0
|
268 |
g_assert (q->tail == NULL);
|
sl@0
|
269 |
g_assert (q->length == 0);
|
sl@0
|
270 |
}
|
sl@0
|
271 |
else
|
sl@0
|
272 |
{
|
sl@0
|
273 |
g_assert (q->head);
|
sl@0
|
274 |
g_assert (q->tail);
|
sl@0
|
275 |
g_assert (q->length > 0);
|
sl@0
|
276 |
}
|
sl@0
|
277 |
}
|
sl@0
|
278 |
break;
|
sl@0
|
279 |
case GET_LENGTH:
|
sl@0
|
280 |
{
|
sl@0
|
281 |
int l;
|
sl@0
|
282 |
|
sl@0
|
283 |
l = g_queue_get_length (q);
|
sl@0
|
284 |
|
sl@0
|
285 |
g_assert (qinf->length == q->length);
|
sl@0
|
286 |
g_assert (qinf->length == l);
|
sl@0
|
287 |
}
|
sl@0
|
288 |
break;
|
sl@0
|
289 |
case REVERSE:
|
sl@0
|
290 |
g_queue_reverse (q);
|
sl@0
|
291 |
g_assert (qinf->tail == q->head);
|
sl@0
|
292 |
g_assert (qinf->head == q->tail);
|
sl@0
|
293 |
g_assert (qinf->length == q->length);
|
sl@0
|
294 |
qinf->tail = q->tail;
|
sl@0
|
295 |
qinf->head = q->head;
|
sl@0
|
296 |
break;
|
sl@0
|
297 |
case COPY:
|
sl@0
|
298 |
{
|
sl@0
|
299 |
QueueInfo *random_queue = RANDOM_QUEUE();
|
sl@0
|
300 |
GQueue *new_queue = g_queue_copy (random_queue->queue);
|
sl@0
|
301 |
|
sl@0
|
302 |
g_queue_free (qinf->queue);
|
sl@0
|
303 |
q = qinf->queue = new_queue;
|
sl@0
|
304 |
qinf->head = new_queue->head;
|
sl@0
|
305 |
qinf->tail = g_list_last (new_queue->head);
|
sl@0
|
306 |
qinf->length = new_queue->length;
|
sl@0
|
307 |
}
|
sl@0
|
308 |
break;
|
sl@0
|
309 |
case FOREACH:
|
sl@0
|
310 |
delete_all (q);
|
sl@0
|
311 |
qinf->head = NULL;
|
sl@0
|
312 |
qinf->tail = NULL;
|
sl@0
|
313 |
qinf->length = 0;
|
sl@0
|
314 |
break;
|
sl@0
|
315 |
case FIND:
|
sl@0
|
316 |
{
|
sl@0
|
317 |
gboolean find_existing = rnd_bool ();
|
sl@0
|
318 |
int first = find_max (q);
|
sl@0
|
319 |
int second = find_min (q);
|
sl@0
|
320 |
|
sl@0
|
321 |
if (q->length == 0)
|
sl@0
|
322 |
find_existing = FALSE;
|
sl@0
|
323 |
|
sl@0
|
324 |
if (!find_existing)
|
sl@0
|
325 |
first++;
|
sl@0
|
326 |
if (!find_existing)
|
sl@0
|
327 |
second--;
|
sl@0
|
328 |
|
sl@0
|
329 |
if (find_existing)
|
sl@0
|
330 |
{
|
sl@0
|
331 |
g_assert (g_queue_find (q, GINT_TO_POINTER (first)));
|
sl@0
|
332 |
g_assert (g_queue_find (q, GINT_TO_POINTER (second)));
|
sl@0
|
333 |
}
|
sl@0
|
334 |
else
|
sl@0
|
335 |
{
|
sl@0
|
336 |
g_assert (!g_queue_find (q, GINT_TO_POINTER (first)));
|
sl@0
|
337 |
g_assert (!g_queue_find (q, GINT_TO_POINTER (second)));
|
sl@0
|
338 |
}
|
sl@0
|
339 |
}
|
sl@0
|
340 |
break;
|
sl@0
|
341 |
case FIND_CUSTOM:
|
sl@0
|
342 |
break;
|
sl@0
|
343 |
case SORT:
|
sl@0
|
344 |
{
|
sl@0
|
345 |
if (!g_queue_is_empty (q))
|
sl@0
|
346 |
{
|
sl@0
|
347 |
int max = find_max (q);
|
sl@0
|
348 |
int min = find_min (q);
|
sl@0
|
349 |
g_queue_remove_all (q, GINT_TO_POINTER (max));
|
sl@0
|
350 |
check_integrity (q);
|
sl@0
|
351 |
g_queue_remove_all (q, GINT_TO_POINTER (min));
|
sl@0
|
352 |
check_integrity (q);
|
sl@0
|
353 |
g_queue_push_head (q, GINT_TO_POINTER (max));
|
sl@0
|
354 |
if (max != min)
|
sl@0
|
355 |
g_queue_push_head (q, GINT_TO_POINTER (min));
|
sl@0
|
356 |
qinf->length = q->length;
|
sl@0
|
357 |
}
|
sl@0
|
358 |
|
sl@0
|
359 |
check_integrity (q);
|
sl@0
|
360 |
|
sl@0
|
361 |
g_queue_sort (q, compare_int, NULL);
|
sl@0
|
362 |
|
sl@0
|
363 |
check_integrity (q);
|
sl@0
|
364 |
|
sl@0
|
365 |
qinf->head = g_queue_find (q, GINT_TO_POINTER (find_min(q)));
|
sl@0
|
366 |
qinf->tail = g_queue_find (q, GINT_TO_POINTER (find_max(q)));
|
sl@0
|
367 |
|
sl@0
|
368 |
g_assert (qinf->tail == q->tail);
|
sl@0
|
369 |
}
|
sl@0
|
370 |
break;
|
sl@0
|
371 |
case PUSH_HEAD:
|
sl@0
|
372 |
{
|
sl@0
|
373 |
int x = g_random_int_range (0, 435435);
|
sl@0
|
374 |
g_queue_push_head (q, GINT_TO_POINTER (x));
|
sl@0
|
375 |
if (!qinf->head)
|
sl@0
|
376 |
qinf->tail = qinf->head = q->head;
|
sl@0
|
377 |
else
|
sl@0
|
378 |
qinf->head = qinf->head->prev;
|
sl@0
|
379 |
qinf->length++;
|
sl@0
|
380 |
}
|
sl@0
|
381 |
break;
|
sl@0
|
382 |
case PUSH_TAIL:
|
sl@0
|
383 |
{
|
sl@0
|
384 |
int x = g_random_int_range (0, 236546);
|
sl@0
|
385 |
g_queue_push_tail (q, GINT_TO_POINTER (x));
|
sl@0
|
386 |
if (!qinf->tail)
|
sl@0
|
387 |
qinf->tail = qinf->head = q->head;
|
sl@0
|
388 |
else
|
sl@0
|
389 |
qinf->tail = qinf->tail->next;
|
sl@0
|
390 |
qinf->length++;
|
sl@0
|
391 |
}
|
sl@0
|
392 |
break;
|
sl@0
|
393 |
case PUSH_NTH:
|
sl@0
|
394 |
{
|
sl@0
|
395 |
int pos = get_random_position (q, TRUE);
|
sl@0
|
396 |
int x = g_random_int_range (0, 236546);
|
sl@0
|
397 |
g_queue_push_nth (q, GINT_TO_POINTER (x), pos);
|
sl@0
|
398 |
if (qinf->head && qinf->head->prev)
|
sl@0
|
399 |
qinf->head = qinf->head->prev;
|
sl@0
|
400 |
else
|
sl@0
|
401 |
qinf->head = q->head;
|
sl@0
|
402 |
if (qinf->tail && qinf->tail->next)
|
sl@0
|
403 |
qinf->tail = qinf->tail->next;
|
sl@0
|
404 |
else
|
sl@0
|
405 |
qinf->tail = g_list_last (qinf->head);
|
sl@0
|
406 |
qinf->length++;
|
sl@0
|
407 |
}
|
sl@0
|
408 |
break;
|
sl@0
|
409 |
case POP_HEAD:
|
sl@0
|
410 |
if (qinf->head)
|
sl@0
|
411 |
qinf->head = qinf->head->next;
|
sl@0
|
412 |
if (!qinf->head)
|
sl@0
|
413 |
qinf->tail = NULL;
|
sl@0
|
414 |
qinf->length = (qinf->length == 0)? 0 : qinf->length - 1;
|
sl@0
|
415 |
g_queue_pop_head (q);
|
sl@0
|
416 |
break;
|
sl@0
|
417 |
case POP_TAIL:
|
sl@0
|
418 |
if (qinf->tail)
|
sl@0
|
419 |
qinf->tail = qinf->tail->prev;
|
sl@0
|
420 |
if (!qinf->tail)
|
sl@0
|
421 |
qinf->head = NULL;
|
sl@0
|
422 |
qinf->length = (qinf->length == 0)? 0 : qinf->length - 1;
|
sl@0
|
423 |
g_queue_pop_tail (q);
|
sl@0
|
424 |
break;
|
sl@0
|
425 |
case POP_NTH:
|
sl@0
|
426 |
if (!g_queue_is_empty (q))
|
sl@0
|
427 |
{
|
sl@0
|
428 |
int n = get_random_position (q, TRUE);
|
sl@0
|
429 |
gpointer elm = g_queue_peek_nth (q, n);
|
sl@0
|
430 |
|
sl@0
|
431 |
if (n == q->length - 1)
|
sl@0
|
432 |
qinf->tail = qinf->tail->prev;
|
sl@0
|
433 |
|
sl@0
|
434 |
if (n == 0)
|
sl@0
|
435 |
qinf->head = qinf->head->next;
|
sl@0
|
436 |
|
sl@0
|
437 |
if (n >= 0 && n < q->length)
|
sl@0
|
438 |
qinf->length--;
|
sl@0
|
439 |
|
sl@0
|
440 |
g_assert (elm == g_queue_pop_nth (q, n));
|
sl@0
|
441 |
}
|
sl@0
|
442 |
break;
|
sl@0
|
443 |
case PEEK_HEAD:
|
sl@0
|
444 |
if (qinf->head)
|
sl@0
|
445 |
g_assert (qinf->head->data == g_queue_peek_head (q));
|
sl@0
|
446 |
else
|
sl@0
|
447 |
g_assert (g_queue_peek_head (q) == NULL);
|
sl@0
|
448 |
break;
|
sl@0
|
449 |
case PEEK_TAIL:
|
sl@0
|
450 |
if (qinf->head)
|
sl@0
|
451 |
g_assert (qinf->tail->data == g_queue_peek_tail (q));
|
sl@0
|
452 |
else
|
sl@0
|
453 |
g_assert (g_queue_peek_tail (q) == NULL);
|
sl@0
|
454 |
break;
|
sl@0
|
455 |
case PEEK_NTH:
|
sl@0
|
456 |
if (g_queue_is_empty (q))
|
sl@0
|
457 |
{
|
sl@0
|
458 |
for (j = -10; j < 10; ++j)
|
sl@0
|
459 |
g_assert (g_queue_peek_nth (q, j) == NULL);
|
sl@0
|
460 |
}
|
sl@0
|
461 |
else
|
sl@0
|
462 |
{
|
sl@0
|
463 |
GList *list;
|
sl@0
|
464 |
int n = get_random_position (q, TRUE);
|
sl@0
|
465 |
if (n < 0 || n >= q->length)
|
sl@0
|
466 |
{
|
sl@0
|
467 |
g_assert (g_queue_peek_nth (q, n) == NULL);
|
sl@0
|
468 |
}
|
sl@0
|
469 |
else
|
sl@0
|
470 |
{
|
sl@0
|
471 |
list = qinf->head;
|
sl@0
|
472 |
for (j = 0; j < n; ++j)
|
sl@0
|
473 |
list = list->next;
|
sl@0
|
474 |
|
sl@0
|
475 |
g_assert (list->data == g_queue_peek_nth (q, n));
|
sl@0
|
476 |
}
|
sl@0
|
477 |
}
|
sl@0
|
478 |
break;
|
sl@0
|
479 |
case INDEX:
|
sl@0
|
480 |
case LINK_INDEX:
|
sl@0
|
481 |
{
|
sl@0
|
482 |
int x = g_random_int_range (0, 386538);
|
sl@0
|
483 |
int n;
|
sl@0
|
484 |
GList *list;
|
sl@0
|
485 |
|
sl@0
|
486 |
g_queue_remove_all (q, GINT_TO_POINTER (x));
|
sl@0
|
487 |
check_integrity (q);
|
sl@0
|
488 |
g_queue_push_tail (q, GINT_TO_POINTER (x));
|
sl@0
|
489 |
check_integrity (q);
|
sl@0
|
490 |
g_queue_sort (q, compare_int, NULL);
|
sl@0
|
491 |
check_integrity (q);
|
sl@0
|
492 |
|
sl@0
|
493 |
n = 0;
|
sl@0
|
494 |
for (list = q->head; list != NULL; list = list->next)
|
sl@0
|
495 |
{
|
sl@0
|
496 |
if (list->data == GINT_TO_POINTER (x))
|
sl@0
|
497 |
break;
|
sl@0
|
498 |
n++;
|
sl@0
|
499 |
}
|
sl@0
|
500 |
g_assert (list);
|
sl@0
|
501 |
g_assert (g_queue_index (q, GINT_TO_POINTER (x)) ==
|
sl@0
|
502 |
g_queue_link_index (q, list));
|
sl@0
|
503 |
g_assert (g_queue_link_index (q, list) == n);
|
sl@0
|
504 |
|
sl@0
|
505 |
qinf->head = q->head;
|
sl@0
|
506 |
qinf->tail = q->tail;
|
sl@0
|
507 |
qinf->length = q->length;
|
sl@0
|
508 |
}
|
sl@0
|
509 |
break;
|
sl@0
|
510 |
case REMOVE:
|
sl@0
|
511 |
if (!g_queue_is_empty (q))
|
sl@0
|
512 |
g_queue_remove (q, qinf->tail->data);
|
sl@0
|
513 |
if (!g_queue_is_empty (q))
|
sl@0
|
514 |
g_queue_remove (q, qinf->head->data);
|
sl@0
|
515 |
if (!g_queue_is_empty (q))
|
sl@0
|
516 |
g_queue_remove (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
|
sl@0
|
517 |
|
sl@0
|
518 |
qinf->head = q->head;
|
sl@0
|
519 |
qinf->tail = q->tail;
|
sl@0
|
520 |
qinf->length = q->length;
|
sl@0
|
521 |
break;
|
sl@0
|
522 |
case REMOVE_ALL:
|
sl@0
|
523 |
if (!g_queue_is_empty (q))
|
sl@0
|
524 |
g_queue_remove_all (q, qinf->tail->data);
|
sl@0
|
525 |
if (!g_queue_is_empty (q))
|
sl@0
|
526 |
g_queue_remove_all (q, qinf->head->data);
|
sl@0
|
527 |
if (!g_queue_is_empty (q))
|
sl@0
|
528 |
g_queue_remove_all (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
|
sl@0
|
529 |
|
sl@0
|
530 |
qinf->head = q->head;
|
sl@0
|
531 |
qinf->tail = q->tail;
|
sl@0
|
532 |
qinf->length = q->length;
|
sl@0
|
533 |
break;
|
sl@0
|
534 |
case INSERT_BEFORE:
|
sl@0
|
535 |
if (!g_queue_is_empty (q))
|
sl@0
|
536 |
{
|
sl@0
|
537 |
gpointer x = GINT_TO_POINTER (g_random_int_range (0, 386538));
|
sl@0
|
538 |
|
sl@0
|
539 |
g_queue_insert_before (q, qinf->tail, x);
|
sl@0
|
540 |
g_queue_insert_before (q, qinf->head, x);
|
sl@0
|
541 |
g_queue_insert_before (q, g_queue_find (q, x), x);
|
sl@0
|
542 |
}
|
sl@0
|
543 |
qinf->head = q->head;
|
sl@0
|
544 |
qinf->tail = q->tail;
|
sl@0
|
545 |
qinf->length = q->length;
|
sl@0
|
546 |
break;
|
sl@0
|
547 |
case INSERT_AFTER:
|
sl@0
|
548 |
if (!g_queue_is_empty (q))
|
sl@0
|
549 |
{
|
sl@0
|
550 |
gpointer x = GINT_TO_POINTER (g_random_int_range (0, 386538));
|
sl@0
|
551 |
|
sl@0
|
552 |
g_queue_insert_after (q, qinf->tail, x);
|
sl@0
|
553 |
g_queue_insert_after (q, qinf->head, x);
|
sl@0
|
554 |
g_queue_insert_after (q, g_queue_find (q, x), x);
|
sl@0
|
555 |
}
|
sl@0
|
556 |
qinf->head = q->head;
|
sl@0
|
557 |
qinf->tail = q->tail;
|
sl@0
|
558 |
qinf->length = q->length;
|
sl@0
|
559 |
break;
|
sl@0
|
560 |
case INSERT_SORTED:
|
sl@0
|
561 |
{
|
sl@0
|
562 |
int max = find_max (q);
|
sl@0
|
563 |
int min = find_min (q);
|
sl@0
|
564 |
|
sl@0
|
565 |
if (g_queue_is_empty (q))
|
sl@0
|
566 |
{
|
sl@0
|
567 |
max = 345;
|
sl@0
|
568 |
min = -12;
|
sl@0
|
569 |
}
|
sl@0
|
570 |
|
sl@0
|
571 |
g_queue_sort (q, compare_int, NULL);
|
sl@0
|
572 |
check_integrity (q);
|
sl@0
|
573 |
g_queue_insert_sorted (q, GINT_TO_POINTER (max + 1), compare_int, NULL);
|
sl@0
|
574 |
check_integrity (q);
|
sl@0
|
575 |
g_assert (GPOINTER_TO_INT (q->tail->data) == max + 1);
|
sl@0
|
576 |
g_queue_insert_sorted (q, GINT_TO_POINTER (min - 1), compare_int, NULL);
|
sl@0
|
577 |
check_integrity (q);
|
sl@0
|
578 |
g_assert (GPOINTER_TO_INT (q->head->data) == min - 1);
|
sl@0
|
579 |
qinf->head = q->head;
|
sl@0
|
580 |
qinf->tail = q->tail;
|
sl@0
|
581 |
qinf->length = q->length;
|
sl@0
|
582 |
}
|
sl@0
|
583 |
break;
|
sl@0
|
584 |
case PUSH_HEAD_LINK:
|
sl@0
|
585 |
{
|
sl@0
|
586 |
GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
|
sl@0
|
587 |
g_queue_push_head_link (q, link);
|
sl@0
|
588 |
if (!qinf->tail)
|
sl@0
|
589 |
qinf->tail = link;
|
sl@0
|
590 |
qinf->head = link;
|
sl@0
|
591 |
qinf->length++;
|
sl@0
|
592 |
}
|
sl@0
|
593 |
break;
|
sl@0
|
594 |
case PUSH_TAIL_LINK:
|
sl@0
|
595 |
{
|
sl@0
|
596 |
GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
|
sl@0
|
597 |
g_queue_push_tail_link (q, link);
|
sl@0
|
598 |
if (!qinf->head)
|
sl@0
|
599 |
qinf->head = link;
|
sl@0
|
600 |
qinf->tail = link;
|
sl@0
|
601 |
qinf->length++;
|
sl@0
|
602 |
}
|
sl@0
|
603 |
break;
|
sl@0
|
604 |
case PUSH_NTH_LINK:
|
sl@0
|
605 |
{
|
sl@0
|
606 |
GList *link = g_list_prepend (NULL, GINT_TO_POINTER (i));
|
sl@0
|
607 |
gint n = get_random_position (q, TRUE);
|
sl@0
|
608 |
g_queue_push_nth_link (q, n, link);
|
sl@0
|
609 |
|
sl@0
|
610 |
if (qinf->head && qinf->head->prev)
|
sl@0
|
611 |
qinf->head = qinf->head->prev;
|
sl@0
|
612 |
else
|
sl@0
|
613 |
qinf->head = q->head;
|
sl@0
|
614 |
if (qinf->tail && qinf->tail->next)
|
sl@0
|
615 |
qinf->tail = qinf->tail->next;
|
sl@0
|
616 |
else
|
sl@0
|
617 |
qinf->tail = g_list_last (qinf->head);
|
sl@0
|
618 |
qinf->length++;
|
sl@0
|
619 |
}
|
sl@0
|
620 |
break;
|
sl@0
|
621 |
case POP_HEAD_LINK:
|
sl@0
|
622 |
if (!g_queue_is_empty (q))
|
sl@0
|
623 |
{
|
sl@0
|
624 |
qinf->head = qinf->head->next;
|
sl@0
|
625 |
if (!qinf->head)
|
sl@0
|
626 |
qinf->tail = NULL;
|
sl@0
|
627 |
qinf->length--;
|
sl@0
|
628 |
g_list_free (g_queue_pop_head_link (q));
|
sl@0
|
629 |
}
|
sl@0
|
630 |
break;
|
sl@0
|
631 |
case POP_TAIL_LINK:
|
sl@0
|
632 |
if (!g_queue_is_empty (q))
|
sl@0
|
633 |
{
|
sl@0
|
634 |
qinf->tail = qinf->tail->prev;
|
sl@0
|
635 |
if (!qinf->tail)
|
sl@0
|
636 |
qinf->head = NULL;
|
sl@0
|
637 |
qinf->length--;
|
sl@0
|
638 |
g_list_free (g_queue_pop_tail_link (q));
|
sl@0
|
639 |
}
|
sl@0
|
640 |
break;
|
sl@0
|
641 |
case POP_NTH_LINK:
|
sl@0
|
642 |
if (g_queue_is_empty (q))
|
sl@0
|
643 |
g_assert (g_queue_pop_nth_link (q, 200) == NULL);
|
sl@0
|
644 |
else
|
sl@0
|
645 |
{
|
sl@0
|
646 |
int n = get_random_position (q, FALSE);
|
sl@0
|
647 |
|
sl@0
|
648 |
if (n == g_queue_get_length (q) - 1)
|
sl@0
|
649 |
qinf->tail = qinf->tail->prev;
|
sl@0
|
650 |
|
sl@0
|
651 |
if (n == 0)
|
sl@0
|
652 |
qinf->head = qinf->head->next;
|
sl@0
|
653 |
|
sl@0
|
654 |
qinf->length--;
|
sl@0
|
655 |
|
sl@0
|
656 |
g_list_free (g_queue_pop_nth_link (q, n));
|
sl@0
|
657 |
}
|
sl@0
|
658 |
break;
|
sl@0
|
659 |
case PEEK_HEAD_LINK:
|
sl@0
|
660 |
if (g_queue_is_empty (q))
|
sl@0
|
661 |
g_assert (g_queue_peek_head_link (q) == NULL);
|
sl@0
|
662 |
else
|
sl@0
|
663 |
g_assert (g_queue_peek_head_link (q) == qinf->head);
|
sl@0
|
664 |
break;
|
sl@0
|
665 |
case PEEK_TAIL_LINK:
|
sl@0
|
666 |
if (g_queue_is_empty (q))
|
sl@0
|
667 |
g_assert (g_queue_peek_tail_link (q) == NULL);
|
sl@0
|
668 |
else
|
sl@0
|
669 |
g_assert (g_queue_peek_tail_link (q) == qinf->tail);
|
sl@0
|
670 |
break;
|
sl@0
|
671 |
case PEEK_NTH_LINK:
|
sl@0
|
672 |
if (g_queue_is_empty(q))
|
sl@0
|
673 |
g_assert (g_queue_peek_nth_link (q, 1000) == NULL);
|
sl@0
|
674 |
else
|
sl@0
|
675 |
{
|
sl@0
|
676 |
gint n = get_random_position (q, FALSE);
|
sl@0
|
677 |
GList *link;
|
sl@0
|
678 |
|
sl@0
|
679 |
link = q->head;
|
sl@0
|
680 |
for (j = 0; j < n; ++j)
|
sl@0
|
681 |
link = link->next;
|
sl@0
|
682 |
|
sl@0
|
683 |
g_assert (g_queue_peek_nth_link (q, n) == link);
|
sl@0
|
684 |
}
|
sl@0
|
685 |
break;
|
sl@0
|
686 |
case UNLINK:
|
sl@0
|
687 |
if (!g_queue_is_empty (q))
|
sl@0
|
688 |
{
|
sl@0
|
689 |
gint n = g_random_int_range (0, g_queue_get_length (q));
|
sl@0
|
690 |
GList *link;
|
sl@0
|
691 |
|
sl@0
|
692 |
link = q->head;
|
sl@0
|
693 |
for (j = 0; j < n; ++j)
|
sl@0
|
694 |
link = link->next;
|
sl@0
|
695 |
|
sl@0
|
696 |
g_queue_unlink (q, link);
|
sl@0
|
697 |
check_integrity (q);
|
sl@0
|
698 |
|
sl@0
|
699 |
g_list_free (link);
|
sl@0
|
700 |
|
sl@0
|
701 |
qinf->head = q->head;
|
sl@0
|
702 |
qinf->tail = q->tail;
|
sl@0
|
703 |
qinf->length--;
|
sl@0
|
704 |
}
|
sl@0
|
705 |
break;
|
sl@0
|
706 |
case DELETE_LINK:
|
sl@0
|
707 |
if (!g_queue_is_empty (q))
|
sl@0
|
708 |
{
|
sl@0
|
709 |
gint n = g_random_int_range (0, g_queue_get_length (q));
|
sl@0
|
710 |
GList *link;
|
sl@0
|
711 |
|
sl@0
|
712 |
link = q->head;
|
sl@0
|
713 |
for (j = 0; j < n; ++j)
|
sl@0
|
714 |
link = link->next;
|
sl@0
|
715 |
|
sl@0
|
716 |
g_queue_delete_link (q, link);
|
sl@0
|
717 |
check_integrity (q);
|
sl@0
|
718 |
|
sl@0
|
719 |
qinf->head = q->head;
|
sl@0
|
720 |
qinf->tail = q->tail;
|
sl@0
|
721 |
qinf->length--;
|
sl@0
|
722 |
}
|
sl@0
|
723 |
break;
|
sl@0
|
724 |
case LAST_OP:
|
sl@0
|
725 |
default:
|
sl@0
|
726 |
g_assert_not_reached();
|
sl@0
|
727 |
break;
|
sl@0
|
728 |
}
|
sl@0
|
729 |
|
sl@0
|
730 |
if (qinf->head != q->head ||
|
sl@0
|
731 |
qinf->tail != q->tail ||
|
sl@0
|
732 |
qinf->length != q->length)
|
sl@0
|
733 |
g_print ("op: %d\n", op);
|
sl@0
|
734 |
|
sl@0
|
735 |
g_assert (qinf->head == q->head);
|
sl@0
|
736 |
g_assert (qinf->tail == q->tail);
|
sl@0
|
737 |
g_assert (qinf->length == q->length);
|
sl@0
|
738 |
|
sl@0
|
739 |
for (j = 0; j < N_QUEUES; ++j)
|
sl@0
|
740 |
check_integrity (queues[j].queue);
|
sl@0
|
741 |
}
|
sl@0
|
742 |
|
sl@0
|
743 |
for (i = 0; i < N_QUEUES; ++i)
|
sl@0
|
744 |
g_queue_free (queues[i].queue);
|
sl@0
|
745 |
}
|
sl@0
|
746 |
|
sl@0
|
747 |
static void
|
sl@0
|
748 |
remove_item (gpointer data, gpointer q)
|
sl@0
|
749 |
{
|
sl@0
|
750 |
GQueue *queue = q;
|
sl@0
|
751 |
|
sl@0
|
752 |
g_queue_remove (queue, data);
|
sl@0
|
753 |
}
|
sl@0
|
754 |
|
sl@0
|
755 |
int main(int argc, gchar *args[])
|
sl@0
|
756 |
{
|
sl@0
|
757 |
GQueue *q, *q2;
|
sl@0
|
758 |
GList *node;
|
sl@0
|
759 |
gpointer data;
|
sl@0
|
760 |
int i;
|
sl@0
|
761 |
|
sl@0
|
762 |
#ifdef __SYMBIAN32__
|
sl@0
|
763 |
g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
|
sl@0
|
764 |
g_set_print_handler(mrtPrintHandler);
|
sl@0
|
765 |
#endif /*__SYMBIAN32__*/
|
sl@0
|
766 |
|
sl@0
|
767 |
if (argc > 1 && args[1][0] == '-' && args[1][1] == 'v')
|
sl@0
|
768 |
verbose = TRUE;
|
sl@0
|
769 |
|
sl@0
|
770 |
q = g_queue_new ();
|
sl@0
|
771 |
|
sl@0
|
772 |
g_assert (g_queue_is_empty (q) == TRUE);
|
sl@0
|
773 |
|
sl@0
|
774 |
g_queue_push_head (q, GINT_TO_POINTER (2));
|
sl@0
|
775 |
check_integrity (q);
|
sl@0
|
776 |
g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (2));
|
sl@0
|
777 |
check_integrity (q);
|
sl@0
|
778 |
g_assert (g_queue_is_empty (q) == FALSE);
|
sl@0
|
779 |
check_integrity (q);
|
sl@0
|
780 |
g_assert (g_list_length (q->head) == 1);
|
sl@0
|
781 |
g_assert (q->head == q->tail);
|
sl@0
|
782 |
g_queue_push_head (q, GINT_TO_POINTER (1));
|
sl@0
|
783 |
check_integrity (q);
|
sl@0
|
784 |
g_assert (q->head->next == q->tail);
|
sl@0
|
785 |
g_assert (q->tail->prev == q->head);
|
sl@0
|
786 |
g_assert (g_list_length (q->head) == 2);
|
sl@0
|
787 |
check_integrity (q);
|
sl@0
|
788 |
g_assert (q->tail->data == GINT_TO_POINTER (2));
|
sl@0
|
789 |
g_assert (q->head->data == GINT_TO_POINTER (1));
|
sl@0
|
790 |
check_integrity (q);
|
sl@0
|
791 |
g_queue_push_tail (q, GINT_TO_POINTER (3));
|
sl@0
|
792 |
g_assert (g_list_length (q->head) == 3);
|
sl@0
|
793 |
g_assert (q->head->data == GINT_TO_POINTER (1));
|
sl@0
|
794 |
g_assert (q->head->next->data == GINT_TO_POINTER (2));
|
sl@0
|
795 |
g_assert (q->head->next->next == q->tail);
|
sl@0
|
796 |
g_assert (q->head->next == q->tail->prev);
|
sl@0
|
797 |
g_assert (q->tail->data == GINT_TO_POINTER (3));
|
sl@0
|
798 |
g_queue_push_tail (q, GINT_TO_POINTER (4));
|
sl@0
|
799 |
check_integrity (q);
|
sl@0
|
800 |
g_assert (g_list_length (q->head) == 4);
|
sl@0
|
801 |
g_assert (q->head->data == GINT_TO_POINTER (1));
|
sl@0
|
802 |
g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (4));
|
sl@0
|
803 |
g_queue_push_tail (q, GINT_TO_POINTER (5));
|
sl@0
|
804 |
check_integrity (q);
|
sl@0
|
805 |
g_assert (g_list_length (q->head) == 5);
|
sl@0
|
806 |
|
sl@0
|
807 |
g_assert (g_queue_is_empty (q) == FALSE);
|
sl@0
|
808 |
check_integrity (q);
|
sl@0
|
809 |
|
sl@0
|
810 |
g_assert (q->length == 5);
|
sl@0
|
811 |
g_assert (q->head->prev == NULL);
|
sl@0
|
812 |
g_assert (q->head->data == GINT_TO_POINTER (1));
|
sl@0
|
813 |
g_assert (q->head->next->data == GINT_TO_POINTER (2));
|
sl@0
|
814 |
g_assert (q->head->next->next->data == GINT_TO_POINTER (3));
|
sl@0
|
815 |
g_assert (q->head->next->next->next->data == GINT_TO_POINTER (4));
|
sl@0
|
816 |
g_assert (q->head->next->next->next->next->data == GINT_TO_POINTER (5));
|
sl@0
|
817 |
g_assert (q->head->next->next->next->next->next == NULL);
|
sl@0
|
818 |
g_assert (q->head->next->next->next->next == q->tail);
|
sl@0
|
819 |
g_assert (q->tail->data == GINT_TO_POINTER (5));
|
sl@0
|
820 |
g_assert (q->tail->prev->data == GINT_TO_POINTER (4));
|
sl@0
|
821 |
g_assert (q->tail->prev->prev->data == GINT_TO_POINTER (3));
|
sl@0
|
822 |
g_assert (q->tail->prev->prev->prev->data == GINT_TO_POINTER (2));
|
sl@0
|
823 |
g_assert (q->tail->prev->prev->prev->prev->data == GINT_TO_POINTER (1));
|
sl@0
|
824 |
g_assert (q->tail->prev->prev->prev->prev->prev == NULL);
|
sl@0
|
825 |
g_assert (q->tail->prev->prev->prev->prev == q->head);
|
sl@0
|
826 |
g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (5));
|
sl@0
|
827 |
g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (1));
|
sl@0
|
828 |
|
sl@0
|
829 |
g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (1));
|
sl@0
|
830 |
check_integrity (q);
|
sl@0
|
831 |
g_assert (g_list_length (q->head) == 4 && q->length == 4);
|
sl@0
|
832 |
g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (5));
|
sl@0
|
833 |
check_integrity (q);
|
sl@0
|
834 |
g_assert (g_list_length (q->head) == 3);
|
sl@0
|
835 |
g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (2));
|
sl@0
|
836 |
check_integrity (q);
|
sl@0
|
837 |
g_assert (g_list_length (q->head) == 2);
|
sl@0
|
838 |
g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (4));
|
sl@0
|
839 |
check_integrity (q);
|
sl@0
|
840 |
g_assert (g_list_length (q->head) == 1);
|
sl@0
|
841 |
g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (3));
|
sl@0
|
842 |
check_integrity (q);
|
sl@0
|
843 |
g_assert (g_list_length (q->head) == 0);
|
sl@0
|
844 |
g_assert (g_queue_pop_tail (q) == NULL);
|
sl@0
|
845 |
check_integrity (q);
|
sl@0
|
846 |
g_assert (g_list_length (q->head) == 0);
|
sl@0
|
847 |
g_assert (g_queue_pop_head (q) == NULL);
|
sl@0
|
848 |
check_integrity (q);
|
sl@0
|
849 |
g_assert (g_list_length (q->head) == 0);
|
sl@0
|
850 |
|
sl@0
|
851 |
g_assert (g_queue_is_empty (q) == TRUE);
|
sl@0
|
852 |
check_integrity (q);
|
sl@0
|
853 |
|
sl@0
|
854 |
/************************/
|
sl@0
|
855 |
|
sl@0
|
856 |
g_queue_push_head (q, GINT_TO_POINTER (1));
|
sl@0
|
857 |
check_integrity (q);
|
sl@0
|
858 |
g_assert (g_list_length (q->head) == 1 && 1 == q->length);
|
sl@0
|
859 |
g_queue_push_head (q, GINT_TO_POINTER (2));
|
sl@0
|
860 |
check_integrity (q);
|
sl@0
|
861 |
g_assert (g_list_length (q->head) == 2 && 2 == q->length);
|
sl@0
|
862 |
g_queue_push_head (q, GINT_TO_POINTER (3));
|
sl@0
|
863 |
check_integrity (q);
|
sl@0
|
864 |
g_assert (g_list_length (q->head) == 3 && 3 == q->length);
|
sl@0
|
865 |
g_queue_push_head (q, GINT_TO_POINTER (4));
|
sl@0
|
866 |
check_integrity (q);
|
sl@0
|
867 |
g_assert (g_list_length (q->head) == 4 && 4 == q->length);
|
sl@0
|
868 |
g_queue_push_head (q, GINT_TO_POINTER (5));
|
sl@0
|
869 |
check_integrity (q);
|
sl@0
|
870 |
g_assert (g_list_length (q->head) == 5 && 5 == q->length);
|
sl@0
|
871 |
|
sl@0
|
872 |
g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (5));
|
sl@0
|
873 |
check_integrity (q);
|
sl@0
|
874 |
g_assert (g_list_length (q->head) == 4);
|
sl@0
|
875 |
node = q->tail;
|
sl@0
|
876 |
g_assert (node == g_queue_pop_tail_link (q));
|
sl@0
|
877 |
check_integrity (q);
|
sl@0
|
878 |
g_assert (g_list_length (q->head) == 3);
|
sl@0
|
879 |
data = q->head->data;
|
sl@0
|
880 |
g_assert (data == g_queue_pop_head (q));
|
sl@0
|
881 |
check_integrity (q);
|
sl@0
|
882 |
g_assert (g_list_length (q->head) == 2);
|
sl@0
|
883 |
g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (2));
|
sl@0
|
884 |
check_integrity (q);
|
sl@0
|
885 |
g_assert (g_list_length (q->head) == 1);
|
sl@0
|
886 |
g_assert (q->head == q->tail);
|
sl@0
|
887 |
g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (3));
|
sl@0
|
888 |
check_integrity (q);
|
sl@0
|
889 |
g_assert (g_list_length (q->head) == 0);
|
sl@0
|
890 |
g_assert (g_queue_pop_head (q) == NULL);
|
sl@0
|
891 |
check_integrity (q);
|
sl@0
|
892 |
g_assert (g_queue_pop_head_link (q) == NULL);
|
sl@0
|
893 |
check_integrity (q);
|
sl@0
|
894 |
g_assert (g_list_length (q->head) == 0);
|
sl@0
|
895 |
g_assert (g_queue_pop_tail_link (q) == NULL);
|
sl@0
|
896 |
check_integrity (q);
|
sl@0
|
897 |
g_assert (g_list_length (q->head) == 0);
|
sl@0
|
898 |
|
sl@0
|
899 |
/* */
|
sl@0
|
900 |
g_queue_reverse (q);
|
sl@0
|
901 |
check_integrity (q);
|
sl@0
|
902 |
g_assert (g_list_length (q->head) == 0);
|
sl@0
|
903 |
|
sl@0
|
904 |
q2 = g_queue_copy (q);
|
sl@0
|
905 |
check_integrity (q);
|
sl@0
|
906 |
check_integrity (q2);
|
sl@0
|
907 |
g_assert (g_list_length (q->head) == 0);
|
sl@0
|
908 |
g_assert (g_list_length (q2->head) == 0);
|
sl@0
|
909 |
g_queue_sort (q, compare_int, NULL);
|
sl@0
|
910 |
check_integrity (q2);
|
sl@0
|
911 |
check_integrity (q);
|
sl@0
|
912 |
g_queue_sort (q2, compare_int, NULL);
|
sl@0
|
913 |
check_integrity (q2);
|
sl@0
|
914 |
check_integrity (q);
|
sl@0
|
915 |
|
sl@0
|
916 |
for (i = 0; i < 200; ++i)
|
sl@0
|
917 |
{
|
sl@0
|
918 |
g_queue_push_nth (q, GINT_TO_POINTER (i), i);
|
sl@0
|
919 |
g_assert (g_queue_find (q, GINT_TO_POINTER (i)));
|
sl@0
|
920 |
check_integrity (q);
|
sl@0
|
921 |
check_integrity (q2);
|
sl@0
|
922 |
}
|
sl@0
|
923 |
|
sl@0
|
924 |
for (i = 0; i < 200; ++i)
|
sl@0
|
925 |
{
|
sl@0
|
926 |
g_queue_remove (q, GINT_TO_POINTER (i));
|
sl@0
|
927 |
check_integrity (q);
|
sl@0
|
928 |
check_integrity (q2);
|
sl@0
|
929 |
}
|
sl@0
|
930 |
|
sl@0
|
931 |
for (i = 0; i < 200; ++i)
|
sl@0
|
932 |
{
|
sl@0
|
933 |
GList *l = g_list_prepend (NULL, GINT_TO_POINTER (i));
|
sl@0
|
934 |
|
sl@0
|
935 |
g_queue_push_nth_link (q, i, l);
|
sl@0
|
936 |
check_integrity (q);
|
sl@0
|
937 |
check_integrity (q2);
|
sl@0
|
938 |
g_queue_reverse (q);
|
sl@0
|
939 |
check_integrity (q);
|
sl@0
|
940 |
check_integrity (q2);
|
sl@0
|
941 |
}
|
sl@0
|
942 |
|
sl@0
|
943 |
g_queue_free (q2);
|
sl@0
|
944 |
q2 = g_queue_copy (q);
|
sl@0
|
945 |
|
sl@0
|
946 |
g_queue_foreach (q2, remove_item, q2);
|
sl@0
|
947 |
check_integrity (q2);
|
sl@0
|
948 |
check_integrity (q);
|
sl@0
|
949 |
|
sl@0
|
950 |
/* some checks for off by one errors */
|
sl@0
|
951 |
g_queue_push_tail (q, GINT_TO_POINTER (1234));
|
sl@0
|
952 |
check_integrity (q);
|
sl@0
|
953 |
node = g_queue_peek_tail_link (q);
|
sl@0
|
954 |
g_assert (node != NULL && node->data == GINT_TO_POINTER (1234));
|
sl@0
|
955 |
node = g_queue_peek_nth_link (q, g_queue_get_length (q));
|
sl@0
|
956 |
g_assert (node == NULL);
|
sl@0
|
957 |
node = g_queue_peek_nth_link (q, g_queue_get_length (q) - 1);
|
sl@0
|
958 |
g_assert (node->data == GINT_TO_POINTER (1234));
|
sl@0
|
959 |
node = g_queue_pop_nth_link (q, g_queue_get_length (q));
|
sl@0
|
960 |
g_assert (node == NULL);
|
sl@0
|
961 |
node = g_queue_pop_nth_link (q, g_queue_get_length (q) - 1);
|
sl@0
|
962 |
g_assert (node != NULL && node->data == GINT_TO_POINTER (1234));
|
sl@0
|
963 |
|
sl@0
|
964 |
g_queue_free (q);
|
sl@0
|
965 |
|
sl@0
|
966 |
if (argc > 2 && args[1][0] == '-' && args[1][1] == 'v')
|
sl@0
|
967 |
random_test (strtol (args[2], NULL, 0));
|
sl@0
|
968 |
if (argc > 1)
|
sl@0
|
969 |
random_test (strtol (args[1], NULL, 0));
|
sl@0
|
970 |
else
|
sl@0
|
971 |
random_test (time (0));
|
sl@0
|
972 |
#ifdef __SYMBIAN32__
|
sl@0
|
973 |
testResultXml("queue-test");
|
sl@0
|
974 |
#endif /* EMULATOR */
|
sl@0
|
975 |
|
sl@0
|
976 |
return 0;
|
sl@0
|
977 |
}
|
sl@0
|
978 |
|