[rx] add missing project file generator helper files.
[mono-project.git] / eglib / test / list.c
blob68dadee612afd0912105c7d880468529ee79173f
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
6 RESULT
7 test_list_length ()
9 GList *list = g_list_prepend (NULL, "foo");
11 if (g_list_length (list) != 1)
12 return FAILED ("length failed. #1");
14 list = g_list_prepend (list, "bar");
15 if (g_list_length (list) != 2)
16 return FAILED ("length failed. #2");
18 list = g_list_append (list, "bar");
19 if (g_list_length (list) != 3)
20 return FAILED ("length failed. #3");
22 g_list_free (list);
23 return NULL;
26 RESULT
27 test_list_nth ()
29 char *foo = "foo";
30 char *bar = "bar";
31 char *baz = "baz";
32 GList *nth, *list;
33 list = g_list_prepend (NULL, baz);
34 list = g_list_prepend (list, bar);
35 list = g_list_prepend (list, foo);
37 nth = g_list_nth (list, 0);
38 if (nth->data != foo)
39 return FAILED ("nth failed. #0");
41 nth = g_list_nth (list, 1);
42 if (nth->data != bar)
43 return FAILED ("nth failed. #1");
45 nth = g_list_nth (list, 2);
46 if (nth->data != baz)
47 return FAILED ("nth failed. #2");
49 nth = g_list_nth (list, 3);
50 if (nth)
51 return FAILED ("nth failed. #3: %s", nth->data);
53 g_list_free (list);
54 return OK;
57 RESULT
58 test_list_index ()
60 int i;
61 char *foo = "foo";
62 char *bar = "bar";
63 char *baz = "baz";
64 GList *list;
65 list = g_list_prepend (NULL, baz);
66 list = g_list_prepend (list, bar);
67 list = g_list_prepend (list, foo);
69 i = g_list_index (list, foo);
70 if (i != 0)
71 return FAILED ("index failed. #0: %d", i);
73 i = g_list_index (list, bar);
74 if (i != 1)
75 return FAILED ("index failed. #1: %d", i);
77 i = g_list_index (list, baz);
78 if (i != 2)
79 return FAILED ("index failed. #2: %d", i);
81 g_list_free (list);
82 return OK;
85 RESULT
86 test_list_append ()
88 GList *list = g_list_prepend (NULL, "first");
89 if (g_list_length (list) != 1)
90 return FAILED ("Prepend failed");
92 list = g_list_append (list, "second");
94 if (g_list_length (list) != 2)
95 return FAILED ("Append failed");
97 g_list_free (list);
98 return OK;
101 RESULT
102 test_list_last ()
104 GList *foo = g_list_prepend (NULL, "foo");
105 GList *bar = g_list_prepend (NULL, "bar");
106 GList *last;
108 foo = g_list_concat (foo, bar);
109 last = g_list_last (foo);
111 if (last != bar)
112 return FAILED ("last failed. #1");
114 foo = g_list_concat (foo, g_list_prepend (NULL, "baz"));
115 foo = g_list_concat (foo, g_list_prepend (NULL, "quux"));
117 last = g_list_last (foo);
118 if (strcmp ("quux", last->data))
119 return FAILED ("last failed. #2");
121 g_list_free (foo);
123 return OK;
126 RESULT
127 test_list_concat ()
129 GList *foo = g_list_prepend (NULL, "foo");
130 GList *bar = g_list_prepend (NULL, "bar");
131 GList *list = g_list_concat (foo, bar);
133 if (g_list_length (list) != 2)
134 return FAILED ("Concat failed. #1");
136 if (strcmp (list->data, "foo"))
137 return FAILED ("Concat failed. #2");
139 if (strcmp (list->next->data, "bar"))
140 return FAILED ("Concat failed. #3");
142 if (g_list_first (list) != foo)
143 return FAILED ("Concat failed. #4");
145 if (g_list_last (list) != bar)
146 return FAILED ("Concat failed. #5");
148 g_list_free (list);
150 return OK;
154 static gint
155 compare (gconstpointer a, gconstpointer b)
157 char *foo = (char *) a;
158 char *bar = (char *) b;
160 if (strlen (foo) < strlen (bar))
161 return -1;
163 return 1;
166 RESULT
167 test_list_insert_sorted ()
169 GList *list = g_list_prepend (NULL, "a");
170 list = g_list_append (list, "aaa");
172 /* insert at the middle */
173 list = g_list_insert_sorted (list, "aa", compare);
174 if (strcmp ("aa", list->next->data))
175 return FAILED ("insert_sorted failed. #1");
177 /* insert at the beginning */
178 list = g_list_insert_sorted (list, "", compare);
179 if (strcmp ("", list->data))
180 return FAILED ("insert_sorted failed. #2");
182 /* insert at the end */
183 list = g_list_insert_sorted (list, "aaaa", compare);
184 if (strcmp ("aaaa", g_list_last (list)->data))
185 return FAILED ("insert_sorted failed. #3");
187 g_list_free (list);
188 return OK;
191 RESULT
192 test_list_copy ()
194 int i, length;
195 GList *list, *copy;
196 list = g_list_prepend (NULL, "a");
197 list = g_list_append (list, "aa");
198 list = g_list_append (list, "aaa");
199 list = g_list_append (list, "aaaa");
201 length = g_list_length (list);
202 copy = g_list_copy (list);
204 for (i = 0; i < length; i++)
205 if (strcmp (g_list_nth (list, i)->data,
206 g_list_nth (copy, i)->data))
207 return FAILED ("copy failed.");
209 g_list_free (list);
210 g_list_free (copy);
211 return OK;
214 RESULT
215 test_list_reverse ()
217 guint i, length;
218 GList *list, *reverse;
219 list = g_list_prepend (NULL, "a");
220 list = g_list_append (list, "aa");
221 list = g_list_append (list, "aaa");
222 list = g_list_append (list, "aaaa");
224 length = g_list_length (list);
225 reverse = g_list_reverse (g_list_copy (list));
227 if (g_list_length (reverse) != length)
228 return FAILED ("reverse failed #1");
230 for (i = 0; i < length; i++){
231 guint j = length - i - 1;
232 if (strcmp (g_list_nth (list, i)->data,
233 g_list_nth (reverse, j)->data))
234 return FAILED ("reverse failed. #2");
237 g_list_free (list);
238 g_list_free (reverse);
239 return OK;
242 RESULT
243 test_list_remove ()
245 GList *list = g_list_prepend (NULL, "three");
246 char *one = "one";
247 list = g_list_prepend (list, "two");
248 list = g_list_prepend (list, one);
250 list = g_list_remove (list, one);
252 if (g_list_length (list) != 2)
253 return FAILED ("Remove failed");
255 if (strcmp ("two", list->data) != 0)
256 return FAILED ("Remove failed");
258 g_list_free (list);
259 return OK;
262 RESULT
263 test_list_remove_link ()
265 GList *foo = g_list_prepend (NULL, "a");
266 GList *bar = g_list_prepend (NULL, "b");
267 GList *baz = g_list_prepend (NULL, "c");
268 GList *list = foo;
270 foo = g_list_concat (foo, bar);
271 foo = g_list_concat (foo, baz);
273 list = g_list_remove_link (list, bar);
275 if (g_list_length (list) != 2)
276 return FAILED ("remove_link failed #1");
278 if (bar->next != NULL)
279 return FAILED ("remove_link failed #2");
281 g_list_free (list);
282 g_list_free (bar);
283 return OK;
286 RESULT
287 test_list_insert_before ()
289 GList *foo, *bar, *baz;
291 foo = g_list_prepend (NULL, "foo");
292 foo = g_list_insert_before (foo, NULL, "bar");
293 bar = g_list_last (foo);
295 if (strcmp (bar->data, "bar"))
296 return FAILED ("1");
298 baz = g_list_insert_before (foo, bar, "baz");
299 if (foo != baz)
300 return FAILED ("2");
302 if (strcmp (g_list_nth_data (foo, 1), "baz"))
303 return FAILED ("3: %s", g_list_nth_data (foo, 1));
305 g_list_free (foo);
306 return OK;
309 #define N_ELEMS 101
311 static int intcompare (gconstpointer p1, gconstpointer p2)
313 return GPOINTER_TO_INT (p1) - GPOINTER_TO_INT (p2);
316 static gboolean verify_sort (GList *list, int len)
318 int prev;
320 if (list->prev)
321 return FALSE;
323 prev = GPOINTER_TO_INT (list->data);
324 len--;
325 for (list = list->next; list; list = list->next) {
326 int curr = GPOINTER_TO_INT (list->data);
327 if (prev > curr)
328 return FALSE;
329 prev = curr;
331 if (!list->prev || list->prev->next != list)
332 return FALSE;
334 if (len == 0)
335 return FALSE;
336 len--;
338 return len == 0;
341 RESULT
342 test_list_sort ()
344 int i, j, mul;
345 GList *list = NULL;
347 for (i = 0; i < N_ELEMS; ++i)
348 list = g_list_prepend (list, GINT_TO_POINTER (i));
349 list = g_list_sort (list, intcompare);
350 if (!verify_sort (list, N_ELEMS))
351 return FAILED ("decreasing list");
353 g_list_free (list);
355 list = NULL;
356 for (i = 0; i < N_ELEMS; ++i)
357 list = g_list_prepend (list, GINT_TO_POINTER (-i));
358 list = g_list_sort (list, intcompare);
359 if (!verify_sort (list, N_ELEMS))
360 return FAILED ("increasing list");
362 g_list_free (list);
364 list = g_list_prepend (NULL, GINT_TO_POINTER (0));
365 for (i = 1; i < N_ELEMS; ++i) {
366 list = g_list_prepend (list, GINT_TO_POINTER (i));
367 list = g_list_prepend (list, GINT_TO_POINTER (-i));
369 list = g_list_sort (list, intcompare);
370 if (!verify_sort (list, 2*N_ELEMS-1))
371 return FAILED ("alternating list");
373 g_list_free (list);
375 list = NULL;
376 mul = 1;
377 for (i = 1; i < N_ELEMS; ++i) {
378 mul = -mul;
379 for (j = 0; j < i; ++j)
380 list = g_list_prepend (list, GINT_TO_POINTER (mul * j));
382 list = g_list_sort (list, intcompare);
383 if (!verify_sort (list, (N_ELEMS*N_ELEMS - N_ELEMS)/2))
384 return FAILED ("wavering list");
386 g_list_free (list);
388 return OK;
391 static gint
392 find_custom (gconstpointer a, gconstpointer b)
394 return(strcmp (a, b));
397 RESULT
398 test_list_find_custom ()
400 GList *list = NULL, *found;
401 char *foo = "foo";
402 char *bar = "bar";
403 char *baz = "baz";
405 list = g_list_prepend (list, baz);
406 list = g_list_prepend (list, bar);
407 list = g_list_prepend (list, foo);
409 found = g_list_find_custom (list, baz, find_custom);
411 if (found == NULL)
412 return FAILED ("Find failed");
414 g_list_free (list);
416 return OK;
419 static Test list_tests [] = {
420 { "length", test_list_length},
421 { "nth", test_list_nth},
422 { "index", test_list_index},
423 { "last", test_list_last},
424 { "append", test_list_append},
425 { "concat", test_list_concat},
426 {"insert_sorted", test_list_insert_sorted},
427 {"insert_before", test_list_insert_before},
428 { "copy", test_list_copy},
429 { "reverse", test_list_reverse},
430 { "remove", test_list_remove},
431 { "remove_link", test_list_remove_link},
432 { "remove_link", test_list_remove_link},
433 { "sort", test_list_sort},
434 { "find_custom", test_list_find_custom},
435 {NULL, NULL}
438 DEFINE_TEST_GROUP_INIT(list_tests_init, list_tests)