File: ll-test.c

package info (click to toggle)
pspp 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,676 kB
  • sloc: ansic: 267,210; xml: 18,446; sh: 5,534; python: 2,881; makefile: 125; perl: 64
file content (2163 lines) | stat: -rw-r--r-- 57,467 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
/* PSPP - a program for statistical analysis.
   Copyright (C) 2006, 2010 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

/* This is a test program for the ll_* routines defined in
   ll.c.  This test program aims to be as comprehensive as
   possible.  "gcov -b" should report 100% coverage of lines and
   branches in the ll_* routines.  "valgrind --leak-check=yes
   --show-reachable=yes" should give a clean report.

   This test program depends only on ll.c and the standard C
   library.

   See llx-test.c for a similar program for the llx_*
   routines. */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libpspp/ll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Support preliminaries. */
#if __GNUC__ >= 2 && !defined UNUSED
#define UNUSED __attribute__ ((unused))
#else
#define UNUSED
#endif

/* Exit with a failure code.
   (Place a breakpoint on this function while debugging.) */
static void
check_die (void)
{
  exit (EXIT_FAILURE);
}

/* If OK is not true, prints a message about failure on the
   current source file and the given LINE and terminates. */
static void
check_func (bool ok, int line)
{
  if (!ok)
    {
      fprintf (stderr, "%s:%d: check failed\n", __FILE__, line);
      check_die ();
    }
}

/* Verifies that EXPR evaluates to true.
   If not, prints a message citing the calling line number and
   terminates. */
#define check(EXPR) check_func ((EXPR), __LINE__)

/* Prints a message about memory exhaustion and exits with a
   failure code. */
static void
xalloc_die (void)
{
  printf ("virtual memory exhausted\n");
  exit (EXIT_FAILURE);
}

/* Allocates and returns N bytes of memory. */
static void *
xmalloc (size_t n)
{
  if (n != 0)
    {
      void *p = malloc (n);
      if (p == NULL)
        xalloc_die ();

      return p;
    }
  else
    return NULL;
}

/* Allocates and returns N * M bytes of memory. */
static void *
xnmalloc (size_t n, size_t m)
{
  if ((size_t) -1 / m <= n)
    xalloc_die ();
  return xmalloc (n * m);
}

/* List type and support routines. */

/* Test data element. */
struct element
  {
    struct ll ll;               /* Embedded list element. */
    int x;                      /* Primary value. */
    int y;                      /* Secondary value. */
  };

static int aux_data;

/* Returns the `struct element' that LL is embedded within. */
static struct element *
ll_to_element (const struct ll *ll)
{
  return ll_data (ll, struct element, ll);
}

/* Prints the elements in LIST. */
static void UNUSED
print_list (struct ll_list *list)
{
  struct ll *x;

  printf ("list:");
  for (x = ll_head (list); x != ll_null (list); x = ll_next (x))
    {
      struct element *e = ll_to_element (x);
      printf (" %d", e->x);
    }
  printf ("\n");
}

/* Prints the value returned by PREDICATE given auxiliary data
   AUX for each element in LIST. */
static void UNUSED
print_pred (struct ll_list *list,
            ll_predicate_func *predicate, void *aux UNUSED)
{
  struct ll *x;

  printf ("pred:");
  for (x = ll_head (list); x != ll_null (list); x = ll_next (x))
    printf (" %d", predicate (x, aux));
  printf ("\n");
}

/* Prints the N numbers in VALUES. */
static void UNUSED
print_array (int values[], size_t n)
{
  size_t i;

  printf ("arry:");
  for (i = 0; i < n; i++)
    printf (" %d", values[i]);
  printf ("\n");
}

/* Compares the `x' values in A and B and returns a strcmp-type
   return value.  Verifies that AUX points to aux_data. */
static int
compare_elements (const struct ll *a_, const struct ll *b_, void *aux)
{
  const struct element *a = ll_to_element (a_);
  const struct element *b = ll_to_element (b_);

  check (aux == &aux_data);
  return a->x < b->x ? -1 : a->x > b->x;
}

/* Compares the `x' and `y' values in A and B and returns a
   strcmp-type return value.  Verifies that AUX points to
   aux_data. */
static int
compare_elements_x_y (const struct ll *a_, const struct ll *b_, void *aux)
{
  const struct element *a = ll_to_element (a_);
  const struct element *b = ll_to_element (b_);

  check (aux == &aux_data);
  if (a->x != b->x)
    return a->x < b->x ? -1 : 1;
  else if (a->y != b->y)
    return a->y < b->y ? -1 : 1;
  else
    return 0;
}

/* Compares the `y' values in A and B and returns a strcmp-type
   return value.  Verifies that AUX points to aux_data. */
static int
compare_elements_y (const struct ll *a_, const struct ll *b_, void *aux)
{
  const struct element *a = ll_to_element (a_);
  const struct element *b = ll_to_element (b_);

  check (aux == &aux_data);
  return a->y < b->y ? -1 : a->y > b->y;
}

/* Returns true if the bit in *PATTERN indicated by `x in
   *ELEMENT is set, false otherwise. */
static bool
pattern_pred (const struct ll *element_, void *pattern_)
{
  const struct element *element = ll_to_element (element_);
  unsigned int *pattern = pattern_;

  return (*pattern & (1u << element->x)) != 0;
}

/* Allocates N elements in *ELEMS.
   Adds the elements to LIST, if it is nonnull.
   Puts pointers to the elements' list elements in *ELEMP,
   followed by a pointer to the list null element, if ELEMP is
   nonnull.
   Allocates space for N values in *VALUES, if VALUES is
   nonnull. */
static void
allocate_elements (size_t n,
                   struct ll_list *list,
                   struct element ***elems,
                   struct ll ***elemp,
                   int **values)
{
  size_t i;

  if (list != NULL)
    ll_init (list);

  *elems = xnmalloc (n, sizeof **elems);
  for (i = 0; i < n; i++)
    {
      (*elems)[i] = xmalloc (sizeof ***elems);
      if (list != NULL)
        ll_push_tail (list, &(*elems)[i]->ll);
    }

  if (elemp != NULL)
    {
      *elemp = xnmalloc (n + 1, sizeof *elemp);
      for (i = 0; i < n; i++)
        (*elemp)[i] = &(*elems)[i]->ll;
      (*elemp)[n] = ll_null (list);
    }

  if (values != NULL)
    *values = xnmalloc (n, sizeof *values);
}

/* Copies the N values of `x' from LIST into VALUES[]. */
static void
extract_values (struct ll_list *list, int values[], size_t n)
{
  struct ll *x;

  check (ll_count (list) == n);
  for (x = ll_head (list); x != ll_null (list); x = ll_next (x))
    {
      struct element *e = ll_to_element (x);
      *values++ = e->x;
    }
}

/* As allocate_elements, but sets ascending values, starting
   from 0, in `x' values in *ELEMS and in *VALUES (if
   nonnull). */
