From: Ralf Wildenhues Date: Wed, 2 Dec 2009 22:11:23 +0000 (+0100) Subject: Test and fix slist.c. X-Git-Tag: v2.2.7b~40 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fea7fdee4e2a36b608708f5cbfaf373edfac916d;p=thirdparty%2Flibtool.git Test and fix slist.c. * libltdl/libltdl/slist.h: Include stddef.h, for size_t. (slist_remove): Return pointer to SList, not void. * libltdl/slist.c: Include stdlib.h, for malloc and free. (slist_remove): Adjust prototype as above. (slist_sort): Do not loop forever on one-item list. * tests/slist.at (SList functionality): New file, new test. * Makefile.am (TESTSUITE_AT): Add tests/slist.at. Signed-off-by: Ralf Wildenhues --- diff --git a/ChangeLog b/ChangeLog index 7961cacac..69f5e782e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2009-12-02 Ralf Wildenhues + + Test and fix slist.c. + * libltdl/libltdl/slist.h: Include stddef.h, for size_t. + (slist_remove): Return pointer to SList, not void. + * libltdl/slist.c: Include stdlib.h, for malloc and free. + (slist_remove): Adjust prototype as above. + (slist_sort): Do not loop forever on one-item list. + * tests/slist.at (SList functionality): New file, new test. + * Makefile.am (TESTSUITE_AT): Add tests/slist.at. + 2009-11-30 Peter Rosin Please C++ compilers when calling strrchr. diff --git a/Makefile.am b/Makefile.am index 2fe2f8867..e5d55eba1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -484,6 +484,7 @@ TESTSUITE_AT = tests/testsuite.at \ tests/lt_dlopen_a.at \ tests/lt_dlopenext.at \ tests/ltdl-api.at \ + tests/slist.at \ tests/need_lib_prefix.at \ tests/standalone.at \ tests/subproject.at \ diff --git a/libltdl/libltdl/slist.h b/libltdl/libltdl/slist.h index e4b7ead8d..4d5650997 100644 --- a/libltdl/libltdl/slist.h +++ b/libltdl/libltdl/slist.h @@ -1,6 +1,6 @@ /* slist.h -- generalised singly linked lists - Copyright (C) 2000, 2004 Free Software Foundation, Inc. + Copyright (C) 2000, 2004, 2009 Free Software Foundation, Inc. Written by Gary V. Vaughan, 2000 NOTE: The canonical source of this file is maintained with the @@ -48,6 +48,8 @@ or obtained by writing to the Free Software Foundation, Inc., # define LT_SCOPE #endif +#include + #if defined(__cplusplus) extern "C" { #endif @@ -65,7 +67,7 @@ LT_SCOPE SList *slist_concat (SList *head, SList *tail); LT_SCOPE SList *slist_cons (SList *item, SList *slist); LT_SCOPE SList *slist_delete (SList *slist, void (*delete_fct) (void *item)); -LT_SCOPE void * slist_remove (SList **phead, SListCallback *find, +LT_SCOPE SList *slist_remove (SList **phead, SListCallback *find, void *matchdata); LT_SCOPE SList *slist_reverse (SList *slist); LT_SCOPE SList *slist_sort (SList *slist, SListCompare *compare, diff --git a/libltdl/slist.c b/libltdl/slist.c index c99f39914..25906a43a 100644 --- a/libltdl/slist.c +++ b/libltdl/slist.c @@ -1,6 +1,6 @@ /* slist.c -- generalised singly linked lists - Copyright (C) 2000, 2004, 2007, 2008 Free Software Foundation, Inc. + Copyright (C) 2000, 2004, 2007, 2008, 2009 Free Software Foundation, Inc. Written by Gary V. Vaughan, 2000 NOTE: The canonical source of this file is maintained with the @@ -32,6 +32,7 @@ or obtained by writing to the Free Software Foundation, Inc., #include "slist.h" #include +#include static SList * slist_sort_merge (SList *left, SList *right, SListCompare *compare, void *userdata); @@ -73,7 +74,7 @@ slist_delete (SList *head, void (*delete_fct) (void *item)) the stale item, you should probably return that from FIND if it makes a successful match. Don't forget to slist_unbox() every item in a boxed list before operating on its contents. */ -void * +SList * slist_remove (SList **phead, SListCallback *find, void *matchdata) { SList *stale = 0; @@ -107,7 +108,7 @@ slist_remove (SList **phead, SListCallback *find, void *matchdata) } } - return result; + return (SList *) result; } /* Call FIND repeatedly with each element of SLIST and MATCHDATA, until @@ -314,6 +315,9 @@ slist_sort (SList *slist, SListCompare *compare, void *userdata) left = slist; right = slist->next; + if (!right) + return left; + /* Skip two items with RIGHT and one with SLIST, until RIGHT falls off the end. SLIST must be about half way along. */ while (right && (right = right->next)) diff --git a/tests/slist.at b/tests/slist.at new file mode 100644 index 000000000..741410c49 --- /dev/null +++ b/tests/slist.at @@ -0,0 +1,168 @@ +# slist.at -- test slist.c -*- Autotest -*- +# +# Copyright (C) 2009 Free Software Foundation, Inc. +# +# This file is part of GNU Libtool. +# +# GNU Libtool is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of +# the License, or (at your option) any later version. +# +# GNU Libtool is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Libtool; see the file COPYING. If not, a copy +# can be downloaded from http://www.gnu.org/licenses/gpl.html, +# or obtained by writing to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +#### + +AT_SETUP([SList functionality]) + +AT_DATA([test-slist.c], [[ +#include +#include +#include +#include +#include +#include "slist.h" + +void *find_string (SList *item, void *data) +{ + if (data != NULL && !strcmp ((const char *) item->userdata, (const char *)data)) + return item; + else + return NULL; +} + +void boxed_delete (void *item) +{ + free (slist_unbox ((SList *) item)); +} + +void *print_item (SList *item, void *userdata) +{ + userdata = userdata; /* unused */ + printf ("%s\n", (const char*)item->userdata); + return NULL; +} + +int list_compare (const SList *item1, const SList *item2, void *userdata) +{ + userdata = userdata; + return strcmp ((const char *) item1->userdata, (const char *)item2->userdata); +} + +int main () +{ + int i; + SList *empty_list = NULL, *list = NULL, *item, *list_save; + char *data = NULL; + + /* slist_cons */ + list = slist_cons (NULL, NULL); + + for (i=0; i < 10; ++i) { + data = (char *) malloc (42); + assert (data); + sprintf (data, "foo%d", i); + list = slist_cons (slist_box (data), list); + } + list_save = list; + list = slist_cons (NULL, list); + assert (list == list_save); + + + /* slist_find */ + assert (slist_find (NULL, find_string, (void *) "whatever") == NULL); + assert (slist_find (empty_list, find_string, (void *) "whatever") == NULL); + assert (slist_find (list, find_string, (void *) "foo10") == NULL); + item = (SList *) slist_find (list, find_string, (void *) "foo1"); + assert (item != NULL); + assert (!strcmp ((const char *) item->userdata, "foo1")); + + item = slist_nth (list, 10); + assert (item != NULL && !strcmp ((const char *) item->userdata, "foo0")); + + puts ("list as inserted:"); + slist_foreach (list, print_item, NULL); + puts ("reversed list:"); + list = slist_reverse (list); + slist_foreach (list, print_item, NULL); + + item = slist_nth (list, 1); + assert (item != NULL && !strcmp ((const char *) item->userdata, "foo0")); + + assert (10 == slist_length (list)); + + /* slist_tail is the second item, not the last one */ + item = slist_tail (list); + assert (item != NULL && !strcmp ((const char *) item->userdata, "foo1")); + + assert (slist_tail (slist_nth (list, 10)) == NULL); + + /* slist_sort and implicitly, slist_sort_merge */ + assert (slist_sort (NULL, list_compare, NULL) == NULL); + list = slist_sort (list, list_compare, NULL); + puts ("list after no-op sort:"); + slist_foreach (list, print_item, NULL); + + list = slist_reverse (list); + puts ("reversed list:"); + slist_foreach (list, print_item, NULL); + puts ("sorting reversed list:"); + list = slist_sort (list, list_compare, NULL); + slist_foreach (list, print_item, NULL); + + /* slist_remove */ + assert (slist_remove (NULL, find_string, NULL) == NULL); + assert (slist_remove (&empty_list, find_string, NULL) == NULL); + + list_save = list; + assert (slist_remove (&list, find_string, NULL) == NULL); + assert (list_save == list); + + /* remove entries: middle, last, first, not present */ + /* slist_reverse above has left us with increasing order */ + list_save = list; + item = slist_remove (&list, find_string, (void *) "foo5"); + assert (list_save == list); + assert (item != NULL && !strcmp (data = (char *) slist_unbox (item), "foo5")); + free (data); + + list_save = list; + item = slist_remove (&list, find_string, (void *) "foo9"); + assert (list_save == list); + assert (item != NULL && !strcmp (data = (char *) slist_unbox (item), "foo9")); + free (data); + + list_save = list; + item = slist_remove (&list, find_string, (void *) "foo0"); + assert (list_save != list); + assert (item != NULL && !strcmp (data = (char *) slist_unbox (item), "foo0")); + free (data); + + list_save = list; + item = slist_remove (&list, find_string, (void *) "foo5"); + assert (list_save == list); + assert (item == NULL); + + assert (slist_delete (list, boxed_delete) == NULL); + return 0; +} +]]) + +CPPFLAGS="-I$top_srcdir/libltdl -I$top_srcdir/libltdl/libltdl -I$abs_top_builddir" +AT_CHECK([$CC $CPPFLAGS $CFLAGS -c test-slist.c], + [], [ignore], [ignore]) +AT_CHECK([$CC $CPPFLAGS $CFLAGS -c $top_srcdir/libltdl/slist.c], + [], [ignore], [ignore]) +AT_CHECK([$CC $CFLAGS $LDFLAGS -o test-slist test-slist.$OBJEXT slist.$OBJEXT], + [], [ignore], [ignore]) +LT_AT_EXEC_CHECK([./test-slist], [], [ignore], [ignore]) + +AT_CLEANUP