return list;
}
+
+GSList*
+g_slist_find (GSList *list, gconstpointer data)
+{
+ while (list){
+ if (list->data == data)
+ return list;
+
+ list = list->next;
+ }
+
+ return NULL;
+}
+
+guint
+g_slist_length (GSList *list)
+{
+ guint length = 0;
+
+ while (list) {
+ length ++;
+ list = list->next;
+ }
+
+ return length;
+}
+
+static GSList*
+_g_slist_remove (GSList *list, gconstpointer data, gboolean free)
+{
+ GSList *prev = NULL;
+ GSList *current = NULL;
+
+ if (!list)
+ return NULL;
+
+ if (list->data == data)
+ return list->next;
+
+ prev = list;
+ current = list->next;
+
+ while (current) {
+ if (current->data == data){
+ prev->next = current->next;
+ if (free)
+ g_slist_free_1 (current);
+ else
+ current->next = NULL;
+ break;
+ }
+ prev = current;
+ current = current->next;
+ }
+
+ return list;
+}
+
+GSList*
+g_slist_remove (GSList *list, gconstpointer data)
+{
+ return _g_slist_remove (list, data, TRUE);
+}
+
+GSList*
+g_slist_remove_link (GSList *list, gconstpointer data)
+{
+ return _g_slist_remove (list, data, FALSE);
+}
test ("s-split", test_split);
test ("s-gstring", test_gstring);
test ("s-gstrreverse", test_strreverse);
+ test ("s-slist-append", test_slist_append);
+ test ("s-slist-concat", test_slist_concat);
+ test ("s-slist-find", test_slist_find);
+ test ("s-slist-remove", test_slist_remove);
+ test ("s-slist-remove-link", test_slist_remove_link);
}
--- /dev/null
+#include <stdio.h>
+#include <glib.h>
+#include "test.h"
+
+char*
+compare (GSList *list1, GSList *list2)
+{
+ while (list1 && list2) {
+ if (list1->data != list2->data)
+ return "the lists are not equal";
+
+ list1 = list1->next;
+ list2 = list2->next;
+ }
+
+ return NULL;
+}
+
+char*
+test_slist_append ()
+{
+ GSList *list = g_slist_prepend (NULL, "first");
+ if (g_slist_length (list) != 1)
+ return "Prepend failed";
+
+ g_slist_append (list, g_slist_prepend (NULL, "second"));
+
+ if (g_slist_length (list) != 2)
+ return "Append failed";
+
+ return NULL;
+}
+
+char *
+test_slist_concat ()
+{
+ GSList *foo = g_slist_prepend (NULL, "foo");
+ GSList *bar = g_slist_prepend (NULL, "bar");
+
+ GSList *list = g_slist_concat (foo, bar);
+
+ if (g_slist_length (list) != 2)
+ return "Concat failed.";
+
+ return NULL;
+}
+
+char*
+test_slist_find ()
+{
+ GSList *list = g_slist_prepend (NULL, "three");
+ list = g_slist_prepend (list, "two");
+ list = g_slist_prepend (list, "one");
+
+ char *data = "four";
+ list = g_slist_append (list, data);
+
+ GSList *found = g_slist_find (list, data);
+
+ if (found->data != data)
+ return "Find failed";
+
+ return NULL;
+}
+
+char*
+test_slist_remove ()
+{
+ GSList *list = g_slist_prepend (NULL, "three");
+ char *one = "one";
+ list = g_slist_prepend (list, "two");
+ list = g_slist_prepend (list, one);
+
+ list = g_slist_remove (list, one);
+
+ if (g_slist_length (list) != 2)
+ return "Remove failed";
+
+ if (strcmp ("two", list->data) != 0)
+ return "Remove failed";
+
+ return NULL;
+}
+
+char*
+test_slist_remove_link ()
+{
+ GSList *foo = g_slist_prepend (NULL, "a");
+ GSList *bar = g_slist_prepend (NULL, "b");
+ GSList *baz = g_slist_prepend (NULL, "c");
+
+ GSList *list = g_slist_append (foo, bar);
+ list = g_slist_append (foo, baz);
+ list = g_slist_remove_link (list, bar);
+
+ if (g_slist_length (list) != 2)
+ return "Remove failed";
+
+ if (bar->next != NULL)
+ return "Remove failed";
+
+ return NULL;
+}