static void
allocate_ascending (size_t n,
                    struct ll_list *list,
                    struct element ***elems,
                    struct ll ***elemp,
                    int **values)
{
  size_t i;

  allocate_elements (n, list, elems, elemp, values);

  for (i = 0; i < n; i++)
    (*elems)[i]->x = i;
  if (values != NULL)
    extract_values (list, *values, n);
}

/* As allocate_elements, but sets binary values extracted from
   successive bits in PATTERN in `x' values in *ELEMS and in
   *VALUES (if nonnull). */
static void
allocate_pattern (size_t n,
                  int pattern,
                  struct ll_list *list,
                  struct element ***elems,
                  struct ll ***elemp,
                  int **values)
{
  size_t i;

  allocate_elements (n, list, elems, elemp, values);

  for (i = 0; i < n; i++)
    (*elems)[i]->x = (pattern & (1 << i)) != 0;
  if (values != NULL)
    extract_values (list, *values, n);
}

/* Randomly shuffles the N elements in ARRAY, each of which is
   SIZE bytes in size. */
static void
random_shuffle (void *array_, size_t n, size_t size)
{
  char *array = array_;
  char *tmp = xmalloc (size);
  size_t i;

  for (i = 0; i < n; i++)
    {
      size_t j = rand () % (n - i) + i;
      if (i != j)
        {
          memcpy (tmp, array + j * size, size);
          memcpy (array + j * size, array + i * size, size);
          memcpy (array + i * size, tmp, size);
        }
    }

  free (tmp);
}

/* As allocate_ascending, but orders the values randomly. */
static void
allocate_random (size_t n,
                 struct ll_list *list,
                 struct element ***elems,
                 struct ll ***elemp,
                 int **values)
{
  size_t i;

  allocate_elements (n, list, elems, elemp, values);

  for (i = 0; i < n; i++)
    (*elems)[i]->x = i;
  random_shuffle (*elems, n, sizeof **elems);
  if (values != NULL)
    extract_values (list, *values, n);
}

/* Frees the N elements of ELEMS, ELEMP, and VALUES. */
static void
free_elements (size_t n,
               struct element **elems,
               struct ll **elemp,
               int *values)
{
  size_t i;

  for (i = 0; i < n; i++)
    free (elems[i]);
  free (elems);
  free (elemp);
  free (values);
}

/* Compares A and B and returns a strcmp-type return value. */
static int
compare_ints (const void *a_, const void *b_, void *aux UNUSED)
{
  const int *a = a_;
  const int *b = b_;

  return *a < *b ? -1 : *a > *b;
}

/* Compares A and B and returns a strcmp-type return value. */
static int
compare_ints_noaux (const void *a_, const void *b_)
{
  const int *a = a_;
  const int *b = b_;

  return *a < *b ? -1 : *a > *b;
}

/* Checks that LIST contains the N values in ELEMENTS. */
static void
check_list_contents (struct ll_list *list, int elements[], size_t n)
{
  struct ll *ll;
  size_t i;

  check ((n == 0) == ll_is_empty (list));

  /* Iterate in forward order. */
  for (ll = ll_head (list), i = 0; i < n; ll = ll_next (ll), i++)
    {
      struct element *e = ll_to_element (ll);
      check (elements[i] == e->x);
      check (ll != ll_null (list));
    }
  check (ll == ll_null (list));

  /* Iterate in reverse order. */
  for (ll = ll_tail (list), i = 0; i < n; ll = ll_prev (ll), i++)
    {
      struct element *e = ll_to_element (ll);
      check (elements[n - i - 1] == e->x);
      check (ll != ll_null (list));
    }
  check (ll == ll_null (list));

  check (ll_count (list) == n);
}

/* Lexicographically compares ARRAY1, which contains COUNT1
   elements of SIZE bytes each, to ARRAY2, which contains COUNT2
   elements of SIZE bytes, according to COMPARE.  Returns a
   strcmp-type result.  AUX is passed to COMPARE as auxiliary
   data. */
static int
lexicographical_compare_3way (const void *array1, size_t count1,
                              const void *array2, size_t count2,
                              size_t size,
                              int (*compare) (const void *, const void *,
                                              void *aux),
                              void *aux)
{
  const char *first1 = array1;
  const char *first2 = array2;
  size_t min_count = count1 < count2 ? count1 : count2;

  while (min_count > 0)
    {
      int cmp = compare (first1, first2, aux);
      if (cmp != 0)
        return cmp;

      first1 += size;
      first2 += size;
      min_count--;
    }

  return count1 < count2 ? -1 : count1 > count2;
}

/* Tests. */

/* Tests list push and pop operations. */
static void
test_push_pop (void)
{
  const int max_elems = 1024;

  struct ll_list list;
  struct element **elems;
  int *values;

  int i;

  allocate_elements (max_elems, NULL, &elems, NULL, &values);

  /* Push on tail. */
  ll_init (&list);
  check_list_contents (&list, NULL, 0);
  for (i = 0; i < max_elems; i++)
    {
      values[i] = elems[i]->x = i;
      ll_push_tail (&list, &elems[i]->ll);
      check_list_contents (&list, values, i + 1);
    }

  /* Remove from tail. */
  for (i = 0; i < max_elems; i++)
    {
      struct element *e = ll_to_element (ll_pop_tail (&list));
      check (e->x == max_elems - i - 1);
      check_list_contents (&list, values, max_elems - i - 1);
    }

  /* Push at start. */
  check_list_contents (&list, NULL, 0);
  for (i = 0; i < max_elems; i++)
    {
      values[max_elems - i - 1] = elems[i]->x = max_elems - i - 1;
      ll_push_head (&list, &elems[i]->ll);
      check_list_contents (&list, &values[max_elems - i - 1], i + 1);
    }

  /* Remove from start. */
  for (i = 0; i < max_elems; i++)
    {
      struct element *e = ll_to_element (ll_pop_head (&list));
      check (e->x == (int) i);
      check_list_contents (&list, &values[i + 1], max_elems - i - 1);
    }

  free_elements (max_elems, elems, NULL, values);
}

/* Tests insertion and removal at arbitrary positions. */
static void
test_insert_remove (void)
{
  const int max_elems = 16;
  int n;

  for (n = 0; n < max_elems; n++)
    {
      struct element **elems;
      struct ll **elemp;
      int *values = xnmalloc (n + 1, sizeof *values);

      struct ll_list list;
      struct element extra;
      int pos;

      allocate_ascending (n, &list, &elems, &elemp, NULL);
      extra.x = -1;
      for (pos = 0; pos <= n; pos++)
        {
          int i, j;

          ll_insert (elemp[pos], &extra.ll);

          j = 0;
          for (i = 0; i < pos; i++)
            values[j++] = i;
          values[j++] = -1;
          for (; i < n; i++)
            values[j++] = i;
          check_list_contents (&list, values, n + 1);

          ll_remove (&extra.ll);
        }
      check_list_contents (&list, values, n);

      free_elements (n, elems, elemp, values);
    }
}

