Add some more cases to the app-id unit tests
[glib.git] / glib / tests / slist.c
blobf1a84956f192bde271ebdcb5d86342fe3674e3ef
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 static void
72 test_slist_insert_sorted (void)
74 GSList *slist = NULL;
75 gint i;
77 for (i = 0; i < SIZE; i++)
78 slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
80 for (i = 0; i < SIZE - 1; i++)
82 gpointer p1, p2;
84 p1 = g_slist_nth_data (slist, i);
85 p2 = g_slist_nth_data (slist, i+1);
87 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
90 g_slist_free (slist);
93 static void
94 test_slist_insert_sorted_with_data (void)
96 GSList *slist = NULL;
97 gint i;
99 for (i = 0; i < SIZE; i++)
100 slist = g_slist_insert_sorted_with_data (slist,
101 GINT_TO_POINTER (array[i]),
102 (GCompareDataFunc)sort,
103 NULL);
105 for (i = 0; i < SIZE - 1; i++)
107 gpointer p1, p2;
109 p1 = g_slist_nth_data (slist, i);
110 p2 = g_slist_nth_data (slist, i+1);
112 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
115 g_slist_free (slist);
118 static void
119 test_slist_reverse (void)
121 GSList *slist = NULL;
122 GSList *st;
123 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
124 gint i;
126 for (i = 0; i < 10; i++)
127 slist = g_slist_append (slist, &nums[i]);
129 slist = g_slist_reverse (slist);
131 for (i = 0; i < 10; i++)
133 st = g_slist_nth (slist, i);
134 g_assert (*((gint*) st->data) == (9 - i));
137 g_slist_free (slist);
140 static void
141 test_slist_nth (void)
143 GSList *slist = NULL;
144 GSList *st;
145 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
146 gint i;
148 for (i = 0; i < 10; i++)
149 slist = g_slist_append (slist, &nums[i]);
151 for (i = 0; i < 10; i++)
153 st = g_slist_nth (slist, i);
154 g_assert (*((gint*) st->data) == i);
157 g_slist_free (slist);
160 static void
161 test_slist_remove (void)
163 GSList *slist = NULL;
164 GSList *st;
165 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
166 gint i;
168 for (i = 0; i < 10; i++)
170 slist = g_slist_append (slist, &nums[i]);
171 slist = g_slist_append (slist, &nums[i]);
174 g_assert_cmpint (g_slist_length (slist), ==, 20);
176 for (i = 0; i < 10; i++)
178 slist = g_slist_remove (slist, &nums[i]);
181 g_assert_cmpint (g_slist_length (slist), ==, 10);
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_all (void)
195 GSList *slist = NULL;
196 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
197 gint i;
199 for (i = 0; i < 10; i++)
201 slist = g_slist_append (slist, &nums[i]);
202 slist = g_slist_append (slist, &nums[i]);
205 g_assert_cmpint (g_slist_length (slist), ==, 20);
207 for (i = 0; i < 5; i++)
209 slist = g_slist_remove_all (slist, &nums[2 * i + 1]);
210 slist = g_slist_remove_all (slist, &nums[8 - 2 * i]);
213 g_assert_cmpint (g_slist_length (slist), ==, 0);
214 g_assert (slist == NULL);
217 static void
218 test_slist_insert (void)
220 GSList *slist = NULL;
221 GSList *st;
222 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
223 gint i;
225 slist = g_slist_insert_before (NULL, NULL, &nums[1]);
226 slist = g_slist_insert (slist, &nums[3], 1);
227 slist = g_slist_insert (slist, &nums[4], -1);
228 slist = g_slist_insert (slist, &nums[0], 0);
229 slist = g_slist_insert (slist, &nums[5], 100);
230 slist = g_slist_insert_before (slist, NULL, &nums[6]);
231 slist = g_slist_insert_before (slist, slist->next->next, &nums[2]);
233 slist = g_slist_insert (slist, &nums[9], 7);
234 slist = g_slist_insert (slist, &nums[8], 7);
235 slist = g_slist_insert (slist, &nums[7], 7);
237 for (i = 0; i < 10; i++)
239 st = g_slist_nth (slist, i);
240 g_assert (*((gint*) st->data) == i);
243 g_slist_free (slist);
245 slist = g_slist_insert (NULL, "a", 1);
246 g_assert (slist->data == (gpointer)"a");
247 g_assert (slist->next == NULL);
248 g_slist_free (slist);
250 slist = g_slist_append (NULL, "a");
251 slist = g_slist_append (slist, "b");
252 slist = g_slist_insert (slist, "c", 5);
254 g_assert (slist->next->next->data == (gpointer)"c");
255 g_assert (slist->next->next->next == NULL);
256 g_slist_free (slist);
258 slist = g_slist_append (NULL, "a");
259 slist = g_slist_insert_before (slist, slist, "b");
260 g_assert (slist->data == (gpointer)"b");
261 g_assert (slist->next->data == (gpointer)"a");
262 g_assert (slist->next->next == NULL);
263 g_slist_free (slist);
266 static gint
267 find_num (gconstpointer l, gconstpointer data)
269 return *(gint*)l - GPOINTER_TO_INT(data);
272 static void
273 test_slist_position (void)
275 GSList *slist = NULL;
276 GSList *st;
277 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
278 gint i;
280 for (i = 0; i < 10; i++)
282 slist = g_slist_append (slist, &nums[i]);
285 g_assert_cmpint (g_slist_index (slist, NULL), ==, -1);
286 g_assert_cmpint (g_slist_position (slist, NULL), ==, -1);
288 for (i = 0; i < 10; i++)
290 g_assert_cmpint (g_slist_index (slist, &nums[i]), ==, i);
291 st = g_slist_find_custom (slist, GINT_TO_POINTER(i), find_num);
292 g_assert (st != NULL);
293 g_assert_cmpint (g_slist_position (slist, st), ==, i);
296 st = g_slist_find_custom (slist, GINT_TO_POINTER (1000), find_num);
297 g_assert (st == NULL);
299 g_slist_free (slist);
302 static void
303 test_slist_concat (void)
305 GSList *s1, *s2, *s;
307 s1 = g_slist_append (NULL, "a");
308 s2 = g_slist_append (NULL, "b");
309 s = g_slist_concat (s1, s2);
310 g_assert (s->data == (gpointer)"a");
311 g_assert (s->next->data == (gpointer)"b");
312 g_assert (s->next->next == NULL);
313 g_slist_free (s);
315 s1 = g_slist_append (NULL, "a");
317 s = g_slist_concat (NULL, s1);
318 g_assert_cmpint (g_slist_length (s), ==, 1);
319 s = g_slist_concat (s1, NULL);
320 g_assert_cmpint (g_slist_length (s), ==, 1);
322 g_slist_free (s);
324 s = g_slist_concat (NULL, NULL);
325 g_assert (s == NULL);
329 main (int argc, char *argv[])
331 gint i;
333 g_test_init (&argc, &argv, NULL);
335 /* Create an array of random numbers. */
336 for (i = 0; i < SIZE; i++)
337 array[i] = g_test_rand_int_range (NUMBER_MIN, NUMBER_MAX);
339 g_test_add_func ("/slist/sort", test_slist_sort);
340 g_test_add_func ("/slist/sort-with-data", test_slist_sort_with_data);
341 g_test_add_func ("/slist/insert-sorted", test_slist_insert_sorted);
342 g_test_add_func ("/slist/insert-sorted-with-data", test_slist_insert_sorted_with_data);
343 g_test_add_func ("/slist/reverse", test_slist_reverse);
344 g_test_add_func ("/slist/nth", test_slist_nth);
345 g_test_add_func ("/slist/remove", test_slist_remove);
346 g_test_add_func ("/slist/remove-all", test_slist_remove_all);
347 g_test_add_func ("/slist/insert", test_slist_insert);
348 g_test_add_func ("/slist/position", test_slist_position);
349 g_test_add_func ("/slist/concat", test_slist_concat);
351 return g_test_run ();