Imported Upstream version 2.4.2
[platform/upstream/libtool.git] / tests / slist.at
1 # slist.at -- test slist.c                   -*- Autotest -*-
2 #
3 #   Copyright (C) 2009 Free Software Foundation, Inc.
4 #
5 #   This file is part of GNU Libtool.
6 #
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.
11 #
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.
16 #
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.
22 ####
23
24 AT_SETUP([SList functionality])
25
26 AT_DATA([test-slist.c], [[
27 #include <config.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include "slist.h"
33
34 void *find_string (SList *item, void *data)
35 {
36   if (data != NULL && !strcmp ((const char *) item->userdata, (const char *)data))
37     return item;
38   else
39     return NULL;
40 }
41
42 void boxed_delete (void *item)
43 {
44   free (slist_unbox ((SList *) item));
45 }
46
47 void *print_item (SList *item, void *userdata)
48 {
49   userdata = userdata; /* unused */
50   printf ("%s\n", (const char*)item->userdata);
51   return NULL;
52 }
53
54 int list_compare (const SList *item1, const SList *item2, void *userdata)
55 {
56   userdata = userdata;
57   return strcmp ((const char *) item1->userdata, (const char *)item2->userdata);
58 }
59
60 int main ()
61 {
62   int i;
63   SList *empty_list = NULL, *list = NULL, *item, *list_save;
64   char *data = NULL;
65
66   /* slist_cons */
67   list = slist_cons (NULL, NULL);
68
69   for (i=0; i < 10; ++i) {
70     data = (char *) malloc (42);
71     assert (data);
72     sprintf (data, "foo%d", i);
73     list = slist_cons (slist_box (data), list);
74   }
75   list_save = list;
76   list = slist_cons (NULL, list);
77   assert (list == list_save);
78
79
80   /* slist_find */
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"));
87
88   item = slist_nth (list, 10);
89   assert (item != NULL && !strcmp ((const char *) item->userdata, "foo0"));
90
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);
96
97   item = slist_nth (list, 1);
98   assert (item != NULL && !strcmp ((const char *) item->userdata, "foo0"));
99
100   assert (10 == slist_length (list));
101
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"));
105
106   assert (slist_tail (slist_nth (list, 10)) == NULL);
107
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);
113
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);
120
121   /* slist_remove */
122   assert (slist_remove (NULL, find_string, NULL) == NULL);
123   assert (slist_remove (&empty_list, find_string, NULL) == NULL);
124
125   list_save = list;
126   assert (slist_remove (&list, find_string, NULL) == NULL);
127   assert (list_save == list);
128
129   /* remove entries: middle, last, first, not present */
130   /* slist_reverse above has left us with increasing order */
131   list_save = list;
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"));
135   free (data);
136
137   list_save = list;
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"));
141   free (data);
142
143   list_save = list;
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"));
147   free (data);
148
149   list_save = list;
150   item = slist_remove (&list, find_string, (void *) "foo5");
151   assert (list_save == list);
152   assert (item == NULL);
153
154   assert (slist_delete (list, boxed_delete) == NULL);
155   return 0;
156 }
157 ]])
158
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])
167
168 AT_CLEANUP