/* Tests swapping individual elements. */
static void
test_swap (void)
{
  const int max_elems = 8;
  int n;

  for (n = 0; n <= max_elems; n++)
    {
      struct ll_list list;
      struct element **elems;
      int *values;

      int i, j, k;

      allocate_ascending (n, &list, &elems, NULL, &values);
      check_list_contents (&list, values, n);

      for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
          for (k = 0; k < 2; k++)
            {
              int t;

              ll_swap (&elems[i]->ll, &elems[j]->ll);
              t = values[i];
              values[i] = values[j];
              values[j] = t;
              check_list_contents (&list, values, n);
            }

      free_elements (n, elems, NULL, values);
    }
}

/* Tests swapping ranges of list elements. */
static void
test_swap_range (void)
{
  const int max_elems = 8;
  int n, a0, a1, b0, b1, r;

  for (n = 0; n <= max_elems; n++)
    for (a0 = 0; a0 <= n; a0++)
      for (a1 = a0; a1 <= n; a1++)
        for (b0 = a1; b0 <= n; b0++)
          for (b1 = b0; b1 <= n; b1++)
            for (r = 0; r < 2; r++)
              {
                struct ll_list list;
                struct element **elems;
                struct ll **elemp;
                int *values;

                int i, j;

                allocate_ascending (n, &list, &elems, &elemp, &values);
                check_list_contents (&list, values, n);

                j = 0;
                for (i = 0; i < a0; i++)
                  values[j++] = i;
                for (i = b0; i < b1; i++)
                  values[j++] = i;
                for (i = a1; i < b0; i++)
                  values[j++] = i;
                for (i = a0; i < a1; i++)
                  values[j++] = i;
                for (i = b1; i < n; i++)
                  values[j++] = i;
                check (j == n);

                if (r == 0)
                  ll_swap_range (elemp[a0], elemp[a1], elemp[b0], elemp[b1]);
                else
                  ll_swap_range (elemp[b0], elemp[b1], elemp[a0], elemp[a1]);
                check_list_contents (&list, values, n);

                free_elements (n, elems, elemp, values);
              }
}

/* Tests removing ranges of list elements. */
static void
test_remove_range (void)
{
  const int max_elems = 8;

  int n, r0, r1;

  for (n = 0; n <= max_elems; n++)
    for (r0 = 0; r0 <= n; r0++)
      for (r1 = r0; r1 <= n; r1++)
        {
          struct ll_list list;
          struct element **elems;
          struct ll **elemp;
          int *values;

          int i, j;

          allocate_ascending (n, &list, &elems, &elemp, &values);
          check_list_contents (&list, values, n);

          j = 0;
          for (i = 0; i < r0; i++)
            values[j++] = i;
          for (i = r1; i < n; i++)
            values[j++] = i;

          ll_remove_range (elemp[r0], elemp[r1]);
          check_list_contents (&list, values, j);

          free_elements (n, elems, elemp, values);
        }
}

/* Tests ll_remove_equal. */
static void
test_remove_equal (void)
{
  const int max_elems = 8;

  int n, r0, r1, eq_pat;

  for (n = 0; n <= max_elems; n++)
    for (r0 = 0; r0 <= n; r0++)
      for (r1 = r0; r1 <= n; r1++)
        for (eq_pat = 0; eq_pat <= 1 << n; eq_pat++)
          {
            struct ll_list list;
            struct element **elems;
            struct ll **elemp;
            int *values;

            struct element to_remove;
            int remaining;
            int i;

            allocate_elements (n, &list, &elems, &elemp, &values);

            remaining = 0;
            for (i = 0; i < n; i++)
              {
                int x = eq_pat & (1 << i) ? -1 : i;
                bool delete = x == -1 && r0 <= i && i < r1;
                elems[i]->x = x;
                if (!delete)
                  values[remaining++] = x;
              }

            to_remove.x = -1;
            check ((int) ll_remove_equal (elemp[r0], elemp[r1], &to_remove.ll,
                                          compare_elements, &aux_data)
                   == n - remaining);
            check_list_contents (&list, values, remaining);

            free_elements (n, elems, elemp, values);
          }
}

/* Tests ll_remove_if. */
static void
test_remove_if (void)
{
  const int max_elems = 8;

  int n, r0, r1, pattern;

  for (n = 0; n <= max_elems; n++)
    for (r0 = 0; r0 <= n; r0++)
      for (r1 = r0; r1 <= n; r1++)
        for (pattern = 0; pattern <= 1 << n; pattern++)
          {
            struct ll_list list;
            struct element **elems;
            struct ll **elemp;
            int *values;

            int remaining;
            int i;

            allocate_elements (n, &list, &elems, &elemp, &values);

            remaining = 0;
            for (i = 0; i < n; i++)
              {
                bool delete = (pattern & (1 << i)) && r0 <= i && i < r1;
                elems[i]->x = i;
                if (!delete)
                  values[remaining++] = i;
              }

            check ((int) ll_remove_if (elemp[r0], elemp[r1],
                                       pattern_pred, &pattern)
                   == n - remaining);
            check_list_contents (&list, values, remaining);

            free_elements (n, elems, elemp, values);
          }
}

/* Tests ll_moved. */
static void
test_moved (void)
{
  const int max_elems = 8;

  int n;

  for (n = 0; n <= max_elems; n++)
    {
      struct ll_list list;
      struct element **elems;
      struct element **new_elems;
      int *values;

      int i;

      allocate_ascending (n, &list, &elems, NULL, &values);
      allocate_elements (n, NULL, &new_elems, NULL, NULL);
      check_list_contents (&list, values, n);

      for (i = 0; i < n; i++)
        {
          *new_elems[i] = *elems[i];
          ll_moved (&new_elems[i]->ll);
          check_list_contents (&list, values, n);
        }

      free_elements (n, elems, NULL, values);
      free_elements (n, new_elems, NULL, NULL);
    }
}

/* Tests, via HELPER, a function that looks at list elements
   equal to some specified element. */
static void
test_examine_equal_range (void (*helper) (int r0, int r1, int eq_pat,
                                          struct ll *to_find,
                                          struct ll **elemp))
{
  const int max_elems = 8;

  int n, r0, r1, eq_pat;

  for (n = 0; n <= max_elems; n++)
    for (eq_pat = 0; eq_pat <= 1 << n; eq_pat++)
      {
        struct ll_list list;
        struct element **elems;
        struct ll **elemp;
        int *values;

        struct element to_find;

        int i;

        allocate_ascending (n, &list, &elems, &elemp, &values);

        for (i = 0; i < n; i++)
          if (eq_pat & (1 << i))
            values[i] = elems[i]->x = -1;

        to_find.x = -1;
        for (r0 = 0; r0 <= n; r0++)
          for (r1 = r0; r1 <= n; r1++)
            helper (r0, r1, eq_pat, &to_find.ll, elemp);

        check_list_contents (&list, values, n);

        free_elements (n, elems, elemp, values);
      }
}

/* Tests, via HELPER, a function that looks at list elements for
   which a given predicate returns true. */
static void
test_examine_if_range (void (*helper) (int r0, int r1, int eq_pat,
                                       struct ll **elemp))
{
  const int max_elems = 8;

