Implment g_list_insert_before.
authorDuncan Mak <duncan@mono-cvs.ximian.com>
Mon, 21 Aug 2006 04:15:35 +0000 (04:15 -0000)
committerDuncan Mak <duncan@mono-cvs.ximian.com>
Mon, 21 Aug 2006 04:15:35 +0000 (04:15 -0000)
svn path=/trunk/mono/; revision=64119

eglib/COPYING
eglib/src/glist.c
eglib/test/list.c

index 1f95d26c5ac2d755547b708616926f8d041a4c1a..1bb5a44356f00884a71ceeefd24ded6caaba2418 100644 (file)
@@ -1,5 +1,17 @@
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
 
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
 
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
index 0fd6c37dba27a5ed69181bb867d18d642c2be529..cf13523103695687894a595447cab0b2d8301213 100644 (file)
@@ -276,23 +276,17 @@ g_list_insert_sorted (GList *list, gpointer data, GCompareFunc func)
        return list;
 }
 
-/* TODO */
 GList*
 g_list_insert_before (GList *list, GList *sibling, gpointer data)
 {
+       if (!list)
+               return g_list_prepend (NULL, data);
+
        if (!sibling)
-               return g_list_append (list, g_list_prepend (NULL, data));
-       
-       while (list){
-               if (list->next == sibling){
-                       g_list_prepend (sibling, g_list_prepend (NULL, data));
-                       break;
-               }
-               
-               list = list->next;
-       }
+               return g_list_append (list, data);
 
-       return list;
+       else 
+               return g_list_prepend (sibling, data);
 }
 
 void
index 1ab8d0ea7b348a4e13e1b0b9ac1937cb81ae1809..7460ec7b53edcf611ca38d7247e2214baeac5fdc 100644 (file)
@@ -279,6 +279,26 @@ test_list_remove_link ()
        return OK;
 }
 
+RESULT
+test_list_insert_before ()
+{
+       GList *foo, *bar;
+
+       foo = g_list_prepend (NULL, "foo");
+       foo = g_list_insert_before (foo, NULL, "bar");
+       bar = g_list_last (foo);
+
+       if (strcmp (bar->data, "bar"))
+               return FAILED ("1");
+
+       g_list_insert_before (foo, bar, "baz");
+
+       if (strcmp (g_list_nth (foo, 1)->data, "baz"))
+               return FAILED ("2");    
+
+       return OK;
+}
+
 static Test list_tests [] = {
        {"list_length", test_list_length},
        {"list_nth", test_list_nth},
@@ -287,6 +307,7 @@ static Test list_tests [] = {
        {"list_append", test_list_append},
        {"list_concat", test_list_concat},
        {"list_insert_sorted", test_list_insert_sorted},
+       {"list_insert_before", test_list_insert_before},
        {"list_copy", test_list_copy},
        {"list_reverse", test_list_reverse},
        {"list_remove", test_list_remove},