Moving files to packaging and extracing new tarball.
[profile/ivi/glib2.git] / glib / tests / list.c
1 #include <glib.h>
2
3 #define SIZE       50
4 #define NUMBER_MIN 0000
5 #define NUMBER_MAX 9999
6
7
8 static guint32 array[SIZE];
9
10
11 static gint
12 sort (gconstpointer p1, gconstpointer p2)
13 {
14   gint32 a, b;
15
16   a = GPOINTER_TO_INT (p1);
17   b = GPOINTER_TO_INT (p2);
18
19   return (a > b ? +1 : a == b ? 0 : -1);
20 }
21
22 /*
23  * glist sort tests
24  */
25 static void
26 test_list_sort (void)
27 {
28   GList *list = NULL;
29   gint   i;
30
31   for (i = 0; i < SIZE; i++)
32     list = g_list_append (list, GINT_TO_POINTER (array[i]));
33
34   list = g_list_sort (list, sort);
35   for (i = 0; i < SIZE - 1; i++)
36     {
37       gpointer p1, p2;
38
39       p1 = g_list_nth_data (list, i);
40       p2 = g_list_nth_data (list, i+1);
41
42       g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
43     }
44
45   g_list_free (list);
46 }
47
48 static void
49 test_list_sort_with_data (void)
50 {
51   GList *list = NULL;
52   gint   i;
53
54   for (i = 0; i < SIZE; i++)
55     list = g_list_append (list, GINT_TO_POINTER (array[i]));
56
57   list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
58   for (i = 0; i < SIZE - 1; i++)
59     {
60       gpointer p1, p2;
61
62       p1 = g_list_nth_data (list, i);
63       p2 = g_list_nth_data (list, i+1);
64
65       g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
66     }
67
68   g_list_free (list);
69 }
70
71 static void
72 test_list_insert_sorted (void)
73 {
74   GList *list = NULL;
75   gint   i;
76
77   for (i = 0; i < SIZE; i++)
78     list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
79
80   for (i = 0; i < SIZE - 1; i++)
81     {
82       gpointer p1, p2;
83
84       p1 = g_list_nth_data (list, i);
85       p2 = g_list_nth_data (list, i+1);
86
87       g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
88     }
89
90   g_list_free (list);
91 }
92
93 static void
94 test_list_insert_sorted_with_data (void)
95 {
96   GList *list = NULL;
97   gint   i;
98
99   for (i = 0; i < SIZE; i++)
100     list = g_list_insert_sorted_with_data (list,
101                                            GINT_TO_POINTER (array[i]),
102                                            (GCompareDataFunc)sort,
103                                            NULL);
104
105   for (i = 0; i < SIZE - 1; i++)
106     {
107       gpointer p1, p2;
108
109       p1 = g_list_nth_data (list, i);
110       p2 = g_list_nth_data (list, i+1);
111
112       g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
113     }
114
115   g_list_free (list);
116 }
117
118 static void
119 test_list_reverse (void)
120 {
121   GList *list = NULL;
122   GList *st;
123   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
124   gint   i;
125
126   for (i = 0; i < 10; i++)
127     list = g_list_append (list, &nums[i]);
128
129   list = g_list_reverse (list);
130
131   for (i = 0; i < 10; i++)
132     {
133       st = g_list_nth (list, i);
134       g_assert (*((gint*) st->data) == (9 - i));
135     }
136
137   g_list_free (list);
138 }
139
140 static void
141 test_list_nth (void)
142 {
143   GList *list = NULL;
144   GList *st;
145   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
146   gint   i;
147
148   for (i = 0; i < 10; i++)
149     list = g_list_append (list, &nums[i]);
150
151   for (i = 0; i < 10; i++)
152     {
153       st = g_list_nth (list, i);
154       g_assert (*((gint*) st->data) == i);
155     }
156
157   g_list_free (list);
158 }
159
160 static void
161 test_list_concat (void)
162 {
163   GList *list1 = NULL;
164   GList *list2 = NULL;
165   GList *st;
166   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
167   gint i;
168
169   for (i = 0; i < 5; i++)
170     {
171       list1 = g_list_append (list1, &nums[i]);
172       list2 = g_list_append (list2, &nums[i+5]);
173     }
174
175   g_assert_cmpint (g_list_length (list1), ==, 5);
176   g_assert_cmpint (g_list_length (list2), ==, 5);
177
178   list1 = g_list_concat (list1, list2);
179
180   g_assert_cmpint (g_list_length (list1), ==, 10);
181
182   for (i = 0; i < 10; i++)
183     {
184       st = g_list_nth (list1, i);
185       g_assert (*((gint*) st->data) == i);
186     }
187
188   list2 = g_list_concat (NULL, list1);
189   g_assert_cmpint (g_list_length (list2), ==, 10);
190
191   list2 = g_list_concat (list1, NULL);
192   g_assert_cmpint (g_list_length (list2), ==, 10);
193
194   list2 = g_list_concat (NULL, NULL);
195   g_assert (list2 == NULL);
196
197   g_list_free (list1);
198 }
199
200 static void
201 test_list_remove (void)
202 {
203   GList *list = NULL;
204   GList *st;
205   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
206   gint   i;
207
208   for (i = 0; i < 10; i++)
209     {
210       list = g_list_append (list, &nums[i]);
211       list = g_list_append (list, &nums[i]);
212     }
213
214   g_assert_cmpint (g_list_length (list), ==, 20);
215
216   for (i = 0; i < 10; i++)
217     {
218       list = g_list_remove (list, &nums[i]);
219     }
220
221   g_assert_cmpint (g_list_length (list), ==, 10);
222
223   for (i = 0; i < 10; i++)
224     {
225       st = g_list_nth (list, i);
226       g_assert (*((gint*) st->data) == i);
227     }
228
229   g_list_free (list);
230 }
231
232 static void
233 test_list_remove_all (void)
234 {
235   GList *list = NULL;
236   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
237   gint   i;
238
239   for (i = 0; i < 10; i++)
240     {
241       list = g_list_append (list, &nums[i]);
242       list = g_list_append (list, &nums[i]);
243     }
244
245   g_assert_cmpint (g_list_length (list), ==, 20);
246
247   for (i = 0; i < 5; i++)
248     {
249       list = g_list_remove_all (list, &nums[2 * i + 1]);
250       list = g_list_remove_all (list, &nums[8 - 2 * i]);
251     }
252
253   g_assert_cmpint (g_list_length (list), ==, 0);
254   g_assert (list == NULL);
255 }
256
257 static void
258 test_list_first_last (void)
259 {
260   GList *list = NULL;
261   GList *st;
262   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
263   gint   i;
264
265   for (i = 0; i < 10; i++)
266     list = g_list_append (list, &nums[i]);
267
268   st = g_list_last (list);
269   g_assert (*((gint*) st->data) == 9);
270   st = g_list_nth_prev (st, 3);
271   g_assert (*((gint*) st->data) == 6);
272   st = g_list_first (st);
273   g_assert (*((gint*) st->data) == 0);
274
275   g_list_free (list);
276 }
277
278 static void
279 test_list_insert (void)
280 {
281   GList *list = NULL;
282   GList *st;
283   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
284   gint   i;
285
286   list = g_list_insert_before (NULL, NULL, &nums[1]);
287   list = g_list_insert (list, &nums[3], 1);
288   list = g_list_insert (list, &nums[4], -1);
289   list = g_list_insert (list, &nums[0], 0);
290   list = g_list_insert (list, &nums[5], 100);
291   list = g_list_insert_before (list, NULL, &nums[6]);
292   list = g_list_insert_before (list, list->next->next, &nums[2]);
293
294   list = g_list_insert (list, &nums[9], 7);
295   list = g_list_insert (list, &nums[8], 7);
296   list = g_list_insert (list, &nums[7], 7);
297
298   for (i = 0; i < 10; i++)
299     {
300       st = g_list_nth (list, i);
301       g_assert (*((gint*) st->data) == i);
302     }
303
304   g_list_free (list);
305 }
306
307 typedef struct
308 {
309   gboolean freed;
310   int x;
311 } ListItem;
312
313 static void
314 free_func (gpointer data)
315 {
316   ListItem *item = data;
317
318   item->freed = TRUE;
319 }
320
321 static ListItem *
322 new_item (int x)
323 {
324   ListItem *item;
325
326   item = g_slice_new (ListItem);
327   item->freed = FALSE;
328   item->x = x;
329
330   return item;
331 }
332
333 static void
334 test_free_full (void)
335 {
336   ListItem *one, *two, *three;
337   GSList *slist = NULL;
338   GList *list = NULL;
339
340   slist = g_slist_prepend (slist, one = new_item (1));
341   slist = g_slist_prepend (slist, two = new_item (2));
342   slist = g_slist_prepend (slist, three = new_item (3));
343   g_assert (!one->freed);
344   g_assert (!two->freed);
345   g_assert (!three->freed);
346   g_slist_free_full (slist, free_func);
347   g_assert (one->freed);
348   g_assert (two->freed);
349   g_assert (three->freed);
350   g_slice_free (ListItem, one);
351   g_slice_free (ListItem, two);
352   g_slice_free (ListItem, three);
353
354   list = g_list_prepend (list, one = new_item (1));
355   list = g_list_prepend (list, two = new_item (2));
356   list = g_list_prepend (list, three = new_item (3));
357   g_assert (!one->freed);
358   g_assert (!two->freed);
359   g_assert (!three->freed);
360   g_list_free_full (list, free_func);
361   g_assert (one->freed);
362   g_assert (two->freed);
363   g_assert (three->freed);
364   g_slice_free (ListItem, one);
365   g_slice_free (ListItem, two);
366   g_slice_free (ListItem, three);
367 }
368
369 static void
370 test_list_copy (void)
371 {
372   GList *l, *l2;
373   GList *u, *v;
374
375   l = NULL;
376   l = g_list_append (l, GINT_TO_POINTER (1));
377   l = g_list_append (l, GINT_TO_POINTER (2));
378   l = g_list_append (l, GINT_TO_POINTER (3));
379
380   l2 = g_list_copy (l);
381
382   for (u = l, v = l2; u && v; u = u->next, v = v->next)
383     {
384       g_assert (u->data == v->data);
385     }
386
387   g_list_free (l);
388   g_list_free (l2);
389 }
390
391 static void
392 test_delete_link (void)
393 {
394   GList *l, *l2;
395
396   l = NULL;
397   l = g_list_append (l, GINT_TO_POINTER (1));
398   l = g_list_append (l, GINT_TO_POINTER (2));
399   l = g_list_append (l, GINT_TO_POINTER (3));
400
401   l2 = l->next;
402
403   l = g_list_delete_link (l, l2);
404   g_assert (l->data == GINT_TO_POINTER (1));
405   g_assert (l->next->data == GINT_TO_POINTER (3));
406
407   g_list_free (l);
408 }
409
410 static void
411 test_prepend (void)
412 {
413   GList *l, *l2;
414
415   l = NULL;
416   l = g_list_prepend (l, "c");
417   l = g_list_prepend (l, "a");
418
419   g_assert (l->data == (gpointer)"a");
420   g_assert (l->next->data == (gpointer)"c");
421   g_assert (l->next->next == NULL);
422
423   l2 = l->next;
424   l2 = g_list_prepend (l2, "b");
425   g_assert (l2->prev == l);
426
427   g_assert (l->data == (gpointer)"a");
428   g_assert (l->next->data == (gpointer)"b");
429   g_assert (l->next->next->data == (gpointer)"c");
430   g_assert (l->next->next->next == NULL);
431
432   g_list_free (l);
433 }
434
435 static void
436 test_position (void)
437 {
438   GList *l, *ll;
439
440   l = NULL;
441   l = g_list_append (l, "a");
442   l = g_list_append (l, "b");
443   l = g_list_append (l, "c");
444
445   ll = g_list_find (l, "a");
446   g_assert_cmpint (g_list_position (l, ll), ==, 0);
447   g_assert_cmpint (g_list_index (l, "a"), ==, 0);
448   ll = g_list_find (l, "b");
449   g_assert_cmpint (g_list_position (l, ll), ==, 1);
450   g_assert_cmpint (g_list_index (l, "b"), ==, 1);
451   ll = g_list_find (l, "c");
452   g_assert_cmpint (g_list_position (l, ll), ==, 2);
453   g_assert_cmpint (g_list_index (l, "c"), ==, 2);
454
455   ll = g_list_append (NULL, "d");
456   g_assert_cmpint (g_list_position (l, ll), ==, -1);
457   g_assert_cmpint (g_list_index (l, "d"), ==, -1);
458
459   g_list_free (l);
460   g_list_free (ll);
461 }
462
463 int
464 main (int argc, char *argv[])
465 {
466   gint i;
467
468   g_test_init (&argc, &argv, NULL);
469
470   /* Create an array of random numbers. */
471   for (i = 0; i < SIZE; i++)
472     array[i] = g_test_rand_int_range (NUMBER_MIN, NUMBER_MAX);
473
474   g_test_add_func ("/list/sort", test_list_sort);
475   g_test_add_func ("/list/sort-with-data", test_list_sort_with_data);
476   g_test_add_func ("/list/insert-sorted", test_list_insert_sorted);
477   g_test_add_func ("/list/insert-sorted-with-data", test_list_insert_sorted_with_data);
478   g_test_add_func ("/list/reverse", test_list_reverse);
479   g_test_add_func ("/list/nth", test_list_nth);
480   g_test_add_func ("/list/concat", test_list_concat);
481   g_test_add_func ("/list/remove", test_list_remove);
482   g_test_add_func ("/list/remove-all", test_list_remove_all);
483   g_test_add_func ("/list/first-last", test_list_first_last);
484   g_test_add_func ("/list/insert", test_list_insert);
485   g_test_add_func ("/list/free-full", test_free_full);
486   g_test_add_func ("/list/copy", test_list_copy);
487   g_test_add_func ("/list/delete-link", test_delete_link);
488   g_test_add_func ("/list/prepend", test_prepend);
489   g_test_add_func ("/list/position", test_position);
490
491   return g_test_run ();
492 }