Permalink
Browse files

Implment g_list_insert_before.

svn path=/trunk/mono/; revision=64119
  • Loading branch information...
1 parent d6bd055 commit 6621b257cbbb59d40589d1cec3577666fe4b6e9c Duncan Mak committed Aug 21, 2006
Showing with 42 additions and 15 deletions.
  1. +15 −3 eglib/COPYING
  2. +6 −12 eglib/src/glist.c
  3. +21 −0 eglib/test/list.c
View
@@ -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.
View
@@ -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
View
@@ -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},

0 comments on commit 6621b25

Please sign in to comment.