2006-08-17 Duncan Mak <duncan@a-chinaman.com>
authorDuncan Mak <duncan@mono-cvs.ximian.com>
Thu, 17 Aug 2006 07:23:32 +0000 (07:23 -0000)
committerDuncan Mak <duncan@mono-cvs.ximian.com>
Thu, 17 Aug 2006 07:23:32 +0000 (07:23 -0000)
* src/gslist.c (g_slist_find):
(g_slist_length):
(g_slist_remove):
(g_slist_remove_link): Implemented.

* test/slist.c: Tests for GSList.

svn path=/trunk/mono/; revision=63876

eglib/ChangeLog
eglib/src/gslist.c
eglib/test/Makefile.am
eglib/test/driver.c
eglib/test/slist.c [new file with mode: 0644]

index 2cd86e1f8d14164a193b6084a1cdf24bcfabdb82..8a880f1169ac7cda9b5825389ea440fd63b3f22d 100644 (file)
@@ -1,3 +1,12 @@
+2006-08-17  Duncan Mak  <duncan@a-chinaman.com>
+
+       * src/gslist.c (g_slist_find): 
+       (g_slist_length):
+       (g_slist_remove):
+       (g_slist_remove_link): Implemented.
+
+       * test/slist.c: Tests for GSList.
+
 2006-08-17  Raja R Harinath  <harinath@gmail.com>
 
        * src/gslist.c (g_slist_free_1): New.  Free a single list node.
index 3e759c3734a832c095187cec0fcba43213a7ba9c..87e5f632c85c266d9dbbe5c786935f8a6c49971b 100644 (file)
@@ -102,3 +102,72 @@ g_slist_last (GSList *list)
 
        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);
+}
index e11e1a8ae812608962aa7627c0562f1c20049788..919f44506d8a8c0f3fd4f3c8fd39b7817ab8dbfe 100644 (file)
@@ -4,7 +4,8 @@ test_SOURCES = \
        test.h          \
        driver.c        \
        hash.c          \
-       str.c
+       str.c           \
+       slist.c
 
 INCLUDES = -I../src
 
index 75792de583d2c695d0d53f78a1cd7495da73b21f..55962655c4dd675306d3fd18c79c7f6e4ada1a94 100644 (file)
@@ -11,4 +11,9 @@ int main ()
        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);
 }
diff --git a/eglib/test/slist.c b/eglib/test/slist.c
new file mode 100644 (file)
index 0000000..89d4da5
--- /dev/null
@@ -0,0 +1,103 @@
+#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;
+}