Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / glib / tests / slist.c
blob1f817432c1f057af9f5e6b671ad7853aa67dcf7a
1 #include <glib.h>
3 #define SIZE 50
4 #define NUMBER_MIN 0000
5 #define NUMBER_MAX 9999
8 static guint32 array[SIZE];
11 static gint
12 sort (gconstpointer p1, gconstpointer p2)
14 gint32 a, b;
16 a = GPOINTER_TO_INT (p1);
17 b = GPOINTER_TO_INT (p2);
19 return (a > b ? +1 : a == b ? 0 : -1);
23 * gslist sort tests
25 static void
26 test_slist_sort (void)
28 GSList *slist = NULL;
29 gint i;
31 for (i = 0; i < SIZE; i++)
32 slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
34 slist = g_slist_sort (slist, sort);
35 for (i = 0; i < SIZE - 1; i++)
37 gpointer p1, p2;
39 p1 = g_slist_nth_data (slist, i);
40 p2 = g_slist_nth_data (slist, i+1);
42 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
45 g_slist_free (slist);
48 static void
49 test_slist_sort_with_data (void)
51 GSList *slist = NULL;
52 gint i;
54 for (i = 0; i < SIZE; i++)
55 slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
57 slist = g_slist_sort_with_data (slist, (GCompareDataFunc)sort, NULL);
58 for (i = 0; i < SIZE - 1; i++)
60 gpointer p1, p2;
62 p1 = g_slist_nth_data (slist, i);
63 p2 = g_slist_nth_data (slist, i+1);
65 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
68 g_slist_free (slist);
71 /* Test that the sort is stable. */
72 static void
73 test_slist_sort_stable (void)
75 GSList *list = NULL; /* (element-type utf8) */
76 GSList *copy = NULL; /* (element-type utf8) */
77 gsize i;
79 /* Build a test list, already ordered. */
80 for (i = 0; i < SIZE; i++)
81 list = g_slist_append (list, g_strdup_printf ("%" G_GSIZE_FORMAT, i / 5));
83 /* Take a copy and sort it. */
84 copy = g_slist_copy (list);
85 copy = g_slist_sort (copy, (GCompareFunc) g_strcmp0);
87 /* Compare the two lists, checking pointers are equal to ensure the elements
88 * have been kept stable. */
89 for (i = 0; i < SIZE; i++)
91 gpointer p1, p2;
93 p1 = g_slist_nth_data (list, i);
94 p2 = g_slist_nth_data (list, i);
96 g_assert (p1 == p2);
99 g_slist_free (copy);
100 g_slist_free_full (list, g_free);
103 static void
104 test_slist_insert_sorted (void)
106 GSList *slist = NULL;
107 gint i;
109 for (i = 0; i < SIZE; i++)
110 slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
112 for (i = 0; i < SIZE - 1; i++)
114 gpointer p1, p2;
116 p1 = g_slist_nth_data (slist, i);
117 p2 = g_slist_nth_data (slist, i+1);
119 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
122 g_slist_free (slist);
125 static void
126 test_slist_insert_sorted_with_data (void)
128 GSList *slist = NULL;
129 gint i;
131 for (i = 0; i < SIZE; i++)
132 slist = g_slist_insert_sorted_with_data (slist,
133 GINT_TO_POINTER (array[i]),
134 (GCompareDataFunc)sort,
135 NULL);
137 for (i = 0; i < SIZE - 1; i++)
139 gpointer p1, p2;
141 p1 = g_slist_nth_data (slist, i);
142 p2 = g_slist_nth_data (slist, i+1);
144 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
147 g_slist_free (slist);
150 static void
151 test_slist_reverse (void)
153 GSList *slist = NULL;
154 GSList *st;
155 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
156 gint i;
158 for (i = 0; i < 10; i++)
159 slist = g_slist_append (slist, &nums[i]);
161 slist = g_slist_reverse (slist);
163 for (i = 0; i < 10; i++)
165 st = g_slist_nth (slist, i);
166 g_assert (*((gint*) st->data) == (9 - i));
169 g_slist_free (slist);
172 static void
173 test_slist_nth (void)
175 GSList *slist = NULL;
176 GSList *st;
177 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
178 gint i;
180 for (i = 0; i < 10; i++)
181 slist = g_slist_append (slist, &nums[i]);
183 for (i = 0; i < 10; i++)
185 st = g_slist_nth (slist, i);
186 g_assert (*((gint*) st->data) == i);
189 g_slist_free (slist);
192 static void
193 test_slist_remove (void)
195 GSList *slist = NULL;
196 GSList *st;
197 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
198 gint i;
200 for (i = 0; i < 10; i++)
202 slist = g_slist_append (slist, &nums[i]);
203 slist = g_slist_append (slist, &nums[i]);
206 g_assert_cmpint (g_slist_length (slist), ==, 20);
208 for (i = 0; i < 10; i++)
210 slist = g_slist_remove (slist, &nums[i]);
213 g_assert_cmpint (g_slist_length (slist), ==, 10);
215 for (i = 0; i < 10; i++)
217 st = g_slist_nth (slist, i);
218 g_assert (*((gint*) st->data) == i);
221 g_slist_free (slist);
224 static void
225 test_slist_remove_all (void)
227 GSList *slist = NULL;
228 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
229 gint i;
231 for (i = 0; i < 10; i++)
233 slist = g_slist_append (slist, &nums[i]);
234 slist = g_slist_append (slist, &nums[i]);
237 g_assert_cmpint (g_slist_length (slist), ==, 20);
239 for (i = 0; i < 5; i++)
241 slist = g_slist_remove_all (slist, &nums[2 * i + 1]);
242 slist = g_slist_remove_all (slist, &nums[8 - 2 * i]);
245 g_assert_cmpint (g_slist_length (slist), ==, 0);
246 g_assert (slist == NULL);
249 static void
250 test_slist_insert (void)
252 GSList *slist = NULL;
253 GSList *st;
254 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
255 gint i;
257 slist = g_slist_insert_before (NULL, NULL, &nums[1]);
258 slist = g_slist_insert (slist, &nums[3], 1);
259 slist = g_slist_insert (slist, &nums[4], -1);
260 slist = g_slist_insert (slist, &nums[0], 0);
261 slist = g_slist_insert (slist, &nums[5], 100);
262 slist = g_slist_insert_before (slist, NULL, &nums[6]);
263 slist = g_slist_insert_before (slist, slist->next->next, &nums[2]);
265 slist = g_slist_insert (slist, &nums[9], 7);
266 slist = g_slist_insert (slist, &nums[8], 7);
267 slist = g_slist_insert (slist, &nums[7], 7);
269 for (i = 0; i < 10; i++)
271 st = g_slist_nth (slist, i);
272 g_assert (*((gint*) st->data) == i);
275 g_slist_free (slist);
277 slist = g_slist_insert (NULL, "a", 1);
278 g_assert (slist->data == (gpointer)"a");
279 g_assert (slist->next == NULL);
280 g_slist_free (slist);
282 slist = g_slist_append (NULL, "a");
283 slist = g_slist_append (slist, "b");
284 slist = g_slist_insert (slist, "c", 5);
286 g_assert (slist->next->next->data == (gpointer)"c");
287 g_assert (slist->next->next->next == NULL);
288 g_slist_free (slist);
290 slist = g_slist_append (NULL, "a");
291 slist = g_slist_insert_before (slist, slist, "b");
292 g_assert (slist->data == (gpointer)"b");
293 g_assert (slist->next->data == (gpointer)"a");
294 g_assert (slist->next->next == NULL);
295 g_slist_free (slist);
298 static gint
299 find_num (gconstpointer l, gconstpointer data)
301 return *(gint*)l - GPOINTER_TO_INT(data);
304 static void
305 test_slist_position (void)
307 GSList *slist = NULL;
308 GSList *st;
309 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
310 gint i;
312 for (i = 0; i < 10; i++)
314 slist = g_slist_append (slist, &nums[i]);
317 g_assert_cmpint (g_slist_index (slist, NULL), ==, -1);
318 g_assert_cmpint (g_slist_position (slist, NULL), ==, -1);
320 for (i = 0; i < 10; i++)
322 g_assert_cmpint (g_slist_index (slist, &nums[i]), ==, i);
323 st = g_slist_find_custom (slist, GINT_TO_POINTER(i), find_num);
324 g_assert (st != NULL);
325 g_assert_cmpint (g_slist_position (slist, st), ==, i);
328 st = g_slist_find_custom (slist, GINT_TO_POINTER (1000), find_num);
329 g_assert (st == NULL);
331 g_slist_free (slist);
334 static void
335 test_slist_concat (void)
337 GSList *s1, *s2, *s;
339 s1 = g_slist_append (NULL, "a");
340 s2 = g_slist_append (NULL, "b");
341 s = g_slist_concat (s1, s2);
342 g_assert (s->data == (gpointer)"a");
343 g_assert (s->next->data == (gpointer)"b");
344 g_assert (s->next->next == NULL);
345 g_slist_free (s);
347 s1 = g_slist_append (NULL, "a");
349 s = g_slist_concat (NULL, s1);
350 g_assert_cmpint (g_slist_length (s), ==, 1);
351 s = g_slist_concat (s1, NULL);
352 g_assert_cmpint (g_slist_length (s), ==, 1);
354 g_slist_free (s);
356 s = g_slist_concat (NULL, NULL);
357 g_assert (s == NULL);
360 static void
361 test_slist_copy (void)
363 GSList *slist = NULL, *copy;
364 GSList *s1, *s2;
365 guint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
366 gsize i;
368 /* Copy and test a many-element list. */
369 for (i = 0; i < 10; i++)
370 slist = g_slist_append (slist, &nums[i]);
372 copy = g_slist_copy (slist);
374 g_assert_cmpuint (g_slist_length (copy), ==, g_slist_length (slist));
376 for (s1 = copy, s2 = slist; s1 != NULL && s2 != NULL; s1 = s1->next, s2 = s2->next)
377 g_assert (s1->data == s2->data);
379 g_slist_free (copy);
380 g_slist_free (slist);
382 /* Copy a NULL list. */
383 copy = g_slist_copy (NULL);
384 g_assert_null (copy);
387 static gpointer
388 copy_and_count_string (gconstpointer src,
389 gpointer data)
391 const gchar *str = src;
392 gsize *count = data;
394 *count = *count + 1;
395 return g_strdup (str);
398 static void
399 test_slist_copy_deep (void)
401 GSList *slist = NULL, *copy;
402 GSList *s1, *s2;
403 gsize count;
405 /* Deep-copy a simple list. */
406 slist = g_slist_append (slist, "a");
407 slist = g_slist_append (slist, "b");
408 slist = g_slist_append (slist, "c");
410 count = 0;
411 copy = g_slist_copy_deep (slist, copy_and_count_string, &count);
413 g_assert_cmpuint (count, ==, g_slist_length (slist));
414 g_assert_cmpuint (g_slist_length (copy), ==, count);
415 for (s1 = slist, s2 = copy; s1 != NULL && s2 != NULL; s1 = s1->next, s2 = s2->next)
417 g_assert_cmpstr (s1->data, ==, s2->data);
418 g_assert (s1->data != s2->data);
421 g_slist_free_full (copy, g_free);
422 g_slist_free (slist);
424 /* Try with an empty list. */
425 count = 0;
426 copy = g_slist_copy_deep (NULL, copy_and_count_string, &count);
427 g_assert_cmpuint (count, ==, 0);
428 g_assert_null (copy);
432 main (int argc, char *argv[])
434 gint i;
436 g_test_init (&argc, &argv, NULL);
438 /* Create an array of random numbers. */
439 for (i = 0; i < SIZE; i++)
440 array[i] = g_test_rand_int_range (NUMBER_MIN, NUMBER_MAX);
442 g_test_add_func ("/slist/sort", test_slist_sort);
443 g_test_add_func ("/slist/sort-with-data", test_slist_sort_with_data);
444 g_test_add_func ("/slist/sort/stable", test_slist_sort_stable);
445 g_test_add_func ("/slist/insert-sorted", test_slist_insert_sorted);
446 g_test_add_func ("/slist/insert-sorted-with-data", test_slist_insert_sorted_with_data);
447 g_test_add_func ("/slist/reverse", test_slist_reverse);
448 g_test_add_func ("/slist/nth", test_slist_nth);
449 g_test_add_func ("/slist/remove", test_slist_remove);
450 g_test_add_func ("/slist/remove-all", test_slist_remove_all);
451 g_test_add_func ("/slist/insert", test_slist_insert);
452 g_test_add_func ("/slist/position", test_slist_position);
453 g_test_add_func ("/slist/concat", test_slist_concat);
454 g_test_add_func ("/slist/copy", test_slist_copy);
455 g_test_add_func ("/slist/copy/deep", test_slist_copy_deep);
457 return g_test_run ();