  int n, r0, r1, eq_pat;

  for (n = 0; n <= max_elems; n++)
    for (eq_pat = 0; eq_pat <= 1 << n; eq_pat++)
      {
        struct ll_list list;
        struct element **elems;
        struct ll **elemp;
        int *values;

        allocate_ascending (n, &list, &elems, &elemp, &values);

        for (r0 = 0; r0 <= n; r0++)
          for (r1 = r0; r1 <= n; r1++)
            helper (r0, r1, eq_pat, elemp);

        check_list_contents (&list, values, n);

        free_elements (n, elems, elemp, values);
      }
}

/* Helper function for testing ll_find_equal. */
static void
test_find_equal_helper (int r0, int r1, int eq_pat,
                        struct ll *to_find, struct ll **elemp)
{
  struct ll *match;
  int i;

  match = ll_find_equal (elemp[r0], elemp[r1], to_find,
                         compare_elements, &aux_data);
  for (i = r0; i < r1; i++)
    if (eq_pat & (1 << i))
      break;

  check (match == elemp[i]);
}

/* Tests ll_find_equal. */
static void
test_find_equal (void)
{
  test_examine_equal_range (test_find_equal_helper);
}

/* Helper function for testing ll_find_if. */
static void
test_find_if_helper (int r0, int r1, int eq_pat, struct ll **elemp)
{
  struct ll *match = ll_find_if (elemp[r0], elemp[r1],
                                 pattern_pred, &eq_pat);
  int i;

  for (i = r0; i < r1; i++)
    if (eq_pat & (1 << i))
      break;

  check (match == elemp[i]);
}

/* Tests ll_find_if. */
static void
test_find_if (void)
{
  test_examine_if_range (test_find_if_helper);
}

/* Tests ll_find_adjacent_equal. */
static void
test_find_adjacent_equal (void)
{
  const int max_elems = 8;

  int n, eq_pat;

  for (n = 0; n <= max_elems; n++)
    for (eq_pat = 0; eq_pat <= 1 << n; eq_pat++)
      {
        struct ll_list list;
        struct element **elems;
        struct ll **elemp;
        int *values;
        int match;

        int i;

        allocate_ascending (n, &list, &elems, &elemp, &values);

        match = -1;
        for (i = 0; i < n - 1; i++)
          {
            elems[i]->y = i;
            if (eq_pat & (1 << i))
              {
                values[i] = elems[i]->x = match;
                values[i + 1] = elems[i + 1]->x = match;
              }
            else
              match--;
          }

        for (i = 0; i <= n; i++)
          {
            struct ll *ll1 = ll_find_adjacent_equal (elemp[i], ll_null (&list),
                                                     compare_elements,
                                                     &aux_data);
            struct ll *ll2;
            int j;

            ll2 = ll_null (&list);
            for (j = i; j < n - 1; j++)
              if (eq_pat & (1 << j))
                {
                  ll2 = elemp[j];
                  break;
                }
            check (ll1 == ll2);
          }
        check_list_contents (&list, values, n);

        free_elements (n, elems, elemp, values);
      }
}

/* Helper function for testing ll_count_range. */
static void
test_count_range_helper (int r0, int r1, int eq_pat UNUSED, struct ll **elemp)
{
  check ((int) ll_count_range (elemp[r0], elemp[r1]) == r1 - r0);
}

/* Tests ll_count_range. */
static void
test_count_range (void)
{
  test_examine_if_range (test_count_range_helper);
}

/* Helper function for testing ll_count_equal. */
static void
test_count_equal_helper (int r0, int r1, int eq_pat,
                         struct ll *to_find, struct ll **elemp)
{
  int count1, count2;
  int i;

  count1 = ll_count_equal (elemp[r0], elemp[r1], to_find,
                           compare_elements, &aux_data);
  count2 = 0;
  for (i = r0; i < r1; i++)
    if (eq_pat & (1 << i))
      count2++;

  check (count1 == count2);
}

/* Tests ll_count_equal. */
static void
test_count_equal (void)
{
  test_examine_equal_range (test_count_equal_helper);
}

/* Helper function for testing ll_count_if. */
static void
test_count_if_helper (int r0, int r1, int eq_pat, struct ll **elemp)
{
  int count1;
  int count2;
  int i;

  count1 = ll_count_if (elemp[r0], elemp[r1], pattern_pred, &eq_pat);

  count2 = 0;
  for (i = r0; i < r1; i++)
    if (eq_pat & (1 << i))
      count2++;

  check (count1 == count2);
}

/* Tests ll_count_if. */
static void
test_count_if (void)
{
  test_examine_if_range (test_count_if_helper);
}

/* Returns N!. */
static unsigned int
factorial (unsigned int n)
{
  unsigned int value = 1;
  while (n > 1)
    value *= n--;
  return value;
}

/* Returns the number of permutations of the N values in
   VALUES.  If VALUES contains duplicates, they must be
   adjacent. */
static unsigned int
expected_perms (int *values, size_t n)
{
  size_t i, j;
  unsigned int n_perms;

  n_perms = factorial (n);
  for (i = 0; i < n; i = j)
    {
      for (j = i + 1; j < n; j++)
        if (values[i] != values[j])
          break;
      n_perms /= factorial (j - i);
    }
  return n_perms;
}

/* Tests ll_min and ll_max. */
static void
test_min_max (void)
{
  const int max_elems = 6;
  int n;

  for (n = 0; n <= max_elems; n++)
    {
      struct ll_list list;
      struct element **elems;
      struct ll **elemp;
      int *values;
      int *new_values = xnmalloc (n, sizeof *values);

      size_t n_perms;

      allocate_ascending (n, &list, &elems, &elemp, &values);

      n_perms = 1;
      while (ll_next_permutation (ll_head (&list), ll_null (&list),
                                  compare_elements, &aux_data))
        {
          int r0, r1;
          struct ll *x;
          int i;

          for (i = 0, x = ll_head (&list); x != ll_null (&list);
               x = ll_next (x), i++)
            {
              struct element *e = ll_to_element (x);
              elemp[i] = x;
              new_values[i] = e->x;
            }
          for (r0 = 0; r0 <= n; r0++)
            for (r1 = r0; r1 <= n; r1++)
              {
                struct ll *min = ll_min (elemp[r0], elemp[r1],
                                         compare_elements, &aux_data);
                struct ll *max = ll_max (elemp[r0], elemp[r1],
                                         compare_elements, &aux_data);
                if (r0 == r1)
                  {
                    check (min == elemp[r1]);
                    check (max == elemp[r1]);
                  }
                else
                  {
                    int min_int, max_int;
                    int i;

                    min_int = max_int = new_values[r0];
                    for (i = r0; i < r1; i++)
                      {
                        int value = new_values[i];
                        if (value < min_int)
                          min_int = value;
                        if (value > max_int)
                          max_int = value;
                      }
                    check (min != elemp[r1]
                           && ll_to_element (min)->x == min_int);
                    check (max != elemp[r1]
                           && ll_to_element (max)->x == max_int);
                  }
              }
          n_perms++;
        }
      check (n_perms == factorial (n));
      check_list_contents (&list, values, n);

      free_elements (n, elems, elemp, values);
      free (new_values);
    }
}

