4 #define NUMBER_MIN 0000
5 #define NUMBER_MAX 9999
8 static guint32 array
[SIZE
];
12 sort (gconstpointer p1
, gconstpointer p2
)
16 a
= GPOINTER_TO_INT (p1
);
17 b
= GPOINTER_TO_INT (p2
);
19 return (a
> b
? +1 : a
== b
? 0 : -1);
26 test_slist_sort (void)
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
++)
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
));
49 test_slist_sort_with_data (void)
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
++)
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
));
72 test_slist_insert_sorted (void)
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
++)
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
));
94 test_slist_insert_sorted_with_data (void)
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
,
105 for (i
= 0; i
< SIZE
- 1; i
++)
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
);
119 test_slist_reverse (void)
121 GSList
*slist
= NULL
;
123 gint nums
[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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
);
141 test_slist_nth (void)
143 GSList
*slist
= NULL
;
145 gint nums
[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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
);
161 test_slist_remove (void)
163 GSList
*slist
= NULL
;
165 gint nums
[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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
);
193 test_slist_remove_all (void)
195 GSList
*slist
= NULL
;
196 gint nums
[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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
);
218 test_slist_insert (void)
220 GSList
*slist
= NULL
;
222 gint nums
[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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
);
267 find_num (gconstpointer l
, gconstpointer data
)
269 return *(gint
*)l
- GPOINTER_TO_INT(data
);
273 test_slist_position (void)
275 GSList
*slist
= NULL
;
277 gint nums
[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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
);
303 test_slist_concat (void)
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
);
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);
324 s
= g_slist_concat (NULL
, NULL
);
325 g_assert (s
== NULL
);
329 main (int argc
, char *argv
[])
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 ();