1 # slist.at -- test slist.c -*- Autotest -*-
3 # Copyright (C) 2009, 2011-2019, 2021-2024 Free Software Foundation,
6 # This file is part of GNU Libtool.
8 # GNU Libtool is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; either version 2 of
11 # the License, or (at your option) any later version.
13 # GNU Libtool is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Libtool; see the file COPYING. If not, a copy
20 # can be downloaded from http://www.gnu.org/licenses/gpl.html,
21 # or obtained by writing to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 AT_SETUP([SList functionality])
27 AT_DATA([test-slist.c], [[
35 #define STREQ(s1, s2) (strcmp ((s1), (s2)) == 0)
37 void *find_string (SList *item, void *data)
39 if (data != NULL && STREQ ((const char *) item->userdata, (const char *)data))
45 void boxed_delete (void *item)
47 free (slist_unbox ((SList *) item));
50 void *print_item (SList *item, void *userdata)
52 userdata = userdata; /* unused */
53 printf ("%s\n", (const char*)item->userdata);
57 int list_compare (const SList *item1, const SList *item2, void *userdata)
60 return strcmp ((const char *) item1->userdata, (const char *)item2->userdata);
66 SList *empty_list = NULL, *list = NULL, *item, *list_save;
70 list = slist_cons (NULL, NULL);
72 for (i=0; i < 10; ++i) {
73 data = (char *) malloc (42);
75 sprintf (data, "foo%d", i);
76 list = slist_cons (slist_box (data), list);
79 list = slist_cons (NULL, list);
80 assert (list == list_save);
84 assert (slist_find (NULL, find_string, (void *) "whatever") == NULL);
85 assert (slist_find (empty_list, find_string, (void *) "whatever") == NULL);
86 assert (slist_find (list, find_string, (void *) "foo10") == NULL);
87 item = (SList *) slist_find (list, find_string, (void *) "foo1");
88 assert (item != NULL);
89 assert (STREQ ((const char *) item->userdata, "foo1"));
91 item = slist_nth (list, 10);
92 assert (item != NULL && STREQ ((const char *) item->userdata, "foo0"));
94 puts ("list as inserted:");
95 slist_foreach (list, print_item, NULL);
96 puts ("reversed list:");
97 list = slist_reverse (list);
98 slist_foreach (list, print_item, NULL);
100 item = slist_nth (list, 1);
101 assert (item != NULL && STREQ ((const char *) item->userdata, "foo0"));
103 assert (10 == slist_length (list));
105 /* slist_tail is the second item, not the last one */
106 item = slist_tail (list);
107 assert (item != NULL && STREQ ((const char *) item->userdata, "foo1"));
109 assert (slist_tail (slist_nth (list, 10)) == NULL);
111 /* slist_sort and implicitly, slist_sort_merge */
112 assert (slist_sort (NULL, list_compare, NULL) == NULL);
113 list = slist_sort (list, list_compare, NULL);
114 puts ("list after no-op sort:");
115 slist_foreach (list, print_item, NULL);
117 list = slist_reverse (list);
118 puts ("reversed list:");
119 slist_foreach (list, print_item, NULL);
120 puts ("sorting reversed list:");
121 list = slist_sort (list, list_compare, NULL);
122 slist_foreach (list, print_item, NULL);
125 assert (slist_remove (NULL, find_string, NULL) == NULL);
126 assert (slist_remove (&empty_list, find_string, NULL) == NULL);
129 assert (slist_remove (&list, find_string, NULL) == NULL);
130 assert (list_save == list);
132 /* remove entries: middle, last, first, not present */
133 /* slist_reverse above has left us with increasing order */
135 item = slist_remove (&list, find_string, (void *) "foo5");
136 assert (list_save == list);
137 assert (item != NULL && STREQ (data = (char *) slist_unbox (item), "foo5"));
141 item = slist_remove (&list, find_string, (void *) "foo9");
142 assert (list_save == list);
143 assert (item != NULL && STREQ (data = (char *) slist_unbox (item), "foo9"));
147 item = slist_remove (&list, find_string, (void *) "foo0");
148 assert (list_save != list);
149 assert (item != NULL && STREQ (data = (char *) slist_unbox (item), "foo0"));
153 item = slist_remove (&list, find_string, (void *) "foo5");
154 assert (list_save == list);
155 assert (item == NULL);
157 assert (slist_delete (list, boxed_delete) == NULL);
162 CPPFLAGS="-I$top_srcdir/libltdl -I$top_srcdir/libltdl/libltdl -I$abs_top_builddir"
163 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c test-slist.c],
164 [], [ignore], [ignore])
165 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c $top_srcdir/libltdl/slist.c],
166 [], [ignore], [ignore])
167 AT_CHECK([$CC $CFLAGS $LDFLAGS -o test-slist test-slist.$OBJEXT slist.$OBJEXT],
168 [], [ignore], [ignore])
169 LT_AT_EXEC_CHECK([./test-slist], [], [ignore], [ignore])