]> git.ipfire.org Git - thirdparty/libtool.git/commitdiff
Test and fix slist.c.
authorRalf Wildenhues <Ralf.Wildenhues@gmx.de>
Wed, 2 Dec 2009 22:11:23 +0000 (23:11 +0100)
committerRalf Wildenhues <Ralf.Wildenhues@gmx.de>
Wed, 2 Dec 2009 22:13:05 +0000 (23:13 +0100)
* 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 <Ralf.Wildenhues@gmx.de>
ChangeLog
Makefile.am
libltdl/libltdl/slist.h
libltdl/slist.c
tests/slist.at [new file with mode: 0644]

index 7961cacac808f79d5af7a7298747984cb717d51f..69f5e782ed60b235316620b3e766873b856c7a07 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2009-12-02  Ralf Wildenhues  <Ralf.Wildenhues@gmx.de>
+
+       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  <peda@lysator.liu.se>
 
        Please C++ compilers when calling strrchr.
index 2fe2f886757945c0691f376263b39cf162c9e58a..e5d55eba1317da8753758769ea6ccd68835a13c5 100644 (file)
@@ -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 \
index e4b7ead8d927a32d40f74869318dcd09a0cbea4b..4d5650997d6dc58718f10418eb4cf73bb5535e3f 100644 (file)
@@ -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 <stddef.h>
+
 #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,
index c99f39914a63599662fd615efa18436425a987b1..25906a43aa02d669d8e4596c3dee625d25fe689c 100644 (file)
@@ -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 <stddef.h>
+#include <stdlib.h>
 
 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 (file)
index 0000000..741410c
--- /dev/null
@@ -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 <config.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#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