/* Tests ll_lexicographical_compare_3way. */
static void
test_lexicographical_compare_3way (void)
{
  const int max_elems = 4;

  int n_a, pat_a, n_b, pat_b;

  for (n_a = 0; n_a <= max_elems; n_a++)
    for (pat_a = 0; pat_a <= 1 << n_a; pat_a++)
      for (n_b = 0; n_b <= max_elems; n_b++)
        for (pat_b = 0; pat_b <= 1 << n_b; pat_b++)
          {
            struct ll_list list_a, list_b;
            struct element **elems_a, **elems_b;
            struct ll **elemp_a, **elemp_b;
            int *values_a, *values_b;

            int a0, a1, b0, b1;

            allocate_pattern (n_a, pat_a,
                              &list_a, &elems_a, &elemp_a, &values_a);
            allocate_pattern (n_b, pat_b,
                              &list_b, &elems_b, &elemp_b, &values_b);

            for (a0 = 0; a0 <= n_a; a0++)
              for (a1 = a0; a1 <= n_a; a1++)
                for (b0 = 0; b0 <= n_b; b0++)
                  for (b1 = b0; b1 <= n_b; b1++)
                    {
                      int a_ordering = lexicographical_compare_3way (
                        values_a + a0, a1 - a0,
                        values_b + b0, b1 - b0,
                        sizeof *values_a,
                        compare_ints, NULL);

                      int b_ordering = ll_lexicographical_compare_3way (
                        elemp_a[a0], elemp_a[a1],
                        elemp_b[b0], elemp_b[b1],
                        compare_elements, &aux_data);

                      check (a_ordering == b_ordering);
                    }

            free_elements (n_a, elems_a, elemp_a, values_a);
            free_elements (n_b, elems_b, elemp_b, values_b);
          }
}

/* Appends the `x' value in element E to the array pointed to by
   NEXT_OUTPUT, and advances NEXT_OUTPUT to the next position. */
static void
apply_func (struct ll *e_, void *next_output_)
{
  struct element *e = ll_to_element (e_);
  int **next_output = next_output_;

  *(*next_output)++ = e->x;
}

/* Tests ll_apply. */
static void
test_apply (void)
{
  const int max_elems = 8;

  int n, r0, r1;

  for (n = 0; n <= max_elems; n++)
    for (r0 = 0; r0 <= n; r0++)
      for (r1 = r0; r1 <= n; r1++)
        {
          struct ll_list list;
          struct element **elems;
          struct ll **elemp;
          int *values;

          int *output;
          int *next_output;

          int i;

          allocate_ascending (n, &list, &elems, &elemp, &values);
          check_list_contents (&list, values, n);

          output = next_output = xnmalloc (n, sizeof *output);
          ll_apply (elemp[r0], elemp[r1], apply_func, &next_output);
          check_list_contents (&list, values, n);

          check (r1 - r0 == next_output - output);
          for (i = 0; i < r1 - r0; i++)
            check (output[i] == r0 + i);

          free_elements (n, elems, elemp, values);
          free (output);
        }
}

/* Tests ll_reverse. */
static void
test_reverse (void)
{
  const int max_elems = 8;

  int n, r0, r1;

  for (n = 0; n <= max_elems; n++)
    for (r0 = 0; r0 <= n; r0++)
      for (r1 = r0; r1 <= n; r1++)
        {
          struct ll_list list;
          struct element **elems;
          struct ll **elemp;
          int *values;

          int i, j;

          allocate_ascending (n, &list, &elems, &elemp, &values);
          check_list_contents (&list, values, n);

          j = 0;
          for (i = 0; i < r0; i++)
            values[j++] = i;
          for (i = r1 - 1; i >= r0; i--)
            values[j++] = i;
          for (i = r1; i < n; i++)
            values[j++] = i;

          ll_reverse (elemp[r0], elemp[r1]);
          check_list_contents (&list, values, n);

          free_elements (n, elems, elemp, values);
        }
}

/* Tests ll_next_permutation and ll_prev_permutation when the
   permuted values have no duplicates. */
static void
test_permutations_no_dups (void)
{
  const int max_elems = 8;
  int n;

  for (n = 0; n <= max_elems; n++)
    {
      struct ll_list list;
      struct element **elems;
      int *values;
      int *old_values = xnmalloc (n, sizeof *values);
      int *new_values = xnmalloc (n, sizeof *values);

      size_t n_perms;

      allocate_ascending (n, &list, &elems, NULL, &values);

      n_perms = 1;
      extract_values (&list, old_values, n);
      while (ll_next_permutation (ll_head (&list), ll_null (&list),
                                  compare_elements, &aux_data))
        {
          extract_values (&list, new_values, n);
          check (lexicographical_compare_3way (new_values, n,
                                               old_values, n,
                                               sizeof *new_values,
                                               compare_ints, NULL) > 0);
          memcpy (old_values, new_values, (n) * sizeof *old_values);
          n_perms++;
        }
      check (n_perms == factorial (n));
      check_list_contents (&list, values, n);

      n_perms = 1;
      ll_reverse (ll_head (&list), ll_null (&list));
      extract_values (&list, old_values, n);
      while (ll_prev_permutation (ll_head (&list), ll_null (&list),
                                  compare_elements, &aux_data))
        {
          extract_values (&list, new_values, n);
          check (lexicographical_compare_3way (new_values, n,
                                               old_values, n,
                                               sizeof *new_values,
                                               compare_ints, NULL) < 0);
          memcpy (old_values, new_values, (n) * sizeof *old_values);
          n_perms++;
        }
      check (n_perms == factorial (n));
      ll_reverse (ll_head (&list), ll_null (&list));
      check_list_contents (&list, values, n);

      free_elements (n, elems, NULL, values);
      free (old_values);
      free (new_values);
    }
}

/* Tests ll_next_permutation and ll_prev_permutation when the
   permuted values contain duplicates. */
