95b549fd8cb79b14068cc79ca4c070a3d7c37ce9
[pspp] / tests / libpspp / llx-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 /* This is a test program for the llx_* routines defined in
18    ll.c.  This test program aims to be as comprehensive as
19    possible.  "gcov -b" should report 100% coverage of lines and
20    branches in llx.c and llx.h.  "valgrind --leak-check=yes
21    --show-reachable=yes" should give a clean report.
22
23    This test program depends only on ll.c, llx.c, and the
24    standard C library.
25
26    See ll-test.c for a similar program for the ll_* routines. */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <libpspp/llx.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 \f
37 /* Support preliminaries. */
38 #if __GNUC__ >= 2 && !defined UNUSED
39 #define UNUSED __attribute__ ((unused))
40 #else
41 #define UNUSED
42 #endif
43
44 /* Exit with a failure code.
45    (Place a breakpoint on this function while debugging.) */
46 static void
47 check_die (void)
48 {
49   exit (EXIT_FAILURE);
50 }
51
52 /* If OK is not true, prints a message about failure on the
53    current source file and the given LINE and terminates. */
54 static void
55 check_func (bool ok, int line)
56 {
57   if (!ok)
58     {
59       fprintf (stderr, "%s:%d: check failed\n", __FILE__, line);
60       check_die ();
61     }
62 }
63
64 /* Verifies that EXPR evaluates to true.
65    If not, prints a message citing the calling line number and
66    terminates. */
67 #define check(EXPR) check_func ((EXPR), __LINE__)
68
69 /* Prints a message about memory exhaustion and exits with a
70    failure code. */
71 static void
72 xalloc_die (void)
73 {
74   printf ("virtual memory exhausted\n");
75   exit (EXIT_FAILURE);
76 }
77
78 /* Allocates and returns N bytes of memory. */
79 static void *
80 xmalloc (size_t n)
81 {
82   if (n != 0)
83     {
84       void *p = malloc (n);
85       if (p == NULL)
86         xalloc_die ();
87
88       return p;
89     }
90   else
91     return NULL;
92 }
93
94 /* Allocates and returns N * M bytes of memory. */
95 static void *
96 xnmalloc (size_t n, size_t m)
97 {
98   if ((size_t) -1 / m <= n)
99     xalloc_die ();
100   return xmalloc (n * m);
101 }
102 \f
103 /* Always returns a null pointer, failing allocation. */
104 static struct llx *
105 null_allocate_node (void *aux UNUSED)
106 {
107   return NULL;
108 }
109
110 /* Does nothing. */
111 static void
112 null_release_node (struct llx *llx UNUSED, void *aux UNUSED)
113 {
114 }
115
116 /* Memory manager that fails all allocations and does nothing on
117    free. */
118 static const struct llx_manager llx_null_mgr =
119   {
120     null_allocate_node,
121     null_release_node,
122     NULL,
123   };
124 \f
125 /* List type and support routines. */
126
127 /* Test data element. */
128 struct element
129   {
130     int x;                      /* Primary value. */
131     int y;                      /* Secondary value. */
132   };
133
134 static int aux_data;
135
136 /* Prints the elements in LIST. */
137 static void UNUSED
138 print_list (struct llx_list *list)
139 {
140   struct llx *x;
141
142   printf ("list:");
143   for (x = llx_head (list); x != llx_null (list); x = llx_next (x))
144     {
145       const struct element *e = llx_data (x);
146       printf (" %d", e->x);
147     }
148   printf ("\n");
149 }
150
151 /* Prints the value returned by PREDICATE given auxiliary data
152    AUX for each element in LIST. */
153 static void UNUSED
154 print_pred (struct llx_list *list,
155             llx_predicate_func *predicate, void *aux UNUSED)
156 {
157   struct llx *x;
158
159   printf ("pred:");
160   for (x = llx_head (list); x != llx_null (list); x = llx_next (x))
161     printf (" %d", predicate (x, aux));
162   printf ("\n");
163 }
164
165 /* Prints the CNT numbers in VALUES. */
166 static void UNUSED
167 print_array (int values[], size_t cnt)
168 {
169   size_t i;
170
171   printf ("arry:");
172   for (i = 0; i < cnt; i++)
173     printf (" %d", values[i]);
174   printf ("\n");
175 }
176
177 /* Compares the `x' values in A and B and returns a strcmp-type
178    return value.  Verifies that AUX points to aux_data. */
179 static int
180 compare_elements (const void *a_, const void *b_, void *aux)
181 {
182   const struct element *a = a_;
183   const struct element *b = b_;
184
185   check (aux == &aux_data);
186   return a->x < b->x ? -1 : a->x > b->x;
187 }
188
189 /* Compares the `x' and `y' values in A and B and returns a
190    strcmp-type return value.  Verifies that AUX points to
191    aux_data. */
192 static int
193 compare_elements_x_y (const void *a_, const void *b_, void *aux)
194 {
195   const struct element *a = a_;
196   const struct element *b = b_;
197
198   check (aux == &aux_data);
199   if (a->x != b->x)
200     return a->x < b->x ? -1 : 1;
201   else if (a->y != b->y)
202     return a->y < b->y ? -1 : 1;
203   else
204     return 0;
205 }
206
207 /* Compares the `y' values in A and B and returns a strcmp-type
208    return value.  Verifies that AUX points to aux_data. */
209 static int
210 compare_elements_y (const void *a_, const void *b_, void *aux)
211 {
212   const struct element *a = a_;
213   const struct element *b = b_;
214
215   check (aux == &aux_data);
216   return a->y < b->y ? -1 : a->y > b->y;
217 }
218
219 /* Returns true if the bit in *PATTERN indicated by `x in
220    *ELEMENT is set, false otherwise. */
221 static bool
222 pattern_pred (const void *element_, void *pattern_)
223 {
224   const struct element *element = element_;
225   unsigned int *pattern = pattern_;
226
227   return (*pattern & (1u << element->x)) != 0;
228 }
229
230 /* Allocates N elements in *ELEMS.
231    Adds the elements to LIST, if it is nonnull.
232    Puts pointers to the elements' list elements in *ELEMP,
233    followed by a pointer to the list null element, if ELEMP is
234    nonnull.
235    Allocates space for N values in *VALUES, if VALUES is
236    nonnull. */
237 static void
238 allocate_elements (size_t n,
239                    struct llx_list *list,
240                    struct element ***elems,
241                    struct llx ***elemp,
242                    int **values)
243 {
244   size_t i;
245
246   if (list != NULL)
247     llx_init (list);
248
249   *elems = xnmalloc (n, sizeof **elems);
250   if (elemp != NULL)
251     {
252       *elemp = xnmalloc (n + 1, sizeof *elemp);
253       (*elemp)[n] = llx_null (list);
254     }
255
256   for (i = 0; i < n; i++)
257     {
258       (*elems)[i] = xmalloc (sizeof ***elems);
259       if (list != NULL)
260         {
261           struct llx *llx = llx_push_tail (list, (*elems)[i], &llx_malloc_mgr);
262           if (elemp != NULL)
263             (*elemp)[i] = llx;
264         }
265     }
266
267   if (values != NULL)
268     *values = xnmalloc (n, sizeof *values);
269 }
270
271 /* Copies the CNT values of `x' from LIST into VALUES[]. */
272 static void
273 extract_values (struct llx_list *list, int values[], size_t cnt)
274 {
275   struct llx *x;
276
277   check (llx_count (list) == cnt);
278   for (x = llx_head (list); x != llx_null (list); x = llx_next (x))
279     {
280       struct element *e = llx_data (x);
281       *values++ = e->x;
282     }
283 }
284
285 /* As allocate_elements, but sets ascending values, starting
286    from 0, in `x' values in *ELEMS and in *VALUES (if
287    nonnull). */
288 static void
289 allocate_ascending (size_t n,
290                     struct llx_list *list,
291                     struct element ***elems,
292                     struct llx ***elemp,
293                     int **values)
294 {
295   size_t i;
296
297   allocate_elements (n, list, elems, elemp, values);
298
299   for (i = 0; i < n; i++)
300     (*elems)[i]->x = i;
301   if (values != NULL)
302     extract_values (list, *values, n);
303 }
304
305 /* As allocate_elements, but sets binary values extracted from
306    successive bits in PATTERN in `x' values in *ELEMS and in
307    *VALUES (if nonnull). */
308 static void
309 allocate_pattern (size_t n,
310                   int pattern,
311                   struct llx_list *list,
312                   struct element ***elems,
313                   struct llx ***elemp,
314                   int **values)
315 {
316   size_t i;
317
318   allocate_elements (n, list, elems, elemp, values);
319
320   for (i = 0; i < n; i++)
321     (*elems)[i]->x = (pattern & (1 << i)) != 0;
322   if (values != NULL)
323     extract_values (list, *values, n);
324 }
325
326 /* Randomly shuffles the CNT elements in ARRAY, each of which is
327    SIZE bytes in size. */
328 static void
329 random_shuffle (void *array_, size_t cnt, size_t size)
330 {
331   char *array = array_;
332   char *tmp = xmalloc (size);
333   size_t i;
334
335   for (i = 0; i < cnt; i++)
336     {
337       size_t j = rand () % (cnt - i) + i;
338       if (i != j)
339         {
340           memcpy (tmp, array + j * size, size);
341           memcpy (array + j * size, array + i * size, size);
342           memcpy (array + i * size, tmp, size);
343         }
344     }
345
346   free (tmp);
347 }
348
349 /* As allocate_ascending, but orders the values randomly. */
350 static void
351 allocate_random (size_t n,
352                  struct llx_list *list,
353                  struct element ***elems,
354                  struct llx ***elemp,
355                  int **values)
356 {
357   size_t i;
358
359   allocate_elements (n, list, elems, elemp, values);
360
361   for (i = 0; i < n; i++)
362     (*elems)[i]->x = i;
363   random_shuffle (*elems, n, sizeof **elems);
364   if (values != NULL)
365     extract_values (list, *values, n);
366 }
367
368 /* Frees LIST, the N elements of ELEMS, ELEMP, and VALUES. */
369 static void
370 free_elements (size_t n,
371                struct llx_list *list,
372                struct element **elems,
373                struct llx **elemp,
374                int *values)
375 {
376   size_t i;
377
378   if (list != NULL)
379     llx_destroy (list, NULL, NULL, &llx_malloc_mgr);
380   for (i = 0; i < n; i++)
381     free (elems[i]);
382   free (elems);
383   free (elemp);
384   free (values);
385 }
386
387 /* Compares A and B and returns a strcmp-type return value. */
388 static int
389 compare_ints (const void *a_, const void *b_, void *aux UNUSED)
390 {
391   const int *a = a_;
392   const int *b = b_;
393
394   return *a < *b ? -1 : *a > *b;
395 }
396
397 /* Compares A and B and returns a strcmp-type return value. */
398 static int
399 compare_ints_noaux (const void *a_, const void *b_)
400 {
401   const int *a = a_;
402   const int *b = b_;
403
404   return *a < *b ? -1 : *a > *b;
405 }
406
407 /* Checks that LIST contains the CNT values in ELEMENTS. */
408 static void
409 check_list_contents (struct llx_list *list, int elements[], size_t cnt)
410 {
411   struct llx *llx;
412   size_t i;
413
414   check ((cnt == 0) == llx_is_empty (list));
415
416   /* Iterate in forward order. */
417   for (llx = llx_head (list), i = 0; i < cnt; llx = llx_next (llx), i++)
418     {
419       struct element *e = llx_data (llx);
420       check (elements[i] == e->x);
421       check (llx != llx_null (list));
422     }
423   check (llx == llx_null (list));
424
425   /* Iterate in reverse order. */
426   for (llx = llx_tail (list), i = 0; i < cnt; llx = llx_prev (llx), i++)
427     {
428       struct element *e = llx_data (llx);
429       check (elements[cnt - i - 1] == e->x);
430       check (llx != llx_null (list));
431     }
432   check (llx == llx_null (list));
433
434   check (llx_count (list) == cnt);
435 }
436
437 /* Lexicographicallxy compares ARRAY1, which contains COUNT1
438    elements of SIZE bytes each, to ARRAY2, which contains COUNT2
439    elements of SIZE bytes, according to COMPARE.  Returns a
440    strcmp-type result.  AUX is passed to COMPARE as auxiliary
441    data. */
442 static int
443 lexicographical_compare_3way (const void *array1, size_t count1,
444                               const void *array2, size_t count2,
445                               size_t size,
446                               int (*compare) (const void *, const void *,
447                                               void *aux),
448                               void *aux)
449 {
450   const char *first1 = array1;
451   const char *first2 = array2;
452   size_t min_count = count1 < count2 ? count1 : count2;
453
454   while (min_count > 0)
455     {
456       int cmp = compare (first1, first2, aux);
457       if (cmp != 0)
458         return cmp;
459
460       first1 += size;
461       first2 += size;
462       min_count--;
463     }
464
465   return count1 < count2 ? -1 : count1 > count2;
466 }
467 \f
468 /* Tests. */
469
470 /* Tests list push and pop operations. */
471 static void
472 test_push_pop (void)
473 {
474   const int max_elems = 1024;
475
476   struct llx_list list;
477   struct element **elems;
478   int *values;
479
480   int i;
481
482   allocate_elements (max_elems, NULL, &elems, NULL, &values);
483
484   /* Push on tail. */
485   llx_init (&list);
486   check_list_contents (&list, NULL, 0);
487   for (i = 0; i < max_elems; i++)
488     {
489       values[i] = elems[i]->x = i;
490       llx_push_tail (&list, elems[i], &llx_malloc_mgr);
491       check_list_contents (&list, values, i + 1);
492     }
493
494   /* Remove from tail. */
495   for (i = 0; i < max_elems; i++)
496     {
497       struct element *e = llx_pop_tail (&list, &llx_malloc_mgr);
498       check (e->x == max_elems - i - 1);
499       check_list_contents (&list, values, max_elems - i - 1);
500     }
501
502   /* Push at start. */
503   check_list_contents (&list, NULL, 0);
504   for (i = 0; i < max_elems; i++)
505     {
506       values[max_elems - i - 1] = elems[i]->x = max_elems - i - 1;
507       llx_push_head (&list, elems[i], &llx_malloc_mgr);
508       check_list_contents (&list, &values[max_elems - i - 1], i + 1);
509     }
510
511   /* Remove from start. */
512   for (i = 0; i < max_elems; i++)
513     {
514       struct element *e = llx_pop_head (&list, &llx_malloc_mgr);
515       check (e->x == (int) i);
516       check_list_contents (&list, &values[i + 1], max_elems - i - 1);
517     }
518
519   free_elements (max_elems, &list, elems, NULL, values);
520 }
521
522 /* Tests insertion and removal at arbitrary positions. */
523 static void
524 test_insert_remove (void)
525 {
526   const int max_elems = 16;
527   int cnt;
528
529   for (cnt = 0; cnt < max_elems; cnt++)
530     {
531       struct element **elems;
532       struct llx **elemp;
533       int *values = xnmalloc (cnt + 1, sizeof *values);
534
535       struct llx_list list;
536       struct element extra;
537       struct llx *extra_llx;
538       int pos;
539
540       allocate_ascending (cnt, &list, &elems, &elemp, NULL);
541       extra.x = -1;
542       for (pos = 0; pos <= cnt; pos++)
543         {
544           int i, j;
545
546           extra_llx = llx_insert (elemp[pos], &extra, &llx_malloc_mgr);
547           check (extra_llx != NULL);
548
549           j = 0;
550           for (i = 0; i < pos; i++)
551             values[j++] = i;
552           values[j++] = -1;
553           for (; i < cnt; i++)
554             values[j++] = i;
555           check_list_contents (&list, values, cnt + 1);
556
557           llx_remove (extra_llx, &llx_malloc_mgr);
558         }
559       check_list_contents (&list, values, cnt);
560
561       free_elements (cnt, &list, elems, elemp, values);
562     }
563 }
564
565 /* Tests swapping individual elements. */
566 static void
567 test_swap (void)
568 {
569   const int max_elems = 8;
570   int cnt;
571
572   for (cnt = 0; cnt <= max_elems; cnt++)
573     {
574       struct llx_list list;
575       struct element **elems;
576       struct llx **elemp;
577       int *values;
578
579       int i, j, k;
580
581       allocate_ascending (cnt, &list, &elems, &elemp, &values);
582       check_list_contents (&list, values, cnt);
583
584       for (i = 0; i < cnt; i++)
585         for (j = 0; j < cnt; j++)
586           for (k = 0; k < 2; k++)
587             {
588               int t;
589
590               llx_swap (elemp[i], elemp[j]);
591               t = values[i];
592               values[i] = values[j];
593               values[j] = t;
594               check_list_contents (&list, values, cnt);
595             }
596
597       free_elements (cnt, &list, elems, elemp, values);
598     }
599 }
600
601 /* Tests swapping ranges of list elements. */
602 static void
603 test_swap_range (void)
604 {
605   const int max_elems = 8;
606   int cnt, a0, a1, b0, b1, r;
607
608   for (cnt = 0; cnt <= max_elems; cnt++)
609     for (a0 = 0; a0 <= cnt; a0++)
610       for (a1 = a0; a1 <= cnt; a1++)
611         for (b0 = a1; b0 <= cnt; b0++)
612           for (b1 = b0; b1 <= cnt; b1++)
613             for (r = 0; r < 2; r++)
614               {
615                 struct llx_list list;
616                 struct element **elems;
617                 struct llx **elemp;
618                 int *values;
619
620                 int i, j;
621
622                 allocate_ascending (cnt, &list, &elems, &elemp, &values);
623                 check_list_contents (&list, values, cnt);
624
625                 j = 0;
626                 for (i = 0; i < a0; i++)
627                   values[j++] = i;
628                 for (i = b0; i < b1; i++)
629                   values[j++] = i;
630                 for (i = a1; i < b0; i++)
631                   values[j++] = i;
632                 for (i = a0; i < a1; i++)
633                   values[j++] = i;
634                 for (i = b1; i < cnt; i++)
635                   values[j++] = i;
636                 check (j == cnt);
637
638                 if (r == 0)
639                   llx_swap_range (elemp[a0], elemp[a1], elemp[b0], elemp[b1]);
640                 else
641                   llx_swap_range (elemp[b0], elemp[b1], elemp[a0], elemp[a1]);
642                 check_list_contents (&list, values, cnt);
643
644                 free_elements (cnt, &list, elems, elemp, values);
645               }
646 }
647
648 /* Tests removing ranges of list elements. */
649 static void
650 test_remove_range (void)
651 {
652   const int max_elems = 8;
653
654   int cnt, r0, r1;
655
656   for (cnt = 0; cnt <= max_elems; cnt++)
657     for (r0 = 0; r0 <= cnt; r0++)
658       for (r1 = r0; r1 <= cnt; r1++)
659         {
660           struct llx_list list;
661           struct element **elems;
662           struct llx **elemp;
663           int *values;
664
665           int i, j;
666
667           allocate_ascending (cnt, &list, &elems, &elemp, &values);
668           check_list_contents (&list, values, cnt);
669
670           j = 0;
671           for (i = 0; i < r0; i++)
672             values[j++] = i;
673           for (i = r1; i < cnt; i++)
674             values[j++] = i;
675
676           llx_remove_range (elemp[r0], elemp[r1], &llx_malloc_mgr);
677           check_list_contents (&list, values, j);
678
679           free_elements (cnt, &list, elems, elemp, values);
680         }
681 }
682
683 /* Tests llx_remove_equal. */
684 static void
685 test_remove_equal (void)
686 {
687   const int max_elems = 8;
688
689   int cnt, r0, r1, eq_pat;
690
691   for (cnt = 0; cnt <= max_elems; cnt++)
692     for (r0 = 0; r0 <= cnt; r0++)
693       for (r1 = r0; r1 <= cnt; r1++)
694         for (eq_pat = 0; eq_pat <= 1 << cnt; eq_pat++)
695           {
696             struct llx_list list;
697             struct element **elems;
698             struct llx **elemp;
699             int *values;
700
701             struct element to_remove;
702             int remaining;
703             int i;
704
705             allocate_elements (cnt, &list, &elems, &elemp, &values);
706
707             remaining = 0;
708             for (i = 0; i < cnt; i++)
709               {
710                 int x = eq_pat & (1 << i) ? -1 : i;
711                 bool delete = x == -1 && r0 <= i && i < r1;
712                 elems[i]->x = x;
713                 if (!delete)
714                   values[remaining++] = x;
715               }
716
717             to_remove.x = -1;
718             check ((int) llx_remove_equal (elemp[r0], elemp[r1], &to_remove,
719                                            compare_elements, &aux_data,
720                                            &llx_malloc_mgr)
721                    == cnt - remaining);
722             check_list_contents (&list, values, remaining);
723
724             free_elements (cnt, &list, elems, elemp, values);
725           }
726 }
727
728 /* Tests llx_remove_if. */
729 static void
730 test_remove_if (void)
731 {
732   const int max_elems = 8;
733
734   int cnt, r0, r1, pattern;
735
736   for (cnt = 0; cnt <= max_elems; cnt++)
737     for (r0 = 0; r0 <= cnt; r0++)
738       for (r1 = r0; r1 <= cnt; r1++)
739         for (pattern = 0; pattern <= 1 << cnt; pattern++)
740           {
741             struct llx_list list;
742             struct element **elems;
743             struct llx **elemp;
744             int *values;
745
746             int remaining;
747             int i;
748
749             allocate_ascending (cnt, &list, &elems, &elemp, &values);
750
751             remaining = 0;
752             for (i = 0; i < cnt; i++)
753               {
754                 bool delete = (pattern & (1 << i))  && r0 <= i && i < r1;
755                 if (!delete)
756                   values[remaining++] = i;
757               }
758
759             check ((int) llx_remove_if (elemp[r0], elemp[r1],
760                                         pattern_pred, &pattern,
761                                         &llx_malloc_mgr)
762                    == cnt - remaining);
763             check_list_contents (&list, values, remaining);
764
765             free_elements (cnt, &list, elems, elemp, values);
766           }
767 }
768
769 /* Tests, via HELPER, a function that looks at list elements
770    equal to some specified element. */
771 static void
772 test_examine_equal_range (void (*helper) (int r0, int r1, int eq_pat,
773                                           const void *to_find,
774                                           struct llx **elemp))
775 {
776   const int max_elems = 8;
777
778   int cnt, r0, r1, eq_pat;
779
780   for (cnt = 0; cnt <= max_elems; cnt++)
781     for (eq_pat = 0; eq_pat <= 1 << cnt; eq_pat++)
782       {
783         struct llx_list list;
784         struct element **elems;
785         struct llx **elemp;
786         int *values;
787
788         struct element to_find;
789
790         int i;
791
792         allocate_ascending (cnt, &list, &elems, &elemp, &values);
793
794         for (i = 0; i < cnt; i++)
795           if (eq_pat & (1 << i))
796             values[i] = elems[i]->x = -1;
797
798         to_find.x = -1;
799         for (r0 = 0; r0 <= cnt; r0++)
800           for (r1 = r0; r1 <= cnt; r1++)
801             helper (r0, r1, eq_pat, &to_find, elemp);
802
803         check_list_contents (&list, values, cnt);
804
805         free_elements (cnt, &list, elems, elemp, values);
806       }
807 }
808
809 /* Tests, via HELPER, a function that looks at list elements for
810    which a given predicate returns true. */
811 static void
812 test_examine_if_range (void (*helper) (int r0, int r1, int eq_pat,
813                                        struct llx **elemp))
814 {
815   const int max_elems = 8;
816
817   int cnt, r0, r1, eq_pat;
818
819   for (cnt = 0; cnt <= max_elems; cnt++)
820     for (eq_pat = 0; eq_pat <= 1 << cnt; eq_pat++)
821       {
822         struct llx_list list;
823         struct element **elems;
824         struct llx **elemp;
825         int *values;
826
827         allocate_ascending (cnt, &list, &elems, &elemp, &values);
828
829         for (r0 = 0; r0 <= cnt; r0++)
830           for (r1 = r0; r1 <= cnt; r1++)
831             helper (r0, r1, eq_pat, elemp);
832
833         check_list_contents (&list, values, cnt);
834
835         free_elements (cnt, &list, elems, elemp, values);
836       }
837 }
838
839 /* Helper function for testing llx_find_equal. */
840 static void
841 test_find_equal_helper (int r0, int r1, int eq_pat,
842                         const void *to_find, struct llx **elemp)
843 {
844   struct llx *match;
845   int i;
846
847   match = llx_find_equal (elemp[r0], elemp[r1], to_find,
848                           compare_elements, &aux_data);
849   for (i = r0; i < r1; i++)
850     if (eq_pat & (1 << i))
851       break;
852
853   check (match == elemp[i]);
854 }
855
856 /* Tests llx_find_equal. */
857 static void
858 test_find_equal (void)
859 {
860   test_examine_equal_range (test_find_equal_helper);
861 }
862
863 /* Tests llx_find(). */
864 static void
865 test_find (void)
866 {
867   const int max_elems = 8;
868
869   int cnt;
870
871   for (cnt = 0; cnt <= max_elems; cnt++)
872     {
873       struct llx_list list;
874       struct element **elems;
875       struct llx **elemp;
876       int *values;
877
878       int i;
879
880       allocate_ascending (cnt, &list, &elems, &elemp, &values);
881
882       for (i = 0; i < cnt; i++)
883         check (llx_find (llx_head (&list), llx_null (&list), elems[i])
884                == elemp[i]);
885       check (llx_find (llx_head (&list), llx_null (&list), NULL) == NULL);
886
887       free_elements (cnt, &list, elems, elemp, values);
888     }
889 }
890
891 /* Helper function for testing llx_find_if. */
892 static void
893 test_find_if_helper (int r0, int r1, int eq_pat, struct llx **elemp)
894 {
895   struct llx *match = llx_find_if (elemp[r0], elemp[r1],
896                                    pattern_pred, &eq_pat);
897   int i;
898
899   for (i = r0; i < r1; i++)
900     if (eq_pat & (1 << i))
901       break;
902
903   check (match == elemp[i]);
904 }
905
906 /* Tests llx_find_if. */
907 static void
908 test_find_if (void)
909 {
910   test_examine_if_range (test_find_if_helper);
911 }
912
913 /* Tests llx_find_adjacent_equal. */
914 static void
915 test_find_adjacent_equal (void)
916 {
917   const int max_elems = 8;
918
919   int cnt, eq_pat;
920
921   for (cnt = 0; cnt <= max_elems; cnt++)
922     for (eq_pat = 0; eq_pat <= 1 << cnt; eq_pat++)
923       {
924         struct llx_list list;
925         struct element **elems;
926         struct llx **elemp;
927         int *values;
928         int match;
929
930         int i;
931
932         allocate_ascending (cnt, &list, &elems, &elemp, &values);
933
934         match = -1;
935         for (i = 0; i < cnt - 1; i++)
936           {
937             elems[i]->y = i;
938             if (eq_pat & (1 << i))
939               {
940                 values[i] = elems[i]->x = match;
941                 values[i + 1] = elems[i + 1]->x = match;
942               }
943             else
944               match--;
945           }
946
947         for (i = 0; i <= cnt; i++)
948           {
949             struct llx *llx1 = llx_find_adjacent_equal (elemp[i], llx_null (&list),
950                                                         compare_elements,
951                                                         &aux_data);
952             struct llx *llx2;
953             int j;
954
955             llx2 = llx_null (&list);
956             for (j = i; j < cnt - 1; j++)
957               if (eq_pat & (1 << j))
958                 {
959                   llx2 = elemp[j];
960                   break;
961                 }
962             check (llx1 == llx2);
963           }
964         check_list_contents (&list, values, cnt);
965
966         free_elements (cnt, &list, elems, elemp, values);
967       }
968 }
969
970 /* Helper function for testing llx_count_range. */
971 static void
972 test_count_range_helper (int r0, int r1, int eq_pat UNUSED, struct llx **elemp)
973 {
974   check ((int) llx_count_range (elemp[r0], elemp[r1]) == r1 - r0);
975 }
976
977 /* Tests llx_count_range. */
978 static void
979 test_count_range (void)
980 {
981   test_examine_if_range (test_count_range_helper);
982 }
983
984 /* Helper function for testing llx_count_equal. */
985 static void
986 test_count_equal_helper (int r0, int r1, int eq_pat,
987                          const void *to_find, struct llx **elemp)
988 {
989   int count1, count2;
990   int i;
991
992   count1 = llx_count_equal (elemp[r0], elemp[r1], to_find,
993                             compare_elements, &aux_data);
994   count2 = 0;
995   for (i = r0; i < r1; i++)
996     if (eq_pat & (1 << i))
997       count2++;
998
999   check (count1 == count2);
1000 }
1001
1002 /* Tests llx_count_equal. */
1003 static void
1004 test_count_equal (void)
1005 {
1006   test_examine_equal_range (test_count_equal_helper);
1007 }
1008
1009 /* Helper function for testing llx_count_if. */
1010 static void
1011 test_count_if_helper (int r0, int r1, int eq_pat, struct llx **elemp)
1012 {
1013   int count1;
1014   int count2;
1015   int i;
1016
1017   count1 = llx_count_if (elemp[r0], elemp[r1], pattern_pred, &eq_pat);
1018
1019   count2 = 0;
1020   for (i = r0; i < r1; i++)
1021     if (eq_pat & (1 << i))
1022       count2++;
1023
1024   check (count1 == count2);
1025 }
1026
1027 /* Tests llx_count_if. */
1028 static void
1029 test_count_if (void)
1030 {
1031   test_examine_if_range (test_count_if_helper);
1032 }
1033
1034 /* Returns N!. */
1035 static unsigned int
1036 factorial (unsigned int n)
1037 {
1038   unsigned int value = 1;
1039   while (n > 1)
1040     value *= n--;
1041   return value;
1042 }
1043
1044 /* Returns the number of permutations of the CNT values in
1045    VALUES.  If VALUES contains duplicates, they must be
1046    adjacent. */
1047 static unsigned int
1048 expected_perms (int *values, size_t cnt)
1049 {
1050   size_t i, j;
1051   unsigned int perm_cnt;
1052
1053   perm_cnt = factorial (cnt);
1054   for (i = 0; i < cnt; i = j)
1055     {
1056       for (j = i + 1; j < cnt; j++)
1057         if (values[i] != values[j])
1058           break;
1059       perm_cnt /= factorial (j - i);
1060     }
1061   return perm_cnt;
1062 }
1063
1064 /* Tests llx_min and llx_max. */
1065 static void
1066 test_min_max (void)
1067 {
1068   const int max_elems = 6;
1069   int cnt;
1070
1071   for (cnt = 0; cnt <= max_elems; cnt++)
1072     {
1073       struct llx_list list;
1074       struct element **elems;
1075       struct llx **elemp;
1076       int *values;
1077       int *new_values = xnmalloc (cnt, sizeof *values);
1078
1079       size_t perm_cnt;
1080
1081       allocate_ascending (cnt, &list, &elems, &elemp, &values);
1082
1083       perm_cnt = 1;
1084       while (llx_next_permutation (llx_head (&list), llx_null (&list),
1085                                    compare_elements, &aux_data))
1086         {
1087           int r0, r1;
1088           struct llx *x;
1089           int i;
1090
1091           for (i = 0, x = llx_head (&list); x != llx_null (&list);
1092                x = llx_next (x), i++)
1093             {
1094               struct element *e = llx_data (x);
1095               elemp[i] = x;
1096               new_values[i] = e->x;
1097             }
1098           for (r0 = 0; r0 <= cnt; r0++)
1099             for (r1 = r0; r1 <= cnt; r1++)
1100               {
1101                 struct llx *min = llx_min (elemp[r0], elemp[r1],
1102                                            compare_elements, &aux_data);
1103                 struct llx *max = llx_max (elemp[r0], elemp[r1],
1104                                            compare_elements, &aux_data);
1105                 if (r0 == r1)
1106                   {
1107                     check (min == elemp[r1]);
1108                     check (max == elemp[r1]);
1109                   }
1110                 else
1111                   {
1112                     struct element *min_elem = llx_data (min);
1113                     struct element *max_elem = llx_data (max);
1114                     int min_int, max_int;
1115                     int i;
1116
1117                     min_int = max_int = new_values[r0];
1118                     for (i = r0; i < r1; i++)
1119                       {
1120                         int value = new_values[i];
1121                         if (value < min_int)
1122                           min_int = value;
1123                         if (value > max_int)
1124                           max_int = value;
1125                       }
1126                     check (min != elemp[r1] && min_elem->x == min_int);
1127                     check (max != elemp[r1] && max_elem->x == max_int);
1128                   }
1129               }
1130           perm_cnt++;
1131         }
1132       check (perm_cnt == factorial (cnt));
1133       check_list_contents (&list, values, cnt);
1134
1135       free_elements (cnt, &list, elems, elemp, values);
1136       free (new_values);
1137     }
1138 }
1139
1140 /* Tests llx_lexicographical_compare_3way. */
1141 static void
1142 test_lexicographical_compare_3way (void)
1143 {
1144   const int max_elems = 4;
1145
1146   int cnt_a, pat_a, cnt_b, pat_b;
1147
1148   for (cnt_a = 0; cnt_a <= max_elems; cnt_a++)
1149     for (pat_a = 0; pat_a <= 1 << cnt_a; pat_a++)
1150       for (cnt_b = 0; cnt_b <= max_elems; cnt_b++)
1151         for (pat_b = 0; pat_b <= 1 << cnt_b; pat_b++)
1152           {
1153             struct llx_list list_a, list_b;
1154             struct element **elems_a, **elems_b;
1155             struct llx **elemp_a, **elemp_b;
1156             int *values_a, *values_b;
1157
1158             int a0, a1, b0, b1;
1159
1160             allocate_pattern (cnt_a, pat_a,
1161                               &list_a, &elems_a, &elemp_a, &values_a);
1162             allocate_pattern (cnt_b, pat_b,
1163                               &list_b, &elems_b, &elemp_b, &values_b);
1164
1165             for (a0 = 0; a0 <= cnt_a; a0++)
1166               for (a1 = a0; a1 <= cnt_a; a1++)
1167                 for (b0 = 0; b0 <= cnt_b; b0++)
1168                   for (b1 = b0; b1 <= cnt_b; b1++)
1169                     {
1170                       int a_ordering = lexicographical_compare_3way (
1171                         values_a + a0, a1 - a0,
1172                         values_b + b0, b1 - b0,
1173                         sizeof *values_a,
1174                         compare_ints, NULL);
1175
1176                       int b_ordering = llx_lexicographical_compare_3way (
1177                         elemp_a[a0], elemp_a[a1],
1178                         elemp_b[b0], elemp_b[b1],
1179                         compare_elements, &aux_data);
1180
1181                       check (a_ordering == b_ordering);
1182                     }
1183
1184             free_elements (cnt_a, &list_a, elems_a, elemp_a, values_a);
1185             free_elements (cnt_b, &list_b, elems_b, elemp_b, values_b);
1186           }
1187 }
1188
1189 /* Appends the `x' value in element E to the array pointed to by
1190    NEXT_OUTPUT, and advances NEXT_OUTPUT to the next position. */
1191 static void
1192 apply_func (void *e_, void *next_output_)
1193 {
1194   struct element *e = e_;
1195   int **next_output = next_output_;
1196
1197   *(*next_output)++ = e->x;
1198 }
1199
1200 /* Tests llx_apply. */
1201 static void
1202 test_apply (void)
1203 {
1204   const int max_elems = 8;
1205
1206   int cnt, r0, r1;
1207
1208   for (cnt = 0; cnt <= max_elems; cnt++)
1209     for (r0 = 0; r0 <= cnt; r0++)
1210       for (r1 = r0; r1 <= cnt; r1++)
1211         {
1212           struct llx_list list;
1213           struct element **elems;
1214           struct llx **elemp;
1215           int *values;
1216
1217           int *output;
1218           int *next_output;
1219           int j;
1220
1221           allocate_ascending (cnt, &list, &elems, &elemp, &values);
1222           check_list_contents (&list, values, cnt);
1223
1224           output = next_output = xnmalloc (cnt, sizeof *output);
1225           llx_apply (elemp[r0], elemp[r1], apply_func, &next_output);
1226           check_list_contents (&list, values, cnt);
1227           llx_destroy (&list, NULL, NULL, &llx_malloc_mgr);
1228
1229           check (r1 - r0 == next_output - output);
1230           for (j = 0; j < r1 - r0; j++)
1231             check (output[j] == r0 + j);
1232
1233           free_elements (cnt, NULL, elems, elemp, values);
1234           free (output);
1235         }
1236 }
1237
1238 /* Tests llx_destroy. */
1239 static void
1240 test_destroy (void)
1241 {
1242   const int max_elems = 8;
1243
1244   int cnt;
1245
1246   for (cnt = 0; cnt <= max_elems; cnt++)
1247     {
1248       struct llx_list list;
1249       struct element **elems;
1250       struct llx **elemp;
1251       int *values;
1252
1253       int *output;
1254       int *next_output;
1255       int j;
1256
1257       allocate_ascending (cnt, &list, &elems, &elemp, &values);
1258       check_list_contents (&list, values, cnt);
1259
1260       output = next_output = xnmalloc (cnt, sizeof *output);
1261       llx_destroy (&list, apply_func, &next_output, &llx_malloc_mgr);
1262
1263       check (cnt == next_output - output);
1264       for (j = 0; j < cnt; j++)
1265         check (output[j] == j);
1266
1267       free_elements (cnt, NULL, elems, elemp, values);
1268       free (output);
1269     }
1270 }
1271
1272 /* Tests llx_reverse. */
1273 static void
1274 test_reverse (void)
1275 {
1276   const int max_elems = 8;
1277
1278   int cnt, r0, r1;
1279
1280   for (cnt = 0; cnt <= max_elems; cnt++)
1281     for (r0 = 0; r0 <= cnt; r0++)
1282       for (r1 = r0; r1 <= cnt; r1++)
1283         {
1284           struct llx_list list;
1285           struct element **elems;
1286           struct llx **elemp;
1287           int *values;
1288
1289           int i, j;
1290
1291           allocate_ascending (cnt, &list, &elems, &elemp, &values);
1292           check_list_contents (&list, values, cnt);
1293
1294           j = 0;
1295           for (i = 0; i < r0; i++)
1296             values[j++] = i;
1297           for (i = r1 - 1; i >= r0; i--)
1298             values[j++] = i;
1299           for (i = r1; i < cnt; i++)
1300             values[j++] = i;
1301
1302           llx_reverse (elemp[r0], elemp[r1]);
1303           check_list_contents (&list, values, cnt);
1304
1305           free_elements (cnt, &list, elems, elemp, values);
1306         }
1307 }
1308
1309 /* Tests llx_next_permutation and llx_prev_permutation when the
1310    permuted values have no duplicates. */
1311 static void
1312 test_permutations_no_dups (void)
1313 {
1314   const int max_elems = 8;
1315   int cnt;
1316
1317   for (cnt = 0; cnt <= max_elems; cnt++)
1318     {
1319       struct llx_list list;
1320       struct element **elems;
1321       int *values;
1322       int *old_values = xnmalloc (cnt, sizeof *values);
1323       int *new_values = xnmalloc (cnt, sizeof *values);
1324
1325       size_t perm_cnt;
1326
1327       allocate_ascending (cnt, &list, &elems, NULL, &values);
1328
1329       perm_cnt = 1;
1330       extract_values (&list, old_values, cnt);
1331       while (llx_next_permutation (llx_head (&list), llx_null (&list),
1332                                    compare_elements, &aux_data))
1333         {
1334           extract_values (&list, new_values, cnt);
1335           check (lexicographical_compare_3way (new_values, cnt,
1336                                                old_values, cnt,
1337                                                sizeof *new_values,
1338                                                compare_ints, NULL) > 0);
1339           memcpy (old_values, new_values, (cnt) * sizeof *old_values);
1340           perm_cnt++;
1341         }
1342       check (perm_cnt == factorial (cnt));
1343       check_list_contents (&list, values, cnt);
1344
1345       perm_cnt = 1;
1346       llx_reverse (llx_head (&list), llx_null (&list));
1347       extract_values (&list, old_values, cnt);
1348       while (llx_prev_permutation (llx_head (&list), llx_null (&list),
1349                                    compare_elements, &aux_data))
1350         {
1351           extract_values (&list, new_values, cnt);
1352           check (lexicographical_compare_3way (new_values, cnt,
1353                                                old_values, cnt,
1354                                                sizeof *new_values,
1355                                                compare_ints, NULL) < 0);
1356           memcpy (old_values, new_values, (cnt) * sizeof *old_values);
1357           perm_cnt++;
1358         }
1359       check (perm_cnt == factorial (cnt));
1360       llx_reverse (llx_head (&list), llx_null (&list));
1361       check_list_contents (&list, values, cnt);
1362
1363       free_elements (cnt, &list, elems, NULL, values);
1364       free (old_values);
1365       free (new_values);
1366     }
1367 }
1368
1369 /* Tests llx_next_permutation and llx_prev_permutation when the
1370    permuted values contain duplicates. */
1371 static void
1372 test_permutations_with_dups (void)
1373 {
1374   const int max_elems = 8;
1375   const int max_dup = 3;
1376   const int repetitions = 1024;
1377
1378   int cnt, repeat;
1379
1380   for (repeat = 0; repeat < repetitions; repeat++)
1381     for (cnt = 0; cnt < max_elems; cnt++)
1382       {
1383         struct llx_list list;
1384         struct element **elems;
1385         struct llx **elemp;
1386         int *values;
1387         int *old_values = xnmalloc (max_elems, sizeof *values);
1388         int *new_values = xnmalloc (max_elems, sizeof *values);
1389
1390         unsigned int permutation_cnt;
1391         int left = cnt;
1392         int value = 0;
1393
1394         allocate_elements (cnt, &list, &elems, &elemp, &values);
1395
1396         value = 0;
1397         while (left > 0)
1398           {
1399             int max = left < max_dup ? left : max_dup;
1400             int n = rand () % max + 1;
1401             while (n-- > 0)
1402               {
1403                 int idx = cnt - left--;
1404                 values[idx] = elems[idx]->x = value;
1405               }
1406             value++;
1407           }
1408
1409         permutation_cnt = 1;
1410         extract_values (&list, old_values, cnt);
1411         while (llx_next_permutation (llx_head (&list), llx_null (&list),
1412                                      compare_elements, &aux_data))
1413           {
1414             extract_values (&list, new_values, cnt);
1415             check (lexicographical_compare_3way (new_values, cnt,
1416                                                  old_values, cnt,
1417                                                  sizeof *new_values,
1418                                                  compare_ints, NULL) > 0);
1419             memcpy (old_values, new_values, cnt * sizeof *old_values);
1420             permutation_cnt++;
1421           }
1422         check (permutation_cnt == expected_perms (values, cnt));
1423         check_list_contents (&list, values, cnt);
1424
1425         permutation_cnt = 1;
1426         llx_reverse (llx_head (&list), llx_null (&list));
1427         extract_values (&list, old_values, cnt);
1428         while (llx_prev_permutation (llx_head (&list), llx_null (&list),
1429                                      compare_elements, &aux_data))
1430           {
1431             extract_values (&list, new_values, cnt);
1432             check (lexicographical_compare_3way (new_values, cnt,
1433                                                  old_values, cnt,
1434                                                  sizeof *new_values,
1435                                                  compare_ints, NULL) < 0);
1436             permutation_cnt++;
1437           }
1438         llx_reverse (llx_head (&list), llx_null (&list));
1439         check (permutation_cnt == expected_perms (values, cnt));
1440         check_list_contents (&list, values, cnt);
1441
1442         free_elements (cnt, &list, elems, elemp, values);
1443         free (old_values);
1444         free (new_values);
1445       }
1446 }
1447
1448 /* Tests llx_merge when no equal values are to be merged. */
1449 static void
1450 test_merge_no_dups (void)
1451 {
1452   const int max_elems = 8;
1453   const int max_fillxer = 3;
1454
1455   int merge_cnt, pattern, pfx, gap, sfx, order;
1456
1457   for (merge_cnt = 0; merge_cnt < max_elems; merge_cnt++)
1458     for (pattern = 0; pattern <= (1 << merge_cnt); pattern++)
1459       for (pfx = 0; pfx < max_fillxer; pfx++)
1460         for (gap = 0; gap < max_fillxer; gap++)
1461           for (sfx = 0; sfx < max_fillxer; sfx++)
1462             for (order = 0; order < 2; order++)
1463               {
1464                 struct llx_list list;
1465                 struct element **elems;
1466                 struct llx **elemp;
1467                 int *values;
1468
1469                 int list_cnt = pfx + merge_cnt + gap + sfx;
1470                 int a0, a1, b0, b1;
1471                 int i, j;
1472
1473                 allocate_elements (list_cnt, &list,
1474                                    &elems, &elemp, &values);
1475
1476                 j = 0;
1477                 for (i = 0; i < pfx; i++)
1478                   elems[j++]->x = 100 + i;
1479                 a0 = j;
1480                 for (i = 0; i < merge_cnt; i++)
1481                   if (pattern & (1u << i))
1482                     elems[j++]->x = i;
1483                 a1 = j;
1484                 for (i = 0; i < gap; i++)
1485                   elems[j++]->x = 200 + i;
1486                 b0 = j;
1487                 for (i = 0; i < merge_cnt; i++)
1488                   if (!(pattern & (1u << i)))
1489                     elems[j++]->x = i;
1490                 b1 = j;
1491                 for (i = 0; i < sfx; i++)
1492                   elems[j++]->x = 300 + i;
1493                 check (list_cnt == j);
1494
1495                 j = 0;
1496                 for (i = 0; i < pfx; i++)
1497                   values[j++] = 100 + i;
1498                 if (order == 0)
1499                   for (i = 0; i < merge_cnt; i++)
1500                     values[j++] = i;
1501                 for (i = 0; i < gap; i++)
1502                   values[j++] = 200 + i;
1503                 if (order == 1)
1504                   for (i = 0; i < merge_cnt; i++)
1505                     values[j++] = i;
1506                 for (i = 0; i < sfx; i++)
1507                   values[j++] = 300 + i;
1508                 check (list_cnt == j);
1509
1510                 if (order == 0)
1511                   llx_merge (elemp[a0], elemp[a1], elemp[b0], elemp[b1],
1512                              compare_elements, &aux_data);
1513                 else
1514                   llx_merge (elemp[b0], elemp[b1], elemp[a0], elemp[a1],
1515                              compare_elements, &aux_data);
1516
1517                 check_list_contents (&list, values, list_cnt);
1518
1519                 free_elements (list_cnt, &list, elems, elemp, values);
1520               }
1521 }
1522
1523 /* Tests llx_merge when equal values are to be merged. */
1524 static void
1525 test_merge_with_dups (void)
1526 {
1527   const int max_elems = 8;
1528
1529   int cnt, merge_pat, inc_pat, order;
1530
1531   for (cnt = 0; cnt <= max_elems; cnt++)
1532     for (merge_pat = 0; merge_pat <= (1 << cnt); merge_pat++)
1533       for (inc_pat = 0; inc_pat <= (1 << cnt); inc_pat++)
1534         for (order = 0; order < 2; order++)
1535           {
1536             struct llx_list list;
1537             struct element **elems;
1538             struct llx **elemp;
1539             int *values;
1540
1541             int mid;
1542             int i, j, k;
1543
1544             allocate_elements (cnt, &list, &elems, &elemp, &values);
1545
1546             j = 0;
1547             for (i = k = 0; i < cnt; i++)
1548               {
1549                 if (merge_pat & (1u << i))
1550                   elems[j++]->x = k;
1551                 if (inc_pat & (1u << i))
1552                   k++;
1553               }
1554             mid = j;
1555             for (i = k = 0; i < cnt; i++)
1556               {
1557                 if (!(merge_pat & (1u << i)))
1558                   elems[j++]->x = k;
1559                 if (inc_pat & (1u << i))
1560                   k++;
1561               }
1562             check (cnt == j);
1563
1564             if (order == 0)
1565               {
1566                 for (i = 0; i < cnt; i++)
1567                   elems[i]->y = i;
1568               }
1569             else
1570               {
1571                 for (i = 0; i < mid; i++)
1572                   elems[i]->y = 100 + i;
1573                 for (i = mid; i < cnt; i++)
1574                   elems[i]->y = i;
1575               }
1576
1577             j = 0;
1578             for (i = k = 0; i < cnt; i++)
1579               {
1580                 values[j++] = k;
1581                 if (inc_pat & (1u << i))
1582                   k++;
1583               }
1584             check (cnt == j);
1585
1586             if (order == 0)
1587               llx_merge (elemp[0], elemp[mid], elemp[mid], elemp[cnt],
1588                          compare_elements, &aux_data);
1589             else
1590               llx_merge (elemp[mid], elemp[cnt], elemp[0], elemp[mid],
1591                          compare_elements, &aux_data);
1592
1593             check_list_contents (&list, values, cnt);
1594             check (llx_is_sorted (llx_head (&list), llx_null (&list),
1595                                   compare_elements_x_y, &aux_data));
1596
1597             free_elements (cnt, &list, elems, elemp, values);
1598           }
1599 }
1600
1601 /* Tests llx_sort on all permutations up to a maximum number of
1602    elements. */
1603 static void
1604 test_sort_exhaustive (void)
1605 {
1606   const int max_elems = 8;
1607   int cnt;
1608
1609   for (cnt = 0; cnt <= max_elems; cnt++)
1610     {
1611       struct llx_list list;
1612       struct element **elems;
1613       int *values;
1614
1615       struct element **perm_elems;
1616       int *perm_values;
1617
1618       size_t perm_cnt;
1619
1620       allocate_ascending (cnt, &list, &elems, NULL, &values);
1621       allocate_elements (cnt, NULL, &perm_elems, NULL, &perm_values);
1622
1623       perm_cnt = 1;
1624       while (llx_next_permutation (llx_head (&list), llx_null (&list),
1625                                    compare_elements, &aux_data))
1626         {
1627           struct llx_list perm_list;
1628           int j;
1629
1630           extract_values (&list, perm_values, cnt);
1631           llx_init (&perm_list);
1632           for (j = 0; j < cnt; j++)
1633             {
1634               perm_elems[j]->x = perm_values[j];
1635               llx_push_tail (&perm_list, perm_elems[j], &llx_malloc_mgr);
1636             }
1637           llx_sort (llx_head (&perm_list), llx_null (&perm_list),
1638                     compare_elements, &aux_data);
1639           check_list_contents (&perm_list, values, cnt);
1640           check (llx_is_sorted (llx_head (&perm_list), llx_null (&perm_list),
1641                                 compare_elements, &aux_data));
1642           llx_destroy (&perm_list, NULL, NULL, &llx_malloc_mgr);
1643           perm_cnt++;
1644         }
1645       check (perm_cnt == factorial (cnt));
1646
1647       free_elements (cnt, &list, elems, NULL, values);
1648       free_elements (cnt, NULL, perm_elems, NULL, perm_values);
1649     }
1650 }
1651
1652 /* Tests that llx_sort is stable in the presence of equal
1653    values. */
1654 static void
1655 test_sort_stable (void)
1656 {
1657   const int max_elems = 6;
1658   int cnt, inc_pat;
1659
1660   for (cnt = 0; cnt <= max_elems; cnt++)
1661     for (inc_pat = 0; inc_pat <= 1 << cnt; inc_pat++)
1662       {
1663         struct llx_list list;
1664         struct element **elems;
1665         int *values;
1666
1667         struct element **perm_elems;
1668         int *perm_values;
1669
1670         size_t perm_cnt;
1671         int i, j;
1672
1673         allocate_elements (cnt, &list, &elems, NULL, &values);
1674         allocate_elements (cnt, NULL, &perm_elems, NULL, &perm_values);
1675
1676         j = 0;
1677         for (i = 0; i < cnt; i++)
1678           {
1679             elems[i]->x = values[i] = j;
1680             if (inc_pat & (1 << i))
1681               j++;
1682             elems[i]->y = i;
1683           }
1684
1685         perm_cnt = 1;
1686         while (llx_next_permutation (llx_head (&list), llx_null (&list),
1687                                      compare_elements_y, &aux_data))
1688           {
1689             struct llx_list perm_list;
1690
1691             extract_values (&list, perm_values, cnt);
1692             llx_init (&perm_list);
1693             for (i = 0; i < cnt; i++)
1694               {
1695                 perm_elems[i]->x = perm_values[i];
1696                 perm_elems[i]->y = i;
1697                 llx_push_tail (&perm_list, perm_elems[i], &llx_malloc_mgr);
1698               }
1699             llx_sort (llx_head (&perm_list), llx_null (&perm_list),
1700                       compare_elements, &aux_data);
1701             check_list_contents (&perm_list, values, cnt);
1702             check (llx_is_sorted (llx_head (&perm_list), llx_null (&perm_list),
1703                                   compare_elements_x_y, &aux_data));
1704             llx_destroy (&perm_list, NULL, NULL, &llx_malloc_mgr);
1705             perm_cnt++;
1706           }
1707         check (perm_cnt == factorial (cnt));
1708
1709         free_elements (cnt, &list, elems, NULL, values);
1710         free_elements (cnt, NULL, perm_elems, NULL, perm_values);
1711       }
1712 }
1713
1714 /* Tests that llx_sort does not disturb elements outside the
1715    range sorted. */
1716 static void
1717 test_sort_subset (void)
1718 {
1719   const int max_elems = 8;
1720
1721   int cnt, r0, r1, repeat;
1722
1723   for (cnt = 0; cnt <= max_elems; cnt++)
1724     for (repeat = 0; repeat < 100; repeat++)
1725       for (r0 = 0; r0 <= cnt; r0++)
1726         for (r1 = r0; r1 <= cnt; r1++)
1727           {
1728             struct llx_list list;
1729             struct element **elems;
1730             struct llx **elemp;
1731             int *values;
1732
1733             allocate_random (cnt, &list, &elems, &elemp, &values);
1734
1735             qsort (&values[r0], r1 - r0, sizeof *values, compare_ints_noaux);
1736             llx_sort (elemp[r0], elemp[r1], compare_elements, &aux_data);
1737             check_list_contents (&list, values, cnt);
1738
1739             free_elements (cnt, &list, elems, elemp, values);
1740           }
1741 }
1742
1743 /* Tests that llx_sort works with large lists. */
1744 static void
1745 test_sort_big (void)
1746 {
1747   const int max_elems = 1024;
1748
1749   int cnt;
1750
1751   for (cnt = 0; cnt < max_elems; cnt++)
1752     {
1753       struct llx_list list;
1754       struct element **elems;
1755       int *values;
1756
1757       allocate_random (cnt, &list, &elems, NULL, &values);
1758
1759       qsort (values, cnt, sizeof *values, compare_ints_noaux);
1760       llx_sort (llx_head (&list), llx_null (&list), compare_elements, &aux_data);
1761       check_list_contents (&list, values, cnt);
1762
1763       free_elements (cnt, &list, elems, NULL, values);
1764     }
1765 }
1766
1767 /* Tests llx_unique. */
1768 static void
1769 test_unique (void)
1770 {
1771   const int max_elems = 10;
1772
1773   int *ascending = xnmalloc (max_elems, sizeof *ascending);
1774
1775   int cnt, inc_pat, i, j, unique_values;
1776
1777   for (i = 0; i < max_elems; i++)
1778     ascending[i] = i;
1779
1780   for (cnt = 0; cnt < max_elems; cnt++)
1781     for (inc_pat = 0; inc_pat < (1 << cnt); inc_pat++)
1782       {
1783         struct llx_list list, dups;
1784         struct element **elems;
1785         int *values;
1786
1787         allocate_elements (cnt, &list, &elems, NULL, &values);
1788
1789         j = unique_values = 0;
1790         for (i = 0; i < cnt; i++)
1791           {
1792             unique_values = j + 1;
1793             elems[i]->x = values[i] = j;
1794             if (inc_pat & (1 << i))
1795               j++;
1796           }
1797         check_list_contents (&list, values, cnt);
1798
1799         llx_init (&dups);
1800         check (llx_unique (llx_head (&list), llx_null (&list),
1801                            llx_null (&dups),
1802                            compare_elements, &aux_data,
1803                            &llx_malloc_mgr)
1804                == (size_t) unique_values);
1805         check_list_contents (&list, ascending, unique_values);
1806
1807         llx_splice (llx_null (&list), llx_head (&dups), llx_null (&dups));
1808         llx_sort (llx_head (&list), llx_null (&list), compare_elements, &aux_data);
1809         check_list_contents (&list, values, cnt);
1810
1811         llx_destroy (&dups, NULL, NULL, &llx_malloc_mgr);
1812         free_elements (cnt, &list, elems, NULL, values);
1813       }
1814
1815   free (ascending);
1816 }
1817
1818 /* Tests llx_sort_unique. */
1819 static void
1820 test_sort_unique (void)
1821 {
1822   const int max_elems = 7;
1823   int cnt, inc_pat;
1824
1825   for (cnt = 0; cnt <= max_elems; cnt++)
1826     for (inc_pat = 0; inc_pat <= 1 << cnt; inc_pat++)
1827       {
1828         struct llx_list list;
1829         struct element **elems;
1830         int *values;
1831
1832         struct element **perm_elems;
1833         int *perm_values;
1834
1835         int unique_cnt;
1836         int *unique_values;
1837
1838         size_t perm_cnt;
1839         int i, j;
1840
1841         allocate_elements (cnt, &list, &elems, NULL, &values);
1842         allocate_elements (cnt, NULL, &perm_elems, NULL, &perm_values);
1843
1844         j = unique_cnt = 0;
1845         for (i = 0; i < cnt; i++)
1846           {
1847             elems[i]->x = values[i] = j;
1848             unique_cnt = j + 1;
1849             if (inc_pat & (1 << i))
1850               j++;
1851           }
1852
1853         unique_values = xnmalloc (unique_cnt, sizeof *unique_values);
1854         for (i = 0; i < unique_cnt; i++)
1855           unique_values[i] = i;
1856
1857         perm_cnt = 1;
1858         while (llx_next_permutation (llx_head (&list), llx_null (&list),
1859                                      compare_elements, &aux_data))
1860           {
1861             struct llx_list perm_list;
1862
1863             extract_values (&list, perm_values, cnt);
1864             llx_init (&perm_list);
1865             for (i = 0; i < cnt; i++)
1866               {
1867                 perm_elems[i]->x = perm_values[i];
1868                 perm_elems[i]->y = i;
1869                 llx_push_tail (&perm_list, perm_elems[i], &llx_malloc_mgr);
1870               }
1871             llx_sort_unique (llx_head (&perm_list), llx_null (&perm_list), NULL,
1872                              compare_elements, &aux_data,
1873                              &llx_malloc_mgr);
1874             check_list_contents (&perm_list, unique_values, unique_cnt);
1875             check (llx_is_sorted (llx_head (&perm_list), llx_null (&perm_list),
1876                                   compare_elements_x_y, &aux_data));
1877             llx_destroy (&perm_list, NULL, NULL, &llx_malloc_mgr);
1878             perm_cnt++;
1879           }
1880         check (perm_cnt == expected_perms (values, cnt));
1881
1882         free_elements (cnt, &list, elems, NULL, values);
1883         free_elements (cnt, NULL, perm_elems, NULL, perm_values);
1884         free (unique_values);
1885       }
1886 }
1887
1888 /* Tests llx_insert_ordered. */
1889 static void
1890 test_insert_ordered (void)
1891 {
1892   const int max_elems = 6;
1893   int cnt, inc_pat;
1894
1895   for (cnt = 0; cnt <= max_elems; cnt++)
1896     for (inc_pat = 0; inc_pat <= 1 << cnt; inc_pat++)
1897       {
1898         struct llx_list list;
1899         struct element **elems;
1900         int *values;
1901
1902         struct element **perm_elems;
1903         int *perm_values;
1904
1905         size_t perm_cnt;
1906         int i, j;
1907
1908         allocate_elements (cnt, &list, &elems, NULL, &values);
1909         allocate_elements (cnt, NULL, &perm_elems, NULL, &perm_values);
1910
1911         j = 0;
1912         for (i = 0; i < cnt; i++)
1913           {
1914             elems[i]->x = values[i] = j;
1915             if (inc_pat & (1 << i))
1916               j++;
1917             elems[i]->y = i;
1918           }
1919
1920         perm_cnt = 1;
1921         while (llx_next_permutation (llx_head (&list), llx_null (&list),
1922                                      compare_elements_y, &aux_data))
1923           {
1924             struct llx_list perm_list;
1925
1926             extract_values (&list, perm_values, cnt);
1927             llx_init (&perm_list);
1928             for (i = 0; i < cnt; i++)
1929               {
1930                 perm_elems[i]->x = perm_values[i];
1931                 perm_elems[i]->y = i;
1932                 llx_insert_ordered (llx_head (&perm_list),
1933                                     llx_null (&perm_list),
1934                                     perm_elems[i],
1935                                     compare_elements, &aux_data,
1936                                     &llx_malloc_mgr);
1937               }
1938             check (llx_is_sorted (llx_head (&perm_list), llx_null (&perm_list),
1939                                   compare_elements_x_y, &aux_data));
1940             llx_destroy (&perm_list, NULL, NULL, &llx_malloc_mgr);
1941             perm_cnt++;
1942           }
1943         check (perm_cnt == factorial (cnt));
1944
1945         free_elements (cnt, &list, elems, NULL, values);
1946         free_elements (cnt, NULL, perm_elems, NULL, perm_values);
1947       }
1948 }
1949
1950 /* Tests llx_partition. */
1951 static void
1952 test_partition (void)
1953 {
1954   const int max_elems = 10;
1955
1956   int cnt;
1957   unsigned int pbase;
1958   int r0, r1;
1959
1960   for (cnt = 0; cnt < max_elems; cnt++)
1961     for (r0 = 0; r0 <= cnt; r0++)
1962       for (r1 = r0; r1 <= cnt; r1++)
1963         for (pbase = 0; pbase <= (1u << (r1 - r0)); pbase++)
1964           {
1965             struct llx_list list;
1966             struct element **elems;
1967             struct llx **elemp;
1968             int *values;
1969
1970             unsigned int pattern = pbase << r0;
1971             int i, j;
1972             int first_false;
1973             struct llx *part_llx;
1974
1975             allocate_ascending (cnt, &list, &elems, &elemp, &values);
1976
1977             /* Check that llx_find_partition works okay in every
1978                case.  We use it after partitioning, too, but that
1979                only tests cases where it returns non-null. */
1980             for (i = r0; i < r1; i++)
1981               if (!(pattern & (1u << i)))
1982                 break;
1983             j = i;
1984             for (; i < r1; i++)
1985               if (pattern & (1u << i))
1986                 break;
1987             part_llx = llx_find_partition (elemp[r0], elemp[r1],
1988                                            pattern_pred,
1989                                            &pattern);
1990             if (i == r1)
1991               check (part_llx == elemp[j]);
1992             else
1993               check (part_llx == NULL);
1994
1995             /* Figure out expected results. */
1996             j = 0;
1997             first_false = -1;
1998             for (i = 0; i < r0; i++)
1999               values[j++] = i;
2000             for (i = r0; i < r1; i++)
2001               if (pattern & (1u << i))
2002                 values[j++] = i;
2003             for (i = r0; i < r1; i++)
2004               if (!(pattern & (1u << i)))
2005                 {
2006                   if (first_false == -1)
2007                     first_false = i;
2008                   values[j++] = i;
2009                 }
2010             if (first_false == -1)
2011               first_false = r1;
2012             for (i = r1; i < cnt; i++)
2013               values[j++] = i;
2014             check (j == cnt);
2015
2016             /* Partition and check for expected results. */
2017             check (llx_partition (elemp[r0], elemp[r1],
2018                                   pattern_pred, &pattern)
2019                    == elemp[first_false]);
2020             check (llx_find_partition (elemp[r0], elemp[r1],
2021                                        pattern_pred, &pattern)
2022                    == elemp[first_false]);
2023             check_list_contents (&list, values, cnt);
2024             check ((int) llx_count (&list) == cnt);
2025
2026             free_elements (cnt, &list, elems, elemp, values);
2027           }
2028 }
2029
2030 /* Tests that allocation failure is gracefully handled. */
2031 static void
2032 test_allocation_failure (void)
2033 {
2034   struct llx_list list;
2035
2036   llx_init (&list);
2037   check (llx_push_head (&list, NULL, &llx_null_mgr) == NULL);
2038   check (llx_push_tail (&list, NULL, &llx_null_mgr) == NULL);
2039   check (llx_insert (llx_null (&list), NULL, &llx_null_mgr) == NULL);
2040   check_list_contents (&list, NULL, 0);
2041 }
2042 \f
2043 /* Main program. */
2044
2045 struct test
2046   {
2047     const char *name;
2048     const char *description;
2049     void (*function) (void);
2050   };
2051
2052 static const struct test tests[] =
2053   {
2054     {
2055       "push-pop",
2056       "push/pop",
2057       test_push_pop
2058     },
2059     {
2060       "insert-remove",
2061       "insert/remove",
2062       test_insert_remove
2063     },
2064     {
2065       "swap",
2066       "swap",
2067       test_swap
2068     },
2069     {
2070       "swap-range",
2071       "swap_range",
2072       test_swap_range
2073     },
2074     {
2075       "remove-range",
2076       "remove_range",
2077       test_remove_range
2078     },
2079     {
2080       "remove-equal",
2081       "remove_equal",
2082       test_remove_equal
2083     },
2084     {
2085       "remove-if",
2086       "remove_if",
2087       test_remove_if
2088     },
2089     {
2090       "find-equal",
2091       "find_equal",
2092       test_find_equal
2093     },
2094     {
2095       "find",
2096       "find",
2097       test_find
2098     },
2099     {
2100       "find-if",
2101       "find_if",
2102       test_find_if
2103     },
2104     {
2105       "find-adjacent-equal",
2106       "find_adjacent_equal",
2107       test_find_adjacent_equal
2108     },
2109     {
2110       "count-range",
2111       "count_range",
2112       test_count_range
2113     },
2114     {
2115       "count-equal",
2116       "count_equal",
2117       test_count_equal
2118     },
2119     {
2120       "count-if",
2121       "count_if",
2122       test_count_if
2123     },
2124     {
2125       "min-max",
2126       "min/max",
2127       test_min_max
2128     },
2129     {
2130       "lexicographical-compare-3way",
2131       "lexicographical_compare_3way",
2132       test_lexicographical_compare_3way
2133     },
2134     {
2135       "apply",
2136       "apply",
2137       test_apply
2138     },
2139     {
2140       "destroy",
2141       "destroy",
2142       test_destroy
2143     },
2144     {
2145       "reverse",
2146       "reverse",
2147       test_reverse
2148     },
2149     {
2150       "permutations-no-dups",
2151       "permutations (no dups)",
2152       test_permutations_no_dups
2153     },
2154     {
2155       "permutations-with-dups",
2156       "permutations (with dups)",
2157       test_permutations_with_dups
2158     },
2159     {
2160       "merge-no-dups",
2161       "merge (no dups)",
2162       test_merge_no_dups
2163     },
2164     {
2165       "merge-with-dups",
2166       "merge (with dups)",
2167       test_merge_with_dups
2168     },
2169     {
2170       "sort-exhaustive",
2171       "sort (exhaustive)",
2172       test_sort_exhaustive
2173     },
2174     {
2175       "sort-stable",
2176       "sort (stability)",
2177       test_sort_stable
2178     },
2179     {
2180       "sort-subset",
2181       "sort (subset)",
2182       test_sort_subset
2183     },
2184     {
2185       "sort-big",
2186       "sort (big)",
2187       test_sort_big
2188     },
2189     {
2190       "unique",
2191       "unique",
2192       test_unique
2193     },
2194     {
2195       "sort-unique",
2196       "sort_unique",
2197       test_sort_unique
2198     },
2199     {
2200       "insert-ordered",
2201       "insert_ordered",
2202       test_insert_ordered
2203     },
2204     {
2205       "partition",
2206       "partition",
2207       test_partition
2208     },
2209     {
2210       "allocation-failure",
2211       "allocation failure",
2212       test_allocation_failure
2213     },
2214   };
2215
2216 enum { N_TESTS = sizeof tests / sizeof *tests };
2217
2218 int
2219 main (int argc, char *argv[])
2220 {
2221   int i;
2222
2223   if (argc != 2)
2224     {
2225       fprintf (stderr, "exactly one argument required; use --help for help\n");
2226       return EXIT_FAILURE;
2227     }
2228   else if (!strcmp (argv[1], "--help"))
2229     {
2230       printf ("%s: test doubly linked list of pointers (llx) library\n"
2231               "usage: %s TEST-NAME\n"
2232               "where TEST-NAME is one of the following:\n",
2233               argv[0], argv[0]);
2234       for (i = 0; i < N_TESTS; i++)
2235         printf ("  %s\n    %s\n", tests[i].name, tests[i].description);
2236       return 0;
2237     }
2238   else
2239     {
2240       for (i = 0; i < N_TESTS; i++)
2241         if (!strcmp (argv[1], tests[i].name))
2242           {
2243             tests[i].function ();
2244             return 0;
2245           }
2246
2247       fprintf (stderr, "unknown test %s; use --help for help\n", argv[1]);
2248       return EXIT_FAILURE;
2249     }
2250 }