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
|
#include <zebra.h>
#include "misc.h"
#include "qlib_init.h"
#include "command.h"
#include "list_util.h"
#include <string.h>
/* List util torture tests
*
*/
/* prototypes */
int main(int argc, char **argv);
static void test_ssl(void);
static void test_sdl(void);
static void test_ddl(void);
#define test_assert(true, message) \
do { if (!(true)) test_assert_fail(#true, message, __func__, __LINE__) ; \
} while (0)
static void
test_assert_fail(const char* truth, const char* message, const char* func,
int line)
{
printf("*** %s %d: (%s) not true: %s\n", func, line, truth, message) ;
} ;
/*==============================================================================
* The tests.
*/
int
main(int argc, char **argv)
{
qlib_init_first_stage(0); /* Absolutely first */
host_init(argv[0]) ;
printf("Starting list_util tests -- %s\n",
#ifdef __GNUC__LIST_UTIL
"GNUC version"
#else
"not GNUC version"
#endif
" -- v0.01 26-Feb-2010") ;
srand(22) ; /* Consistent testing required */
test_ssl() ;
test_sdl() ;
test_ddl() ;
return 0;
}
/*------------------------------------------------------------------------------
* Construct a majic mark from two addresses
*/
static unsigned majic(void* a, void* b)
{
uintptr_t z = (uintptr_t)a ^ (uintptr_t)b ^ (uintptr_t)&majic ;
return z ;
} ;
/*==============================================================================
* Single Base, Single Link
*
* ssl_push(base, item, next) -- add at head of list
* ssl_del(base, item, next) -- delete from list
* ssl_del_head(base, next) -- delete head of list
* ssl_pop(dst, base, next) -- pop head of list, if any
* ssl_head(base) -- return head of list
* ssl_next(item, next) -- step to next item, if any
*
* Cases to cover:
*
* a) adding when list is empty
* b) adding when list is not empty
* c) deletion of first item and more than one item on list
* d) deletion of first item when only one item on the list
* e) deletion of arbitrary item (list implicitly not empty)
* f) deletion when item not on list and list not empty
* g) deletion when item not on list and list is empty
* h) deletion of NULL item and list not empty
* i) deletion of NULL item and list empty
* j) pop of head when list not empty
* k) pop of head when list contains one item
* l) pop of head when list empty
* m) deletion of head when list not empty
* n) deletion of head when contains one item
* o) deletion of head when list empty
* p) head when list not empty
* q) head when list is empty
* r) next when not last
* s) next when last
*/
typedef struct ssl_test* ssl_test ;
struct ssl_test
{
ssl_test next ; /* pointer at start of structure */
unsigned majic ;
char dummy ;
ssl_test other_next ; /* pointer elsewhere in structure */
} ;
struct ssl_test_parent
{
unsigned rubbish ;
char fred ;
ssl_test base ;
int z[5] ;
} ;
static struct ssl_test_parent ssl_parent ;
static void
test_ssl(void)
{
ssl_test base = NULL ;
ssl_test del = NULL ;
ssl_test other_del = NULL ;
ssl_test last = NULL ;
ssl_test first = NULL ;
struct ssl_test dummy ;
int n = 47 ;
int i_del = 7 ; /* NB: neither of these may be 0 or 1 */
int i_other_del = 37 ;
ssl_test prev ;
ssl_test this ;
ssl_test other_this ;
ssl_test take ;
ssl_test temp ;
int i ;
int ret ;
static struct ssl_test_parent* other = &ssl_parent ;
memset(other, 77, sizeof(struct ssl_test_parent)) ;
other->base = NULL ;
/* Repeated insertion, starting from empty list
*
* a) adding when list is empty
* b) adding when list is not empty
*
* Creates lists for following tests.
*/
printf("=== Testing ssl -- Single Base, Single Link -- stuff\n") ;
printf(" Creating list of items") ;
for (i = 1 ; i <= n ; ++i)
{
ssl_test this = calloc(1, sizeof(struct ssl_test)) ;
if (last == NULL)
last = this ;
this->majic = majic(this, &base) ;
this->dummy = i ;
ssl_push(base, this, next) ;
if (i & 1)
ssl_push(other->base, this, other_next) ;
else
ssl_push(ssl_parent.base, this, other_next) ;
if (i == i_del)
del = this ;
if (i == i_other_del)
other_del = this ;
first = this ;
printf("+") ;
} ;
test_assert((base == first) && (other->base == first),
"Failed to create consistent lists") ;
printf("\n") ;
passert((del != base) && (del != last)) ;
passert((other_del != base) && (other_del != last)) ;
/* Walk to check that have the expected items
*
* l) head when list not empty
* r) next when not last
* s) next when last
*/
printf(" Walking list of items") ;
this = ssl_head(base) ;
test_assert(this == first, "ssl_head failed") ;
this = ssl_head(other->base) ;
test_assert(this == first, "ssl_head failed") ;
this = ssl_head(ssl_parent.base) ;
test_assert(this == first, "ssl_head failed") ;
this = ssl_next(del, next) ;
test_assert((this == del->next) && (this != NULL), "ssl_next failed") ;
this = ssl_next(last, next) ;
test_assert((this == last->next) && (this == NULL),
"ssl_next failed at end") ;
this = ssl_next(other_del, other_next) ;
test_assert((this == other_del->other_next) && (this != NULL),
"ssl_next failed") ;
this = ssl_next(last, other_next) ;
test_assert((this == last->other_next) && (this == NULL),
"ssl_next failed at end") ;
this = base ;
other_this = other->base ;
prev = NULL ;
i = n ;
while (1)
{
test_assert(this == other_this, "this and other_this not in step") ;
if (this == NULL)
break ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf(".") ;
this = ssl_next(this, next) ;
other_this = ssl_next(other_this, other_next) ;
} ;
printf("\n") ;
/* Deletion specifically at the start of the list
*
* c) deletion of first item and more than one item on list
*/
printf(" Deleting the first item") ;
this = base ;
first = base->next ;
ret = ssl_del(base, this, next) ;
test_assert(ret == true, "ssl_del did not return true") ;
test_assert(first == base, "ssl_del of first item failed") ;
this = other->base ;
ret = ssl_del(other->base, this, other_next) ;
test_assert(ret == true, "ssl_del did not return true") ;
test_assert(first == other->base, "ssl_del of first item failed") ;
printf("\n") ;
--n ; /* one less on the lists ! */
/* Deletion of items from arbitrary place in list
*
* e) deletion of arbitrary item (list implicitly not empty)
*/
printf(" Deleting arbitrary items") ;
ret = ssl_del(base, del, next) ;
test_assert(ret == true, "ssl_del did not return true") ;
ret = ssl_del(ssl_parent.base, other_del, other_next) ;
test_assert(ret == true, "ssl_del did not return true") ;
printf("\n") ;
/* Deletion of items from arbitrary place in list
*
* f) deletion when item not on list and list not empty
*/
printf(" Deleting non-existant items") ;
ret = ssl_del(base, &dummy, next) ;
test_assert(ret == false, "ssl_del did not return false") ;
ret = ssl_del(other->base, &dummy, other_next) ;
test_assert(ret == false, "ssl_del did not return false") ;
printf("\n") ;
/* Deletion of NULL items
*
* h) deletion of NULL item and list not empty
*/
printf(" Deleting NULL items") ;
this = NULL ;
ret = ssl_del(base, this, next) ;
test_assert(ret == false, "ssl_del did not return false") ;
ret = ssl_del(ssl_parent.base, this, other_next) ;
test_assert(ret == false, "ssl_del did not return false") ;
printf("\n") ;
/* Scan lists to check after deletion */
printf(" Base list scan") ;
this = base ;
prev = NULL ;
i = n ;
while (1)
{
if (this == NULL)
break ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
if (i == i_del)
--i ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("*") ;
this = ssl_next(this, next) ;
} ;
printf("\n") ;
printf(" Other list scan") ;
other_this = other->base ;
prev = NULL ;
i = n ;
while (1)
{
if (other_this == NULL)
break ;
test_assert(other_this != prev, "this is same as prev") ;
prev = other_this ;
if (i == i_other_del)
--i ;
test_assert(other_this->dummy == i--, "don't have the right dummy") ;
test_assert(other_this->majic == majic(other_this, &base),
"don't have the right majic") ;
printf("*") ;
other_this = ssl_next(other_this, other_next) ;
} ;
printf("\n") ;
/* Dismantle lists
*
* j) pop of head when list not empty
* k) pop of head when list contains one item
* l) pop of head when list empty
* p) head when list not empty
* q) head when list is empty
*/
printf(" Popping the head until list is empty\n") ;
printf(" Base list") ;
prev = NULL ;
i = n ;
while (1)
{
this = base ;
test_assert(this == ssl_head(base), "this is not head !") ;
temp = ssl_pop(&take, base, next) ;
test_assert(this == take, "this is not same as deleted head !") ;
test_assert(temp == take, "temp is not same as deleted head !") ;
if (this == NULL)
break ;
test_assert(base == this->next, "ssl_pop broken") ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
if (i == i_del)
--i ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("-") ;
} ;
test_assert(i == 0, "not the expected final value of 'i'") ;
test_assert(base == NULL, "Base list should be empty") ;
this = ssl_head(base) ;
test_assert(this == NULL, "ssl_head of empty list failed") ;
printf("\n") ;
printf(" Other list") ;
prev = NULL ;
i = n ;
while (1)
{
this = other->base ;
test_assert(this == ssl_head(other->base), "this is not head !") ;
if (i & 1)
temp = ssl_pop(&take, other->base, other_next) ;
else
temp = ssl_pop(&take, ssl_parent.base, other_next) ;
test_assert(this == take, "this is not same as deleted head !") ;
test_assert(temp == take, "temp is not same as deleted head !") ;
if (this == NULL)
break ;
test_assert(other->base == this->other_next, "ssl_pop broken") ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
if (i == i_other_del)
--i ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("-") ;
} ;
test_assert(i == 0, "not the expected final value of 'i'") ;
test_assert(other->base == NULL, "Other list should be empty") ;
this = ssl_head(other->base) ;
test_assert(this == NULL, "ssl_head of empty list failed") ;
printf("\n") ;
/* Rebuild lists to do:
*
* m) deletion of head when list not empty
* n) deletion of head when contains one item
* o) deletion of head when list empty
*/
passert((base == NULL) && (other->base == NULL)) ;
last = NULL ;
first = NULL ;
prev = NULL ;
printf(" Building list of items again") ;
for (i = 1 ; i <= n ; ++i)
{
ssl_test this = calloc(1, sizeof(struct ssl_test)) ;
if (last == NULL)
last = this ;
this->majic = majic(this, &base) ;
this->dummy = i ;
ssl_push(base, this, next) ;
if (i & 1)
ssl_push(ssl_parent.base, this, other_next) ;
else
ssl_push(other->base, this, other_next) ;
test_assert(this->next == prev, "broken ssl_push") ;
test_assert(this->other_next == prev, "broken ssl_push") ;
test_assert(base == this, "broken ssl_push") ;
test_assert(other->base == this, "broken ssl_push") ;
first = this ;
prev = this ;
printf("+") ;
} ;
printf("\n") ;
printf(" Deleting the head until list is empty\n") ;
printf(" Base list") ;
prev = NULL ;
i = n ;
while (1)
{
this = base ;
ssl_del_head(base, next) ;
if (this == NULL)
break ;
test_assert(base == this->next, "ssl_del_head broken") ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("-") ;
} ;
test_assert(i == 0, "not the expected final value of 'i'") ;
test_assert(base == NULL, "Base list should be empty") ;
printf("\n") ;
printf(" Other list") ;
prev = NULL ;
i = n ;
while (1)
{
this = other->base ;
if (i & 1)
ssl_del_head(ssl_parent.base, other_next) ;
else
ssl_del_head(other->base, other_next) ;
if (this == NULL)
break ;
test_assert(other->base == this->other_next, "ssl_del_head broken") ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("-") ;
} ;
test_assert(i == 0, "not the expected final value of 'i'") ;
test_assert(other->base == NULL, "Other list should be empty") ;
printf("\n") ;
/* Final few tests
*
* d) deletion of first item when only one item on the list
* g) deletion when item not on list and list is empty
* i) deletion of NULL item and list empty
*/
/* Deletion of items from arbitrary place in list */
printf(" Miscellaneous") ;
del->next = &dummy ;
ssl_push(base, del, next) ;
test_assert((base == del) && (del->next == NULL), "ssl_push failed ??") ;
ssl_del(base, del, next) ;
test_assert(base == NULL, "ssl_del of first and only item failed") ;
other_del->other_next = &dummy ;
ssl_push(other->base, other_del, other_next) ;
test_assert((other->base == other_del) && (other_del->other_next == NULL),
"ssl_push failed ??") ;
ssl_del(other->base, other_del, other_next) ;
test_assert(other->base == NULL, "ssl_del of first and only item failed") ;
ret = ssl_del(base, del, next) ;
test_assert(ret == false, "ssl_del did not return false") ;
test_assert(base == NULL, "ssl_del on empty list") ;
ret = ssl_del(other->base, other_del, other_next) ;
test_assert(ret == false, "ssl_del did not return false") ;
test_assert(other->base == NULL, "ssl_del on empty list") ;
this = NULL ;
ret = ssl_del(base, this, next) ;
test_assert(ret == false, "ssl_del did not return false") ;
test_assert(base == NULL, "ssl_del on empty list") ;
ret = ssl_del(other->base, this, other_next) ;
test_assert(ret == false, "ssl_del did not return false") ;
test_assert(other->base == NULL, "ssl_del on empty list") ;
printf("\n") ;
} ;
/*==============================================================================
* Single Base, Double Link
*
* sdl_push(base, item, list) -- add at head of list
* sdl_del(base, item, list) -- delete from list
* sdl_pop(&dst, base, next) -- pop head of list, if any
* sdl_head(base) -- return head of list
* sdl_next(item, next) -- step to next item, if any
* sdl_prev(item, next) -- step to prev item, if any
*
* Cases to cover:
*
* a) adding when list is empty
* b) adding when list is not empty
* c) deletion of first item and more than one item on list
* d) deletion of first item when only one item on the list
* e) deletion of arbitrary item (list implicitly not empty)
* f) deletion of NULL item and list not empty
* g) deletion of NULL item and list empty
* h) pop of head when list not empty
* i) pop of head when list contains one item
* j) pop of head when list empty
* k) deletion of head when list not empty
* l) deletion of head when list contains one item
* m) deletion of head when list empty
* n) head when list not empty
* o) head when list is empty
* p) next when not last
* q) next when last
* r) prev when not first
* s) prev when first
*
* NB: unlike single link stuff, cannot attempt to remove item which is
* not on the list !
*/
typedef struct sdl_test* sdl_test ;
struct sdl_test
{
struct dl_list_pair(sdl_test) list ;
unsigned majic ;
char dummy ;
struct dl_list_pair(sdl_test) other_list ;
};
struct sdl_test_parent
{
long rubbish ;
char fred ;
sdl_test base ;
int z[7] ;
} ;
static struct sdl_test_parent sdl_parent ;
static void
test_sdl(void)
{
sdl_test base = NULL ;
sdl_test del = NULL ;
sdl_test other_del = NULL ;
sdl_test last = NULL ;
sdl_test first = NULL ;
struct sdl_test dummy ;
int n = 57 ;
int i_del = 9 ; /* NB: neither of these may be 0 or 1 */
int i_other_del = 49 ;
sdl_test prev ;
sdl_test this ;
sdl_test other_this ;
sdl_test take ;
sdl_test temp ;
int i ;
static struct sdl_test_parent* other = &sdl_parent ;
memset(other, 99, sizeof(struct sdl_test_parent)) ;
other->base = NULL ;
/* Repeated insertion, starting from empty list
*
* a) adding when list is empty
* b) adding when list is not empty
*
* Creates lists for following tests.
*/
printf("=== Testing sdl -- Single Base, Double Link -- stuff\n") ;
printf(" Creating list of items") ;
for (i = 1 ; i <= n ; ++i)
{
sdl_test this = calloc(1, sizeof(struct sdl_test)) ;
if (last == NULL)
last = this ;
this->majic = majic(this, &base) ;
this->dummy = i ;
sdl_push(base, this, list) ;
if (i & 1)
sdl_push(other->base, this, other_list) ;
else
sdl_push(sdl_parent.base, this, other_list) ;
if (i == i_del)
del = this ;
if (i == i_other_del)
other_del = this ;
first = this ;
printf("+") ;
} ;
test_assert((base == first) && (other->base == first),
"Failed to create consistent lists") ;
printf("\n") ;
passert((del != base) && (del != last)) ;
passert((other_del != base) && (other_del != last)) ;
/* Walk to check that have the expected items
*
* n) head when list not empty
* p) next when not last
* q) next when last
* r) prev when not first
* s) prev when first
*/
printf(" Walking list of items") ;
this = sdl_head(base) ;
test_assert(this == first, "sdl_head failed") ;
this = sdl_head(other->base) ;
test_assert(this == first, "sdl_head failed") ;
this = sdl_head(sdl_parent.base) ;
test_assert(this == first, "sdl_head failed") ;
/* next on both lists */
this = sdl_next(first, list) ;
test_assert((this == first->list.next) && (this != NULL),
"sdl_next failed at start") ;
this = sdl_next(del, list) ;
test_assert((this == del->list.next) && (this != NULL), "sdl_next failed") ;
this = sdl_next(last, list) ;
test_assert((this == last->list.next) && (this == NULL),
"sdl_next failed at end") ;
this = sdl_next(first, other_list) ;
test_assert((this == first->other_list.next) && (this != NULL),
"sdl_next failed at start") ;
this = sdl_next(other_del, other_list) ;
test_assert((this == other_del->other_list.next) && (this != NULL),
"sdl_next failed") ;
this = sdl_next(last, other_list) ;
test_assert((this == last->other_list.next) && (this == NULL),
"sdl_next failed at end") ;
/* prev on both lists */
this = sdl_prev(first, list) ;
test_assert((this == first->list.prev) && (this == NULL),
"sdl_prev failed at start") ;
this = sdl_prev(del, list) ;
test_assert((this == del->list.prev) && (this != NULL), "sdl_prev failed") ;
this = sdl_prev(last, list) ;
test_assert((this == last->list.prev) && (this != NULL),
"sdl_prev failed at end") ;
this = sdl_prev(first, other_list) ;
test_assert((this == first->other_list.prev) && (this == NULL),
"sdl_prev failed at start") ;
this = sdl_prev(other_del, other_list) ;
test_assert((this == other_del->other_list.prev) && (this != NULL),
"sdl_prev failed") ;
this = sdl_prev(last, other_list) ;
test_assert((this == last->other_list.prev) && (this != NULL),
"sdl_prev failed at end") ;
this = base ;
other_this = other->base ;
prev = NULL ;
i = n ;
while (1)
{
test_assert(this == other_this, "this and other_this not in step") ;
if (this == NULL)
break ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf(".") ;
this = sdl_next(this, list) ;
other_this = sdl_next(other_this, other_list) ;
} ;
printf("\n") ;
/* Deletion specifically at the start of the list
*
* c) deletion of first item and more than one item on list
*/
printf(" Deleting the first item") ;
this = base ;
first = base->list.next ;
sdl_del(base, this, list) ;
test_assert(first == base, "sdl_del of first item failed") ;
test_assert((base == NULL) || (base->list.prev == NULL), "sdl_del failed") ;
this = other->base ;
sdl_del(sdl_parent.base, this, other_list) ;
test_assert(first == other->base, "sdl_del of first item failed") ;
test_assert((base == NULL) || (base->other_list.prev == NULL),
"sdl_del failed") ;
printf("\n") ;
--n ; /* one less on the lists ! */
/* Deletion of items from arbitrary place in list
*
* e) deletion of arbitrary item (list implicitly not empty)
*/
printf(" Deleting arbitrary items") ;
sdl_del(base, del, list) ;
test_assert((base == NULL) || (base->list.prev == NULL), "sdl_del failed") ;
sdl_del(sdl_parent.base, other_del, other_list) ;
test_assert((base == NULL) || (base->other_list.prev == NULL),
"sdl_del failed") ;
printf("\n") ;
/* Deletion of NULL items
*
* f) deletion of NULL item and list not empty
*/
printf(" Deleting NULL items") ;
this = NULL ;
sdl_del(base, this, list) ;
test_assert((base == NULL) || (base->list.prev == NULL), "sdl_del failed") ;
sdl_del(other->base, this, other_list) ;
test_assert((base == NULL) || (base->other_list.prev == NULL),
"sdl_del failed") ;
printf("\n") ;
/* Scan lists to check after deletion */
printf(" Base list scan") ;
this = base ;
prev = NULL ;
i = n ;
while (1)
{
if (this == NULL)
break ;
test_assert(this != prev, "this is same as prev") ;
test_assert(this->list.prev == prev, "broken prev pointer") ;
if (i == i_del)
--i ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("*") ;
prev = this ;
this = sdl_next(this, list) ;
test_assert(this == prev->list.next, "broken sdl_next") ;
if (this != NULL)
test_assert(prev == sdl_prev(this, list), "broken sdl_prev") ;
} ;
printf("\n") ;
printf(" Other list scan") ;
this = other->base ;
prev = NULL ;
i = n ;
while (1)
{
if (this == NULL)
break ;
test_assert(this != prev, "this is same as prev") ;
test_assert(this->other_list.prev == prev, "broken prev pointer") ;
if (i == i_other_del)
--i ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("*") ;
prev = this ;
this = sdl_next(this, other_list) ;
test_assert(this == prev->other_list.next, "broken sdl_next") ;
if (this != NULL)
test_assert(prev == sdl_prev(this, other_list), "broken sdl_prev") ;
} ;
printf("\n") ;
/* Dismantle lists
*
* h) pop of head when list not empty
* i) pop of head when list contains one item
* j) pop of head when list empty
* o) head when list is empty
*/
printf(" Popping the head until list is empty\n") ;
printf(" Base list") ;
prev = NULL ;
i = n ;
while (1)
{
this = sdl_head(base) ;
test_assert(this == base, "broken sdl_head !") ;
temp = sdl_pop(&take, base, list) ;
test_assert(this == take, "take is not same as deleted head !") ;
test_assert(this == temp, "temp is not same as deleted head !") ;
if (base != NULL)
test_assert(base->list.prev == NULL, "sdl_pop failed") ;
if (this == NULL)
break ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
if (i == i_del)
--i ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("-") ;
} ;
test_assert(i == 0, "not the expected final value of 'i'") ;
test_assert(base == NULL, "Base list should be empty") ;
this = sdl_head(base) ;
test_assert(this == NULL, "sdl_head of empty list failed") ;
printf("\n") ;
printf(" Other list") ;
prev = NULL ;
i = n ;
while (1)
{
this = sdl_head(other->base) ;
test_assert(this == other->base, "broken sdl_head !") ;
if (i & 1)
temp = sdl_pop(&take, sdl_parent.base, other_list) ;
else
temp = sdl_pop(&take, other->base, other_list) ;
test_assert(this == take, "take is not same as deleted head !") ;
test_assert(this == temp, "temp is not same as deleted head !") ;
if (other->base != NULL)
test_assert(other->base->other_list.prev == NULL,
"sdl_pop failed") ;
if (this == NULL)
break ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
if (i == i_other_del)
--i ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("-") ;
} ;
test_assert(i == 0, "not the expected final value of 'i'") ;
test_assert(other->base == NULL, "Other list should be empty") ;
this = sdl_head(other->base) ;
test_assert(this == NULL, "sdl_head of empty list failed") ;
printf("\n") ;
/* Rebuild lists to do:
*
* k) deletion of head when list not empty
* l) deletion of head when list contains one item
* m) deletion of head when list empty
*/
passert((base == NULL) && (other->base == NULL)) ;
last = NULL ;
first = NULL ;
prev = NULL ;
printf(" Building list of items again") ;
for (i = 1 ; i <= n ; ++i)
{
sdl_test this = calloc(1, sizeof(struct sdl_test)) ;
if (last == NULL)
last = this ;
this->majic = majic(this, &base) ;
this->dummy = i ;
sdl_push(base, this, list) ;
if (i & 1)
sdl_push(sdl_parent.base, this, other_list) ;
else
sdl_push(other->base, this, other_list) ;
test_assert(this->list.next == prev, "broken sdl_push") ;
test_assert(this->other_list.next == prev, "broken sdl_push") ;
test_assert(base == this, "broken sdl_push") ;
test_assert(other->base == this, "broken sdl_push") ;
first = this ;
prev = this ;
printf("+") ;
} ;
printf("\n") ;
printf(" Deleting the head until list is empty\n") ;
printf(" Base list") ;
prev = NULL ;
i = n ;
while (1)
{
this = base ;
sdl_del_head(base, list) ;
if (this == NULL)
break ;
test_assert(base == this->list.next, "sdl_del_head broken") ;
if (base != NULL)
test_assert(base->list.prev == NULL, "sdl_del_head broken") ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("-") ;
} ;
test_assert(i == 0, "not the expected final value of 'i'") ;
test_assert(base == NULL, "Base list should be empty") ;
printf("\n") ;
printf(" Other list") ;
prev = NULL ;
i = n ;
while (1)
{
this = other->base ;
if (i & 1)
sdl_del_head(other->base, other_list) ;
else
sdl_del_head(sdl_parent.base, other_list) ;
if (this == NULL)
break ;
test_assert(other->base == this->other_list.next, "sdl_del_head broken") ;
if (other->base != NULL)
test_assert(other->base->other_list.prev == NULL,
"sdl_del_head broken") ;
test_assert(this != prev, "this is same as prev") ;
prev = this ;
test_assert(this->dummy == i--, "don't have the right dummy") ;
test_assert(this->majic == majic(this, &base),
"don't have the right majic") ;
printf("-") ;
} ;
test_assert(i == 0, "not the expected final value of 'i'") ;
test_assert(other->base == NULL, "Other list should be empty") ;
printf("\n") ;
/* Final few tests
*
* d) deletion of first item when only one item on the list
* g) deletion of NULL item and list empty
*/
/* Deletion of items from arbitrary place in list */
printf(" Miscellaneous") ;
del->list.next = &dummy ;
sdl_push(base, del, list) ;
test_assert((base == del) && (del->list.next == NULL), "sdl_push failed ??") ;
sdl_del(base, del, list) ;
test_assert(base == NULL, "sdl_del of first and only item failed") ;
other_del->other_list.next = &dummy ;
sdl_push(other->base, other_del, other_list) ;
test_assert((other->base == other_del) && (other_del->other_list.next == NULL),
"sdl_push failed ??") ;
sdl_del(other->base, other_del, other_list) ;
test_assert(other->base == NULL, "sdl_del of first and only item failed") ;
this = NULL ;
sdl_del(base, this, list) ;
test_assert(base == NULL, "sdl_del of NULL item with empty list") ;
sdl_del(other->base, this, other_list) ;
test_assert(other->base == NULL, "sdl_del of NULL item with empty list") ;
printf("\n") ;
} ;
/*==============================================================================
* Double Base, Double Link
*
* ddl_init(base) -- initialise base
* ddl_push(base, item, list) -- insert at head of list
* ddl_append(base, item, list) -- insert at tail of list
* ddl_in_after(after, base, item, list) -- insert after
* ddl_in_before(before, base, item, list) -- insert before
* ddl_pop(&dst, base, next) -- pop head of list, if any
* ddl_crop(&dst, base, next) -- crop tail of list, if any
* ddl_del(base, item, list) -- delete from list
* ddl_del_head(base, next) -- delete head of list
* ddl_del_tail(base, next) -- delete tail of list
* ddl_head(base) -- return head of list
* ddl_tail(base) -- return tail of list
* ddl_next(item, next) -- step to next item, if any
* ddl_prev(item, next) -- step to prev item, if any
*
* Cases to cover:
*/
/* Testing runs two lists through struct ddt_item objects. */
enum list { a_list, b_list, list_count } ;
typedef struct ddt_item* ddt_item ;
struct ddt_list_pair dl_list_pair(ddt_item) ; /* Example struct constructor */
struct ddt_base_pair dl_base_pair(ddt_item) ;
typedef struct dl_base_pair(ddt_item) ddt_base_pair_t ;
/* Example typedef constructor */
typedef struct dl_base_pair(ddt_item)* p_ddt_base_pair ;
/* Example typedef constructor */
typedef struct ddt_list_pair* ddt_list_pair ;
typedef struct ddt_base_pair* ddt_base_pair ;
typedef struct ddt_rank* ddt_rank ;
struct ddt_rank /* information for each list */
{
struct ddt_list_pair list ; /* the thing we are testing */
int lister ;
int list_found ;
unsigned majic ;
int ordinal ;
};
struct ddt_item /* the test items */
{
struct ddt_rank a ;
char a_rubbish[21] ;
struct ddt_rank b ;
char b_rubbish[19] ;
} ;
/* The test list base pairs, and pointers to the actual bases, for use in
* the verification code.
*/
static ddt_base_pair ddt_bases[list_count] ;
/* For some tests want to construct lists and know the first, last and
* somewhere between items.
*/
enum where { first, middle, last, where_count } ;
struct ddt_test_list_items
{
struct
{
ddt_item where[where_count] ;
} list[list_count] ;
} ;
/*------------------------------------------------------------------------------
* The test list items -- keep track here also for use in verification.
*/
enum { ddt_max_items = 1000 } ;
static unsigned ddt_item_count = 0 ;
static unsigned ddt_item_alloc = 0 ;
static ddt_item ddt_items[ddt_max_items] ;
static inline ddt_item
ddt_new_item(void)
{
ddt_item item ;
assert(ddt_item_count <= ddt_item_alloc) ;
if (ddt_item_count == ddt_item_alloc)
{
assert(ddt_item_alloc < ddt_max_items) ;
ddt_items[ddt_item_alloc++] = malloc(sizeof(struct ddt_item)) ;
} ;
item = ddt_items[ddt_item_count++] ;
memset(item, ddt_item_count & 0xFF, sizeof(struct ddt_item)) ;
item->a.lister = 0 ;
item->b.lister = 0 ;
item->a.ordinal = 0 ;
item->b.ordinal = 0 ;
return item ;
} ;
/*------------------------------------------------------------------------------
* Given an item and a list ordinal, return pointer to "rank" for item.
*/
static inline ddt_rank
ddt_get_rank(ddt_item item, enum list l)
{
if (item == NULL)
return NULL ;
if (l == a_list)
return &item->a ;
if (l == b_list)
return &item->b ;
assert(0) ;
} ;
/*------------------------------------------------------------------------------
* Keep track of what should be found on the lists, and majic marker to check
* that don't get lost and point into space.
*/
static inline unsigned
ddt_get_majic(ddt_item item, enum list l)
{
return majic(item, ddt_bases[l]) ;
} ;
static void
ddt_test_list_add(ddt_item item, enum list l)
{
ddt_rank rank = ddt_get_rank(item, l) ;
test_assert(rank->lister == 0, "already on list") ;
rank->lister = 1 ;
rank->majic = ddt_get_majic(item, l) ;
rank->ordinal = 0 ; /* unknown ordinal */
} ;
static void
ddt_test_list_del(ddt_item item, enum list l)
{
ddt_rank rank ;
if (item == NULL)
return ;
rank = ddt_get_rank(item, l) ;
test_assert(rank->lister == 1, "not on list") ;
rank->lister = 0 ;
rank->majic = 0 ;
} ;
/*------------------------------------------------------------------------------
* Verification code.
*
* Blunt instrument to check that all known lists are valid. Checks:
*
* * bases are both NULL together, or both not NULL.
*
* * first and last items on the list have suitable prev/next pointers.
*
* * walk list to confirm, for each item:
*
* -- prev pointer valid for each item
* -- item majic is correct (so not pointing somewhere invalid)
* -- item is supposed to be on the list
* -- item has not already been seen on list (list bent)
* -- ordinal, if not zero, is bigger than any previous non-zero ordinal
*
* * last item visited on walk is what the tail points to
*
* * for any items which are supposed to be on list, but were not found
*/
static void
ddt_verify_lists(void)
{
ddt_base_pair base ;
ddt_rank r ;
ddt_item this ;
ddt_item prev ;
int l ;
int i ;
/* Wash the found flags */
for (l = 0 ; l < list_count ; ++l)
for (i = 0 ; i < (int)ddt_item_count ; ++i)
ddt_get_rank(ddt_items[i], l)->list_found = 0 ;
/* Walk the lists */
for (l = 0 ; l < list_count ; ++l)
{
int ordinal = 0 ;
base = ddt_bases[l] ;
if (base == NULL)
continue ;
if ((base->head == NULL) || (base->tail == NULL))
test_assert(base->head == base->tail, "broken list bases") ;
else
{
r = ddt_get_rank(base->head, l) ;
test_assert(r->list.prev == NULL, "broken list first item->prev") ;
r = ddt_get_rank(base->tail, l) ;
test_assert(r->list.next == NULL, "broken list last item->next") ;
this = base->head ;
prev = NULL ;
while (this != NULL)
{
r = ddt_get_rank(this, l) ;
test_assert(r->list.prev == prev, "broken item->prev") ;
test_assert(r->lister, "should not be on this list") ;
test_assert(!r->list_found, "circular list") ;
r->list_found = 1 ;
if (r->ordinal != 0)
{
test_assert(r->ordinal > ordinal, "list out of order") ;
ordinal = r->ordinal ;
}
test_assert(r->majic == ddt_get_majic(this, l),
"wrong sort of majic") ;
prev = this ;
this = r->list.next ;
} ;
test_assert(base->tail == prev, "broken tail pointer") ;
} ;
} ;
/* Verify that did not miss anything should have found */
/* Wash the found flags */
for (l = 0 ; l < list_count ; ++l)
for (i = 0 ; i < (int)ddt_item_count ; ++i)
{
r = ddt_get_rank(ddt_items[i], l) ;
if (r->lister)
test_assert(r->list_found, "should have found this on list") ;
} ;
} ;
/*------------------------------------------------------------------------------
* Reset the test list handling
*
*/
static void
ddt_reset_lists(void)
{
int l ;
for (l = 0 ; l < list_count ; ++l)
ddl_init(*(ddt_bases[l])) ;
ddt_item_count = 0 ;
} ;
/*------------------------------------------------------------------------------
* Make lists with 'n' items each.
*
* If 'n' 0..3, makes lists with exactly that many items.
*
* Otherwise, list length has +/- 25% jitter.
*/
static void
ddt_test_make_lists(struct ddt_test_list_items* test, int n)
{
ddt_base_pair base ;
ddt_item item ;
ddt_rank rank ;
enum list l ;
int t ;
int m ;
int req[list_count] ;
int mid[list_count] ;
/* Capture the requirements */
t = 0 ;
m = 1 ;
for (l = 0 ; l < list_count ; ++l)
{
m += m ;
int j = (n + 1) / 4 ;
if (n <= 3)
req[l] = n ;
else
req[l] = n - j + (rand() % (j + j + 1)) ;
mid[l] = req[l] / 2 ;
t += req[l] ;
test->list[l].where[first] = NULL ;
test->list[l].where[middle] = NULL ;
test->list[l].where[last] = NULL ;
} ;
ddt_reset_lists() ;
/* Have t = total number of items still required
* m = 2^n -- where there are 'n' lists
*/
while (t != 0)
{
int b ;
int r ;
r = (rand() % (m - 1)) + 1 ; /* bit pattern, at least one set */
item = ddt_new_item() ;
b = 1 ;
for (l = 0 ; l < list_count ; ++l)
{
if ((req[l] != 0) && ((r & b) != 0))
{
--req[l] ;
--t ;
ddt_test_list_add(item, l) ;
if (test->list[l].where[first] == NULL)
test->list[l].where[first] = item ;
if (mid[l]-- == 0)
test->list[l].where[middle] = item ;
test->list[l].where[last] = item ;
base = ddt_bases[l] ;
rank = ddt_get_rank(item, l) ;
if (base->head == NULL)
{
base->head = item ;
base->tail = item ;
rank->list.next = NULL ;
rank->list.prev = NULL ;
}
else if (rand() & 1)
{
rank->list.next = base->head ;
rank->list.prev = NULL ;
ddt_get_rank(base->head, l)->list.prev = item ;
base->head = item ;
}
else
{
rank->list.next = NULL ;
rank->list.prev = base->tail ;
ddt_get_rank(base->tail, l)->list.next = item ;
base->tail = item ;
}
} ;
b <<= 1 ;
}
} ;
/* Number the items */
for (l = 0 ; l < list_count ; ++l)
{
int o = 0 ;
base = ddt_bases[l] ;
item = base->head ;
while (item != NULL)
{
rank = ddt_get_rank(item, l) ;
rank->ordinal = ++o ; /* first is 1 */
item = rank->list.next ;
} ;
} ;
ddt_verify_lists() ;
} ;
/*------------------------------------------------------------------------------
* ddl_init(base) -- initialise base
* ddl_push(base, item, list) -- insert at head of list
* ddl_append(base, item, list) -- insert at tail of list
* ddl_in_after(after, base, item, list) -- insert after
* ddl_in_before(before, base, item, list) -- insert before
* ddl_pop(&dst, base, next) -- pop head of list, if any
* ddl_crop(&dst, base, next) -- crop tail of list, if any
* ddl_del(base, item, list) -- delete from list
* ddl_del_head(base, next) -- delete head of list
* ddl_del_tail(base, next) -- delete tail of list
* ddl_head(base) -- return head of list
* ddl_tail(base) -- return tail of list
* ddl_next(item, next) -- step to next item, if any
* ddl_prev(item, next) -- step to prev item, if any
*/
static struct ddt_parent
{
char zlxq[37] ;
struct ddt_base_pair base ;
char qxlz[45] ;
} ddt_parent ;
static void
test_ddl(void)
{
struct ddt_base_pair a_base ;
struct ddt_parent* b ;
struct ddt_test_list_items test ;
int n ;
int o ;
int base_n = 23 ;
int rand_n = 17 ;
printf("=== Testing ddl -- Double Base, Double Link -- stuff\n") ;
/* Initialise the test support */
ddt_bases[a_list] = &a_base ;
ddt_bases[b_list] = &ddt_parent.base ;
ddt_item_count = 0 ;
ddt_item_alloc = 0 ;
/* Initialise the list bases */
b = &ddt_parent ;
memset(b, 42, sizeof(struct ddt_parent)) ;
ddl_init(a_base) ;
ddl_init(b->base) ;
ddt_verify_lists() ; /* start as mean to go on */
/* ddl_push(base, item, list) -- insert at head of list
*
* Cases: (a) empty list
* (b) list with one item
* (c) list with multiple items
*/
printf(" ddl_push test") ;
ddt_reset_lists() ;
n = base_n + (rand() % rand_n) ;
while (n)
{
ddt_item item ;
int r ;
printf(".") ;
item = ddt_new_item() ;
r = (rand() % 3) + 1 ;
if (r & 1)
{
ddl_push(a_base, item, a.list) ;
test_assert(a_base.head == item, "ddl_push broken") ;
ddt_test_list_add(item, a_list) ;
item->a.ordinal = n ;
} ;
ddt_verify_lists() ;
if (r & 2)
{
ddl_push(b->base, item, b.list) ;
test_assert(b->base.head == item, "ddl_push broken") ;
ddt_test_list_add(item, b_list) ;
item->b.ordinal = n ;
} ;
ddt_verify_lists() ;
--n ;
} ;
printf("\n") ;
/* ddl_append(base, item, list) -- insert at tail of list
*
* Cases: (a) empty list
* (b) list with one item
* (c) list with multiple items
*/
printf(" ddl_append test") ;
ddt_reset_lists() ;
n = base_n + (rand() % rand_n) ;
o = 0 ;
while (n)
{
ddt_item item ;
int r ;
printf(".") ;
++o ;
item = ddt_new_item() ;
r = (rand() % 3) + 1 ;
if (r & 1)
{
ddl_append(a_base, item, a.list) ;
test_assert(a_base.tail == item, "ddl_append broken") ;
ddt_test_list_add(item, a_list) ;
item->a.ordinal = o ;
} ;
ddt_verify_lists() ;
if (r & 2)
{
ddl_append(b->base, item, b.list) ;
test_assert(b->base.tail == item, "ddl_append broken") ;
ddt_test_list_add(item, b_list) ;
item->b.ordinal = o ;
} ;
ddt_verify_lists() ;
--n ;
} ;
printf("\n") ;
/* ddl_in_after(after, base, item, list) -- insert after
*
* Cases: (a) after first and only (so is also last)
* (b) after first when more than one
* (c) after last when more than one
* (d) after something between
*/
printf(" ddl_in_after test") ;
n = base_n + (rand() % rand_n) ;
while (n)
{
ddt_item item ;
ddt_item after ;
printf(".") ;
ddt_test_make_lists(&test, n) ;
item = ddt_new_item() ;
after = test.list[a_list].where[n % where_count] ;
ddl_in_after(after, a_base, item, a.list) ;
test_assert(after->a.list.next == item, "ddl_in_after broken") ;
test_assert(item->a.list.prev == after, "ddl_in_after broken") ;
ddt_test_list_add(item, a_list) ;
ddt_verify_lists() ;
item = ddt_new_item() ;
after = test.list[b_list].where[n % where_count] ;
ddl_in_after(after, b->base, item, b.list) ;
test_assert(after->b.list.next == item, "ddl_in_after broken") ;
test_assert(item->b.list.prev == after, "ddl_in_after broken") ;
ddt_test_list_add(item, b_list) ;
ddt_verify_lists() ;
--n ;
} ;
printf("\n") ;
/* ddl_in_before(before, base, item, list) -- insert before
*
* Cases: (a) before first and only (so is also last)
* (b) before first when more than one
* (c) before last when more than one
* (d) before something between
*/
printf(" ddl_in_before test") ;
n = base_n + (rand() % rand_n) ;
while (n)
{
ddt_item item ;
ddt_item before ;
printf(".") ;
ddt_test_make_lists(&test, n) ;
item = ddt_new_item() ;
before = test.list[a_list].where[n % where_count] ;
ddl_in_before(before, a_base, item, a.list) ;
test_assert(before->a.list.prev == item, "ddl_in_before broken") ;
test_assert(item->a.list.next == before, "ddl_in_before broken") ;
ddt_test_list_add(item, a_list) ;
ddt_verify_lists() ;
item = ddt_new_item() ;
before = test.list[b_list].where[n % where_count] ;
ddl_in_before(before, b->base, item, b.list) ;
test_assert(before->b.list.prev == item, "ddl_in_before broken") ;
test_assert(item->b.list.next == before, "ddl_in_before broken") ;
ddt_test_list_add(item, b_list) ;
ddt_verify_lists() ;
--n ;
} ;
printf("\n") ;
/* ddl_pop(&dst, base, next) -- pop head of list, if any
*
* Cases: (a) list with more than one item
* (b) list with one item
* (c) empty list
*/
printf(" ddl_pop test") ;
n = base_n + (rand() % rand_n) ;
while (n >= 0)
{
ddt_item item ;
ddt_item temp ;
ddt_item peek ;
int ordinal ;
printf(".") ;
ddt_test_make_lists(&test, n) ;
ordinal = 0 ;
while (1)
{
peek = a_base.head ;
temp = ddl_pop(&item, a_base, a.list) ;
test_assert(temp == item, "ddl_pop broken") ;
test_assert(peek == item, "ddl_pop broken") ;
ddt_test_list_del(item, a_list) ;
ddt_verify_lists() ;
if (item == NULL)
break ;
++ordinal ;
test_assert(item->a.ordinal == ordinal, "ddl_pop not in order") ;
} ;
ordinal = 0 ;
while (1)
{
peek = b->base.head ;
temp = ddl_pop(&item, b->base, b.list) ;
test_assert(temp == item, "ddl_pop broken") ;
test_assert(peek == item, "ddl_pop broken") ;
ddt_test_list_del(item, b_list) ;
ddt_verify_lists() ;
if (item == NULL)
break ;
++ordinal ;
test_assert(item->b.ordinal == ordinal, "ddl_pop not in order") ;
} ;
--n ;
} ;
printf("\n") ;
/* ddl_crop(&dst, base, next) -- crop tail of list, if any
*
* Cases: (a) list with more than one item
* (b) list with one item
* (c) empty list
*/
printf(" ddl_crop test") ;
n = base_n + (rand() % rand_n) ;
while (n >= 0)
{
ddt_item item ;
ddt_item temp ;
ddt_item peek ;
int ordinal ;
printf(".") ;
ddt_test_make_lists(&test, n) ;
ordinal = 0 ;
while (1)
{
peek = a_base.tail ;
temp = ddl_crop(&item, a_base, a.list) ;
test_assert(temp == item, "ddl_crop broken") ;
test_assert(peek == item, "ddl_crop broken") ;
ddt_test_list_del(item, a_list) ;
ddt_verify_lists() ;
if (item == NULL)
break ;
if (ordinal == 0)
ordinal = item->a.ordinal ;
else
{
test_assert(ordinal > 0, "ordinal out of whack") ;
--ordinal ;
test_assert(item->a.ordinal == ordinal, "ddl_crop not in order") ;
} ;
} ;
ordinal = 0 ;
while (1)
{
peek = b->base.tail ;
temp = ddl_crop(&item, b->base, b.list) ;
test_assert(temp == item, "ddl_crop broken") ;
test_assert(peek == item, "ddl_crop broken") ;
ddt_test_list_del(item, b_list) ;
ddt_verify_lists() ;
if (item == NULL)
break ;
if (ordinal == 0)
ordinal = item->b.ordinal ;
else
{
test_assert(ordinal > 0, "ordinal out of whack") ;
--ordinal ;
test_assert(item->b.ordinal == ordinal, "ddl_crop not in order") ;
} ;
} ;
--n ;
} ;
printf("\n") ;
/* ddl_del(base, item, list) -- delete from list
*
* Cases: (a) first and only (so is also last)
* (b) first when more than one
* (c) last when more than one
* (d) something between
*/
printf(" ddl_del test") ;
n = base_n + (rand() % rand_n) ;
while (n)
{
ddt_item item ;
printf(".") ;
ddt_test_make_lists(&test, n) ;
item = test.list[a_list].where[n % where_count] ;
ddl_del(a_base, item, a.list) ;
ddt_test_list_del(item, a_list) ;
ddt_verify_lists() ;
item = test.list[b_list].where[n % where_count] ;
ddl_del(b->base, item, b.list) ;
ddt_test_list_del(item, b_list) ;
ddt_verify_lists() ;
--n ;
} ;
printf("\n") ;
/* ddl_del_head(base, next) -- delete head of list
*
* Cases: (a) list with more than one item
* (b) list with one item
* (c) empty list
*/
printf(" ddl_del_head test") ;
n = base_n + (rand() % rand_n) ;
while (n >= 0)
{
ddt_item item ;
ddt_item peek ;
printf(".") ;
ddt_test_make_lists(&test, n) ;
while (1)
{
item = a_base.head ;
peek = (item != NULL) ? item->a.list.next : NULL ;
ddl_del_head(a_base, a.list) ;
test_assert(a_base.head == peek, "ddl_del_head broken") ;
ddt_test_list_del(item, a_list) ;
ddt_verify_lists() ;
if (item == NULL)
break ;
} ;
while (1)
{
item = b->base.head ;
peek = (item != NULL) ? item->b.list.next : NULL ;
ddl_del_head(b->base, b.list) ;
test_assert(b->base.head == peek, "ddl_del_head broken") ;
ddt_test_list_del(item, b_list) ;
ddt_verify_lists() ;
if (item == NULL)
break ;
} ;
--n ;
} ;
printf("\n") ;
/* ddl_del_tail(base, next) -- delete tail of list
*
* Cases: (a) list with more than one item
* (b) list with one item
* (c) empty list
*/
printf(" ddl_del_tail test") ;
n = base_n + (rand() % rand_n) ;
while (n >= 0)
{
ddt_item item ;
ddt_item peek ;
printf(".") ;
ddt_test_make_lists(&test, n) ;
while (1)
{
item = a_base.tail ;
peek = (item != NULL) ? item->a.list.prev : NULL ;
ddl_del_tail(a_base, a.list) ;
test_assert(a_base.tail == peek, "ddl_del_tail broken") ;
ddt_test_list_del(item, a_list) ;
ddt_verify_lists() ;
if (item == NULL)
break ;
} ;
while (1)
{
item = b->base.tail ;
peek = (item != NULL) ? item->b.list.prev : NULL ;
ddl_del_tail(b->base, b.list) ;
test_assert(b->base.tail == peek, "ddl_del_tail broken") ;
ddt_test_list_del(item, b_list) ;
ddt_verify_lists() ;
if (item == NULL)
break ;
} ;
--n ;
} ;
printf("\n") ;
/* ddl_head(base) -- return head of list
* ddl_tail(base) -- return tail of list
*
* Cases: (a) list with more than one item
* (b) list with one item
* (c) empty list
*/
printf(" ddl_head & ddl_tail test") ;
n = base_n + (rand() % rand_n) ;
while (n >= 0)
{
printf(".") ;
ddt_test_make_lists(&test, n) ;
test_assert(ddl_head(a_base) == a_base.head, "ddl_head broken") ;
test_assert(ddl_tail(a_base) == a_base.tail, "ddl_head broken") ;
test_assert(ddl_head(b->base) == b->base.head, "ddl_head broken") ;
test_assert(ddl_tail(b->base) == b->base.tail, "ddl_head broken") ;
--n ;
} ;
printf("\n") ;
/* ddl_next(item, next) -- step to next item, if any
* ddl_prev(item, next) -- step to prev item, if any
*
* Cases: (a) at first and only (so is also last)
* (b) at first when more than one
* (c) at last when more than one
* (d) at something between
*/
printf(" ddl_next and ddl_prev test") ;
n = base_n + (rand() % rand_n) ;
while (n)
{
ddt_item item ;
ddt_item where ;
printf(".") ;
ddt_test_make_lists(&test, n) ;
where = test.list[a_list].where[n % where_count] ;
item = ddl_next(where, a.list) ;
test_assert(item == where->a.list.next, "ddl_next broken") ;
where = test.list[b_list].where[n % where_count] ;
item = ddl_next(where, b.list) ;
test_assert(item == where->b.list.next, "ddl_next broken") ;
where = test.list[a_list].where[n % where_count] ;
item = ddl_prev(where, a.list) ;
test_assert(item == where->a.list.prev, "ddl_prev broken") ;
where = test.list[b_list].where[n % where_count] ;
item = ddl_prev(where, b.list) ;
test_assert(item == where->b.list.prev, "ddl_prev broken") ;
--n ;
} ;
printf("\n") ;
}
/*
* TODO
*
*/
|