static void
test_permutations_with_dups (void)
{
  const int max_elems = 8;
  const int max_dup = 3;
  const int repetitions = 1024;

  for (int repeat = 0; repeat < repetitions; repeat++)
    for (int n_elems = 0; n_elems < max_elems; n_elems++)
      {
        struct ll_list list;
        struct element **elems;
        int *values;
        int *old_values = xnmalloc (max_elems, sizeof *values);
        int *new_values = xnmalloc (max_elems, sizeof *values);

        unsigned int n_permutations;
        int left = n_elems;
        int value = 0;

        allocate_elements (n_elems, &list, &elems, NULL, &values);

        value = 0;
        while (left > 0)
          {
            int max = left < max_dup ? left : max_dup;
            int n = rand () % max + 1;
            while (n-- > 0)
              {
                int idx = n_elems - left--;
                values[idx] = elems[idx]->x = value;
              }
            value++;
          }

        n_permutations = 1;
        extract_values (&list, old_values, n_elems);
        while (ll_next_permutation (ll_head (&list), ll_null (&list),
                                    compare_elements, &aux_data))
          {
            extract_values (&list, new_values, n_elems);
            check (lexicographical_compare_3way (new_values, n_elems,
                                                 old_values, n_elems,
                                                 sizeof *new_values,
                                                 compare_ints, NULL) > 0);
            memcpy (old_values, new_values, n_elems * sizeof *old_values);
            n_permutations++;
          }
        check (n_permutations == expected_perms (values, n_elems));
        check_list_contents (&list, values, n_elems);

        n_permutations = 1;
        ll_reverse (ll_head (&list), ll_null (&list));
        extract_values (&list, old_values, n_elems);
        while (ll_prev_permutation (ll_head (&list), ll_null (&list),
                                    compare_elements, &aux_data))
          {
            extract_values (&list, new_values, n_elems);
            check (lexicographical_compare_3way (new_values, n_elems,
                                                 old_values, n_elems,
                                                 sizeof *new_values,
                                                 compare_ints, NULL) < 0);
            n_permutations++;
          }
        ll_reverse (ll_head (&list), ll_null (&list));
        check (n_permutations == expected_perms (values, n_elems));
        check_list_contents (&list, values, n_elems);

        free_elements (n_elems, elems, NULL, values);
        free (old_values);
        free (new_values);
      }
}

/* Tests ll_merge when no equal values are to be merged. */
static void
test_merge_no_dups (void)
{
  const int max_elems = 8;
  const int max_filler = 3;

  int n_merges, pattern, pfx, gap, sfx, order;

  for (n_merges = 0; n_merges < max_elems; n_merges++)
    for (pattern = 0; pattern <= (1 << n_merges); pattern++)
      for (pfx = 0; pfx < max_filler; pfx++)
        for (gap = 0; gap < max_filler; gap++)
          for (sfx = 0; sfx < max_filler; sfx++)
            for (order = 0; order < 2; order++)
              {
                struct ll_list list;
                struct element **elems;
                struct ll **elemp;
                int *values;

                int n_lists = pfx + n_merges + gap + sfx;
                int a0, a1, b0, b1;
                int i, j;

                allocate_elements (n_lists, &list,
                                   &elems, &elemp, &values);

                j = 0;
                for (i = 0; i < pfx; i++)
                  elems[j++]->x = 100 + i;
                a0 = j;
                for (i = 0; i < n_merges; i++)
                  if (pattern & (1u << i))
                    elems[j++]->x = i;
                a1 = j;
                for (i = 0; i < gap; i++)
                  elems[j++]->x = 200 + i;
                b0 = j;
                for (i = 0; i < n_merges; i++)
                  if (!(pattern & (1u << i)))
                    elems[j++]->x = i;
                b1 = j;
                for (i = 0; i < sfx; i++)
                  elems[j++]->x = 300 + i;
                check (n_lists == j);

                j = 0;
                for (i = 0; i < pfx; i++)
                  values[j++] = 100 + i;
                if (order == 0)
                  for (i = 0; i < n_merges; i++)
                    values[j++] = i;
                for (i = 0; i < gap; i++)
                  values[j++] = 200 + i;
                if (order == 1)
                  for (i = 0; i < n_merges; i++)
                    values[j++] = i;
                for (i = 0; i < sfx; i++)
                  values[j++] = 300 + i;
                check (n_lists == j);

                if (order == 0)
                  ll_merge (elemp[a0], elemp[a1], elemp[b0], elemp[b1],
                            compare_elements, &aux_data);
                else
                  ll_merge (elemp[b0], elemp[b1], elemp[a0], elemp[a1],
                            compare_elements, &aux_data);

                check_list_contents (&list, values, n_lists);

                free_elements (n_lists, elems, elemp, values);
              }
}

/* Tests ll_merge when equal values are to be merged. */
static void
test_merge_with_dups (void)
{
  const int max_elems = 8;

  int n, merge_pat, inc_pat, order;

  for (n = 0; n <= max_elems; n++)
    for (merge_pat = 0; merge_pat <= (1 << n); merge_pat++)
      for (inc_pat = 0; inc_pat <= (1 << n); inc_pat++)
        for (order = 0; order < 2; order++)
          {
            struct ll_list list;
            struct element **elems;
            struct ll **elemp;
            int *values;

            int mid;
            int i, j, k;

            allocate_elements (n, &list, &elems, &elemp, &values);

            j = 0;
            for (i = k = 0; i < n; i++)
              {
                if (merge_pat & (1u << i))
                  elems[j++]->x = k;
                if (inc_pat & (1u << i))
                  k++;
              }
            mid = j;
            for (i = k = 0; i < n; i++)
              {
                if (!(merge_pat & (1u << i)))
                  elems[j++]->x = k;
                if (inc_pat & (1u << i))
                  k++;
              }
            check (n == j);

            if (order == 0)
              {
                for (i = 0; i < n; i++)
                  elems[i]->y = i;
              }
            else
              {
                for (i = 0; i < mid; i++)
                  elems[i]->y = 100 + i;
                for (i = mid; i < n; i++)
                  elems[i]->y = i;
              }

            j = 0;
            for (i = k = 0; i < n; i++)
              {
                values[j++] = k;
                if (inc_pat & (1u << i))
                  k++;
              }
            check (n == j);

            if (order == 0)
              ll_merge (elemp[0], elemp[mid], elemp[mid], elemp[n],
                        compare_elements, &aux_data);
            else
              ll_merge (elemp[mid], elemp[n], elemp[0], elemp[mid],
                        compare_elements, &aux_data);

            check_list_contents (&list, values, n);
            check (ll_is_sorted (ll_head (&list), ll_null (&list),
                                 compare_elements_x_y, &aux_data));

            free_elements (n, elems, elemp, values);
          }
}

/* Tests ll_sort on all permutations up to a maximum number of
   elements. */
static void
test_sort_exhaustive (void)
{
  const int max_elems = 8;
  int n;

  for (n = 0; n <= max_elems; n++)
    {
      struct ll_list list;
      struct element **elems;
      int *values;

      struct element **perm_elems;
      int *perm_values;

      size_t n_perms;

      allocate_ascending (n, &list, &elems, NULL, &values);
      allocate_elements (n, NULL, &perm_elems, NULL, &perm_values);

      n_perms = 1;
      while (ll_next_permutation (ll_head (&list), ll_null (&list),
                                  compare_elements, &aux_data))
        {
          struct ll_list perm_list;
          int j;

          extract_values (&list, perm_values, n);
          ll_init (&perm_list);
          for (j = 0; j < n; j++)
            {
              perm_elems[j]->x = perm_values[j];
              ll_push_tail (&perm_list, &perm_elems[j]->ll);
            }
          ll_sort (ll_head (&perm_list), ll_null (&perm_list),
                   compare_elements, &aux_data);
          check_list_contents (&perm_list, values, n);
          check (ll_is_sorted (ll_head (&perm_list), ll_null (&perm_list),
                               compare_elements, &aux_data));
          n_perms++;
        }
      check (n_perms == factorial (n));

      free_elements (n, elems, NULL, values);
      free_elements (n, perm_elems, NULL, perm_values);
    }
}

