1 # slist.at -- test slist.c -*- Autotest -*-
3 # Copyright (C) 2009 Free Software Foundation, Inc.
5 # This file is part of GNU Libtool.
7 # GNU Libtool is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of
10 # the License, or (at your option) any later version.
12 # GNU Libtool is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Libtool; see the file COPYING. If not, a copy
19 # can be downloaded from http://www.gnu.org/licenses/gpl.html,
20 # or obtained by writing to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 AT_SETUP([SList functionality])
26 AT_DATA([test-slist.c], [[
34 void *find_string (SList *item, void *data)
36 if (data != NULL && !strcmp ((const char *) item->userdata, (const char *)data))
42 void boxed_delete (void *item)
44 free (slist_unbox ((SList *) item));
47 void *print_item (SList *item, void *userdata)
49 userdata = userdata; /* unused */
50 printf ("%s\n", (const char*)item->userdata);
54 int list_compare (const SList *item1, const SList *item2, void *userdata)
57 return strcmp ((const char *) item1->userdata, (const char *)item2->userdata);
63 SList *empty_list = NULL, *list = NULL, *item, *list_save;
67 list = slist_cons (NULL, NULL);
69 for (i=0; i < 10; ++i) {
70 data = (char *) malloc (42);
72 sprintf (data, "foo%d", i);
73 list = slist_cons (slist_box (data), list);
76 list = slist_cons (NULL, list);
77 assert (list == list_save);
81 assert (slist_find (NULL, find_string, (void *) "whatever") == NULL);
82 assert (slist_find (empty_list, find_string, (void *) "whatever") == NULL);
83 assert (slist_find (list, find_string, (void *) "foo10") == NULL);
84 item = (SList *) slist_find (list, find_string, (void *) "foo1");
85 assert (item != NULL);
86 assert (!strcmp ((const char *) item->userdata, "foo1"));
88 item = slist_nth (list, 10);
89 assert (item != NULL && !strcmp ((const char *) item->userdata, "foo0"));
91 puts ("list as inserted:");
92 slist_foreach (list, print_item, NULL);
93 puts ("reversed list:");
94 list = slist_reverse (list);
95 slist_foreach (list, print_item, NULL);
97 item = slist_nth (list, 1);
98 assert (item != NULL && !strcmp ((const char *) item->userdata, "foo0"));
100 assert (10 == slist_length (list));
102 /* slist_tail is the second item, not the last one */
103 item = slist_tail (list);
104 assert (item != NULL && !strcmp ((const char *) item->userdata, "foo1"));
106 assert (slist_tail (slist_nth (list, 10)) == NULL);
108 /* slist_sort and implicitly, slist_sort_merge */
109 assert (slist_sort (NULL, list_compare, NULL) == NULL);
110 list = slist_sort (list, list_compare, NULL);
111 puts ("list after no-op sort:");
112 slist_foreach (list, print_item, NULL);
114 list = slist_reverse (list);
115 puts ("reversed list:");
116 slist_foreach (list, print_item, NULL);
117 puts ("sorting reversed list:");
118 list = slist_sort (list, list_compare, NULL);
119 slist_foreach (list, print_item, NULL);
122 assert (slist_remove (NULL, find_string, NULL) == NULL);
123 assert (slist_remove (&empty_list, find_string, NULL) == NULL);
126 assert (slist_remove (&list, find_string, NULL) == NULL);
127 assert (list_save == list);
129 /* remove entries: middle, last, first, not present */
130 /* slist_reverse above has left us with increasing order */
132 item = slist_remove (&list, find_string, (void *) "foo5");
133 assert (list_save == list);
134 assert (item != NULL && !strcmp (data = (char *) slist_unbox (item), "foo5"));
138 item = slist_remove (&list, find_string, (void *) "foo9");
139 assert (list_save == list);
140 assert (item != NULL && !strcmp (data = (char *) slist_unbox (item), "foo9"));
144 item = slist_remove (&list, find_string, (void *) "foo0");
145 assert (list_save != list);
146 assert (item != NULL && !strcmp (data = (char *) slist_unbox (item), "foo0"));
150 item = slist_remove (&list, find_string, (void *) "foo5");
151 assert (list_save == list);
152 assert (item == NULL);
154 assert (slist_delete (list, boxed_delete) == NULL);
159 CPPFLAGS="-I$top_srcdir/libltdl -I$top_srcdir/libltdl/libltdl -I$abs_top_builddir"
160 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c test-slist.c],
161 [], [ignore], [ignore])
162 AT_CHECK([$CC $CPPFLAGS $CFLAGS -c $top_srcdir/libltdl/slist.c],
163 [], [ignore], [ignore])
164 AT_CHECK([$CC $CFLAGS $LDFLAGS -o test-slist test-slist.$OBJEXT slist.$OBJEXT],
165 [], [ignore], [ignore])
166 LT_AT_EXEC_CHECK([./test-slist], [], [ignore], [ignore])