Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-array_list.c @ 16616:2f2ef742aa4b
New module 'exp2'.
* lib/math.in.h (exp2): New declaration.
* lib/exp2.c: New file.
* m4/exp2.m4: New file.
* m4/math_h.m4 (gl_MATH_H): Test whether exp2 is declared.
(gl_MATH_H_DEFAULTS): Initialize GNULIB_EXP2, HAVE_DECL_EXP2,
REPLACE_EXP2.
* modules/math (Makefile.am): Substitute GNULIB_EXP2, HAVE_DECL_EXP2,
REPLACE_EXP2.
* modules/exp2: New file.
* tests/test-math-c++.cc: Check the declaration of exp2.
* doc/posix-functions/exp2.texi: Mention the new module and the IRIX
and OpenBSD problems.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Wed, 07 Mar 2012 03:29:32 +0100 (2012-03-07) |
parents | 8250f2777afc |
children | e542fd46ad6f |
rev | line source |
---|---|
7867 | 1 /* Test of sequential list data type implementation. |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
2 Copyright (C) 2006-2012 Free Software Foundation, Inc. |
7867 | 3 Written by Bruno Haible <bruno@clisp.org>, 2007. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
7867 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
8 (at your option) any later version. |
7867 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
7867 | 17 |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8754
diff
changeset
|
18 #include <config.h> |
7867 | 19 |
8287
6ce7f76c71eb
Include gl_array_list.h first.
Bruno Haible <bruno@clisp.org>
parents:
8286
diff
changeset
|
20 #include "gl_array_list.h" |
6ce7f76c71eb
Include gl_array_list.h first.
Bruno Haible <bruno@clisp.org>
parents:
8286
diff
changeset
|
21 |
7867 | 22 #include <stdlib.h> |
23 | |
8286
bd763d866c77
Avoid link error due to 'program_name'.
Bruno Haible <bruno@clisp.org>
parents:
7867
diff
changeset
|
24 #include "progname.h" |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12445
diff
changeset
|
25 #include "macros.h" |
7867 | 26 |
27 static const char *objects[15] = | |
28 { | |
29 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o" | |
30 }; | |
31 | |
32 #define RANDOM(n) (rand () % (n)) | |
33 #define RANDOM_OBJECT() objects[RANDOM (SIZEOF (objects))] | |
34 | |
35 static void | |
36 check_equals (gl_list_t list1, gl_list_t list2) | |
37 { | |
38 size_t n, i; | |
39 | |
40 n = gl_list_size (list1); | |
41 ASSERT (n == gl_list_size (list2)); | |
42 for (i = 0; i < n; i++) | |
43 { | |
44 ASSERT (gl_list_get_at (list1, i) == gl_list_get_at (list2, i)); | |
45 } | |
46 } | |
47 | |
48 int | |
49 main (int argc, char *argv[]) | |
50 { | |
51 gl_list_t list1, list2; | |
52 | |
8286
bd763d866c77
Avoid link error due to 'program_name'.
Bruno Haible <bruno@clisp.org>
parents:
7867
diff
changeset
|
53 set_program_name (argv[0]); |
bd763d866c77
Avoid link error due to 'program_name'.
Bruno Haible <bruno@clisp.org>
parents:
7867
diff
changeset
|
54 |
7867 | 55 /* Allow the user to provide a non-default random seed on the command line. */ |
56 if (argc > 1) | |
57 srand (atoi (argv[1])); | |
58 | |
59 { | |
60 size_t initial_size = RANDOM (50); | |
61 const void **contents = | |
62 (const void **) malloc (initial_size * sizeof (const void *)); | |
63 size_t i; | |
64 unsigned int repeat; | |
65 | |
66 for (i = 0; i < initial_size; i++) | |
67 contents[i] = RANDOM_OBJECT (); | |
68 | |
69 /* Create list1. */ | |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
70 list1 = gl_list_nx_create (GL_ARRAY_LIST, NULL, NULL, NULL, true, |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
71 initial_size, contents); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
72 ASSERT (list1 != NULL); |
7867 | 73 /* Create list2. */ |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
74 list2 = gl_list_nx_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
75 ASSERT (list2 != NULL); |
7867 | 76 for (i = 0; i < initial_size; i++) |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
77 ASSERT (gl_list_nx_add_last (list2, contents[i]) != NULL); |
7867 | 78 |
79 check_equals (list1, list2); | |
80 | |
81 for (repeat = 0; repeat < 10000; repeat++) | |
82 { | |
83 unsigned int operation = RANDOM (16); | |
84 switch (operation) | |
85 { | |
86 case 0: | |
87 if (gl_list_size (list1) > 0) | |
88 { | |
89 size_t index = RANDOM (gl_list_size (list1)); | |
90 const char *obj = RANDOM_OBJECT (); | |
91 gl_list_node_t node1, node2; | |
92 | |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
93 node1 = gl_list_nx_set_at (list1, index, obj); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
94 ASSERT (node1 != NULL); |
7867 | 95 ASSERT (gl_list_get_at (list1, index) == obj); |
96 ASSERT (gl_list_node_value (list1, node1) == obj); | |
97 | |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
98 node2 = gl_list_nx_set_at (list2, index, obj); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
99 ASSERT (node2 != NULL); |
7867 | 100 ASSERT (gl_list_get_at (list2, index) == obj); |
101 ASSERT (gl_list_node_value (list2, node2) == obj); | |
102 | |
103 if (index > 0) | |
104 { | |
105 ASSERT (gl_list_node_value (list1, gl_list_previous_node (list1, node1)) | |
106 == gl_list_get_at (list1, index - 1)); | |
107 } | |
108 if (index + 1 < gl_list_size (list1)) | |
109 { | |
110 ASSERT (gl_list_node_value (list1, gl_list_next_node (list1, node1)) | |
111 == gl_list_get_at (list1, index + 1)); | |
112 } | |
113 } | |
114 break; | |
115 case 1: | |
116 { | |
117 const char *obj = RANDOM_OBJECT (); | |
118 gl_list_node_t node1, node2; | |
119 node1 = gl_list_search (list1, obj); | |
120 node2 = gl_list_search (list2, obj); | |
121 if (node1 == NULL) | |
122 { | |
123 ASSERT (node2 == NULL); | |
124 } | |
125 else | |
126 { | |
127 ASSERT (node2 != NULL); | |
128 ASSERT (gl_list_node_value (list1, node1) == obj); | |
129 ASSERT (gl_list_node_value (list2, node2) == obj); | |
130 } | |
131 } | |
132 break; | |
133 case 2: | |
134 { | |
135 const char *obj = RANDOM_OBJECT (); | |
136 size_t index1, index2; | |
137 index1 = gl_list_indexof (list1, obj); | |
138 index2 = gl_list_indexof (list2, obj); | |
139 if (index1 == (size_t)(-1)) | |
140 { | |
141 ASSERT (index2 == (size_t)(-1)); | |
142 } | |
143 else | |
144 { | |
145 ASSERT (index2 != (size_t)(-1)); | |
146 ASSERT (gl_list_get_at (list1, index1) == obj); | |
147 ASSERT (gl_list_get_at (list2, index2) == obj); | |
148 ASSERT (index2 == index1); | |
149 } | |
150 } | |
151 break; | |
152 case 3: /* add 1 element */ | |
153 { | |
154 const char *obj = RANDOM_OBJECT (); | |
155 gl_list_node_t node1, node2; | |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
156 node1 = gl_list_nx_add_first (list1, obj); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
157 ASSERT (node1 != NULL); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
158 node2 = gl_list_nx_add_first (list2, obj); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
159 ASSERT (node2 != NULL); |
7867 | 160 ASSERT (gl_list_node_value (list1, node1) == obj); |
161 ASSERT (gl_list_node_value (list2, node2) == obj); | |
162 ASSERT (gl_list_get_at (list1, 0) == obj); | |
163 ASSERT (gl_list_get_at (list2, 0) == obj); | |
164 } | |
165 break; | |
166 case 4: /* add 1 element */ | |
167 { | |
168 const char *obj = RANDOM_OBJECT (); | |
169 gl_list_node_t node1, node2; | |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
170 node1 = gl_list_nx_add_last (list1, obj); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
171 ASSERT (node1 != NULL); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
172 node2 = gl_list_nx_add_last (list2, obj); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
173 ASSERT (node2 != NULL); |
7867 | 174 ASSERT (gl_list_node_value (list1, node1) == obj); |
175 ASSERT (gl_list_node_value (list2, node2) == obj); | |
176 ASSERT (gl_list_get_at (list1, gl_list_size (list1) - 1) == obj); | |
177 ASSERT (gl_list_get_at (list2, gl_list_size (list2) - 1) == obj); | |
178 } | |
179 break; | |
180 case 5: /* add 3 elements */ | |
181 { | |
182 const char *obj0 = RANDOM_OBJECT (); | |
183 const char *obj1 = RANDOM_OBJECT (); | |
184 const char *obj2 = RANDOM_OBJECT (); | |
185 gl_list_node_t node1, node2; | |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
186 node1 = gl_list_nx_add_first (list1, obj2); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
187 ASSERT (node1 != NULL); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
188 node1 = gl_list_nx_add_before (list1, node1, obj0); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
189 ASSERT (node1 != NULL); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
190 node1 = gl_list_nx_add_after (list1, node1, obj1); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
191 ASSERT (node1 != NULL); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
192 node2 = gl_list_nx_add_first (list2, obj2); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
193 ASSERT (node2 != NULL); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
194 node2 = gl_list_nx_add_before (list2, node2, obj0); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
195 ASSERT (node2 != NULL); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
196 node2 = gl_list_nx_add_after (list2, node2, obj1); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
197 ASSERT (node2 != NULL); |
7867 | 198 ASSERT (gl_list_node_value (list1, node1) == obj1); |
199 ASSERT (gl_list_node_value (list2, node2) == obj1); | |
200 ASSERT (gl_list_get_at (list1, 0) == obj0); | |
201 ASSERT (gl_list_get_at (list1, 1) == obj1); | |
202 ASSERT (gl_list_get_at (list1, 2) == obj2); | |
203 ASSERT (gl_list_get_at (list2, 0) == obj0); | |
204 ASSERT (gl_list_get_at (list2, 1) == obj1); | |
205 ASSERT (gl_list_get_at (list2, 2) == obj2); | |
206 } | |
207 break; | |
208 case 6: /* add 1 element */ | |
209 { | |
210 size_t index = RANDOM (gl_list_size (list1) + 1); | |
211 const char *obj = RANDOM_OBJECT (); | |
212 gl_list_node_t node1, node2; | |
12445
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
213 node1 = gl_list_nx_add_at (list1, index, obj); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
214 ASSERT (node1 != NULL); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
215 node2 = gl_list_nx_add_at (list2, index, obj); |
a8c91b846640
Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
216 ASSERT (node2 != NULL); |
7867 | 217 ASSERT (gl_list_get_at (list1, index) == obj); |
218 ASSERT (gl_list_node_value (list1, node1) == obj); | |
219 ASSERT (gl_list_get_at (list2, index) == obj); | |
220 ASSERT (gl_list_node_value (list2, node2) == obj); | |
221 if (index > 0) | |
222 { | |
223 ASSERT (gl_list_node_value (list1, gl_list_previous_node (list1, node1)) | |
224 == gl_list_get_at (list1, index - 1)); | |
225 } | |
226 if (index + 1 < gl_list_size (list1)) | |
227 { | |
228 ASSERT (gl_list_node_value (list1, gl_list_next_node (list1, node1)) | |
229 == gl_list_get_at (list1, index + 1)); | |
230 } | |
231 } | |
232 break; | |
233 case 7: case 8: /* remove 1 element */ | |
234 if (gl_list_size (list1) > 0) | |
235 { | |
236 size_t n = gl_list_size (list1); | |
237 const char *obj = gl_list_get_at (list1, RANDOM (n)); | |
238 gl_list_node_t node1, node2; | |
239 node1 = gl_list_search (list1, obj); | |
240 node2 = gl_list_search (list2, obj); | |
241 ASSERT (node1 != NULL); | |
242 ASSERT (node2 != NULL); | |
243 ASSERT (gl_list_remove_node (list1, node1)); | |
244 ASSERT (gl_list_remove_node (list2, node2)); | |
245 ASSERT (gl_list_size (list1) == n - 1); | |
246 } | |
247 break; | |
248 case 9: case 10: /* remove 1 element */ | |
249 if (gl_list_size (list1) > 0) | |
250 { | |
251 size_t n = gl_list_size (list1); | |
252 size_t index = RANDOM (n); | |
253 ASSERT (gl_list_remove_at (list1, index)); | |
254 ASSERT (gl_list_remove_at (list2, index)); | |
255 ASSERT (gl_list_size (list1) == n - 1); | |
256 } | |
257 break; | |
258 case 11: case 12: /* remove 1 element */ | |
259 if (gl_list_size (list1) > 0) | |
260 { | |
261 size_t n = gl_list_size (list1); | |
262 const char *obj = gl_list_get_at (list1, RANDOM (n)); | |
263 ASSERT (gl_list_remove (list1, obj)); | |
264 ASSERT (gl_list_remove (list2, obj)); | |
265 ASSERT (gl_list_size (list1) == n - 1); | |
266 } | |
267 break; | |
268 case 13: | |
269 if (gl_list_size (list1) > 0) | |
270 { | |
271 size_t n = gl_list_size (list1); | |
272 const char *obj = "xyzzy"; | |
273 ASSERT (!gl_list_remove (list1, obj)); | |
274 ASSERT (!gl_list_remove (list2, obj)); | |
275 ASSERT (gl_list_size (list1) == n); | |
276 } | |
277 break; | |
278 case 14: | |
279 { | |
280 size_t n = gl_list_size (list1); | |
281 gl_list_iterator_t iter1, iter2; | |
282 const void *elt; | |
283 iter1 = gl_list_iterator (list1); | |
284 iter2 = gl_list_iterator (list2); | |
285 for (i = 0; i < n; i++) | |
286 { | |
287 ASSERT (gl_list_iterator_next (&iter1, &elt, NULL)); | |
288 ASSERT (gl_list_get_at (list1, i) == elt); | |
289 ASSERT (gl_list_iterator_next (&iter2, &elt, NULL)); | |
290 ASSERT (gl_list_get_at (list2, i) == elt); | |
291 } | |
292 ASSERT (!gl_list_iterator_next (&iter1, &elt, NULL)); | |
293 ASSERT (!gl_list_iterator_next (&iter2, &elt, NULL)); | |
294 gl_list_iterator_free (&iter1); | |
295 gl_list_iterator_free (&iter2); | |
296 } | |
297 break; | |
298 case 15: | |
299 { | |
300 size_t end = RANDOM (gl_list_size (list1) + 1); | |
301 size_t start = RANDOM (end + 1); | |
302 gl_list_iterator_t iter1, iter2; | |
303 const void *elt; | |
304 iter1 = gl_list_iterator_from_to (list1, start, end); | |
305 iter2 = gl_list_iterator_from_to (list2, start, end); | |
306 for (i = start; i < end; i++) | |
307 { | |
308 ASSERT (gl_list_iterator_next (&iter1, &elt, NULL)); | |
309 ASSERT (gl_list_get_at (list1, i) == elt); | |
310 ASSERT (gl_list_iterator_next (&iter2, &elt, NULL)); | |
311 ASSERT (gl_list_get_at (list2, i) == elt); | |
312 } | |
313 ASSERT (!gl_list_iterator_next (&iter1, &elt, NULL)); | |
314 ASSERT (!gl_list_iterator_next (&iter2, &elt, NULL)); | |
315 gl_list_iterator_free (&iter1); | |
316 gl_list_iterator_free (&iter2); | |
317 } | |
318 break; | |
319 } | |
320 check_equals (list1, list2); | |
321 } | |
322 | |
323 gl_list_free (list1); | |
324 gl_list_free (list2); | |
325 free (contents); | |
326 } | |
327 | |
328 return 0; | |
329 } |