/* Tests that ll_sort is stable in the presence of equal
   values. */
static void
test_sort_stable (void)
{
  const int max_elems = 6;
  int n, inc_pat;

  for (n = 0; n <= max_elems; n++)
    for (inc_pat = 0; inc_pat <= 1 << n; inc_pat++)
      {
        struct ll_list list;
        struct element **elems;
        int *values;

        struct element **perm_elems;
        int *perm_values;

        size_t n_perms;
        int i, j;

        allocate_elements (n, &list, &elems, NULL, &values);
        allocate_elements (n, NULL, &perm_elems, NULL, &perm_values);

        j = 0;
        for (i = 0; i < n; i++)
          {
            elems[i]->x = values[i] = j;
            if (inc_pat & (1 << i))
              j++;
            elems[i]->y = i;
          }

        n_perms = 1;
        while (ll_next_permutation (ll_head (&list), ll_null (&list),
                                    compare_elements_y, &aux_data))
          {
            struct ll_list perm_list;

            extract_values (&list, perm_values, n);
            ll_init (&perm_list);
            for (i = 0; i < n; i++)
              {
                perm_elems[i]->x = perm_values[i];
                perm_elems[i]->y = i;
                ll_push_tail (&perm_list, &perm_elems[i]->ll);
              }
            ll_sort (ll_head (&perm_list), ll_null (&perm_list),
                     compare_elements, &aux_data);
            check_list_contents (&perm_list, values, n);
            check (ll_is_sorted (ll_head (&perm_list), ll_null (&perm_list),
                                 compare_elements_x_y, &aux_data));
            n_perms++;
          }
        check (n_perms == factorial (n));

        free_elements (n, elems, NULL, values);
        free_elements (n, perm_elems, NULL, perm_values);
      }
}

/* Tests that ll_sort does not disturb elements outside the
   range sorted. */
static void
test_sort_subset (void)
{
  const int max_elems = 8;

  int n, r0, r1, repeat;

  for (n = 0; n <= max_elems; n++)
    for (repeat = 0; repeat < 100; repeat++)
      for (r0 = 0; r0 <= n; r0++)
        for (r1 = r0; r1 <= n; r1++)
          {
            struct ll_list list;
            struct element **elems;
            struct ll **elemp;
            int *values;

            allocate_random (n, &list, &elems, &elemp, &values);

            qsort (&values[r0], r1 - r0, sizeof *values, compare_ints_noaux);
            ll_sort (elemp[r0], elemp[r1], compare_elements, &aux_data);
            check_list_contents (&list, values, n);

            free_elements (n, elems, elemp, values);
          }
}

/* Tests that ll_sort works with large lists. */
static void
test_sort_big (void)
{
  const int max_elems = 1024;

  int n;

  for (n = 0; n < max_elems; n++)
    {
      struct ll_list list;
      struct element **elems;
      int *values;

      allocate_random (n, &list, &elems, NULL, &values);

      qsort (values, n, sizeof *values, compare_ints_noaux);
      ll_sort (ll_head (&list), ll_null (&list), compare_elements, &aux_data);
      check_list_contents (&list, values, n);

      free_elements (n, elems, NULL, values);
    }
}

/* Tests ll_unique. */
static void
test_unique (void)
{
  const int max_elems = 10;

  int *ascending = xnmalloc (max_elems, sizeof *ascending);

  int n, inc_pat, i, j, unique_values;

  for (i = 0; i < max_elems; i++)
    ascending[i] = i;

  for (n = 0; n < max_elems; n++)
    for (inc_pat = 0; inc_pat < (1 << n); inc_pat++)
      {
        struct ll_list list, dups;
        struct element **elems;
        int *values;

        allocate_elements (n, &list, &elems, NULL, &values);

        j = unique_values = 0;
        for (i = 0; i < n; i++)
          {
            unique_values = j + 1;
            elems[i]->x = values[i] = j;
            if (inc_pat & (1 << i))
              j++;
          }
        check_list_contents (&list, values, n);

        ll_init (&dups);
        check (ll_unique (ll_head (&list), ll_null (&list), ll_null (&dups),
                          compare_elements, &aux_data)
               == (size_t) unique_values);
        check_list_contents (&list, ascending, unique_values);

        ll_splice (ll_null (&list), ll_head (&dups), ll_null (&dups));
        ll_sort (ll_head (&list), ll_null (&list), compare_elements, &aux_data);
        check_list_contents (&list, values, n);

        free_elements (n, elems, NULL, values);
      }

  free (ascending);
}

/* Tests ll_sort_unique. */
static void
test_sort_unique (void)
{
  const int max_elems = 7;
  int n, inc_pat;

  for (n = 0; n <= max_elems; n++)
    for (inc_pat = 0; inc_pat <= 1 << n; inc_pat++)
      {
        struct ll_list list;
        struct element **elems;
        int *values;

        struct element **perm_elems;
        int *perm_values;

        int n_uniques;
        int *unique_values;

        size_t n_perms;
        int i, j;

        allocate_elements (n, &list, &elems, NULL, &values);
        allocate_elements (n, NULL, &perm_elems, NULL, &perm_values);

        j = n_uniques = 0;
        for (i = 0; i < n; i++)
          {
            elems[i]->x = values[i] = j;
            n_uniques = j + 1;
            if (inc_pat & (1 << i))
              j++;
          }

        unique_values = xnmalloc (n_uniques, sizeof *unique_values);
        for (i = 0; i < n_uniques; i++)
          unique_values[i] = i;

        n_perms = 1;
        while (ll_next_permutation (ll_head (&list), ll_null (&list),
                                    compare_elements, &aux_data))
          {
            struct ll_list perm_list;

            extract_values (&list, perm_values, n);
            ll_init (&perm_list);
            for (i = 0; i < n; i++)
              {
                perm_elems[i]->x = perm_values[i];
                perm_elems[i]->y = i;
                ll_push_tail (&perm_list, &perm_elems[i]->ll);
              }
            ll_sort_unique (ll_head (&perm_list), ll_null (&perm_list), NULL,
                            compare_elements, &aux_data);
            check_list_contents (&perm_list, unique_values, n_uniques);
            check (ll_is_sorted (ll_head (&perm_list), ll_null (&perm_list),
                                 compare_elements_x_y, &aux_data));
            n_perms++;
          }
        check (n_perms == expected_perms (values, n));

        free_elements (n, elems, NULL, values);
        free_elements (n, perm_elems, NULL, perm_values);
        free (unique_values);
      }
}

/* Tests ll_insert_ordered. */
static void
test_insert_ordered (void)
{
  const int max_elems = 6;
  int n, inc_pat;

  for (n = 0; n <= max_elems; n++)
    for (inc_pat = 0; inc_pat <= 1 << n; inc_pat++)
      {
        struct ll_list list;
        struct element **elems;
        int *values;

        struct element **perm_elems;
        int *perm_values;

        size_t n_perms;
        int i, j;

        allocate_elements (n, &list, &elems, NULL, &values);
        allocate_elements (n, NULL, &perm_elems, NULL, &perm_values);

        j = 0;
        for (i = 0; i < n; i++)
          {
            elems[i]->x = values[i] = j;
            if (inc_pat & (1 << i))
              j++;
            elems[i]->y = i;
          }

        n_perms = 1;
        while (ll_next_permutation (ll_head (&list), ll_null (&list),
                                    compare_elements_y, &aux_data))
          {
            struct ll_list perm_list;

            extract_values (&list, perm_values, n);
            ll_init (&perm_list);
            for (i = 0; i < n; i++)
              {
                perm_elems[i]->x = perm_values[i];
                perm_elems[i]->y = i;
                ll_insert_ordered (ll_head (&perm_list), ll_null (&perm_list),
                                   &perm_elems[i]->ll,
                                   compare_elements, &aux_data);
              }
            check (ll_is_sorted (ll_head (&perm_list), ll_null (&perm_list),
                                 compare_elements_x_y, &aux_data));
            n_perms++;
          }
        check (n_perms == factorial (n));

        free_elements (n, elems, NULL, values);
        free_elements (n, perm_elems, NULL, perm_values);
      }
}

/* Tests ll_partition. */
static void
test_partition (void)
{
  const int max_elems = 10;

  int n;
  unsigned int pbase;
  int r0, r1;

  for (n = 0; n < max_elems; n++)
    for (r0 = 0; r0 <= n; r0++)
      for (r1 = r0; r1 <= n; r1++)
        for (pbase = 0; pbase <= (1u << (r1 - r0)); pbase++)
          {
            struct ll_list list;
            struct element **elems;
            struct ll **elemp;
            int *values;

            unsigned int pattern = pbase << r0;
            int i, j;
            int first_false;
            struct ll *part_ll;

            allocate_ascending (n, &list, &elems, &elemp, &values);

            /* Check that ll_find_partition works okay in every
               case.  We use it after partitioning, too, but that
               only tests cases where it returns non-null. */
            for (i = r0; i < r1; i++)
              if (!(pattern & (1u << i)))
                break;
            j = i;
            for (; i < r1; i++)
              if (pattern & (1u << i))
                break;
            part_ll = ll_find_partition (elemp[r0], elemp[r1],
                                         pattern_pred,
                                         &pattern);
            if (i == r1)
              check (part_ll == elemp[j]);
            else
              check (part_ll == NULL);

            /* Figure out expected results. */
            j = 0;
            first_false = -1;
            for (i = 0; i < r0; i++)
              values[j++] = i;
            for (i = r0; i < r1; i++)
              if (pattern & (1u << i))
                values[j++] = i;
            for (i = r0; i < r1; i++)
              if (!(pattern & (1u << i)))
                {
                  if (first_false == -1)
                    first_false = i;
                  values[j++] = i;
                }
            if (first_false == -1)
              first_false = r1;
            for (i = r1; i < n; i++)
              values[j++] = i;
            check (j == n);

            /* Partition and check for expected results. */
            check (ll_partition (elemp[r0], elemp[r1],
                                 pattern_pred, &pattern)
                   == elemp[first_false]);
            check (ll_find_partition (elemp[r0], elemp[r1],
                                      pattern_pred, &pattern)
                   == elemp[first_false]);
            check_list_contents (&list, values, n);
            check ((int) ll_count (&list) == n);

            free_elements (n, elems, elemp, values);
          }
}

/* Main program. */

struct test
  {
    const char *name;
    const char *description;
    void (*function) (void);
  };

static const struct test tests[] =
  {
    {
      "push-pop",
      "push/pop",
      test_push_pop
    },
    {
      "insert-remove",
      "insert/remove",
      test_insert_remove
    },
    {
      "swap",
      "swap",
      test_swap
    },
    {
      "swap-range",
      "swap_range",
      test_swap_range
    },
    {
      "remove-range",
      "remove_range",
      test_remove_range
    },
    {
      "remove-equal",
      "remove_equal",
      test_remove_equal
    },
    {
      "remove-if",
      "remove_if",
      test_remove_if
    },
    {
      "moved",
      "moved",
      test_moved
    },
    {
      "find-equal",
      "find_equal",
      test_find_equal
    },
    {
      "find-if",
      "find_if",
      test_find_if
    },
    {
      "find-adjacent-equal",
      "find_adjacent_equal",
      test_find_adjacent_equal
    },
    {
      "count-range",
      "count_range",
      test_count_range
    },
    {
      "count-equal",
      "count_equal",
      test_count_equal
    },
    {
      "count-if",
      "count_if",
      test_count_if
    },
    {
      "min-max",
      "min/max",
      test_min_max
    },
    {
      "lexicographical-compare-3way",
      "lexicographical_compare_3way",
      test_lexicographical_compare_3way
    },
    {
      "apply",
      "apply",
      test_apply
    },
    {
      "reverse",
      "reverse",
      test_reverse
    },
    {
      "permutations-no-dups",
      "permutations (no dups)",
      test_permutations_no_dups
    },
    {
      "permutations-with-dups",
      "permutations (with dups)",
      test_permutations_with_dups
    },
    {
      "merge-no-dups",
      "merge (no dups)",
      test_merge_no_dups
    },
    {
      "merge-with-dups",
      "merge (with dups)",
      test_merge_with_dups
    },
    {
      "sort-exhaustive",
      "sort (exhaustive)",
      test_sort_exhaustive
    },
    {
      "sort-stable",
      "sort (stability)",
      test_sort_stable
    },
    {
      "sort-subset",
      "sort (subset)",
      test_sort_subset
    },
    {
      "sort-big",
      "sort (big)",
      test_sort_big
    },
    {
      "unique",
      "unique",
      test_unique
    },
    {
      "sort-unique",
      "sort_unique",
      test_sort_unique
    },
    {
      "insert-ordered",
      "insert_ordered",
      test_insert_ordered
    },
    {
      "partition",
      "partition",
      test_partition
    },
  };

enum { N_TESTS = sizeof tests / sizeof *tests };

int
main (int argc, char *argv[])
{
  int i;

  if (argc != 2)
    {
      fprintf (stderr, "exactly one argument required; use --help for help\n");
      return EXIT_FAILURE;
    }
  else if (!strcmp (argv[1], "--help"))
    {
      printf ("%s: test doubly linked list (ll) library\n"
              "usage: %s TEST-NAME\n"
              "where TEST-NAME is one of the following:\n",
              argv[0], argv[0]);
      for (i = 0; i < N_TESTS; i++)
        printf ("  %s\n    %s\n", tests[i].name, tests[i].description);
      return 0;
    }
  else
    {
      for (i = 0; i < N_TESTS; i++)
        if (!strcmp (argv[1], tests[i].name))
          {
            tests[i].function ();
            return 0;
          }

      fprintf (stderr, "unknown test %s; use --help for help\n", argv[1]);
      return EXIT_FAILURE;
    }
}