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
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#![allow(
dead_code,
unused_imports,
clippy::unnecessary_safety_comment,
reason = "residue of Rc-test removal: orphaned helpers/imports kept to preserve surrounding test bodies verbatim"
)]
//! Tests for the [`Arena`] type itself: constructors, builder, stats,
//! cache behavior, byte budget, preallocation.
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std for thread/sync primitives")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain smart pointers to keep chunks alive")]
#![allow(clippy::manual_assert, reason = "explicit panic clarifies safety-net intent")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded indices fit in u32")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "explicit borrows clarify intent in tests")]
use multitude::Arena;
mod common;
#[test]
fn allocator_accessor() {
let arena = Arena::new();
let _: &allocator_api2::alloc::Global = arena.allocator();
}
// `ChunkSizeOutOfRange` was removed from `BuildError` along with the
// `chunk_size` builder knob; the adaptive ramp manages chunk sizes
// itself. The previous boundary tests (chunk_size below min, chunk_size
// above CHUNK_ALIGN) are no longer reachable via the public API and
// have been deleted.
#[test]
#[should_panic(expected = "max_normal_alloc must be in")]
fn builder_max_normal_alloc_zero_rejected() {
let _ = Arena::builder().max_normal_alloc(0).try_build();
}
#[test]
#[should_panic(expected = "max_normal_alloc must be in")]
fn builder_max_normal_alloc_below_min_rejected() {
// Anything below the 4 KiB floor is rejected.
let _ = Arena::builder().max_normal_alloc(2048).try_build();
}
#[test]
#[should_panic(expected = "max_normal_alloc must be in")]
fn builder_max_normal_alloc_too_large_rejected() {
let _ = Arena::builder().max_normal_alloc(128 * 1024).try_build();
}
#[test]
fn builder_with_capacity_too_small_rejected() {
let result = std::panic::catch_unwind(|| Arena::builder().with_capacity(256).try_build());
assert!(result.is_err(), "with_capacity(256) must panic (below MIN_CHUNK_BYTES = 512)");
}
#[test]
fn try_alloc_str_returns_mutable_str() {
let arena = Arena::new();
let mut s = arena.try_alloc_str("hello").unwrap();
s.make_ascii_uppercase();
assert_eq!(&*s, "HELLO");
}
#[test]
fn try_alloc_str_arc_returns_handle() {
let arena = Arena::new();
let s = arena.try_alloc_str_arc("arc").unwrap();
assert_eq!(&*s, "arc");
}
#[test]
fn try_alloc_str_accepts_string() {
// impl AsRef<str> covers both &str and String.
let arena = Arena::new();
let owned = std::string::String::from("from String");
let s = arena.try_alloc_str(owned).unwrap();
assert_eq!(&*s, "from String");
}
#[test]
fn try_alloc_string_with_capacity_succeeds() {
let arena = Arena::new();
let mut s = arena.try_alloc_string_with_capacity(64).unwrap();
s.push_str("preallocated");
assert!(s.capacity() >= 64);
assert_eq!(s.as_str(), "preallocated");
}
#[test]
fn try_alloc_string_with_capacity_zero_works() {
let arena = Arena::new();
let s = arena.try_alloc_string_with_capacity(0).unwrap();
assert_eq!(s.capacity(), 0);
assert_eq!(s.len(), 0);
}
#[test]
fn try_alloc_vec_with_capacity_succeeds() {
let arena = Arena::new();
let mut v = arena.try_alloc_vec_with_capacity::<u32>(50).unwrap();
for i in 0..50 {
v.push(i);
}
assert!(v.capacity() >= 50);
assert_eq!(v.len(), 50);
}
#[test]
fn try_alloc_vec_with_capacity_zero_works() {
let arena = Arena::new();
let v: multitude::vec::Vec<u8, _> = arena.try_alloc_vec_with_capacity(0).unwrap();
assert_eq!(v.capacity(), 0);
assert_eq!(v.len(), 0);
}
/// Size that forces an oversized one-shot chunk allocation (i.e. a
/// chunk whose total size exceeds `MAX_CHUNK_BYTES = 64 KiB`).
/// 65 KiB is the *minimum* size that triggers the oversized branch;
/// any larger value exercises the same code path, so use the minimum
/// to keep tests fast under Miri.
const OVERSIZED_BYTES: usize = 65 * 1024;
#[test]
fn oversized_bump_alloc_does_not_leak_on_drop() {
let alloc = common::TrackingAllocator::new();
// Heap-allocate the source so cargo-careful's instrumented build
// doesn't blow the stack on the large literal.
let src = vec![0_u8; OVERSIZED_BYTES];
{
let arena = Arena::builder_in(alloc.clone()).build();
// The source exceeds `MAX_CHUNK_BYTES` (64 KiB), so this
// allocation must take the oversized one-shot chunk path.
// Previously these chunks were leaked because they were never
// linked into current_*, the pinned list, or the cache.
let _slice = arena.alloc_slice_copy(&src);
assert!(alloc.live_chunks() >= 1);
}
assert_eq!(alloc.live_chunks(), 0, "arena drop must free all chunks");
assert_eq!(alloc.live_bytes(), 0);
}
#[test]
fn oversized_bump_alloc_does_not_leak_on_reset() {
let alloc = common::TrackingAllocator::new();
let src = vec![0_u8; OVERSIZED_BYTES];
let mut arena = Arena::builder_in(alloc.clone()).build();
let _ = arena.alloc_slice_copy(&src);
let after_alloc = alloc.live_chunks();
arena.reset();
assert!(
alloc.live_chunks() < after_alloc,
"reset must release oversized chunks (had {after_alloc}, now {})",
alloc.live_chunks()
);
drop(arena);
assert_eq!(alloc.live_chunks(), 0);
assert_eq!(alloc.live_bytes(), 0);
}
#[test]
fn oversized_alloc_with_does_not_leak() {
let alloc = common::TrackingAllocator::new();
{
let arena = Arena::builder_in(alloc.clone()).build();
// Force oversized via a large array.
let _r = arena.alloc_with(|| [0_u32; 8 * 1024]);
}
assert_eq!(alloc.live_chunks(), 0);
assert_eq!(alloc.live_bytes(), 0);
}
#[test]
fn oversized_slice_fill_with_does_not_leak() {
let alloc = common::TrackingAllocator::new();
{
let arena = Arena::builder_in(alloc.clone()).build();
let _slice = arena.alloc_slice_fill_with::<u32, _>(8 * 1024, |i| i as u32);
}
assert_eq!(alloc.live_chunks(), 0);
assert_eq!(alloc.live_bytes(), 0);
}
// Regression: a panic inside the user closure of `alloc_with` on a
// payload large enough to land on an oversized chunk used to leak the
// chunk because the pin/inc-ref happened only after the closure
// returned.
#[test]
fn panic_in_oversized_alloc_with_does_not_leak() {
use std::panic::{AssertUnwindSafe, catch_unwind};
let alloc = common::TrackingAllocator::new();
{
let arena = Arena::builder_in(alloc.clone()).build();
let result = catch_unwind(AssertUnwindSafe(|| {
let _r = arena.alloc_with(|| panic!("synthetic panic"));
}));
assert!(result.is_err());
}
assert_eq!(alloc.live_chunks(), 0);
assert_eq!(alloc.live_bytes(), 0);
}
// Mirror of `panic_in_normal_alloc_rc_with_does_not_leak` for the
// `Box` path of `ProtectiveHold` (same Box branch in
// `ProtectiveHold::drop`).
#[test]
fn panic_in_normal_alloc_box_with_does_not_leak() {
use std::panic::{AssertUnwindSafe, catch_unwind};
let alloc = common::TrackingAllocator::new();
{
let arena = Arena::builder_in(alloc.clone()).build();
let result = catch_unwind(AssertUnwindSafe(|| {
let _b: multitude::Box<u64, _> = arena.alloc_box_with(|| panic!("synthetic panic"));
}));
assert!(result.is_err());
}
assert_eq!(alloc.live_chunks(), 0);
assert_eq!(alloc.live_bytes(), 0);
}
// Kills mutants on the per-allocation surplus-handout decrement. Mirrors
// the `ProtectiveHold` test above but on the `Arc` allocation path.
#[test]
fn panic_in_normal_alloc_arc_with_does_not_leak() {
use std::panic::{AssertUnwindSafe, catch_unwind};
let alloc = common::SendTrackingAllocator::new();
{
let arena = Arena::builder_in(alloc.clone()).build();
let result = catch_unwind(AssertUnwindSafe(|| {
let _a: multitude::Arc<u64, _> = arena.alloc_arc_with(|| panic!("synthetic panic"));
}));
assert!(result.is_err());
}
assert_eq!(alloc.live_chunks(), 0);
assert_eq!(alloc.live_bytes(), 0);
}
// Kills mutants on `chunk_end_addr_fits_in_isize` (and the call-site
// negation) in `Chunk::allocate`. A pathological allocator that
// returns a pointer in the upper half of the address space must be
// rejected by the bounds check: the user-facing observable is a clean
// `AllocError` from `try_alloc_with` rather than a write through a
// kernel-space pointer.
#[test]
fn local_chunk_allocate_rejects_high_address_from_pathological_allocator() {
let arena = Arena::builder_in(common::BadAddressAllocator).build();
let result = arena.try_alloc_with(|| 0_u64);
assert!(result.is_err(), "high-address allocator must produce AllocError");
}
// Mirror for the `Arc` allocation path. Same pathological allocator;
// the regression covers the symmetric bounds check in `Chunk::allocate`.
#[test]
fn chunk_allocate_rejects_high_address_from_pathological_allocator() {
use allocator_api2::alloc::AllocError;
let arena = Arena::builder_in(common::BadAddressAllocator).build();
let result: Result<multitude::Arc<u64, _>, AllocError> = arena.try_alloc_arc_with(|| 0_u64);
assert!(result.is_err(), "high-address allocator must produce AllocError");
}
#[test]
fn try_alloc_slice_huge_len_returns_alloc_error() {
let arena: Arena = Arena::new();
let result = arena.try_alloc_slice_fill_with(usize::MAX / 4, |_| 0_u64);
assert!(result.is_err(), "expected AllocError for huge len");
}
#[test]
fn try_alloc_slice_clone_huge_len_returns_alloc_error() {
let arena: Arena = Arena::new();
let result = arena.try_alloc_slice_fill_with(usize::MAX, |_| 0_u64);
result.unwrap_err();
}
mod reset {
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
use core::sync::atomic::{AtomicUsize, Ordering};
use multitude::{Arc, Arena};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
#[test]
fn reset_idempotent() {
let mut arena: Arena = Arena::new();
arena.reset();
arena.reset();
arena.reset();
let _ = arena.alloc(0_u8);
arena.reset();
arena.reset();
}
#[test]
fn alloc_style_value_destructor_runs_when_handle_drops() {
static COUNT: AtomicUsize = AtomicUsize::new(0);
struct Tracked;
impl Drop for Tracked {
fn drop(&mut self) {
let _ = COUNT.fetch_add(1, Ordering::SeqCst);
}
}
COUNT.store(0, Ordering::SeqCst);
let mut arena = Arena::new();
{
let _v = arena.alloc(Tracked);
assert_eq!(COUNT.load(Ordering::SeqCst), 0, "drop hasn't fired before handle drops");
}
assert_eq!(COUNT.load(Ordering::SeqCst), 1, "destructor must run when handle drops");
arena.reset();
assert_eq!(COUNT.load(Ordering::SeqCst), 1, "reset must not drop the value again");
}
#[test]
fn alloc_style_values_drop_once_before_reset() {
static COUNT: AtomicUsize = AtomicUsize::new(0);
struct Tracked;
impl Drop for Tracked {
fn drop(&mut self) {
let _ = COUNT.fetch_add(1, Ordering::SeqCst);
}
}
COUNT.store(0, Ordering::SeqCst);
let mut arena = Arena::new();
for _ in 0..5 {
let _ = arena.alloc(Tracked);
}
assert_eq!(COUNT.load(Ordering::SeqCst), 5);
arena.reset();
assert_eq!(COUNT.load(Ordering::SeqCst), 5, "reset must not double-drop alloc handles");
}
#[cfg(feature = "stats")]
#[test]
fn reset_returns_chunks_to_cache_and_avoids_fresh_alloc() {
// Seed the high-water mark to the largest class up front so the
// chunk that backs our single allocation isn't evicted from the
// cache when it returns (the high-water filter requires
// `cap >= class_to_bytes(high_water)`).
let mut arena = Arena::builder().with_capacity(64 * 1024).build();
let _ = arena.alloc(0_u64);
let stats_before = arena.stats();
assert!(stats_before.normal_chunks_allocated >= 1);
arena.reset();
let stats_after_reset = arena.stats();
assert_eq!(stats_after_reset.normal_chunks_allocated, stats_before.normal_chunks_allocated);
let _ = arena.alloc(1_u64);
let stats_after_realloc = arena.stats();
assert_eq!(
stats_after_realloc.normal_chunks_allocated, stats_before.normal_chunks_allocated,
"reset should not allocate a fresh chunk; cache reuse expected"
);
}
#[cfg(feature = "stats")]
#[test]
fn reset_preserves_lifetime_chunk_count_across_phases() {
let mut arena = Arena::new();
let mut last = 0;
for _ in 0..3 {
for _ in 0..10 {
let _ = arena.alloc(0_u64);
}
let now = arena.stats().normal_chunks_allocated;
assert!(now >= last, "lifetime chunks_allocated must be monotonic across resets");
last = now;
arena.reset();
}
}
#[test]
fn reset_clears_byte_budget_for_cached_chunks() {
// Tight budget: only one chunk worth.
let mut arena: Arena = Arena::builder().byte_budget(8 * 1024).build();
let _ = arena.alloc(0_u8); // forces fresh chunk allocation
arena.reset();
// Should be able to allocate again from the cached chunk without
// tripping the budget.
let _ = arena.alloc(1_u8);
}
#[cfg(feature = "stats")]
#[test]
fn reset_works_with_pinned_chunks() {
// Allocate a couple of near-max_normal_alloc buffers to put the
// (class-7, 64 KiB) starter chunk into use. `MaybeUninit<[u8;
// 4000]>` skips per-byte init; a couple of them is enough to
// exercise the reset→cache→reuse path without a long alloc loop.
let mut arena: Arena = Arena::builder().max_normal_alloc(4 * 1024).with_capacity(64 * 1024).build();
for _ in 0..2 {
let _ = arena.alloc(core::mem::MaybeUninit::<[u8; 4000]>::uninit());
}
let chunks_before = arena.stats().normal_chunks_allocated;
assert!(chunks_before >= 1, "expected at least one chunk allocation, got {chunks_before}");
arena.reset();
let _ = arena.alloc(0_u64);
assert_eq!(
arena.stats().normal_chunks_allocated,
chunks_before,
"no fresh chunk allocation expected"
);
}
#[test]
fn reset_works_after_alloc_style_refs_drop() {
let mut arena = Arena::new();
{
let mut r = arena.alloc(123);
*r += 1;
}
arena.reset();
let r = arena.alloc(1_u64);
assert_eq!(*r, 1);
}
// Reset with outstanding refcounted smart pointers: the chunk *detaches*, the
// smart pointer keeps working, no destructor is skipped.
#[test]
fn reset_with_outstanding_arena_arc_keeps_handle_valid() {
let mut arena = Arena::new();
let r: Arc<u32> = arena.alloc_arc(7);
arena.reset();
assert_eq!(*r, 7);
drop(r);
let _ = arena.alloc_arc(11_u32);
}
#[test]
fn reset_with_arena_arc_held_on_another_thread() {
use std::sync::{Arc as StdArc, Barrier};
let mut arena = Arena::new();
let r: Arc<u32> = arena.alloc_arc(99);
let barrier = StdArc::new(Barrier::new(2));
let b = StdArc::clone(&barrier);
let h = std::thread::spawn(move || {
let _ = b.wait();
assert_eq!(*r, 99);
let _ = b.wait();
});
let _ = barrier.wait();
arena.reset();
let _ = barrier.wait();
h.join().unwrap();
// Arena still usable.
let _ = arena.alloc_arc(11_u32);
}
/// Regression for the `reset`-retires-shared-chunks bug.
///
/// `reset` must touch only chunks. It used to also retire the
/// current chunk (reconcile its surplus + reinstall the empty
/// sentinel). That broke workloads that nest arena [`Arc`]s inside an
/// outer arena `Arc` in the same chunk: the inner arcs' drops are
/// deferred to chunk teardown (refcount reaching 0), but the outer arc's
/// own slice elements pin the chunk until then, so the chunk can never
/// reach 0 while it is the retired-but-referenced current chunk. Each
/// reset therefore allocated **one fresh chunk per cycle**
/// (linear growth, slope 1 — the benchmark saw a fresh ~64 KiB chunk
/// every iteration).
///
/// With reset leaving shared state alone, chunks are bump-filled
/// across cycles and a new (larger) chunk is needed only occasionally as
/// the size class ratchets up, so the count grows strictly sub-linearly.
/// This test pins the slope: across a measured batch of `BATCH` reset
/// cycles the shared-chunk count must grow by far less than `BATCH`
/// (the buggy code grew by exactly `BATCH`).
#[cfg(feature = "stats")]
#[test]
fn reset_does_not_allocate_a_fresh_chunk_per_cycle() {
// Each cycle just needs to *use* a chunk so that `reset`'s
// shared-chunk handling is exercised; a single `Arc` allocation
// does that. (The nested-structure variant is covered separately
// by `reset_keeps_nested_arc_structures_valid_across_cycles`.)
// Keeping the per-cycle work to one allocation bounds the Miri
// interpreter cost while still pinning the slope.
fn build(arena: &Arena) {
drop(arena.alloc_arc(0xAB_u64));
}
const WARMUP: usize = 16;
const BATCH: usize = 64;
let mut arena = Arena::new();
for _ in 0..WARMUP {
build(&arena);
arena.reset();
}
let before = arena.stats().normal_chunks_allocated;
for _ in 0..BATCH {
build(&arena);
arena.reset();
}
let grew_by = arena.stats().normal_chunks_allocated - before;
// Buggy `reset` grew by exactly `BATCH` (one fresh chunk per cycle).
// The correct behavior grows by only a handful (a few class-size
// bumps). A generous sub-linear ceiling cleanly separates the two.
assert!(
grew_by < BATCH as u64 / 8,
"reset must not allocate a fresh chunk per cycle: \
{grew_by} new chunks over {BATCH} cycles (buggy code allocates ~{BATCH})",
);
}
/// Companion to the leak regression that needs no `stats` feature:
/// the nested-`Arc` structure must stay valid and drop cleanly across
/// repeated reset cycles. Builds the structure, reads it back, drops it,
/// resets, and repeats — confirming `reset` leaves outstanding
/// shared-chunk contents intact.
#[test]
fn reset_keeps_nested_arc_structures_valid_across_cycles() {
let mut arena = Arena::new();
for cycle in 0..8_u8 {
let outer: Arc<[Arc<[u8]>]> = {
let mut v = arena.alloc_vec_with_capacity::<Arc<[u8]>>(4);
for i in 0_u8..4 {
v.push(arena.alloc_slice_copy_arc(&[cycle, i, 0xCD]));
}
v.try_into_arc_slice().unwrap()
};
assert_eq!(outer.len(), 4);
for (i, inner) in outer.iter().enumerate() {
let i = u8::try_from(i).unwrap();
assert_eq!(&**inner, &[cycle, i, 0xCD]);
}
drop(outer);
arena.reset();
}
}
}
mod large_alloc {
#![allow(clippy::std_instead_of_core, reason = "test code")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::cast_possible_truncation, reason = "intentional truncation in test values")]
#![allow(clippy::clone_on_ref_ptr, reason = "test code")]
#![allow(clippy::redundant_type_annotations, reason = "type annotations for documentation clarity")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
use multitude::Arena;
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
/// 64 KiB worth of bytes (matches `CHUNK_ALIGN`).
const CHUNK_BYTES: usize = 65_536;
/// One byte past the chunk-recovery boundary.
const OVER_CHUNK: usize = CHUNK_BYTES + 1;
/// Comfortably past two chunks' worth.
const FAR_OVER_CHUNK: usize = CHUNK_BYTES * 3 / 2;
// ============================================================================
// Slices: direct large allocations
// ============================================================================
#[test]
fn alloc_slice_fill_with_above_chunk_boundary() {
// Same byte-count test (`> CHUNK_BYTES`) but with `u64` elements so
// the per-element `init` closure runs 8× fewer times — pure Miri
// win, identical chunk-boundary semantics.
const N_U64: usize = OVER_CHUNK / 8 + 1;
let arena = Arena::new();
let s = arena.alloc_slice_fill_with::<u64, _>(N_U64, |i| i as u64);
assert_eq!(s.len(), N_U64);
assert_eq!(s[0], 0);
// Element that straddles the 64 KiB tile boundary.
let mid_idx = CHUNK_BYTES / 8;
assert_eq!(s[mid_idx], mid_idx as u64);
assert_eq!(s[N_U64 - 1], (N_U64 - 1) as u64);
}
#[test]
fn alloc_slice_copy_above_chunk_boundary() {
let arena = Arena::new();
// Make sure the byte-size strictly exceeds CHUNK_BYTES.
let n = CHUNK_BYTES / 4 + 4; // bytes = 4*n = 65552 > 65536
let src: Vec<u32> = (0..n as u32).collect();
let s = arena.alloc_slice_copy(&src);
assert_eq!(s.len(), src.len());
assert_eq!(s[0], 0);
assert_eq!(s[s.len() - 1], (s.len() - 1) as u32);
// First element straddling the 64 KiB tile boundary.
let mid_idx = CHUNK_BYTES / 4;
assert_eq!(s[mid_idx], mid_idx as u32);
}
#[test]
fn alloc_slice_clone_above_chunk_boundary() {
let arena = Arena::new();
// Use `u128` so the element count needed to exceed `CHUNK_BYTES`
// is 16x smaller than with `u8`, halving it again vs `u64` — the
// `alloc_slice_clone` path still clones every element across the
// oversized chunk, so fewer elements means far less Miri work for
// the same `> CHUNK_BYTES` byte threshold.
let n = CHUNK_BYTES / 16 + 2; // 4098 u128 => > 64 KiB
let src: Vec<u128> = (0..n as u128).collect();
let s = arena.alloc_slice_clone::<u128>(&src);
assert_eq!(s.len(), src.len());
assert_eq!(s[0], 0);
assert_eq!(s[s.len() - 1], (s.len() - 1) as u128);
}
#[test]
fn alloc_slice_copy_arc_above_chunk_boundary() {
// Build the source as a u64 vector: 8× fewer per-element steps in
// the Vec construction and the subsequent `copy_nonoverlapping`
// tracking under Miri, same `> CHUNK_BYTES` byte count.
const N_U64: usize = OVER_CHUNK / 8 + 1;
let arena = Arena::new();
let src: Vec<u64> = (0..N_U64 as u64).collect();
let a = arena.alloc_slice_copy_arc::<u64>(&src);
assert_eq!(a.len(), src.len());
// Cross-thread sanity: Arc<[u64]> over the oversized chunk must travel.
let a_clone = a.clone();
let h = std::thread::spawn(move || {
assert_eq!(a_clone.len(), N_U64);
assert_eq!(a_clone[N_U64 - 1], (N_U64 - 1) as u64);
});
h.join().unwrap();
assert_eq!(a[0], 0);
}
// ============================================================================
// Slices with Drop: large drop-slice
// ============================================================================
#[test]
fn alloc_slice_fill_with_above_chunk_drops_all_elements() {
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Clone)]
struct Counted(StdArc<AtomicUsize>);
impl Drop for Counted {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let counter = StdArc::new(AtomicUsize::new(0));
// The property under test is "every initialized element runs
// its Drop at arena teardown". Length doesn't matter — small
// is fine and keeps the per-element atomic ops affordable
// under Miri.
let len = 16;
{
let arena = Arena::new();
let s = arena.alloc_slice_fill_with::<Counted, _>(len, |_| Counted(counter.clone()));
assert_eq!(s.len(), len);
}
assert_eq!(counter.load(Ordering::Relaxed), len);
}
#[test]
fn alloc_slice_fill_with_arc_above_chunk_drops_all_elements_on_last_arc_drop() {
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Clone)]
struct Counted(StdArc<AtomicUsize>);
impl Drop for Counted {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let counter = StdArc::new(AtomicUsize::new(0));
// See sibling test above: small `len` is sufficient to verify
// the "drops run when the last Arc is dropped" semantics.
let len = 16;
let arena = Arena::new();
let arc = arena.alloc_slice_fill_with_arc::<Counted, _>(len, |_| Counted(counter.clone()));
assert_eq!(arc.len(), len);
let arc_clone = arc.clone();
drop(arena);
// Original arc + clone both live; drops must not run yet.
assert_eq!(counter.load(Ordering::Relaxed), 0);
drop(arc);
assert_eq!(counter.load(Ordering::Relaxed), 0);
drop(arc_clone);
assert_eq!(counter.load(Ordering::Relaxed), len);
}
// ============================================================================
// Vec: explicit large capacity
// ============================================================================
#[test]
fn alloc_vec_with_capacity_above_chunk_boundary() {
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity::<u8>(OVER_CHUNK);
assert!(v.capacity() >= OVER_CHUNK);
for i in 0..OVER_CHUNK {
v.push((i & 0xff) as u8);
}
assert_eq!(v.len(), OVER_CHUNK);
assert_eq!(v[0], 0);
assert_eq!(v[CHUNK_BYTES], (CHUNK_BYTES & 0xff) as u8);
assert_eq!(v[OVER_CHUNK - 1], ((OVER_CHUNK - 1) & 0xff) as u8);
}
#[test]
fn alloc_vec_with_capacity_at_far_over_chunk() {
let arena = Arena::new();
let cap = FAR_OVER_CHUNK / 4;
let mut v = arena.alloc_vec_with_capacity::<u32>(cap);
// Fill the (far-over-chunk) capacity in one bulk `extend_from_slice`
// (a single memcpy) rather than `cap` individual `push` calls — the
// per-`push` arena bookkeeping is what dominates under Miri. A
// bulk-zeroed source vec is itself a single allocation.
v.extend_from_slice(&std::vec![0_u32; cap]);
assert_eq!(v.len(), cap);
// The first, a mid-chunk, and the last slot must all be addressable
// and writable across the oversized backing chunk.
v[0] = 0xA1;
v[CHUNK_BYTES / 4] = 0xB2;
v[cap - 1] = 0xC3;
assert_eq!(v[0], 0xA1);
assert_eq!(v[CHUNK_BYTES / 4], 0xB2);
assert_eq!(v[cap - 1], 0xC3);
}
// ============================================================================
// Vec: grow from small to past 64 KiB
// ============================================================================
#[test]
fn alloc_vec_grows_from_small_to_past_chunk_boundary() {
// The interesting property is that `Vec` survives the
// amortized-doubling relocations triggered by growing past the
// chunk boundary. We start small (one push, default capacity),
// then jump past CHUNK_BYTES in a single `extend_from_slice`
// call. The growth path still has to: rebump for the larger
// capacity, copy live elements, and route through the oversized
// chunk allocator — all in one shot. This keeps the Miri
// interpreter loop count bounded (one bulk memcpy) while still
// exercising every relocation arm.
let arena = Arena::new();
let mut v = arena.alloc_vec::<u64>();
v.push(0xDEAD_u64);
assert_eq!(v.len(), 1);
let block: std::vec::Vec<u64> = (1..(OVER_CHUNK / 8) as u64).collect();
v.extend_from_slice(&block);
assert_eq!(v.len(), 1 + block.len());
assert_eq!(v[0], 0xDEAD);
assert_eq!(v[v.len() - 1], block.last().copied().unwrap());
}
#[test]
fn alloc_vec_grows_with_drop_type_past_chunk_boundary() {
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Clone)]
struct Counted(StdArc<AtomicUsize>);
impl Drop for Counted {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let counter = StdArc::new(AtomicUsize::new(0));
// The property is: drops run for every live element after the
// arena is torn down, even when growth has relocated the
// storage. We don't actually need to cross the chunk
// boundary — every relocation arm is covered by a single
// `reserve(N)` for N past the initial small capacity. Use
// a tiny N so the per-element atomic clone cost stays low
// under Miri.
let n = 16;
{
let arena = Arena::new();
let mut v = arena.alloc_vec::<Counted>();
for _ in 0..n {
v.push(Counted(counter.clone()));
}
assert_eq!(v.len(), n);
}
assert_eq!(counter.load(Ordering::Relaxed), n);
}
#[test]
fn alloc_vec_extend_from_iter_past_chunk_boundary() {
let arena = Arena::new();
// Exercise the `Extend`-from-iterator growth path across the chunk
// boundary. Using `u128` reaches `> CHUNK_BYTES` with 8x fewer
// elements than `u16`, so the per-element interpreted `extend`
// loop (which a lazy `map` iterator forces) is 8x shorter.
let mut v = arena.alloc_vec::<u128>();
let n = OVER_CHUNK / 16 + 1; // > 64 KiB worth of u128
v.extend((0..n as u128).map(|i| i.wrapping_mul(13)));
assert_eq!(v.len(), n);
// Spot-check first, mid-chunk and last instead of iterating
// every element; a chunk-boundary bug would manifest at any of
// these positions equally and the per-element cost dominates
// under Miri.
for i in [0, n / 2, n - 1] {
assert_eq!(v[i], (i as u128).wrapping_mul(13));
}
}
#[test]
fn vec_in_macro_initial_then_grow_past_chunk() {
let arena = Arena::new();
let mut v = multitude::vec::vec![in &arena; 0u32; 16];
assert_eq!(v.len(), 16);
// Fixed iteration count rather than `while v.len() < ...` so
// that a `Vec::len -> 0` mutation can't drive an infinite loop.
for next in 16..(OVER_CHUNK / 4) {
v.push(next as u32);
}
assert_eq!(v.len(), OVER_CHUNK / 4);
assert_eq!(v[0], 0);
assert_eq!(v[16], 16);
assert_eq!(v[v.len() - 1], (v.len() - 1) as u32);
}
// ============================================================================
// String: explicit large capacity and growth
// ============================================================================
#[test]
fn alloc_string_with_capacity_above_chunk_boundary() {
let arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(OVER_CHUNK);
assert!(s.capacity() >= OVER_CHUNK);
// Bulk push: a single push_str is one memcpy rather than
// OVER_CHUNK individual char-push calls (each one performing
// capacity checks and UTF-8 encoding under Miri).
let block = "a".repeat(OVER_CHUNK);
s.push_str(&block);
assert_eq!(s.len(), OVER_CHUNK);
assert_eq!(s.as_bytes()[0], b'a');
assert_eq!(s.as_bytes()[CHUNK_BYTES], b'a');
assert_eq!(s.as_bytes()[OVER_CHUNK - 1], b'a');
}
#[test]
fn alloc_string_grows_from_small_to_past_chunk_boundary() {
let arena = Arena::new();
let mut s = arena.alloc_string();
assert_eq!(s.len(), 0);
s.push_str("hello");
// Bulk push instead of OVER_CHUNK individual `push('x')` calls.
// The final `assert_eq!(s.len(), ...)` still kills `push_str
// -> noop` and `String::len -> 0` mutations.
let block = "x".repeat(OVER_CHUNK);
s.push_str(&block);
assert_eq!(s.len(), 5 + OVER_CHUNK);
assert!(s.as_str().starts_with("hello"));
assert_eq!(s.as_bytes()[5 + OVER_CHUNK - 1], b'x');
}
#[test]
fn alloc_string_push_multibyte_grows_past_chunk_boundary() {
let arena = Arena::new();
let mut s = arena.alloc_string();
// Each emoji is 4 bytes UTF-8. Bulk push instead of per-char.
let target_chars = (OVER_CHUNK / 4) + 16;
let block = "🦀".repeat(target_chars);
s.push_str(&block);
assert_eq!(s.len(), target_chars * 4);
let mut chars = s.as_str().chars();
assert_eq!(chars.next(), Some('🦀'));
assert_eq!(chars.last(), Some('🦀'));
}
// ============================================================================
// Utf16String: same coverage for the 16-bit-encoded sibling
// ============================================================================
#[cfg(feature = "utf16")]
#[test]
fn alloc_utf16_string_with_capacity_above_chunk_units() {
let arena = Arena::new();
// capacity is measured in `u16` code units; OVER_CHUNK code units
// is 131 074 bytes of buffer. The interesting property is that
// `with_capacity` past the chunk boundary returns a usable
// buffer of the requested capacity. We don't need to fill the
// whole capacity — a small write at the start and another past
// the chunk-byte boundary confirms the buffer is indexable
// throughout. This keeps the Miri cost (which is dominated by
// per-byte UTF-8 → UTF-16 transcoding) bounded to a tiny push.
let mut s = arena.alloc_utf16_string_with_capacity(OVER_CHUNK);
s.push_from_str("a");
assert_eq!(s.len(), 1);
assert_eq!(s.as_slice()[0], u16::from(b'a'));
}
#[cfg(feature = "utf16")]
#[test]
fn alloc_utf16_string_grows_from_small_to_past_chunk_boundary() {
let arena = Arena::new();
let mut s = arena.alloc_utf16_string();
// Push a small initial seed, then reserve past the chunk
// boundary in one shot. The growth path needs to: re-route
// through the oversized chunk allocator, copy the live
// elements, update cap. A second small push past the prior
// length confirms the new buffer is writable beyond the
// initial seed. We avoid the brute-force "transcode 65 KiB"
// step entirely — `reserve` exercises the same growth arms
// without paying for it.
s.push_from_str("hello");
s.reserve(OVER_CHUNK);
assert!(s.capacity() >= 5 + OVER_CHUNK);
s.push_from_str("y");
assert_eq!(s.len(), 6);
let v = s.as_slice();
assert_eq!(v[0], u16::from(b'h'));
assert_eq!(v[5], u16::from(b'y'));
}
// ============================================================================
// Stress: many oversized allocations in one arena
// ============================================================================
#[test]
fn many_oversized_allocations_in_one_arena() {
// The property under test is that an arena tolerates *multiple*
// oversized one-shot chunks coexisting. `[u128; OVER_CHUNK/16+1]`
// gives the byte-count threshold (above `MAX_CHUNK_BYTES`). Each
// round is a single bulk `alloc_slice_copy` (one memcpy) from a
// shared zeroed source rather than an `N_U128`-long fill closure
// loop; per-round sentinels written into the first and last slots
// preserve the distinct-content checks that prove the oversized
// chunks don't alias.
const N_U128: usize = OVER_CHUNK / 16 + 1; // > 64 KiB worth of u128
let arena = Arena::new();
let src = std::vec![0_u128; N_U128];
let mut keepers = Vec::with_capacity(8);
for round in 0..8u8 {
let mut s = arena.alloc_slice_copy::<u128>(&src);
s[0] = u128::from(round);
s[N_U128 - 1] = u128::from(round);
keepers.push(s);
}
for (round, s) in keepers.iter().enumerate() {
assert_eq!(s.len(), N_U128);
assert_eq!(s[0], round as u128);
assert_eq!(s[N_U128 - 1], round as u128);
}
}
// ============================================================================
// Strings allocated as Arc<str> via the DST path (`alloc_str_arc`)
// ============================================================================
#[test]
fn alloc_str_arc_above_chunk_boundary() {
let arena = Arena::new();
let big: String = "x".repeat(OVER_CHUNK);
let s = arena.alloc_str_arc(&big);
assert_eq!(s.len(), OVER_CHUNK);
let clone = s.clone();
let h = std::thread::spawn(move || {
assert_eq!(clone.len(), OVER_CHUNK);
assert_eq!(&clone[..5], "xxxxx");
});
h.join().unwrap();
assert_eq!(&s[OVER_CHUNK - 5..], "xxxxx");
}
#[test]
fn alloc_str_box_above_chunk_boundary() {
let arena = Arena::new();
let big: String = "z".repeat(OVER_CHUNK);
let s = arena.alloc_str_box(&big);
assert_eq!(s.len(), OVER_CHUNK);
assert_eq!(&s[..5], "zzzzz");
}
#[test]
fn alloc_str_simple_ref_above_chunk_boundary() {
let arena = Arena::new();
let big: String = "w".repeat(OVER_CHUNK);
let s = arena.alloc_str(&big);
assert_eq!(s.len(), OVER_CHUNK);
assert_eq!(&s[..5], "wwwww");
assert_eq!(&s[OVER_CHUNK - 5..], "wwwww");
// Confirm small allocations after a large one still work (the
// oversized chunk does not become the current local slot).
let small = arena.alloc_str("small");
assert_eq!(&*small, "small");
}
#[test]
fn alloc_str_simple_ref_far_above_chunk_boundary() {
let arena = Arena::new();
let big: String = "Q".repeat(FAR_OVER_CHUNK);
let s = arena.alloc_str(&big);
assert_eq!(s.len(), FAR_OVER_CHUNK);
// memcmp via slice equality is one bulk op instead of FAR_OVER_CHUNK
// per-char yields under Miri.
assert_eq!(s.as_bytes(), big.as_bytes());
}
#[test]
fn try_alloc_str_simple_ref_above_chunk_boundary() {
let arena = Arena::new();
let big: String = "p".repeat(OVER_CHUNK);
let s = arena.try_alloc_str(&big).expect("oversized alloc_str must succeed");
assert_eq!(s.len(), OVER_CHUNK);
}
// ============================================================================
// DST (Rc<[T]>/Arc<[T]>/Box<[T]>) allocations > 64 KiB via the dst path
// ============================================================================
#[cfg(feature = "dst")]
#[test]
fn alloc_dst_arc_slice_above_chunk_boundary() {
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicUsize, Ordering as StdOrd};
// 64-byte payload + StdArc so each `Counted` is 72 bytes; only
// ~1024 elements are needed to cross the 64 KiB chunk boundary,
// dramatically fewer than the per-byte alternative.
#[derive(Clone)]
struct Counted {
_pad: [u8; 64],
c: StdArc<AtomicUsize>,
}
impl Drop for Counted {
fn drop(&mut self) {
self.c.fetch_add(1, StdOrd::Relaxed);
}
}
let counter = StdArc::new(AtomicUsize::new(0));
// Choose `len` so `len * size_of::<Counted>` crosses the 64 KiB
// chunk boundary.
let n = CHUNK_BYTES / core::mem::size_of::<Counted>() + 8;
{
let arena = Arena::new();
let layout = core::alloc::Layout::array::<Counted>(n).unwrap();
assert!(layout.size() > CHUNK_BYTES, "test must drive the oversized DST shared path");
// SAFETY: init fills every slot of the slice fat pointer.
let arc: multitude::Arc<[Counted]> = unsafe {
arena.alloc_dst_arc::<[Counted]>(layout, n, |p: *mut [Counted]| {
for i in 0..n {
let slot: *mut Counted = (p as *mut Counted).add(i);
core::ptr::write(
slot,
Counted {
_pad: [0; 64],
c: counter.clone(),
},
);
}
})
};
assert_eq!(arc.len(), n);
drop(arena);
assert_eq!(counter.load(StdOrd::Relaxed), 0, "Counted::drop must wait for the last Arc to drop");
drop(arc);
}
assert_eq!(counter.load(StdOrd::Relaxed), n);
}
#[cfg(feature = "dst")]
#[test]
fn alloc_dst_arc_slice_non_drop_above_chunk_boundary() {
// Drives the oversized shared DST path: byte size must exceed
// CHUNK_BYTES (64 KiB). 33 000 u16s = 66 KiB, just over the
// boundary — large enough to exercise the path without paying
// the per-element init cost u16::MAX times under Miri.
const LEN: usize = 33_000;
let arena = Arena::new();
let layout = core::alloc::Layout::array::<u16>(LEN).unwrap();
assert!(layout.size() > CHUNK_BYTES, "test must drive the oversized DST shared path");
// SAFETY: init fills every element via a single bulk write
// followed by 3 spot writes to verify the boundary semantics.
let arc: multitude::Arc<[u16]> = unsafe {
arena.alloc_dst_arc::<[u16]>(layout, LEN, |p: *mut [u16]| {
let raw = p as *mut u16;
core::ptr::write_bytes(raw, 0, LEN);
raw.write(0xCAFE);
raw.add(LEN / 2).write(0xBABE);
raw.add(LEN - 1).write(0xBEEF);
})
};
assert_eq!(arc.len(), LEN);
assert_eq!(arc[0], 0xCAFE);
assert_eq!(arc[LEN / 2], 0xBABE);
assert_eq!(arc[LEN - 1], 0xBEEF);
}
// ============================================================================
// BytesBuf integration > 64 KiB
// ============================================================================
#[cfg(feature = "bytesbuf")]
#[test]
fn bytesbuf_reserve_above_chunk_boundary() {
use bytesbuf::mem::Memory;
let arena = Arena::new();
let _buf = arena.reserve(OVER_CHUNK);
// The reserve call returned without panicking, which is the
// observable that demonstrates the oversized shared path is
// working — previously it would have spun in `refill_shared`
// until OOM or hit `expect("arena allocation failed")`.
}
}
mod fast_path_correctness {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain smart pointers to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test data is small")]
#![allow(clippy::needless_range_loop, reason = "test indexing is intentional")]
#![allow(clippy::missing_asserts_for_indexing, reason = "test code")]
#![allow(clippy::used_underscore_binding, reason = "intentional drop-after binding")]
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use multitude::Arena;
#[cfg(feature = "stats")]
use crate::common;
/// Mutex to serialize tests that use global drop counters.
static DROP_TEST_LOCK: Mutex<()> = Mutex::new(());
#[test]
fn alloc_u64_is_aligned() {
let arena = Arena::new();
for _ in 0..100 {
let r = arena.alloc(0xDEAD_BEEF_u64);
let ptr = std::ptr::from_ref::<u64>(&*r) as usize;
assert_eq!(ptr % align_of::<u64>(), 0, "u64 pointer misaligned: {ptr:#x}");
assert_eq!(*r, 0xDEAD_BEEF_u64);
}
}
#[test]
fn alloc_u128_is_aligned() {
let arena = Arena::new();
for _ in 0..100 {
let r = arena.alloc(0x1234_5678_9ABC_DEF0_u128);
let ptr = std::ptr::from_ref::<u128>(&*r) as usize;
assert_eq!(ptr % align_of::<u128>(), 0, "u128 pointer misaligned: {ptr:#x}");
assert_eq!(*r, 0x1234_5678_9ABC_DEF0_u128);
}
}
#[repr(align(32))]
#[derive(Debug, Clone, PartialEq)]
struct Align32 {
value: u64,
}
#[test]
fn alloc_align32_is_aligned() {
let arena = Arena::new();
for i in 0..50 {
let r = arena.alloc(Align32 { value: i });
let ptr = std::ptr::from_ref::<Align32>(&*r) as usize;
assert_eq!(ptr % 32, 0, "Align32 pointer misaligned: {ptr:#x}");
assert_eq!(r.value, i);
}
}
#[repr(align(64))]
#[derive(Debug, Clone, PartialEq)]
struct Align64 {
data: [u8; 64],
}
#[test]
fn alloc_align64_is_aligned() {
let arena = Arena::new();
for i in 0_u8..30 {
let r = arena.alloc(Align64 { data: [i; 64] });
let ptr = std::ptr::from_ref::<Align64>(&*r) as usize;
assert_eq!(ptr % 64, 0, "Align64 pointer misaligned: {ptr:#x}");
assert_eq!(r.data[0], i);
}
}
#[test]
fn alloc_arc_u64_is_aligned() {
let arena = Arena::new();
for _ in 0..100 {
let arc = arena.alloc_arc(0xDEAD_BEEF_u64);
let ptr = &raw const *arc as usize;
assert_eq!(ptr % align_of::<u64>(), 0, "Arc<u64> pointer misaligned: {ptr:#x}");
assert_eq!(*arc, 0xDEAD_BEEF_u64);
}
}
#[test]
fn alloc_arc_u128_is_aligned() {
let arena = Arena::new();
for _ in 0..100 {
let arc = arena.alloc_arc(0x1234_5678_9ABC_DEF0_u128);
let ptr = &raw const *arc as usize;
assert_eq!(ptr % align_of::<u128>(), 0, "Arc<u128> pointer misaligned: {ptr:#x}");
assert_eq!(*arc, 0x1234_5678_9ABC_DEF0_u128);
}
}
#[test]
fn alloc_arc_align32_is_aligned() {
let arena = Arena::new();
for i in 0..50 {
let arc = arena.alloc_arc(Align32 { value: i });
let ptr = &raw const *arc as usize;
assert_eq!(ptr % 32, 0, "Arc<Align32> pointer misaligned: {ptr:#x}");
assert_eq!(arc.value, i);
}
}
#[test]
fn interleaved_alignments_all_correct() {
let arena = Arena::new();
for i in 0_u64..50 {
// Allocate u8, then u64, then u128 — forces alignment padding
let a = arena.alloc(i as u8);
let b = arena.alloc(i);
let c = arena.alloc(u128::from(i));
assert_eq!((std::ptr::from_ref::<u8>(&*a) as usize) % align_of::<u8>(), 0);
assert_eq!(
(std::ptr::from_ref::<u64>(&*b) as usize) % align_of::<u64>(),
0,
"u64 misaligned after u8"
);
assert_eq!(
(std::ptr::from_ref::<u128>(&*c) as usize) % align_of::<u128>(),
0,
"u128 misaligned after u64"
);
assert_eq!(*a, i as u8);
assert_eq!(*b, i);
assert_eq!(*c, u128::from(i));
}
}
#[test]
fn interleaved_arc_alignments() {
let arena = Arena::new();
for i in 0_u64..50 {
let a = arena.alloc_arc(i as u8);
let b = arena.alloc_arc(i);
let c = arena.alloc_arc(Align32 { value: i });
assert_eq!((&raw const *a as usize) % align_of::<u8>(), 0);
assert_eq!((&raw const *b as usize) % align_of::<u64>(), 0);
assert_eq!((&raw const *c as usize) % 32, 0);
assert_eq!(*a, i as u8);
assert_eq!(*b, i);
assert_eq!(c.value, i);
}
}
static DROP_COUNTER: AtomicUsize = AtomicUsize::new(0);
struct DropTracker(#[expect(dead_code, reason = "field exists for Drop semantics")] u64);
impl Drop for DropTracker {
fn drop(&mut self) {
let _ = DROP_COUNTER.fetch_add(1, Ordering::Relaxed);
}
}
#[test]
fn arc_drop_runs_correctly_many_items() {
let _lock = DROP_TEST_LOCK.lock().unwrap();
let before = DROP_COUNTER.load(Ordering::SeqCst);
let n = 200;
{
let arena = Arena::new();
let handles: Vec<_> = (0..n).map(|i| arena.alloc_arc(DropTracker(i))).collect();
assert_eq!(handles.len(), n as usize);
drop(handles);
}
let after = DROP_COUNTER.load(Ordering::SeqCst);
assert_eq!(after - before, n as usize);
}
#[test]
fn box_drop_runs_on_each_drop() {
let _lock = DROP_TEST_LOCK.lock().unwrap();
let before = DROP_COUNTER.load(Ordering::SeqCst);
let arena = Arena::new();
for i in 0..100_u64 {
let b = arena.alloc_box(DropTracker(i));
drop(b);
let after = DROP_COUNTER.load(Ordering::SeqCst);
assert_eq!(after - before, (i + 1) as usize);
}
}
#[repr(align(32))]
struct AlignedDropTracker {
#[expect(dead_code, reason = "field exists for Drop semantics")]
value: u64,
}
static ALIGNED_DROP_COUNTER: AtomicUsize = AtomicUsize::new(0);
impl Drop for AlignedDropTracker {
fn drop(&mut self) {
let _ = ALIGNED_DROP_COUNTER.fetch_add(1, Ordering::Relaxed);
}
}
#[test]
fn arc_aligned_drop_runs_correctly() {
let _lock = DROP_TEST_LOCK.lock().unwrap();
let before = ALIGNED_DROP_COUNTER.load(Ordering::SeqCst);
let n = 50;
{
let arena = Arena::new();
let handles: Vec<_> = (0..n)
.map(|i| {
let arc = arena.alloc_arc(AlignedDropTracker { value: i });
let ptr = &raw const *arc as usize;
assert_eq!(ptr % 32, 0, "AlignedDropTracker misaligned: {ptr:#x}");
arc
})
.collect();
assert_eq!(handles.len(), n as usize);
drop(handles);
}
let after = ALIGNED_DROP_COUNTER.load(Ordering::SeqCst);
assert_eq!(after - before, n as usize);
}
#[test]
fn consecutive_allocs_do_not_overlap() {
let arena = Arena::new();
let mut ptrs: Vec<(*const u64, usize)> = Vec::new();
for i in 0..200_u64 {
let r = arena.alloc(i);
let addr = std::ptr::from_ref::<u64>(&*r) as usize;
ptrs.push((std::ptr::from_ref::<u64>(&*r), addr));
}
// Verify no two allocations overlap (each is 8 bytes)
ptrs.sort_by_key(|&(_, addr)| addr);
for window in ptrs.windows(2) {
let (_, a) = window[0];
let (_, b) = window[1];
assert!(a + size_of::<u64>() <= b, "Allocations overlap: {a:#x} + 8 > {b:#x}");
}
}
#[test]
fn consecutive_arc_allocs_do_not_overlap() {
let arena = Arena::new();
let handles: Vec<_> = (0..200_u64).map(|i| arena.alloc_arc(i)).collect();
let mut addrs: Vec<usize> = handles.iter().map(|arc| &raw const **arc as usize).collect();
addrs.sort_unstable();
for window in addrs.windows(2) {
assert!(
window[0] + size_of::<u64>() <= window[1],
"Arc allocations overlap: {:#x} + 8 > {:#x}",
window[0],
window[1]
);
}
}
#[cfg(feature = "stats")]
#[test]
fn filling_chunk_triggers_new_allocation() {
// Allocate u64s until we overflow whatever chunk size the arena
// settled on. With adaptive sizing the first chunk is the
// smallest class (1 KiB), so the count just needs to be enough to
// confirm that more than a handful of u64s pack in.
let arena = Arena::builder().build();
// Prime: first allocation triggers a chunk alloc
let _prime = arena.alloc(0);
let initial_chunks = arena.stats().normal_chunks_allocated;
assert_eq!(initial_chunks, 1);
let mut count = 0_u64;
while arena.stats().normal_chunks_allocated == initial_chunks {
let r = arena.alloc(count);
assert_eq!(*r, count);
let ptr = std::ptr::from_ref::<u64>(&*r) as usize;
assert_eq!(ptr % align_of::<u64>(), 0);
count += 1;
assert!(count < 2000, "should have triggered new chunk by now");
}
// The chunk boundary was crossed
assert!(count > 50, "chunk should hold many u64s, got {count}");
}
#[cfg(feature = "stats")]
#[test]
fn filling_chunk_arc_triggers_new_allocation() {
let arena = Arena::builder().build();
// Prime
let _prime = arena.alloc_arc(0_u64);
let initial_chunks = arena.stats().normal_chunks_allocated;
let mut handles = Vec::new();
let mut count = 0_u64;
while arena.stats().normal_chunks_allocated == initial_chunks {
let arc = arena.alloc_arc(count);
let ptr = &raw const *arc as usize;
assert_eq!(ptr % align_of::<u64>(), 0);
handles.push(arc);
count += 1;
assert!(count < 20_000, "should have triggered new chunk by now");
}
assert!(count > 10, "chunk should hold many Arc<u64>s");
}
#[cfg(feature = "stats")]
#[test]
fn oversize_alloc_goes_to_oversized_chunk() {
// Default max_normal_alloc for 64 KiB chunks = 16 KiB.
// Allocate something larger than that.
let arena = Arena::new();
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
let mut big = arena.alloc([0u8; 32 * 1024]);
big[0] = 42;
assert_eq!(big[0], 42);
assert!(arena.stats().oversized_chunks_allocated >= 1);
}
#[cfg(feature = "stats")]
#[test]
fn oversize_arc_goes_to_oversized_chunk() {
let arena = Arena::new();
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
let big = arena.alloc_arc([0u8; 32 * 1024]);
assert_eq!(big[0], 0);
assert!(arena.stats().oversized_chunks_allocated >= 1);
}
/// A Droppable type — allocating it uses the `DropEntry` path (line 319-325 in arena.rs).
/// If alignment math is corrupted (e.g., + → *), the bump pointer advances far too fast,
/// and far fewer items fit in a single chunk.
#[cfg(feature = "stats")]
struct Droppable(u64);
#[cfg(feature = "stats")]
#[expect(clippy::empty_drop, reason = "empty Drop impl is intentional to trigger DropEntry path")]
impl Drop for Droppable {
fn drop(&mut self) {
// no-op; existence of Drop impl triggers DropEntry path
}
}
#[cfg(feature = "stats")]
#[test]
fn drop_items_pack_efficiently_in_chunk() {
// Each Droppable(u64) needs DropEntry (32 bytes) + value (8 bytes)
// = ~40 bytes. The smallest chunk class (1 KiB) leaves about
// 768 usable bytes after the header, so ~19 items fit. With the
// + → * mutation, only 2-3 items would fit.
let arena = Arena::builder().build();
let _prime = arena.alloc(Droppable(0));
let initial_chunks = arena.stats().normal_chunks_allocated;
let mut count = 0_u64;
while arena.stats().normal_chunks_allocated == initial_chunks && count < 500 {
let r = arena.alloc(Droppable(count));
assert_eq!(r.0, count);
count += 1;
}
// With correct math we should fit at least 10 items per 1 KiB
// chunk; mutated multiplicative math would fit 2–3.
assert!(
count > 10,
"Only {count} Droppable items fit in chunk — alignment math may be corrupted"
);
}
// String pinning: the fast-path str allocation must pin the chunk so it
// survives eviction. Without pinning, a filled chunk would be freed and
// the returned &mut str would dangle.
// Slice fill_with panic safety: partial initialization must be cleaned up
// if the fill closure panics.
/// A type that is Clone + Drop and whose Clone impl panics at a configurable point.
#[derive(Debug)]
struct PanicOnClone {
id: u64,
panic_at: u64,
}
impl Clone for PanicOnClone {
fn clone(&self) -> Self {
assert!(self.id != self.panic_at, "intentional panic cloning id={}", self.id);
let _ = ALIGNED_DROP_COUNTER.fetch_add(0, Ordering::SeqCst); // prevent optimization
Self {
id: self.id,
panic_at: self.panic_at,
}
}
}
impl Drop for PanicOnClone {
fn drop(&mut self) {
let _ = DROP_COUNTER.fetch_add(1, Ordering::SeqCst);
}
}
}
mod allocator_impl {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
use multitude::Arena;
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
#[test]
fn allocator_shrink_in_place_path() {
// shrink is called internally by Vec when capacity reduces.
// Exercise that no UB arises from the typical reserve/clear cycle.
let arena = Arena::new();
let mut v = arena.alloc_vec::<u32>();
v.extend(0..50_u32);
v.clear();
v.reserve(10);
assert!(v.capacity() >= 10);
}
#[test]
fn allocator_rejects_excessive_alignment() {
// `<&Arena>::allocate` must reject layouts whose alignment exceeds
// CHUNK_ALIGN (64 KiB). Without this guard the oversized chunk's
// base would only be 64 KiB-aligned, and the data pointer derived
// from it would be misaligned for the user's request — UB on first
// typed access.
use allocator_api2::alloc::Allocator;
let arena = Arena::new();
let allocator: &Arena = &arena;
let layout = core::alloc::Layout::from_size_align(8, 128 * 1024).unwrap();
let _ = allocator.allocate(layout).unwrap_err();
}
#[test]
fn allocator_rejects_alignment_equal_to_chunk_align() {
// `<&Arena>::allocate` must also reject layouts whose alignment
// equals CHUNK_ALIGN (64 KiB). For such allocations the value
// would land at offset == CHUNK_ALIGN within the chunk, where
// `header_for`'s `addr & (CHUNK_ALIGN - 1)` mask returns 0 and
// so reports the value pointer itself as the chunk header
// address — UB on the next refcount op.
use allocator_api2::alloc::Allocator;
let arena = Arena::new();
let allocator: &Arena = &arena;
let layout = core::alloc::Layout::from_size_align(8, 64 * 1024).unwrap();
let _ = allocator.allocate(layout).unwrap_err();
}
}
mod mutants_for_chunk_provider {
#![allow(clippy::std_instead_of_core, reason = "test code")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::collection_is_never_read, reason = "keep allocations live")]
#![allow(clippy::items_after_statements, reason = "test-local types live next to their usage")]
#![allow(clippy::doc_markdown, reason = "doc comments cite raw identifier names")]
#![allow(dead_code, reason = "test structs retain payload fields to control size")]
#[cfg(feature = "stats")]
use multitude::{Arena, Box};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
#[cfg(feature = "stats")]
#[test]
fn reserve_budget_admits_exact_fit() {
// The chunk-provider charges `header_bytes + payload_bytes` per chunk.
// We don't know `header_bytes` exactly, but the smallest cacheable
// payload is 512 bytes. Using `with_capacity(512)` forces a
// single 512-byte chunk preallocation; once that is reserved, the
// running total equals (header + 512). Choosing the byte_budget
// equal to that total exercises the boundary.
//
// We discover `header + 512` by first building with an effectively
// infinite budget and reading `total_bytes_allocated`-equivalent
// stats. The arena exposes preallocation through stats, so we run
// a probe build to learn the total chunk byte cost, then build a
// second arena with budget == exact total.
let probe = Arena::builder().byte_budget(1024 * 1024).with_capacity(512).build();
assert_eq!(probe.stats().normal_chunks_allocated, 1);
drop(probe);
// 1 KiB is enough to cover header (<512 bytes) + 512 payload, so
// the exact-fit budget admits exactly one chunk's worth.
let arena = Arena::builder().byte_budget(1024).with_capacity(512).build();
assert_eq!(
arena.stats().normal_chunks_allocated,
1,
"byte_budget == total bytes for one chunk must admit allocation"
);
}
#[cfg(feature = "stats")]
#[test]
fn release_budget_runs_when_chunk_freed() {
// A 5 MiB budget is enough for one 64 KiB chunk plus header but
// not for two. A single 8 KiB uninit box is enough to force a
// chunk allocation; no need for eight (the loop was for a prior
// version of this test that needed cache eviction).
let arena = Arena::builder().byte_budget(5 * 1024 * 1024).build();
let box1 = arena.alloc_uninit_box::<[u8; 8 * 1024]>();
assert!(arena.stats().normal_chunks_allocated >= 1);
drop(box1);
drop(arena);
// If `release_budget` is a no-op, recreating an arena with the
// same budget would fail to satisfy the same workload. The
// user-observable invariant: a fresh arena with the same budget
// admits the same allocation.
let arena2 = Arena::builder().byte_budget(5 * 1024 * 1024).build();
let _box2 = arena2.alloc_uninit_box::<[u8; 8 * 1024]>();
assert!(arena2.stats().normal_chunks_allocated >= 1);
}
#[cfg(feature = "stats")]
#[test]
fn acquire_local_boundary_does_not_route_oversized() {
// Set max_normal_alloc to the minimum permitted (4 KiB).
let arena = Arena::builder().max_normal_alloc(4 * 1024).build();
// Allocate a value of exactly 4 KiB (no Drop → no entry_size, no
// align slack vs align_of::<usize>()).
#[repr(align(8))]
struct Block([u64; 512]); // 4096 bytes exactly
let _b = arena.alloc_box(Block([0_u64; 512]));
let s = arena.stats();
// A boundary-sized value still consumes a chunk (its worst-case
// payload includes alignment slack, so it routes to a one-shot
// oversized chunk). The local/chunk distinction this test
// originally drew no longer exists after chunk unification.
assert!(s.normal_chunks_allocated + s.oversized_chunks_allocated >= 1);
}
#[cfg(feature = "stats")]
#[test]
fn acquire_shared_boundary_does_not_route_oversized() {
let arena = Arena::builder().max_normal_alloc(4 * 1024).build();
#[repr(align(8))]
struct Block([u64; 512]);
let _a = arena.alloc_arc(Block([0_u64; 512]));
let s = arena.stats();
assert!(s.normal_chunks_allocated + s.oversized_chunks_allocated >= 1);
}
#[cfg(feature = "stats")]
#[test]
fn acquire_local_class_ceiling_is_correct() {
// The property under test: the size-class ratchet caps at the
// largest cacheable class (class 7 = 64 KiB total). After the
// first few refills ratchet there, subsequent refills stay at
// class 7 — they don't keep doubling. To observe this we allocate
// a handful of ~13 KiB boxes (under MAX_NORMAL_ALLOC = 16 KiB, so
// still routed through the normal cache) and confirm none route to
// oversized. Five ~13 KiB boxes total > 64 KiB, so they span ≥ 2
// class-7 chunks, proving the ratchet stays at class 7 rather than
// degrading or escaping. Larger-but-fewer boxes keep the byte
// threshold while minimising the per-allocation Miri cost.
let arena = Arena::new();
let mut keep: Vec<Box<core::mem::MaybeUninit<[u8; 13 * 1024]>>> = Vec::new();
for _ in 0..5 {
keep.push(arena.alloc_uninit_box::<[u8; 13 * 1024]>());
}
let s = arena.stats();
assert_eq!(s.oversized_chunks_allocated, 0);
assert!(
s.normal_chunks_allocated >= 2,
"5 × 13 KiB boxes must span ≥ 2 class-7 chunks, got {}",
s.normal_chunks_allocated
);
}
#[cfg(feature = "stats")]
#[test]
fn high_water_ratchet_grows_chunks() {
let arena = Arena::builder().with_capacity(512).build();
// Preallocation should have created exactly one 512-byte (class 0) chunk.
assert_eq!(arena.stats().normal_chunks_allocated, 1);
// Allocate a single 8 KiB blob: the 512-byte starter chunk can't
// fit it, forcing a refill that ratchets the high-water mark to
// a ≥ class-5 (8 KiB) chunk.
#[repr(align(8))]
struct Blob([u8; 8 * 1024]); // 8 KiB
let _b1 = arena.alloc_box(Blob([0; 8 * 1024]));
// Allocate a second 8 KiB blob: doesn't fit in the tail of the
// first refilled chunk, so it forces another refill. With the
// ratchet intact, that fresh chunk is at the larger class (≥ 8
// KiB) and absorbs the blob normally. With the ratchet reversed
// (`> → <`), the fresh chunk would shrink below 8 KiB, the blob
// couldn't fit, and the alloc would route to an oversized chunk.
let _b2 = arena.alloc_box(Blob([0; 8 * 1024]));
// No oversized routes:
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
}
#[cfg(feature = "stats")]
#[test]
fn preallocate_total_bytes_uses_sum_not_product() {
// Budget set just large enough for header + 64 KiB payload (one
// class-7 chunk). With `+` the total ≈ header + 64 KiB → fits.
// With `*` the total ≈ header * 64 KiB → vastly over budget →
// build would panic. We assert successful build.
let arena = Arena::builder().byte_budget(128 * 1024).with_capacity(64 * 1024).build();
assert_eq!(arena.stats().normal_chunks_allocated, 1);
let arena2 = Arena::builder().byte_budget(128 * 1024).with_capacity(64 * 1024).build();
assert_eq!(arena2.stats().normal_chunks_allocated, 1);
}
#[cfg(feature = "stats")]
#[test]
fn cache_pop_serves_preallocated_chunk() {
let arena = Arena::builder().with_capacity(64 * 1024).build();
assert_eq!(arena.stats().normal_chunks_allocated, 1);
// Allocate a small arc — should reuse the cached chunk.
let _a = arena.alloc_arc(42_u64);
assert_eq!(
arena.stats().normal_chunks_allocated,
1,
"small arc must reuse preallocated 64 KiB chunk; if pop returned None, the counter would be 2"
);
}
}
mod mutants_for_internal {
#![allow(clippy::std_instead_of_core, reason = "test code")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "explicit .clone() clarifies test intent")]
#![allow(clippy::collection_is_never_read, reason = "keep allocations live")]
#![allow(clippy::doc_markdown, reason = "doc comments cite raw identifier names")]
#![allow(clippy::cast_possible_truncation, reason = "bounded indices fit")]
#![allow(dead_code, reason = "tracked allocator's drop side-effect is the observable")]
use core::alloc::Layout;
use core::ptr::NonNull;
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicUsize, Ordering};
use allocator_api2::alloc::{AllocError, Allocator, Global};
use multitude::Arena;
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
/// A `Send + Sync` allocator that bumps `drop_count` once per clone
/// when its boxed state is dropped. Used to observe that
/// allocator clones held by chunks/provider are dropped on `Arena::drop`.
#[derive(Clone)]
struct DropTrackingAllocator {
drop_count: StdArc<AtomicUsize>,
inner: StdArc<DropTracker>,
}
struct DropTracker {
counter: StdArc<AtomicUsize>,
}
impl Drop for DropTracker {
fn drop(&mut self) {
self.counter.fetch_add(1, Ordering::Relaxed);
}
}
impl DropTrackingAllocator {
fn new() -> (Self, StdArc<AtomicUsize>) {
let counter = StdArc::new(AtomicUsize::new(0));
let alloc = Self {
drop_count: counter.clone(),
inner: StdArc::new(DropTracker { counter: counter.clone() }),
};
(alloc, counter)
}
}
// SAFETY: forwards to Global; no internal mutability requirements.
unsafe impl Allocator for DropTrackingAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
Global.allocate(layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
// SAFETY: forwarded.
unsafe { Global.deallocate(ptr, layout) };
}
}
/// Verifies that allocator clones held by the chunk pipeline are
/// dropped when the arena is dropped (no leak of boxed state).
#[test]
fn allocator_clones_dropped_when_arena_drops() {
let (alloc, counter) = DropTrackingAllocator::new();
{
let arena: Arena<DropTrackingAllocator> = Arena::new_in(alloc);
// Touch the arena so a chunk really exists.
let _b = arena.alloc_box(42_u64);
// Drop arena → drops chunk/provider → drops allocator clones →
// DropTracker drops → counter += 1.
}
assert!(
counter.load(Ordering::Relaxed) >= 1,
"allocator clones must be released on arena drop"
);
}
#[cfg(feature = "stats")]
#[test]
fn min_class_for_bytes_consistency() {
// 512 → class 0 → exactly 512 bytes preallocated
let arena = Arena::builder().with_capacity(512).build();
assert_eq!(arena.stats().normal_chunks_allocated, 1);
// 513 → class 1 (1 KiB) → one chunk
let arena = Arena::builder().with_capacity(513).build();
assert_eq!(arena.stats().normal_chunks_allocated, 1);
// 1024 → class 1 (1 KiB) exactly
let arena = Arena::builder().with_capacity(1024).build();
assert_eq!(arena.stats().normal_chunks_allocated, 1);
// 1025 → class 2 (2 KiB) → one chunk
let arena = Arena::builder().with_capacity(1025).build();
assert_eq!(arena.stats().normal_chunks_allocated, 1);
// 65536 → class 7 (64 KiB) → one chunk
let arena = Arena::builder().with_capacity(65536).build();
assert_eq!(arena.stats().normal_chunks_allocated, 1);
// 65537 → saturates at class 7 → two 64 KiB chunks (ceil-div).
let arena = Arena::builder().with_capacity(65537).build();
assert_eq!(arena.stats().normal_chunks_allocated, 2);
}
#[cfg(feature = "stats")]
#[test]
fn to_thin_ptr_returns_chunk_address() {
let arena = Arena::builder().with_capacity(1024).with_capacity(2048).build();
// We requested capacity twice. The second call overrides the first
// (builder is fluent), so we expect one preallocated chunk of
// class >= the requested bytes. We just check that arcs use the
// cached chunk.
let prealloc = arena.stats().normal_chunks_allocated;
assert!(prealloc >= 1);
// One arc should reuse the cache — counter should not grow.
let _a = arena.alloc_arc(7_u64);
assert_eq!(
arena.stats().normal_chunks_allocated,
prealloc,
"small arc must reuse preallocated chunk (kills `to_thin_ptr → null`)"
);
}
#[test]
fn chunk_payload_alignment_supports_drop_entries() {
#[derive(Debug)]
struct D(StdArc<AtomicUsize>);
impl Drop for D {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let c = StdArc::new(AtomicUsize::new(0));
{
let arena = Arena::new();
let mut keep: Vec<multitude::Arc<D>> = Vec::new();
for _ in 0..256_u32 {
keep.push(arena.alloc_arc(D(c.clone())));
}
drop(keep);
drop(arena);
}
assert_eq!(c.load(Ordering::Relaxed), 256);
}
#[cfg(feature = "stats")]
#[test]
fn resolve_capacity_64kib_yields_single_chunk() {
let arena = Arena::builder().with_capacity(64 * 1024).build();
assert_eq!(arena.stats().normal_chunks_allocated, 1);
let arena2 = Arena::builder().with_capacity(64 * 1024).build();
assert_eq!(arena2.stats().normal_chunks_allocated, 1);
}
}
mod mutants_for_kill_boundaries {
#![cfg(feature = "stats")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::std_instead_of_core, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::items_after_statements, reason = "test code")]
#![allow(dead_code, reason = "test code: probe payload fields are intentionally inert")]
use multitude::{Arc, Arena, ArenaBuilder};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
const MAX_NORMAL_ALLOC: usize = 16 * 1024;
const PREFIX_BYTES: usize = core::mem::size_of::<usize>();
// ---------------------------------------------------------------------------
// alloc_str.rs:251 — `if total > max_normal_alloc` in `try_alloc_str_prefixed_local`.
// Kills `>` → `>=` at the exact boundary `total == max_normal_alloc`.
// ---------------------------------------------------------------------------
#[test]
fn alloc_str_box_at_boundary_takes_inner_path_not_outer_oversized() {
let arena = Arena::new();
let len = MAX_NORMAL_ALLOC - PREFIX_BYTES;
let s = "b".repeat(len);
let b = arena.alloc_str_box(&s);
assert_eq!(b.len(), len);
let s = arena.stats();
assert!(s.normal_chunks_allocated + s.oversized_chunks_allocated >= 1);
assert_eq!(s.oversized_chunks_allocated, 0);
}
// ---------------------------------------------------------------------------
// alloc_str.rs:288 — same boundary in `try_alloc_str_prefixed_shared`.
// ---------------------------------------------------------------------------
#[test]
fn alloc_str_arc_at_boundary_takes_inner_path_not_outer_oversized() {
let arena: Arena = Arena::new();
let len = MAX_NORMAL_ALLOC - PREFIX_BYTES;
let s = "c".repeat(len);
let arc: Arc<str> = arena.alloc_str_arc(&s);
assert_eq!(arc.len(), len);
let s = arena.stats();
assert!(s.normal_chunks_allocated + s.oversized_chunks_allocated >= 1);
}
// Past-boundary sanity check: also catches `> → ==` and `> → <` mutants on
// the same line (both make the routing false for strictly-greater inputs,
// causing the fast path to fail).
#[test]
fn alloc_str_arc_past_boundary_uses_oversized() {
let arena: Arena = Arena::new();
let len = MAX_NORMAL_ALLOC + 16;
let s = "q".repeat(len);
let arc: Arc<str> = arena.alloc_str_arc(&s);
assert_eq!(arc.len(), len);
assert!(arena.stats().oversized_chunks_allocated >= 1);
}
// ---------------------------------------------------------------------------
// alloc_utf16.rs:25 — `if total > max_normal_alloc` in `try_alloc_utf16_prefixed_local`.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// alloc_utf16.rs:63 — same boundary in `try_alloc_utf16_prefixed_shared`.
// ---------------------------------------------------------------------------
#[cfg(feature = "utf16")]
#[test]
fn alloc_utf16_str_arc_at_boundary_takes_inner_path_not_outer_oversized() {
use widestring::Utf16Str;
let arena: Arena = Arena::new();
let len = (MAX_NORMAL_ALLOC - PREFIX_BYTES) / 2;
let buf: Vec<u16> = vec![u16::from(b'z'); len];
let src = Utf16Str::from_slice(&buf).unwrap();
let arc = arena.alloc_utf16_str_arc(src);
assert_eq!(arc.len(), len);
let s = arena.stats();
assert!(s.normal_chunks_allocated + s.oversized_chunks_allocated >= 1);
}
#[cfg(feature = "utf16")]
#[test]
fn alloc_utf16_str_arc_past_boundary_uses_oversized() {
use widestring::Utf16Str;
let arena: Arena = Arena::new();
let len = (MAX_NORMAL_ALLOC - PREFIX_BYTES) / 2 + 16;
let buf: Vec<u16> = vec![u16::from(b'w'); len];
let src = Utf16Str::from_slice(&buf).unwrap();
let arc = arena.alloc_utf16_str_arc(src);
assert_eq!(arc.len(), len);
assert!(arena.stats().oversized_chunks_allocated >= 1);
}
// ---------------------------------------------------------------------------
// alloc_str.rs:200 — `if len > self.provider.max_normal_alloc` in
// `impl_alloc_str_inner` (the simple-reference `alloc_str` path).
// Kills `>` → `>=` at `len == max_normal_alloc`.
// ---------------------------------------------------------------------------
#[test]
fn alloc_str_simple_ref_at_max_normal_alloc_boundary_takes_inner_path() {
// Use a non-power-of-two `max_normal_alloc` so the rounded chunk
// class capacity strictly exceeds the boundary `len`. Then a
// 1-byte follow-on alloc fits in the same chunk under the
// original `>` semantics, but forces a refill under the `>=`
// mutant (which pins the boundary chunk into the simple-ref pin
// list, leaving `current` empty).
let arena = Arena::builder().max_normal_alloc(5000).build();
let _ = arena.alloc_str("x".repeat(5000));
let _ = arena.alloc_str("y");
assert_eq!(
arena.stats().normal_chunks_allocated,
1,
"boundary alloc_str must route via inner refill (which keeps the chunk as `current`), not the oversized pin path"
);
}
#[test]
fn alloc_str_simple_ref_past_max_normal_alloc_uses_oversized() {
// Past-boundary sanity: kills `> → <` / `> → ==` on the same line
// — any flip leaves the oversized path unused for a strictly
// larger string, breaking the no-cap promise of `alloc_str`.
let arena = Arena::builder().max_normal_alloc(5000).build();
let _ = arena.alloc_str("x".repeat(5001));
assert!(arena.stats().oversized_chunks_allocated >= 1);
}
// ---------------------------------------------------------------------------
// alloc_utf16.rs:63 — `if total > max_normal_alloc` in
// `try_alloc_utf16_prefixed_shared` for sizes past `MAX_CHUNK_BYTES`.
// Kills `>` → `<` on inputs where the mutant would route through
// `refill_shared`, which rejects `total > MAX_CHUNK_BYTES` outright.
// ---------------------------------------------------------------------------
#[cfg(feature = "utf16")]
#[test]
fn alloc_utf16_str_arc_above_max_chunk_bytes_uses_oversized() {
use widestring::Utf16Str;
// Shrink `max_normal_alloc` to its minimum (4 KiB) so a small
// (4 KiB + 1)-byte payload triggers the oversized routing
// without having to actually copy 80 KiB under Miri. The
// mutation under test is `min_payload > max_normal_alloc`
// → `<` in `ChunkProvider::acquire_shared`: with `<`, a
// request in the gap `(max_normal_alloc, MAX_CHUNK_BYTES]`
// wrongly routes to the normal cache instead of oversized,
// and `oversized_chunks_allocated == 0` fails the
// assertion.
let arena: Arena = Arena::builder().max_normal_alloc(4096).build();
// 2049 u16s = 4098 payload bytes, strictly above 4 KiB.
let len_u16 = 2049_usize;
let buf: Vec<u16> = vec![u16::from(b'a'); len_u16];
let src = Utf16Str::from_slice(&buf).unwrap();
let arc = arena.alloc_utf16_str_arc(src);
assert_eq!(arc.len(), len_u16);
assert!(arena.stats().oversized_chunks_allocated >= 1);
}
// ---------------------------------------------------------------------------
// inner_value.rs:805 — `if cur_chunk_addr != chunk_addr` chunk-eviction check
// in `impl_alloc_inner_with`. Mutant `==` would take the eviction path when
// chunks are equal (the common no-eviction case), corrupting state. Any
// `alloc_with` of `T: Drop` exercises this check.
// ---------------------------------------------------------------------------
#[test]
fn alloc_with_drop_type_no_eviction_returns_correct_value() {
struct DropProbe(u64);
impl Drop for DropProbe {
fn drop(&mut self) {}
}
let arena: Arena = Arena::new();
let r = arena.alloc_with::<DropProbe, _>(|| DropProbe(0x1234_5678_9abc_def0));
assert_eq!(r.0, 0x1234_5678_9abc_def0);
}
// ---------------------------------------------------------------------------
// internals.rs:302 — `align_up`'s arithmetic mutations.
// `value.saturating_add(align - 1) & !(align - 1)` —
// mutants `- → +/`/`, `& → |/^` would all return wrong aligned values
// for any `value` that isn't already aligned. Allocate a `u128`
// (align=16) right after a `u8` to force a non-trivial alignment step.
// ---------------------------------------------------------------------------
#[test]
fn align_up_used_by_oversized_dst_alloc_produces_aligned_pointer() {
use allocator_api2::alloc::{Allocator, Layout};
// Going through `&Arena as Allocator::allocate` reaches
// `Arena::allocate_layout` (in `arena/primitives.rs`), which is
// the call site that uses the standalone `align_up` helper.
// (The slice / value fast paths inline the same arithmetic via
// `try_bump_fit`, not via `align_up`.)
let arena: Arena = Arena::new();
let allocator: &Arena = &arena;
// A 16-byte-aligned, 48-byte layout. Original `align_up` rounds
// the chunk's data pointer up to a 16-aligned address; the
// `& → |/^` and `- → +/`/` mutants compute the wrong mask and
// return an address whose low 4 bits are non-zero.
let layout = Layout::from_size_align(48, 16).unwrap();
let p = allocator.allocate(layout).unwrap();
let addr = p.as_ptr().cast::<u8>() as usize;
assert_eq!(addr % 16, 0, "align_up must produce a 16-aligned pointer");
// Deallocate so the chunk reclaims its refcount; otherwise Miri (and
// any leak-aware allocator) would flag the chunk as leaked.
// SAFETY: `p` came from `allocator.allocate(layout)` with the same layout.
unsafe { allocator.deallocate(p.cast(), layout) };
}
// ---------------------------------------------------------------------------
// alloc_utf16.rs:25 / :63 — `> → <` mutation: at boundary `total < max_normal_alloc`
// the original `>` is false (fast path, normal chunk); the `<` mutant is
// true and routes to the outer oversized helper → an oversized chunk gets
// allocated even though a normal chunk would have served. Detect via the
// `oversized_*_chunks_allocated` counters.
// ---------------------------------------------------------------------------
#[cfg(feature = "utf16")]
#[test]
fn alloc_utf16_str_arc_small_string_stays_in_normal_chunk() {
use widestring::Utf16Str;
let arena: Arena = Arena::new();
let buf: Vec<u16> = vec![u16::from(b'b'); 10];
let src = Utf16Str::from_slice(&buf).unwrap();
let arc = arena.alloc_utf16_str_arc(src);
assert_eq!(arc.len(), 10);
assert_eq!(
arena.stats().oversized_chunks_allocated,
0,
"small utf16 alloc must take the fast path, not the outer oversized helper (shared)"
);
}
// Mirror small-alloc tests for `alloc_str_rc/_box/_arc` so the `> → <`
// mutation on those boundary checks is also caught.
#[test]
fn alloc_str_box_small_stays_in_normal_chunk() {
let arena = Arena::new();
let b = arena.alloc_str_box("world");
assert_eq!(b.len(), 5);
assert_eq!(
arena.stats().oversized_chunks_allocated,
0,
"small str alloc must take the fast path"
);
}
#[test]
fn alloc_str_arc_small_stays_in_normal_chunk() {
let arena: Arena = Arena::new();
let arc: Arc<str> = arena.alloc_str_arc("test");
assert_eq!(arc.len(), 4);
assert_eq!(
arena.stats().oversized_chunks_allocated,
0,
"small str alloc must take the fast path (shared)"
);
}
// ---------------------------------------------------------------------------
// owned_in_chunk.rs:82 / :128 — `Drop` impls of `OwnedIn{Local,Shared}Chunk`
// release one chunk refcount. The mutant replaces the body with `()` so the
// refcount stays bumped → chunk leaks. Detect via `total_bytes_allocated`
// staying nonzero (chunk never freed) plus a follow-up alloc that has to
// allocate a NEW chunk (because the leaked one keeps the cache empty).
// ---------------------------------------------------------------------------
#[test]
fn drop_of_owned_in_chunk_decrements_refcount_releases_chunk() {
use multitude::Arc;
let arena: Arena = Arena::new();
let arc: Arc<u64> = arena.alloc_arc(7_u64);
assert_eq!(*arc, 7);
drop(arc);
drop(arena);
}
}
mod coverage_arena_gaps {
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(
clippy::large_stack_arrays,
reason = "tests deliberately use large allocations to drive oversized paths"
)]
#![allow(clippy::undocumented_unsafe_blocks, reason = "test code")]
extern crate alloc;
use allocator_api2::alloc::Global;
use multitude::{Arc, Arena, ArenaBuilder};
#[cfg(feature = "std")]
use crate::common;
// ============================================================================
// Helpers
// ============================================================================
/// Half-chunk-aligned (`MAX_SMART_PTR_ALIGN`) type without `Drop`.
/// Used to drive over-alignment rejection in `_with` family functions
/// without ever instantiating the value on the test stack frame
/// (the over-alignment guard fires before the closure is invoked).
#[cfg(not(utc_backend))]
#[repr(align(32768))]
#[derive(Clone, Copy)]
struct HalfChunkAlign;
/// Chunk-aligned (`CHUNK_ALIGN`) Copy type used to drive the
/// `layout.align() >= CHUNK_ALIGN` guard in the slice-copy family.
/// Same Windows-stack caveat as [`HalfChunkAlign`]: never lives on
/// the test stack.
#[cfg(not(utc_backend))]
#[repr(align(65536))]
#[derive(Clone, Copy)]
struct ChunkAlign;
// ============================================================================
// alloc_value.rs:319 — `try_alloc` (simple reference) success path.
// ============================================================================
#[test]
fn try_alloc_simple_ref_returns_mutable_reference() {
let arena = Arena::<Global>::new();
let mut r = arena.try_alloc(42_u32).unwrap();
assert_eq!(*r, 42);
*r = 7;
assert_eq!(*r, 7);
}
// ============================================================================
// alloc_uninit.rs:231,233,235 — `try_alloc_uninit_arc` success path.
// The existing coverage tests only exercise the failure paths
// (failing-allocator and over-aligned).
// ============================================================================
#[test]
fn try_alloc_uninit_arc_succeeds() {
let arena = Arena::<Global>::new();
let arc = arena.try_alloc_uninit_arc::<u32>().unwrap();
// Just checking that the Arc is well-formed. The MaybeUninit's
// content is not initialized; dropping the Arc only releases the
// chunk slot via `noop_drop_shim`.
drop(arc);
}
// ============================================================================
// inner_value.rs:43, 83–114 — `try_alloc_inner_arc_with` `needs_drop` branch.
// The integration suite tests `try_alloc_arc` only with `!needs_drop`
// types (`u32` etc.), so the `entry_size > 0` arc fast path is
// unobserved.
// ============================================================================
#[cfg(feature = "std")]
#[test]
fn try_alloc_arc_with_needs_drop_value_runs_drop_on_arena_teardown() {
use std::sync::Mutex;
struct D(&'static Mutex<u32>);
impl Drop for D {
fn drop(&mut self) {
*self.0.lock().unwrap() += 1;
}
}
static DROPS: Mutex<u32> = Mutex::new(0);
let baseline = *DROPS.lock().unwrap();
let arena = Arena::<Global>::new();
let a = arena.try_alloc_arc(D(&DROPS)).unwrap();
drop(a);
drop(arena);
assert_eq!(
*DROPS.lock().unwrap() - baseline,
1,
"needs-drop arc fast path must install a real drop shim"
);
}
// ============================================================================
// inner_value.rs:50 — `try_alloc_inner_arc_with` oversized routing.
// Exercises `try_alloc_inner_arc_oversized_with` via the try-Arc
// surface (the existing oversized arc tests use the `alloc_arc`
// panicking wrapper, which routes through a different function).
// ============================================================================
#[test]
fn try_alloc_arc_oversized_value_succeeds() {
let arena = Arena::<Global>::new();
let arc = arena.try_alloc_arc([7_u8; 70_000]).unwrap();
assert_eq!(arc[0], 7);
assert_eq!(arc[69_999], 7);
}
// ============================================================================
// inner_value.rs:147 — `alloc_inner_arc_with_or_panic` over-alignment panic.
// ============================================================================
#[cfg(not(utc_backend))]
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_arc_with_over_aligned_panics() {
let arena = Arena::<Global>::new();
let _ = arena.alloc_arc_with::<HalfChunkAlign, _>(|| HalfChunkAlign);
}
// ============================================================================
// inner_value.rs:770 — `try_alloc_inner_with` oversized routing.
// The closure form ensures we exercise `try_alloc_inner_with` itself
// rather than the by-value `try_alloc_inner_value` path.
// ============================================================================
#[test]
fn try_alloc_with_oversized_value_succeeds() {
let arena = Arena::<Global>::new();
let r = arena.try_alloc_with(|| [3_u8; 70_000]).unwrap();
assert_eq!(r[0], 3);
assert_eq!(r[69_999], 3);
}
// ============================================================================
// inner_value.rs:927 — `alloc_inner_with_or_panic` over-alignment panic.
// ============================================================================
#[cfg(not(utc_backend))]
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_with_over_aligned_panics() {
let arena = Arena::<Global>::new();
let _ = arena.alloc_with(|| HalfChunkAlign);
}
#[cfg(not(utc_backend))]
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_box_with_over_aligned_panics() {
let arena = Arena::<Global>::new();
let _ = arena.alloc_box_with::<HalfChunkAlign, _>(|| HalfChunkAlign);
}
// ============================================================================
// inner_slice.rs:430 — `alloc_slice_local_with_or_panic` over-alignment panic.
// inner_slice.rs:1003 — shared sibling.
// inner_slice.rs:769 — `alloc_slice_local_copy_or_panic` over-alignment panic.
//
// The panicking *_with helpers are reached via `alloc_uninit_box` /
// `alloc_uninit_rc` (local) and `alloc_uninit_arc` (shared). Their
// over-alignment check fires before any closure runs, so no value
// ever lives on the test stack frame.
// ============================================================================
#[cfg(not(utc_backend))]
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_uninit_box_over_aligned_panics() {
let arena = Arena::<Global>::new();
let _ = arena.alloc_uninit_box::<HalfChunkAlign>();
}
#[cfg(not(utc_backend))]
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_uninit_arc_over_aligned_panics() {
let arena = Arena::<Global>::new();
let _ = arena.alloc_uninit_arc::<HalfChunkAlign>();
}
#[cfg(not(utc_backend))]
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_slice_copy_over_aligned_panics() {
let arena = Arena::<Global>::new();
// Empty slice of a `CHUNK_ALIGN`-aligned `Copy` type triggers the
// `layout.align() >= CHUNK_ALIGN` guard at the top of
// `alloc_slice_local_copy_or_panic` without instantiating a value.
let src: &[ChunkAlign] = &[];
let _ = arena.alloc_slice_copy(src);
}
// ============================================================================
// inner_slice.rs:550 — `try_alloc_slice_local_no_drop_with` over-alignment.
// inner_slice.rs:667 — `try_alloc_slice_local_copy` over-alignment.
// inner_slice.rs:833 — `try_alloc_slice_shared_copy` over-alignment.
// ============================================================================
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_slice_no_drop_over_aligned_returns_err() {
let arena = Arena::<Global>::new();
// `try_alloc_slice_fill_with` routes through
// `try_alloc_slice_local_no_drop_with` for `!needs_drop` T. The cap
// for the reference is `CHUNK_ALIGN` (the chunk-recovery
// limit), not the smart-pointer cap — so use a 64 KiB-aligned
// type to drive the rejection.
let res = arena.try_alloc_slice_fill_with::<ChunkAlign, _>(1, |_| ChunkAlign);
assert!(res.is_err());
}
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_slice_copy_over_aligned_returns_err() {
let arena = Arena::<Global>::new();
let src: &[ChunkAlign] = &[];
let res = arena.try_alloc_slice_copy(src);
assert!(res.is_err());
}
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_slice_copy_arc_over_aligned_returns_err() {
let arena = Arena::<Global>::new();
let src: &[ChunkAlign] = &[];
let res = arena.try_alloc_slice_copy_arc(src);
assert!(res.is_err());
}
// ============================================================================
// Per-`Arc` reference counting removes the `u16` element-count cap on
// `Arc<[T]>` slices: a Drop-typed slice longer than `u16::MAX` is now
// dropped via `drop_in_place::<[T]>` in `Arc::drop`, not a counted
// chunk drop entry, so it allocates successfully.
// ============================================================================
#[cfg(all(feature = "std", not(miri)))]
#[test]
fn alloc_slice_fill_with_arc_drop_long_succeeds() {
#[derive(Clone)]
struct D;
#[expect(clippy::empty_drop, reason = "Drop impl makes needs_drop::<D>() true")]
impl Drop for D {
fn drop(&mut self) {}
}
let arena = Arena::<Global>::new();
let arc = arena.alloc_slice_fill_with_arc(u16::MAX as usize + 1, |_| D);
assert_eq!(arc.len(), u16::MAX as usize + 1);
}
// ============================================================================
// inner_slice.rs:443–444 — `alloc_slice_local_with_or_panic` oversized.
// inner_slice.rs:553–554 — `try_alloc_slice_local_no_drop_with` oversized.
// inner_slice.rs:670–671 — `try_alloc_slice_local_copy` oversized.
// inner_slice.rs:774–775 — `alloc_slice_local_copy_or_panic` oversized.
// inner_slice.rs:836–837 — `try_alloc_slice_shared_copy` oversized.
// inner_slice.rs:1016–1017 — `alloc_slice_shared_with_or_panic` oversized.
// ============================================================================
#[test]
fn try_alloc_slice_fill_with_oversized() {
let arena = Arena::builder().max_normal_alloc(4096).build();
let slice = arena.try_alloc_slice_fill_with(2048, |i| u32::try_from(i).unwrap()).unwrap();
assert_eq!(slice[0], 0);
assert_eq!(slice[2047], 2047);
}
#[test]
fn try_alloc_slice_copy_oversized() {
let arena = Arena::builder().max_normal_alloc(4096).build();
let src: alloc::vec::Vec<u32> = (0..2048_u32).collect();
let slice = arena.try_alloc_slice_copy(&*src).unwrap();
assert_eq!(slice[0], 0);
assert_eq!(slice[2047], 2047);
}
#[test]
fn alloc_slice_copy_oversized() {
let arena = Arena::builder().max_normal_alloc(4096).build();
let src: alloc::vec::Vec<u32> = (0..2048_u32).collect();
let slice = arena.alloc_slice_copy(&*src);
assert_eq!(slice[0], 0);
assert_eq!(slice[2047], 2047);
}
#[test]
fn try_alloc_slice_copy_arc_oversized() {
let arena = Arena::builder().max_normal_alloc(4096).build();
let src: alloc::vec::Vec<u32> = (0..2048_u32).collect();
let arc = arena.try_alloc_slice_copy_arc(&*src).unwrap();
assert_eq!(arc[0], 0);
assert_eq!(arc[2047], 2047);
}
#[test]
fn alloc_slice_fill_with_arc_oversized() {
let arena = Arena::builder().max_normal_alloc(4096).build();
let arc: Arc<[u32]> = arena.alloc_slice_fill_with_arc(2048, |i| u32::try_from(i).unwrap());
assert_eq!(arc[0], 0);
assert_eq!(arc[2047], 2047);
}
// ============================================================================
// inner_value.rs:481 — `alloc_inner_value_or_panic` over-alignment
// panic. Reached via the by-value `alloc`/`alloc_rc`/`alloc_box`
// entry points. Using a ZST with high alignment keeps the value's
// stack footprint at zero bytes so the Windows chkstk-on-large-align
// hazard that blocks the `Drop`-typed `TooAligned` tests in
// `coverage_more.rs` does not apply.
// ============================================================================
// ============================================================================
// inner_value.rs:39 — `try_alloc_inner_arc_with` over-alignment err path.
// Closure form avoids placing a high-alignment value on the test
// stack frame (the guard fires before the closure is invoked).
// ============================================================================
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_arc_with_over_aligned_returns_err() {
let arena = Arena::<Global>::new();
let res = arena.try_alloc_arc_with::<HalfChunkAlign, _>(|| HalfChunkAlign);
assert!(res.is_err());
}
// ============================================================================
// inner_value.rs:1000–1011 — `alloc_inner_with_or_panic`
// closure-induced eviction commit path. A reentrant allocation that
// fills the current chunk during the closure forces a refill
// that evicts the chunk the outer allocation reserved on; the outer
// then takes the cold `commit_alloc_after_eviction` branch.
// ============================================================================
#[cfg(feature = "std")]
#[test]
fn alloc_with_closure_induced_eviction_commits_drop_entry() {
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicU32, Ordering};
// `Send` drop type (the deferred-drop `alloc_with` path requires
// `T: Send`; a `!Send` value here would be a soundness hazard at
// cross-thread arena teardown).
struct D(StdArc<AtomicU32>);
impl Drop for D {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let drops = StdArc::new(AtomicU32::new(0));
let arena = Arena::builder().max_normal_alloc(4096).build();
// Warm up so the outer `alloc_with` below takes the fast path
// (the cold slow path bypasses the eviction-commit branch).
let _ = arena.alloc::<u64>(0);
let counter = drops.clone();
let arena_ref = &arena;
let outer = arena.alloc_with(move || {
// Fill the current chunk so the OUTER allocation's
// reserved slot ends up in a chunk that gets evicted before
// the closure returns. The outer must then take the
// `commit_alloc_after_eviction` branch.
// 2048 u64 allocs still force multiple refills with this 4 KiB
// normal-allocation cap, so the reserved chunk is evicted.
for _ in 0..2048_u32 {
let _ = arena_ref.alloc::<u64>(0);
}
D(counter)
});
drop(outer);
drop(arena);
assert_eq!(drops.load(Ordering::Relaxed), 1, "outer D's drop must run via eviction commit path");
}
#[test]
fn refill_local_oversized_chunk_capacity() {
// `with_capacity` preallocates space; verify the arena
// works correctly when a generous capacity is requested.
let arena = Arena::builder().with_capacity(128 * 1024).build();
let _ = arena.alloc::<u8>(0);
}
#[test]
// ICE in Miri's weak-memory model (src/tools/miri/src/concurrency/
// weak_memory.rs:233 — "cannot have empty store buffer when previous
// write was atomic"). Skip under Miri until upstream Miri is fixed;
// the regular test runner exercises this path.
#[cfg_attr(miri, ignore)]
fn refill_shared_oversized_chunk_capacity() {
let arena = Arena::builder().with_capacity(128 * 1024).build();
let _ = arena.alloc_arc::<u8>(0);
}
// ============================================================================
// inner_slice.rs:788 — `alloc_slice_local_copy_or_panic` panics when
// the cold-refill path returns `Err`. Exercised with
// `FailingAllocator` configured to fail after the first chunk.
// ============================================================================
#[cfg(feature = "std")]
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_slice_copy_panics_when_refill_fails() {
let alloc = common::FailingAllocator::new(1);
let arena = Arena::new_in(alloc);
// Consume the first chunk's bump space, then force a refill that
// the exhausted allocator cannot satisfy.
let _filler = arena.alloc_slice_fill_with::<u8, _>(256, |_| 0);
let src: alloc::vec::Vec<u8> = alloc::vec![0_u8; 4096];
let _ = arena.alloc_slice_copy(&*src);
}
// ============================================================================
// primitives.rs:243 — `try_install_slice_drop_entry` returns false
// when the value's chunk is no longer the current chunk.
// Reached via `Vec::into_arena_rc`'s freeze-fast-path when an
// intervening allocation has evicted the chunk hosting the buffer.
// ============================================================================
}
#[cfg(feature = "stats")]
mod from_mutants_extras_stats {
#![allow(clippy::items_after_statements, reason = "relocated tests put inner types near use")]
#![allow(clippy::clone_on_ref_ptr, reason = "relocated tests use .clone() on Arc/Rc")]
#![allow(dead_code, reason = "relocated helpers retain fields for layout")]
#![allow(
unfulfilled_lint_expectations,
reason = "relocated #[expect] may be fulfilled at file or feature level"
)]
#![allow(
clippy::undocumented_unsafe_blocks,
reason = "relocated test bodies preserve original safety reasoning"
)]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "relocated tests group related unsafe ops")]
#![allow(clippy::cast_possible_truncation, reason = "relocated tests use bounded values")]
#![allow(clippy::cast_sign_loss, reason = "relocated tests use non-negative values")]
#![allow(clippy::empty_drop, reason = "relocated tests use empty Drop impls to mark dropability")]
#![allow(clippy::assertions_on_result_states, reason = "relocated tests deliberately assert error returns")]
#![allow(clippy::empty_line_after_doc_comments, reason = "relocated test doc-comments")]
use multitude::Box as ArenaBox;
#[repr(align(64))]
#[derive(Debug)]
#[expect(dead_code, reason = "helper for relocated over-alignment tests")]
struct Align64(u32);
use multitude::vec::Vec as ArenaVec;
use multitude::{Arc, Arena, ArenaBuilder};
#[expect(unused_imports, reason = "relocated tests may reference common helpers")]
use crate::common::{self, DropCounter, FailingAllocator, SendFailingAllocator};
#[test]
fn preallocate_one_shared_actually_allocates_chunk() {
let arena = Arena::builder().with_capacity(1024).build();
assert!(
arena.stats().normal_chunks_allocated >= 1,
"with_capacity(1024) must preallocate at least one chunk"
);
}
#[test]
fn resolve_capacity_uses_correct_class_minus_one_clamp() {
// 128 KiB > MAX_CHUNK_BYTES (= 64 KiB), so target_class saturates
// at NUM_CHUNK_CLASSES - 1 = 7 → 64 KiB chunks → 2 chunks.
let arena = Arena::builder().with_capacity(128 * 1024).build();
assert_eq!(
arena.stats().normal_chunks_allocated,
2,
"128 KiB shared capacity should preallocate exactly two 64 KiB chunks"
);
// Same for local, to exercise the equivalent path through
// `preallocate_one_local`.
let arena2 = Arena::builder().with_capacity(128 * 1024).build();
assert_eq!(arena2.stats().normal_chunks_allocated, 2);
}
#[test]
fn oversized_shared_guard_drop_releases_on_panic() {
// Each oversized arc below uses an 8 KiB blob (1024 u64s) which
// is still > max_normal_alloc(4096) and so routes oversized.
// The byte budget is sized to fit exactly one chunk plus a small
// overhead — *not* two. With the guard's drop running, the
// panicked chunk's bytes are released and the second arc fits;
// with the drop no-op'd, the budget is exhausted and the second
// try_alloc returns AllocError.
//
// We deliberately keep the payload modest so debug-build callers
// (e.g. `cargo careful`) don't stack-overflow on the closure's
// by-value return type before the panic-on-drop guard logic runs.
//
// Tight byte_budget — accommodates exactly one 8 KiB oversized
// chunk + header overhead, not two.
let arena = Arena::builder().byte_budget(18 * 1024).max_normal_alloc(4096).build();
// Trigger the panic-during-init oversized path on the Arc/Box.
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _ = arena.try_alloc_arc_with::<[u64; 1024], _>(|| panic!("boom"));
}));
assert!(result.is_err(), "panic must propagate");
// Stats should reflect that a chunk was allocated *and*
// reconciled (not leaked).
let stats_after_panic = arena.stats();
assert!(
stats_after_panic.oversized_chunks_allocated >= 1,
"first arc alloc should have acquired an oversized chunk"
);
// A second oversized arc must succeed under the tight budget,
// proving the first chunk's bytes were credited back. With the
// guard's drop a no-op, the leaked chunk pins the budget and
// this allocation returns Err.
let arc_ok = arena.try_alloc_arc::<[u64; 1024]>([7_u64; 1024]);
assert!(
arc_ok.is_ok(),
"second oversized arc must succeed after first panic (budget released)"
);
}
#[test]
fn min_class_for_bytes_classifies_513_below_saturation() {
// 4 KiB budget easily covers (header + 1 KiB) but not a 64 KiB chunk.
let res = Arena::builder().byte_budget(4 * 1024).with_capacity(513).try_build();
assert!(res.is_ok(), "513 must resolve to class 1 (1 KiB), fitting a 4 KiB budget");
}
#[test]
fn min_class_inner_loop_uses_strict_less() {
// Probe: an unbudgeted arena easily allocates a 1 KiB chunk and a
// 2 KiB chunk; the difference is observed only via the budget.
// (Header is bounded; we pick numbers with comfortable margin.)
let ok = Arena::builder().byte_budget(1500).with_capacity(513).try_build();
assert!(
ok.is_ok(),
"513 must resolve to class 1 (1 KiB); a budget of 1500 bytes (>1 KiB) admits one chunk"
);
}
#[test]
fn reserve_budget_admits_exact_equal() {
fn ok(b: usize) -> bool {
Arena::builder().byte_budget(b).with_capacity(512).try_build().is_ok()
}
fn bisect(probe: impl Fn(usize) -> bool) -> usize {
let (mut lo, mut hi) = (1_usize, 64 * 1024_usize);
assert!(probe(hi));
while lo + 1 < hi {
let m = usize::midpoint(lo, hi);
if probe(m) {
hi = m;
} else {
lo = m;
}
}
hi
}
// `exact` is the smallest budget that admits one preallocated 512-byte
// chunk — i.e. its total allocation size. The budget check is
// `new > byte_budget`, so a budget exactly equal to the total must
// admit and one byte less must reject.
let exact = bisect(ok);
assert!(ok(exact), "byte_budget == chunk total must admit (exact-equal under `>`)");
assert!(
!ok(exact - 1),
"byte_budget == chunk total - 1 must reject under unmutated `>` (exact={exact})"
);
}
#[test]
fn release_budget_frees_accounted_bytes() {
let arena = Arena::builder().byte_budget(128 * 1024).max_normal_alloc(4 * 1024).build();
let big1 = arena.alloc_box([0u8; 80 * 1024]);
let s1 = arena.stats();
assert_eq!(s1.oversized_chunks_allocated, 1);
drop(big1);
let big2 = arena.alloc_box([0u8; 80 * 1024]);
let s2 = arena.stats();
assert_eq!(s2.oversized_chunks_allocated, 2);
drop(big2);
drop(arena);
}
#[test]
fn acquire_shared_total_bytes_is_sum_not_product() {
let arena = Arena::builder().byte_budget(2 * 1024).build();
// First arc forces a fresh chunk. Unmutated header + 512
// <= 2 KiB succeeds; mutated header * 512 >> 2 KiB fails.
let res = arena.try_alloc_arc(0u32);
assert!(res.is_ok(), "header + payload must sum (not multiply) for budget check");
}
#[test]
fn arc_with_size_equal_max_normal_routes_normal() {
let arena = Arena::builder().max_normal_alloc(4096).build();
#[repr(align(8))]
struct Block([u64; 512]); // 4096 bytes exactly
let _a = arena.alloc_arc(Block([0u64; 512]));
let s = arena.stats();
assert!(s.normal_chunks_allocated + s.oversized_chunks_allocated >= 1);
}
#[test]
fn oversized_shared_guard_drop_releases_chunk_on_panic() {
let arena = Arena::builder().byte_budget(256 * 1024).max_normal_alloc(4096).build();
// First oversized arc with a panicking initialiser.
let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
// 8 KiB arc whose init panics.
let _a: multitude::Arc<[u64; 1024]> = arena.alloc_arc_with(|| panic!("test"));
}));
assert!(res.is_err());
// Without the Drop running reconcile_swap_out, the chunk is
// unreleased and the byte_budget is exhausted. We probe via
// a second oversized arc.
let _a2: multitude::Arc<[u64; 1024]> = arena.alloc_arc([0u64; 1024]);
let s = arena.stats();
assert_eq!(s.oversized_chunks_allocated, 2);
}
#[test]
fn arena_728_exact_max_normal_alloc_arc() {
let arena = Arena::builder().max_normal_alloc(4096).build();
let _arc = arena.alloc_arc([0u8; 4096]);
let stats = arena.stats();
assert!(stats.normal_chunks_allocated + stats.oversized_chunks_allocated >= 1);
}
/// Verifies the one-shot oversized routing for chunks at the
/// `max_normal_alloc` boundary.
///
/// `try_alloc_uninit_slice_arc::<u8>(max_normal_alloc)` reserves a
/// length prefix + drop-entry placeholder on top of the payload, so
/// the worst-case payload exceeds `max_normal_alloc` and routes to
/// a dedicated one-shot oversized chunk. With the one-shot fix in
/// place, that chunk is **not** installed as `current`, so a
/// subsequent small `Arc<u8>` allocation forces refilling
/// `current` with a fresh normal chunk.
#[test]
fn alloc_slice_arc_at_max_normal_alloc_uses_dedicated_oversized_chunk() {
const MAX_NORMAL: usize = 16 * 1024;
let arena = Arena::builder().max_normal_alloc(MAX_NORMAL).build();
let before_normal = arena.stats().normal_chunks_allocated;
let before_oversized = arena.stats().oversized_chunks_allocated;
let big = arena
.try_alloc_uninit_slice_arc::<u8>(MAX_NORMAL)
.expect("alloc at max_normal_alloc must succeed");
assert_eq!(big.len(), MAX_NORMAL);
let after_big_normal = arena.stats().normal_chunks_allocated;
let after_big_oversized = arena.stats().oversized_chunks_allocated;
assert_eq!(
after_big_oversized - before_oversized,
1,
"boundary slice must come from a dedicated one-shot oversized chunk",
);
assert_eq!(
after_big_normal, before_normal,
"oversized routing must not touch the normal-chunk count",
);
let tiny = arena.alloc_arc(0_u8);
assert_eq!(*tiny, 0);
let after_tiny_normal = arena.stats().normal_chunks_allocated;
let after_tiny_oversized = arena.stats().oversized_chunks_allocated;
assert_eq!(
after_tiny_normal - after_big_normal,
1,
"follow-up tiny Arc must refill `current` with a fresh normal chunk",
);
assert_eq!(
after_tiny_oversized, after_big_oversized,
"follow-up tiny Arc must not allocate another oversized chunk",
);
}
#[test]
fn alloc_slice_just_above_max_normal_alloc_uses_oversized_path_shared() {
let arena = Arena::builder().max_normal_alloc(8 * 1024).build();
let before = arena.stats().oversized_chunks_allocated;
let n = (8 * 1024) / core::mem::size_of::<u32>() + 1;
let _a: Arc<[u32]> = arena.alloc_slice_fill_with_arc(n, |_| 0_u32);
let after = arena.stats().oversized_chunks_allocated;
assert_eq!(after - before, 1);
}
#[test]
fn vec_realloc_first_growth_does_not_count_as_relocation() {
// The first realloc happens when `old_cap == 0` (allocating the
// initial buffer). The `if old_cap > 0` gate prevents counting this
// as a relocation. Mutants that change the guard to `>=` or `==`
// would either count the initial alloc as a relocation or skip a
// real one.
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec();
v.push(0);
let r1 = arena.stats().relocations;
// First push triggered the initial allocation; that's not a
// relocation.
assert_eq!(r1, 0);
// Subsequent grows that move the buffer are relocations.
for i in 1..1000_u32 {
v.push(i);
}
assert!(arena.stats().relocations >= 1);
}
#[test]
fn vec_resize_with_reserves_exactly_required_amount() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u8> = arena.alloc_vec();
v.push(0_u8);
// After this resize, the vec's capacity must be >= 4.
v.resize_with(4, || 99_u8);
assert_eq!(v.as_slice(), &[0_u8, 99, 99, 99]);
// The Vec only requested `new_len - self.len == 3` extra bytes.
// With the mutant `new_len + self.len == 5` would over-allocate
// (harmless but technically different). Hard to detect
// deterministically — capacity is amortized to a power of 2.
}
#[test]
fn arena_builder_capacity_preallocates_correct_chunk_count() {
use multitude::ArenaBuilder;
let arena: Arena = Arena::builder().with_capacity(64 * 1024).build();
// Preallocation creates >= 1 chunk before any user allocation.
assert!(arena.stats().normal_chunks_allocated >= 1);
}
#[test]
fn chunk_release_returns_budget() {
use multitude::ArenaBuilder;
let arena: Arena = Arena::builder().byte_budget(64 * 1024 * 1024).build();
for _ in 0..32 {
let a: Arc<u32> = arena.alloc_arc(7);
drop(a);
}
// After many alloc-drop cycles, the running budget shouldn't have
// monotonically grown (it must drop back as chunks are released).
assert!(arena.stats().normal_chunks_allocated > 0);
}
#[test]
fn small_arc_allocations_do_not_use_oversized_chunks() {
let arena = Arena::new();
for i in 0_u32..256 {
let _a: Arc<u32> = arena.alloc_arc(i);
}
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
}
#[test]
fn small_box_allocations_do_not_use_oversized_chunks() {
let arena = Arena::new();
for i in 0_u32..256 {
let _b: ArenaBox<u32> = arena.alloc_box(i);
}
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
}
#[test]
fn small_aligned_allocations_do_not_use_oversized_chunks() {
let arena = Arena::new();
for _ in 0..32 {
let _a: Arc<Align64> = arena.alloc_arc(Align64(0));
}
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
}
#[test]
fn small_drop_arc_allocations_do_not_use_oversized_chunks() {
use core::cell::Cell;
struct D<'a>(&'a Cell<u32>);
impl Drop for D<'_> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
// SAFETY: read-only counter shared via reference.
unsafe impl Send for D<'_> {}
unsafe impl Sync for D<'_> {}
let c = Cell::new(0);
let arena = Arena::new();
for _ in 0..32 {
let _a: Arc<D<'_>> = arena.alloc_arc(D(&c));
}
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
}
#[test]
fn slow_path_arc_allocs_do_not_use_oversized_chunks() {
let arena = Arena::new();
// Ratchet the chunk class via a few large uninit fillers
// (`alloc_uninit_arc` skips per-byte init cost).
for _ in 0..4 {
let _filler: Arc<core::mem::MaybeUninit<[u8; 8 * 1024]>> = arena.alloc_uninit_arc::<[u8; 8 * 1024]>();
}
// A short burst still exercises the small-allocation slow refill path
// at the peak chunk class.
for i in 0_u32..16 {
let _a: Arc<u32> = arena.alloc_arc(i);
}
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
}
#[test]
fn slow_path_drop_arc_allocs_do_not_use_oversized_chunks() {
use core::cell::Cell;
struct D<'a>(&'a Cell<u32>);
impl Drop for D<'_> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
// SAFETY: only references shared state via &Cell.
unsafe impl Send for D<'_> {}
unsafe impl Sync for D<'_> {}
let c = Cell::new(0);
let arena = Arena::new();
// Drive the chunk-class ratchet via a few large uninit
// allocations rather than 8 × 8 KiB filled allocs; under Miri,
// `alloc_uninit_arc` skips the per-byte init cost.
for _ in 0..2 {
let _filler: Arc<core::mem::MaybeUninit<[u8; 8 * 1024]>> = arena.alloc_uninit_arc::<[u8; 8 * 1024]>();
}
// A short burst still reaches the peak-class slow refill path; a
// mutated `needed` computation would route the first one oversized.
for _ in 0..32 {
let _a: Arc<D<'_>> = arena.alloc_arc(D(&c));
}
assert_eq!(arena.stats().oversized_chunks_allocated, 0);
}
#[test]
fn vec_into_box_allocates_no_additional_local_chunk() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
for i in 0..4_u32 {
v.push(i);
}
let chunks_before = arena.stats().normal_chunks_allocated;
let _b: ArenaBox<[u32]> = v.into_boxed_slice();
assert_eq!(arena.stats().normal_chunks_allocated, chunks_before);
}
#[test]
fn chunk_release_budget_remains_bounded_through_many_cycles() {
use multitude::ArenaBuilder;
let arena: Arena = Arena::builder().byte_budget(2 * 1024 * 1024).build();
// Any leak in the release-budget bookkeeping compounds linearly
// with the cycle count, so a handful of iterations is enough
// to expose a leak; the test gains nothing from a large count
// and Miri pays for every cycle.
for _ in 0..8 {
let _a: Arc<[u8; 1024]> = arena.alloc_arc([0_u8; 1024]);
}
}
}
#[cfg(feature = "stats")]
mod oversized_routing {
use multitude::Arena;
// is_oversized: threshold == max_normal_alloc routes via normal path
#[test]
fn is_oversized_routes_shared_at_threshold_via_normal() {
const MNA: usize = 4 * 1024;
let arena = Arena::builder().max_normal_alloc(MNA).build();
let before_normal = arena.stats().normal_chunks_allocated;
let before_oversized = arena.stats().oversized_chunks_allocated;
// wcp = MNA exactly: strong prefix (4) + value (MNA-8) + arc block align (4).
let _arc = arena.alloc_arc([0_u8; MNA - 8]);
let after_normal = arena.stats().normal_chunks_allocated;
let after_oversized = arena.stats().oversized_chunks_allocated;
assert!(after_normal > before_normal);
assert_eq!(
after_oversized, before_oversized,
"threshold must NOT route oversized (kills `>=` mutant)"
);
}
#[test]
fn is_oversized_routes_shared_above_threshold_via_oversized() {
const MNA: usize = 4 * 1024;
let arena = Arena::builder().max_normal_alloc(MNA).build();
let before_oversized = arena.stats().oversized_chunks_allocated;
// wcp = MNA + 1: strong prefix (4) + value (MNA-7) + arc block align (4).
let _arc = arena.alloc_arc([0_u8; MNA - 7]);
let after_oversized = arena.stats().oversized_chunks_allocated;
assert!(
after_oversized > before_oversized,
"above-threshold must route oversized (kills `==` mutant)"
);
}
#[test]
fn is_oversized_routes_local_at_threshold_via_normal() {
const MNA: usize = 4 * 1024;
let arena = Arena::builder().max_normal_alloc(MNA).build();
let before_normal = arena.stats().normal_chunks_allocated;
let before_oversized = arena.stats().oversized_chunks_allocated;
let s = "x".repeat(MNA);
let _r = arena.alloc_str(&s);
let after_normal = arena.stats().normal_chunks_allocated;
let after_oversized = arena.stats().oversized_chunks_allocated;
assert!(after_normal > before_normal);
assert_eq!(after_oversized, before_oversized, "threshold must NOT route oversized");
}
#[test]
fn is_oversized_routes_local_above_threshold_via_oversized() {
const MNA: usize = 4 * 1024;
let arena = Arena::builder().max_normal_alloc(MNA).build();
let before_oversized = arena.stats().oversized_chunks_allocated;
let s = "x".repeat(MNA + 1);
let _r = arena.alloc_str(&s);
let after_oversized = arena.stats().oversized_chunks_allocated;
assert!(after_oversized > before_oversized);
}
// Vec::shrink_to_fit boundary: total < mna must reclaim (catches `==`/`>=`
// mutants that would early-return at total == mna and below).
#[test]
fn shrink_to_fit_reclaims_strictly_below_max_normal_alloc() {
let mna = 4 * 1024;
let arena: Arena = Arena::builder().max_normal_alloc(mna).build();
// cap = mna - 16 ensures refill_hint = cap + 16 = mna <= mna, so the Vec
// is allocated in the normal current chunk (not oversized) and its end IS
// at the bump cursor. The freezable buffer reserves the `Arc<[u8]>` freeze
// prefix, so the hint is `cap + 16` (≈12B strong+len prefix + 4B
// alignment slack); `total_bytes` stays strictly below the threshold.
let cap = mna - 16;
let mut v: multitude::vec::Vec<'_, u8> = arena.alloc_vec_with_capacity(cap);
v.extend_from_slice([7_u8; 16]);
assert_eq!(v.capacity(), cap);
v.shrink_to_fit();
assert_eq!(v.capacity(), v.len(), "Vec strictly below max_normal_alloc must reclaim tail");
}
// Post-reset cache reuse for the floor-bump `==`/`<` mutants on
// chunk_provider:219. With the mutant in effect, the floor never
// advances (or only at no-op intervals), so post-reset the cache
// holds chunks of mixed (smaller) classes. A subsequent alloc at the
// saturated class then pops a smaller chunk, fails to fit, refills
// → allocates a fresh chunk. Original code: post-reset alloc at the
// saturated class pops a class-7 chunk and reuses it (no fresh alloc).
#[test]
fn local_cache_floor_advances_so_post_reset_alloc_reuses_chunk() {
let mut arena = Arena::new();
// Drive next_local_class up to its saturated value by issuing enough
// local refills. Each `alloc_str` of a string larger than the current
// chunk forces a refill and advances the ratchet.
let stride = 1024_usize;
for _ in 0..8 {
let s = "y".repeat(stride);
let _r = arena.alloc_str(&s);
}
let before_reset = arena.stats().normal_chunks_allocated;
arena.reset();
// After reset, retired_local clears → chunks go to cache. Floor
// should equal the saturated class so only saturated-class chunks
// are retained (smaller ones returned to system).
//
// Allocate a single small value: the refill triggers acquire_local
// at the saturated ratchet class. With the original code, the
// cache pops a saturated-class chunk → no fresh allocation.
// With `<` / `==` mutants, the floor never grew → cache holds
// mixed-class chunks → pop returns one but it might be too small
// → refill spin → MORE fresh allocations.
let _ = arena.alloc(0_u8);
let after_reset = arena.stats().normal_chunks_allocated;
// The fresh-alloc count should NOT explode after the small alloc.
// Original: at most 1 additional fresh alloc (cache miss for the
// saturated class). Mutant: many more as the alloc spins through
// smaller cached chunks that don't fit subsequent refills.
assert!(
after_reset - before_reset <= 1,
"post-reset alloc must reuse cached saturated-class chunk; got {} fresh allocs (kills floor-bump mutants)",
after_reset - before_reset,
);
}
// `config().max_normal_alloc` decides whether an allocation routes to the
// normal-cache size classes or to a one-shot oversized chunk. Set a
// non-default `max_normal_alloc` below the default `16 * 1024` and allocate
// at a size between the two: the config gates it to oversized.
#[test]
fn config_returns_custom_max_normal_alloc_local() {
// Default max_normal_alloc = 16 KiB. Set 4 KiB and request a 12 KiB
// local allocation: routes to oversized.
let arena: Arena = Arena::builder().max_normal_alloc(4 * 1024).build();
let baseline = arena.stats().oversized_chunks_allocated;
let _ = arena.alloc_slice_fill_with::<u8, _>(12 * 1024, |_| 0);
let after = arena.stats().oversized_chunks_allocated;
assert!(
after > baseline,
"12 KiB local allocation with 4 KiB max_normal_alloc must route to an oversized chunk; stats: {after} vs baseline {baseline}",
);
}
#[test]
fn config_returns_custom_max_normal_alloc_shared() {
let arena: Arena = Arena::builder().max_normal_alloc(4 * 1024).build();
let baseline = arena.stats().oversized_chunks_allocated;
// `alloc_str_box` runs through the shared-chunk path
// (`impl_alloc_str_box_prefixed_shared`).
let s: std::string::String = (0..12 * 1024).map(|_| 'a').collect();
let _ = arena.alloc_str_box(&s);
let after = arena.stats().oversized_chunks_allocated;
assert!(
after > baseline,
"12 KiB shared allocation with 4 KiB max_normal_alloc must route to an oversized chunk; stats: {after} vs baseline {baseline}",
);
}
// `if min_payload > self.config.max_normal_alloc { allocate_oversized }`:
// at `min_payload == max_normal_alloc` the allocation stays on the normal
// cache path.
#[test]
fn acquire_local_at_max_normal_alloc_boundary_stays_normal_class() {
let mna = 4 * 1024;
let arena: Arena = Arena::builder().max_normal_alloc(mna).build();
let baseline = arena.stats().oversized_chunks_allocated;
// `worst_case_slice_payload::<u8>(len) = len * 1 + align_of::<u8>()
// = len + 1`; choose `len == mna - 1` so the refill_hint =
// `min_payload` arrives at `acquire_local` exactly equal to
// `max_normal_alloc`, which stays on the normal path.
let len = mna - 1;
let _ = arena.alloc_slice_fill_with::<u8, _>(len, |_| 0);
let after = arena.stats().oversized_chunks_allocated;
assert_eq!(
after - baseline,
0,
"min_payload == max_normal_alloc must stay on the normal cache path",
);
}
#[test]
fn acquire_shared_at_max_normal_alloc_boundary_stays_normal_class() {
let mna = 4 * 1024;
let arena: Arena = Arena::builder().max_normal_alloc(mna).build();
let baseline = arena.stats().oversized_chunks_allocated;
// `impl_alloc_str_box_prefixed_shared` calls
// `refill_shared(words * size_of::<usize>())`. With
// `words = 1 + len.div_ceil(8).max(1)`, picking `len` so that
// `words * 8 == mna` lands at the boundary. For `mna = 4096`,
// `words = 512`, so we need `1 + ceil(len/8) == 512` ⇒
// `ceil(len/8) == 511` ⇒ `len in 4081..=4088` covers it; choose
// 4088 for a clean multiple-of-8 boundary.
let len: usize = 4088;
debug_assert_eq!(1 + len.div_ceil(core::mem::size_of::<usize>()).max(1), 512);
let s: std::string::String = (0..len).map(|_| 'a').collect();
let _ = arena.alloc_str_box(&s);
let after = arena.stats().oversized_chunks_allocated;
assert_eq!(
after - baseline,
0,
"min_payload == max_normal_alloc must stay on the normal cache path (shared)",
);
}
}
mod construction_boundaries {
use multitude::Arena;
#[test]
fn try_new_succeeds_with_default_globals() {
// `try_new` produces a working arena on the global allocator and a
// subsequent allocation yields a valid `&mut T`.
let arena = Arena::try_new().expect("try_new must succeed on Global");
let r = arena.alloc(42_u32);
assert_eq!(*r, 42);
}
#[test]
fn builder_build_produces_functional_arena() {
// `Arena::builder().build()` produces a functional arena that can
// allocate.
let arena: Arena = Arena::builder().build();
let r = arena.alloc(99_u64);
assert_eq!(*r, 99);
}
#[cfg(feature = "stats")]
#[test]
fn preallocate_with_max_class_capacity_does_not_double_ratchet() {
// A builder that pins a capacity class produces exactly one chunk; a
// subsequent allocation within it must not trigger a re-preallocation.
let arena = Arena::builder().with_capacity(512).build();
let s = arena.stats();
assert_eq!(s.normal_chunks_allocated, 1);
// Allocate within the preallocated chunk: no new chunk acquired.
let _ = arena.alloc(0_u32);
let s2 = arena.stats();
assert_eq!(s2.normal_chunks_allocated, 1);
}
#[cfg(feature = "stats")]
#[test]
fn preallocate_shared_with_capacity_does_not_double_ratchet() {
let arena = Arena::builder().with_capacity(512).build();
let s = arena.stats();
assert_eq!(s.normal_chunks_allocated, 1);
// First arc within preallocated chunk: still 1.
let _ = arena.alloc_arc(0_u32);
let s2 = arena.stats();
assert_eq!(s2.normal_chunks_allocated, 1);
}
}
mod drop_slice_over_u16_max_succeeds {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
use multitude::Arena;
#[derive(Clone)]
struct D(#[expect(dead_code, reason = "field gives the type a non-zero size")] u8);
impl Drop for D {
fn drop(&mut self) {}
}
const TOO_LONG: usize = (u16::MAX as usize) + 1;
#[test]
fn try_alloc_slice_clone_drop_over_u16_succeeds() {
let a = Arena::new();
let v: std::vec::Vec<D> = (0..TOO_LONG).map(|i| D(i as u8)).collect();
assert_eq!(a.try_alloc_slice_clone(&v[..]).unwrap().len(), TOO_LONG);
}
#[test]
fn try_alloc_slice_fill_with_drop_over_u16_succeeds() {
let a = Arena::new();
assert_eq!(
a.try_alloc_slice_fill_with::<D, _>(TOO_LONG, |i| D(i as u8)).unwrap().len(),
TOO_LONG
);
}
#[test]
fn try_alloc_slice_fill_iter_drop_over_u16_succeeds() {
let a = Arena::new();
assert_eq!(
a.try_alloc_slice_fill_iter::<D, _>((0..TOO_LONG).map(|i| D(i as u8)))
.unwrap()
.len(),
TOO_LONG
);
}
// `Arc<[T]>` uninit/zeroed slices have no `u16` element-count cap
// under per-`Arc` reference counting (they drop via
// `drop_in_place::<[T]>`, not a `u16`-counted chunk entry), so a
// Drop-typed slice longer than `u16::MAX` now allocates successfully.
#[cfg(not(miri))]
#[test]
fn uninit_slice_arc_over_u16_succeeds() {
struct D(u32);
impl Drop for D {
fn drop(&mut self) {}
}
let a = Arena::new();
let arc = a.try_alloc_uninit_slice_arc::<D>(TOO_LONG).expect("Arc slices have no u16 cap");
assert_eq!(arc.len(), TOO_LONG);
}
#[cfg(not(miri))]
#[test]
fn zeroed_slice_arc_over_u16_succeeds() {
struct D(u32);
impl Drop for D {
fn drop(&mut self) {}
}
let a = Arena::new();
let arc = a.try_alloc_zeroed_slice_arc::<D>(TOO_LONG).expect("Arc slices have no u16 cap");
assert_eq!(arc.len(), TOO_LONG);
}
}
mod allocator_impl_paths {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
use core::alloc::Layout;
use allocator_api2::alloc::{Allocator, Global};
use multitude::Arena;
#[test]
fn arena_as_allocator_zst_allocate_returns_dangling() {
let arena: Arena<Global> = Arena::new();
let alloc = &arena;
let layout = Layout::from_size_align(0, 8).unwrap();
let nn = alloc.allocate(layout).unwrap();
assert_eq!(nn.len(), 0);
}
#[test]
fn arena_as_allocator_zst_dealloc_is_noop() {
let arena: Arena<Global> = Arena::new();
let alloc = &arena;
let layout = Layout::from_size_align(0, 8).unwrap();
let nn = alloc.allocate(layout).unwrap();
// SAFETY: pair the dealloc with the allocation above; layout matches.
unsafe { alloc.deallocate(nn.cast::<u8>(), layout) };
}
#[test]
fn arena_as_allocator_grow_copies_overlap() {
let arena: Arena<Global> = Arena::new();
let alloc = &arena;
let old = Layout::from_size_align(8, 1).unwrap();
let nn = alloc.allocate(old).unwrap();
// Write into the old allocation.
// SAFETY: nn points to 8 writable bytes inside the arena.
unsafe {
for i in 0..8 {
nn.cast::<u8>().as_ptr().add(i).write(i as u8);
}
}
let new = Layout::from_size_align(32, 1).unwrap();
// SAFETY: nn was returned by Self::allocate with `old` layout.
let grown = unsafe { alloc.grow(nn.cast::<u8>(), old, new).unwrap() };
// Verify the prefix was copied.
// SAFETY: grown is a fresh allocation of at least 32 bytes.
unsafe {
for i in 0..8_u8 {
assert_eq!(*grown.cast::<u8>().as_ptr().add(usize::from(i)), i);
}
// Allocator API requires the caller to release the +1 chunk
// refcount the grow path took; otherwise the chunk leaks.
alloc.deallocate(grown.cast::<u8>(), new);
}
}
}
mod in_chunk_clone_is_copy_proxy {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
// InChunk is a pub(crate) type; we exercise its Clone via the public
// Arc-clone path (each clone of an arena Arc internally derives a
// chunk reference whose pointer is wrapped through InChunk machinery).
use multitude::Arena;
#[test]
fn arc_clone_exercises_inchunk_clone() {
let arena = Arena::new();
let a = arena.alloc_arc(7_u32);
let b = a.clone();
assert_eq!(*a, 7);
assert_eq!(*b, 7);
}
}
mod chunk_ops_destroy_branch {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
use multitude::Arena;
#[test]
fn box_outlives_arena_takes_destroy_branch_on_release() {
// Create an arena, allocate a Box, drop the arena. The Box keeps
// its (shared) chunk alive via +1; when the Box drops, the chunk's
// release path runs after the arena (and its ChunkProvider) is
// already gone, exercising the `Chunk::destroy` arm.
let arena = Arena::new();
let b = arena.alloc_box(42_u32);
drop(arena);
assert_eq!(*b, 42);
drop(b);
}
}
mod arena_constructors_coverage {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
use allocator_api2::alloc::Global;
use multitude::Arena;
use crate::common::SyncFailingAllocator;
#[test]
fn arena_try_new_ok() {
let a: Arena<Global> = Arena::try_new().unwrap();
let _ = a.alloc(0_u32);
}
#[test]
fn arena_default_constructs_global() {
let a: Arena<Global> = Arena::default();
let _ = a.alloc(0_u32);
}
#[test]
fn arena_try_new_in_ok() {
let a = Arena::try_new_in(SyncFailingAllocator::new(usize::MAX)).unwrap();
let _ = a.alloc(0_u32);
}
}
mod alloc_slice_overflow_paths {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
use std::panic::{AssertUnwindSafe, catch_unwind};
use multitude::Arena;
const HUGE: usize = usize::MAX / 2;
// -- try_alloc_slice_* fallible overflow paths --
#[test]
fn try_alloc_slice_fill_with_u32_huge_len_returns_err() {
let a = Arena::new();
let r = a.try_alloc_slice_fill_with::<u32, _>(HUGE, |_| 0);
assert!(r.is_err());
}
#[test]
fn try_alloc_slice_fill_iter_u32_huge_len_returns_err() {
let a = Arena::new();
let r = a.try_alloc_slice_fill_iter::<u32, _>((0..HUGE).map(|_| 0_u32));
assert!(r.is_err());
}
#[test]
fn try_alloc_slice_fill_with_box_u32_huge_len_returns_err() {
let a = Arena::new();
let r = a.try_alloc_slice_fill_with_box::<u32, _>(HUGE, |_| 0);
assert!(r.is_err());
}
#[test]
fn try_alloc_slice_fill_iter_box_u32_huge_len_returns_err() {
let a = Arena::new();
let r = a.try_alloc_slice_fill_iter_box::<u32, _>((0..HUGE).map(|_| 0_u32));
assert!(r.is_err());
}
#[test]
fn try_alloc_slice_fill_with_arc_u32_huge_len_returns_err() {
let a = Arena::new();
let r = a.try_alloc_slice_fill_with_arc::<u32, _>(HUGE, |_| 0);
assert!(r.is_err());
}
#[test]
fn try_alloc_slice_fill_iter_arc_u32_huge_len_returns_err() {
let a = Arena::new();
let r = a.try_alloc_slice_fill_iter_arc::<u32, _>((0..HUGE).map(|_| 0_u32));
assert!(r.is_err());
}
// -- panicking variants overflow paths --
fn p<F: FnOnce()>(f: F) -> bool {
catch_unwind(AssertUnwindSafe(f)).is_err()
}
#[test]
fn alloc_slice_fill_with_u32_huge_len_panics() {
assert!(p(|| {
let a = Arena::new();
let _ = a.alloc_slice_fill_with::<u32, _>(HUGE, |_| 0);
}));
}
#[test]
fn alloc_slice_fill_iter_u32_huge_len_panics() {
assert!(p(|| {
let a = Arena::new();
let _ = a.alloc_slice_fill_iter::<u32, _>((0..HUGE).map(|_| 0_u32));
}));
}
#[test]
fn alloc_slice_fill_with_box_u32_huge_len_panics() {
assert!(p(|| {
let a = Arena::new();
let _ = a.alloc_slice_fill_with_box::<u32, _>(HUGE, |_| 0);
}));
}
#[test]
fn alloc_slice_fill_iter_box_u32_huge_len_panics() {
assert!(p(|| {
let a = Arena::new();
let _ = a.alloc_slice_fill_iter_box::<u32, _>((0..HUGE).map(|_| 0_u32));
}));
}
#[test]
fn alloc_slice_fill_with_arc_u32_huge_len_panics() {
assert!(p(|| {
let a = Arena::new();
let _ = a.alloc_slice_fill_with_arc::<u32, _>(HUGE, |_| 0);
}));
}
#[test]
fn alloc_slice_fill_iter_arc_u32_huge_len_panics() {
assert!(p(|| {
let a = Arena::new();
let _ = a.alloc_slice_fill_iter_arc::<u32, _>((0..HUGE).map(|_| 0_u32));
}));
}
// -- reject_drop_slice_too_long panic path for &mut [T] (PANIC=true)
// Use a heap Vec to avoid stack overflow; `alloc_slice_clone(&v[..])`
// takes the panicking path and `reject_drop_slice_too_long` rejects
// up front for T:Drop with len > u16::MAX.
#[derive(Clone)]
struct D(#[expect(dead_code, reason = "field gives the type a non-zero size")] u8);
impl Drop for D {
fn drop(&mut self) {}
}
// The rejection happens up front (`reject_drop_slice_too_long`)
// before any element is read, but a real allocation is required
// to satisfy Miri's reference-validity check at slice
// construction time. `vec![value; n]` for a `Clone`-derivable
// type lowers to a single capacity allocation plus a bulk
// initializing loop — much cheaper than `(0..N).map(...).collect()`
// which runs the closure N times.
#[test]
fn alloc_slice_clone_drop_over_u16_succeeds() {
let v: std::vec::Vec<D> = std::vec![D(0); u16::MAX as usize + 1];
let arena = Arena::new();
let s = arena.alloc_slice_clone(&v[..]);
assert_eq!(s.len(), u16::MAX as usize + 1);
}
#[test]
fn alloc_slice_fill_with_drop_over_u16_succeeds() {
let arena = Arena::new();
let s = arena.alloc_slice_fill_with::<D, _>(u16::MAX as usize + 1, |i| D(i as u8));
assert_eq!(s.len(), u16::MAX as usize + 1);
}
#[test]
fn alloc_slice_fill_iter_drop_over_u16_succeeds() {
let arena = Arena::new();
let s = arena.alloc_slice_fill_iter::<D, _>((0..(u16::MAX as usize + 1)).map(|i| D(i as u8)));
assert_eq!(s.len(), u16::MAX as usize + 1);
}
}
mod allocator_impl_grow_to_zero_overlap {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
use core::alloc::Layout;
use allocator_api2::alloc::{Allocator, Global};
use multitude::Arena;
#[test]
fn grow_from_zero_old_does_not_copy() {
let arena: Arena<Global> = Arena::new();
let alloc = &arena;
let zero = Layout::from_size_align(0, 1).unwrap();
let nn = alloc.allocate(zero).unwrap();
let one = Layout::from_size_align(8, 1).unwrap();
// SAFETY: nn was returned by Self::allocate with `zero` layout.
let grown = unsafe { alloc.grow(nn.cast::<u8>(), zero, one).unwrap() };
assert_eq!(grown.len(), 8);
// SAFETY: pair with the grow above; the resulting +1 chunk
// refcount must be released or the chunk leaks.
unsafe { alloc.deallocate(grown.cast::<u8>(), one) };
}
}
mod oversized_paths_coverage {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
use multitude::Arena;
// Drop wrapper so the slice/value allocators take their
// `needs_drop::<T>()` oversized arm (registers a drop entry).
#[derive(Clone)]
struct DropU64(u64);
impl Drop for DropU64 {
fn drop(&mut self) {}
}
// 24 KiB single value with Drop ⇒ oversized-local value arm
// (`alloc_value.rs` 433-436).
#[derive(Clone)]
struct BigDrop([u64; 3000]);
impl Drop for BigDrop {
fn drop(&mut self) {}
}
// Counter-backed Drop element to assert that oversized-chunk drop
// entries are actually replayed (not just that the branch is taken).
struct CountedDrop<'a>(&'a core::sync::atomic::AtomicUsize);
impl Drop for CountedDrop<'_> {
fn drop(&mut self) {
self.0.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
}
}
// Verifies the oversized-local slice drop path *replays* destructors
// at arena teardown (correctness, not just coverage of the branch).
#[test]
fn alloc_slice_fill_with_oversized_drop_replays_destructors() {
use core::sync::atomic::{AtomicUsize, Ordering};
let counter = AtomicUsize::new(0);
{
let arena = Arena::new();
let out = arena.alloc_slice_fill_with(3000, |_| CountedDrop(&counter));
assert_eq!(out.len(), 3000);
assert_eq!(counter.load(Ordering::SeqCst), 0, "no drops before teardown");
}
assert_eq!(counter.load(Ordering::SeqCst), 3000, "every element dropped at arena teardown");
}
#[test]
fn alloc_slice_clone_oversized_drop() {
let arena = Arena::new();
let src: Vec<DropU64> = (0..3000).map(DropU64).collect();
let out = arena.alloc_slice_clone(&src);
assert_eq!(out.len(), 3000);
assert_eq!(out[2999].0, 2999);
}
#[test]
fn alloc_slice_fill_with_oversized_drop() {
let arena = Arena::new();
let out = arena.alloc_slice_fill_with(3000, |i| DropU64(i as u64));
assert_eq!(out.len(), 3000);
assert_eq!(out[2999].0, 2999);
}
#[test]
fn alloc_slice_fill_iter_oversized_drop() {
let arena = Arena::new();
let out = arena.alloc_slice_fill_iter((0_u32..3000).map(|i| DropU64(u64::from(i))));
assert_eq!(out.len(), 3000);
assert_eq!(out[0].0, 0);
}
#[test]
fn alloc_with_oversized_drop_value() {
let arena = Arena::new();
let v = arena.alloc_with(|| BigDrop([7_u64; 3000]));
assert_eq!(v.0[0], 7);
assert_eq!(v.0[2999], 7);
}
#[test]
fn alloc_uninit_arc_oversized() {
use core::mem::MaybeUninit;
use multitude::Arc;
let arena = Arena::new();
// A 24 KiB *Drop* value: `alloc_uninit_arc` only routes through
// `impl_alloc_uninit_arc` (the placeholder-drop-entry path) for
// `T: Drop`; a fresh arena's small current chunk can't hold it,
// so the request goes to a one-shot oversized chunk.
let a = arena.alloc_uninit_arc::<BigDrop>();
// SAFETY: `a` is the unique handle, so we have exclusive write access.
unsafe {
let p = Arc::as_ptr(&a).cast::<MaybeUninit<BigDrop>>().cast_mut();
(*p).write(BigDrop([9_u64; 3000]));
}
// SAFETY: just initialized above.
let typed = unsafe { a.assume_init() };
assert_eq!(typed.0[0], 9);
assert_eq!(typed.0[2999], 9);
}
#[test]
fn alloc_uninit_slice_arc_oversized() {
use core::mem::MaybeUninit;
use multitude::Arc;
let arena = Arena::new();
// A Drop element type routes through `impl_alloc_uninit_slice_arc`;
// 3000 × 8 B = 24 KiB exceeds the fresh arena's current chunk, so
// the slice lands in a one-shot oversized chunk.
let len = 3000_usize;
let s = arena.alloc_uninit_slice_arc::<DropU64>(len);
// SAFETY: `s` is the unique handle, so we have exclusive write access.
unsafe {
let base = Arc::as_ptr(&s).cast::<MaybeUninit<DropU64>>().cast_mut();
for i in 0..len {
(*base.add(i)).write(DropU64(i as u64));
}
}
// SAFETY: every element initialized above.
let typed = unsafe { s.assume_init() };
assert_eq!(typed.len(), len);
assert_eq!(typed[123].0, 123);
}
#[cfg(feature = "utf16")]
#[test]
fn alloc_utf16_str_arc_oversized() {
let arena = Arena::new();
// ~10 000 ASCII chars ⇒ 20 KiB of UTF-16 payload ⇒ oversized.
let s = "a".repeat(10_000);
let u = arena.alloc_utf16_str_arc_from_str(&s);
assert_eq!(u.len(), 10_000);
}
#[cfg(feature = "utf16")]
#[test]
fn alloc_utf16_str_box_oversized() {
let arena = Arena::new();
let s = "b".repeat(10_000);
let u = arena.alloc_utf16_str_box_from_str(&s);
assert_eq!(u.len(), 10_000);
}
#[test]
fn alloc_zeroed_arc_oversized() {
use core::mem::MaybeUninit;
use multitude::Arc;
let arena = Arena::new();
// Zeroed variant of the oversized uninit-arc path (Drop type ⇒
// `impl_alloc_uninit_arc(zeroed = true)`); never `assume_init`,
// so the placeholder drop shim tears down without touching the
// zeroed bytes.
let _a: Arc<MaybeUninit<BigDrop>> = arena.alloc_zeroed_arc::<BigDrop>();
}
#[test]
fn box_str_as_mut_str() {
let arena = Arena::new();
let mut b = arena.alloc_str_box("hello");
// Exercises `Box<str>::as_mut_str` (str_impls.rs 93-95).
let m: &mut str = b.as_mut_str();
m.make_ascii_uppercase();
assert_eq!(b.as_str(), "HELLO");
}
}
mod refactor_coverage_gaps {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::items_after_statements, reason = "test layout")]
#![allow(dead_code, reason = "test scaffolding may be conditionally used")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded test indices")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::cast_sign_loss, reason = "test code")]
#![allow(clippy::range_plus_one, reason = "test code")]
#![allow(clippy::assertions_on_result_states, reason = "test code")]
#![allow(clippy::ptr_as_ptr, reason = "test code")]
#![allow(clippy::as_pointer_underscore, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "test code")]
#![allow(clippy::empty_drop, reason = "test code: probe types use empty Drop on purpose")]
#![allow(clippy::deref_by_slicing, reason = "tests prefer explicit slicing")]
#![allow(clippy::needless_borrow, reason = "tests prefer explicit borrows")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "tests prefer explicit borrows")]
#![allow(clippy::redundant_slicing, reason = "tests prefer explicit slicing")]
use multitude::Arena;
// `reserve_slice_box` oversized branch (+ `try_alloc_uninit_slice_prefixed`
// and `try_alloc_prefixed_slice_payload`): a `Box<[T]>` built via the
// fill path whose payload exceeds MAX_NORMAL_ALLOC routes through the
// dedicated oversized chunk.
#[test]
fn oversized_fill_box_routes_through_reserve_slice_box() {
let arena = Arena::new();
// 5000 × u32 = 20 KiB > MAX_NORMAL_ALLOC (16 KiB) ⇒ oversized box path.
let b: multitude::Box<[u32]> = arena.alloc_slice_fill_with_box(5000, |i| i as u32);
assert_eq!(b.len(), 5000);
assert_eq!(b[0], 0);
assert_eq!(b[4999], 4999);
}
// vec/mod.rs non-freezable paths: the refill-hint else-branch,
// `try_reserve_local_slice`, and the non-freezable oversized arm. An
// over-aligned element (align ≥ max_smart_ptr_align == CHUNK_ALIGN/2) is
// non-freezable, and at 32 KiB each it forces the oversized refill.
#[test]
fn non_freezable_overaligned_vec_grows_via_oversized_path() {
#[repr(align(32768))]
#[derive(Clone, Copy)]
struct Over(u8);
let arena = Arena::new();
// Reserve (rather than `push`) capacity for the over-aligned element:
// this drives the same non-freezable oversized growth path without
// ever materializing a 32 KiB-aligned `Over` on the stack — such
// over-aligned stack temporaries fault on Windows.
let mut v = arena.alloc_vec::<Over>();
v.try_reserve(2).expect("reserve over-aligned capacity");
assert!(v.capacity() >= 2);
assert!(v.is_empty());
}
// A grow request whose raw payload (`size_of::<T>() * new_cap`) overflows
// `usize` must be a recoverable `AllocError`, never a panic.
#[test]
fn vec_try_reserve_overflowing_capacity_returns_err() {
let arena = Arena::new();
let mut v = arena.alloc_vec::<u64>();
// size_of::<u64>() (8) * (usize::MAX / 4) overflows usize.
v.try_reserve(usize::MAX / 4)
.expect_err("overflowing capacity must be a recoverable error");
}
// `From<Box<Utf16Str>> for Box<[u16]>` zero-copy retag.
#[cfg(feature = "utf16")]
#[test]
fn box_utf16str_into_box_u16_slice_retags_without_copy() {
use widestring::utf16str;
let arena = Arena::new();
let b = arena.alloc_utf16_str_box(utf16str!("hello"));
let raw = b.as_widestring_utf16_str().as_slice().as_ptr();
let u16box: multitude::Box<[u16]> = multitude::Box::from(b);
assert_eq!((*u16box).as_ptr(), raw, "Box<Utf16Str> -> Box<[u16]> retag must not copy");
assert_eq!(&*u16box, utf16str!("hello").as_slice());
}
}
mod alloc_drop_behavior {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std for thread/sync primitives")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain smart pointers to keep chunks alive")]
#![allow(clippy::manual_assert, reason = "explicit panic clarifies safety-net intent")]
#![allow(clippy::cast_possible_truncation, reason = "test code: bounded indices fit")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "explicit borrows clarify intent in tests")]
#![allow(clippy::doc_markdown, reason = "doc comments cite raw identifier names")]
#![allow(
dead_code,
reason = "test types intentionally retain unused fields to keep their Drop side-effects observable"
)]
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicUsize, Ordering};
use multitude::{Arc, Arena};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
// --------------------------------------------------------------------
// A. Trait-impl mutants: hash forwarders and Pointer formatter.
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// B/I. Builder defaults / preallocation paths / resolve_capacity.
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// G. OversizedSharedGuard::drop — panic-recovery for arc-oversized.
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// C/D/E. Drop-counter exhaustive coverage. Many missed mutants live in
// the allocation hot paths and corrupt either the bump
// cursor (`+ → -/*`), the drop-entry index/chain (`+1 → *1`), or the
// fit/refill comparisons (`> → >=/==`). A test that allocates many
// drop-tracking values, drops them, and asserts the exact count would
// fail under any of those mutations: a wrong `data_ptr`/`drop_back`
// segfaults; a wrong `drop_count` increment leaves entries unrun; a
// flipped fit comparison either drops allocations or oversteps the
// chunk.
// --------------------------------------------------------------------
#[derive(Debug)]
struct DropCounter(StdArc<AtomicUsize>);
impl Drop for DropCounter {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
#[test]
fn many_drop_typed_arc_allocs_run_drop_exactly_once_each() {
let counter = StdArc::new(AtomicUsize::new(0));
{
let arena = Arena::new();
let mut keep: std::vec::Vec<Arc<DropCounter>> = std::vec::Vec::new();
// 256 Arc allocations still exercise multiple chunks and the
// same drop-entry paths this test is targeting.
for _ in 0..256_u32 {
keep.push(arena.alloc_arc_with(|| DropCounter(counter.clone())));
}
drop(keep);
drop(arena);
}
assert_eq!(counter.load(Ordering::Relaxed), 256);
}
#[test]
fn oversized_drop_typed_alloc_runs_drop_and_respects_alignment() {
#[repr(align(64))]
struct Big {
// 32 KiB > default max_normal_alloc (16 KiB) → oversized path.
_payload: [u64; 4 * 1024],
token: DropCounter,
}
let counter = StdArc::new(AtomicUsize::new(0));
{
let arena = Arena::new();
let b = arena.alloc_box_with(|| Big {
_payload: [0; 4 * 1024],
token: DropCounter(counter.clone()),
});
// Verify alignment: any pointer-arithmetic mutation that
// breaks the `align - 1` masking or the `aligned + size`
// end-address computation would land us off-alignment.
let p: *const Big = std::ptr::from_ref::<Big>(&b);
assert_eq!((p as usize) % 64, 0, "Big must be 64-byte aligned");
drop(b);
drop(arena);
}
assert_eq!(counter.load(Ordering::Relaxed), 1, "oversized Box's Drop must run");
// Same path for Arc (oversized shared): exercises the
// `try_alloc_inner_arc_oversized_with` match-guard at line 1185
// and the `OversizedSharedGuard` happy path (drop is forgotten on
// success).
let counter2 = StdArc::new(AtomicUsize::new(0));
{
let arena = Arena::new();
let a = arena.alloc_arc_with(|| Big {
_payload: [0; 4 * 1024],
token: DropCounter(counter2.clone()),
});
let p: *const Big = std::ptr::from_ref::<Big>(&a);
assert_eq!((p as usize) % 64, 0);
drop(a);
drop(arena);
}
assert_eq!(counter2.load(Ordering::Relaxed), 1);
}
// --------------------------------------------------------------------
// H. align_offset — exercised transitively via oversized aligned alloc.
// --------------------------------------------------------------------
#[test]
fn oversized_high_alignment_drives_align_offset() {
#[repr(align(128))]
struct Aligned128 {
_pad: [u64; 4 * 1024], // 32 KiB, oversized
}
let arena = Arena::new();
let b = arena.alloc_box(Aligned128 { _pad: [0; 4 * 1024] });
let p: *const Aligned128 = std::ptr::from_ref::<Aligned128>(&b);
assert_eq!((p as usize) % 128, 0);
// Same for Arc (oversized shared).
let a = arena.alloc_arc(Aligned128 { _pad: [0; 4 * 1024] });
let p: *const Aligned128 = std::ptr::from_ref::<Aligned128>(&a);
assert_eq!((p as usize) % 128, 0);
}
// --------------------------------------------------------------------
// D. try_bump_fit boundary mutant.
// --------------------------------------------------------------------
#[test]
fn many_distinct_size_and_align_combinations_succeed() {
let arena = Arena::new();
// Mix of size classes and alignments to maximize the chance
// of hitting `aligned == max_aligned`.
let mut keep_u8 = std::vec::Vec::new();
let mut keep_u16 = std::vec::Vec::new();
let mut keep_u32 = std::vec::Vec::new();
let mut keep_u64 = std::vec::Vec::new();
for i in 0..256_u32 {
keep_u8.push(arena.alloc((i & 0xff) as u8));
keep_u16.push(arena.alloc((i & 0xffff) as u16));
keep_u32.push(arena.alloc(i));
keep_u64.push(arena.alloc(u64::from(i)));
}
for (i, p) in keep_u8.iter().enumerate() {
assert_eq!(**p, (i as u32 & 0xff) as u8);
}
for (i, p) in keep_u16.iter().enumerate() {
assert_eq!(**p, (i as u32 & 0xffff) as u16);
}
for (i, p) in keep_u32.iter().enumerate() {
assert_eq!(**p, i as u32);
}
for (i, p) in keep_u64.iter().enumerate() {
assert_eq!(**p, i as u64);
}
}
// --------------------------------------------------------------------
// D/E. allocate_layout `+` arithmetic.
// --------------------------------------------------------------------
#[test]
fn vec_with_alignment_grows_across_chunks() {
let arena = Arena::new();
// Allocate vecs that together exceed a single chunk so
// allocate_layout's refill arm is exercised.
let mut all: std::vec::Vec<multitude::vec::Vec<'_, u64>> = std::vec::Vec::new();
for _ in 0..64 {
let mut v = arena.alloc_vec_with_capacity::<u64>(64);
for j in 0..64_u64 {
v.push(j);
}
all.push(v);
}
for v in &all {
for (i, x) in v.iter().enumerate() {
assert_eq!(*x, i as u64);
}
}
}
// --------------------------------------------------------------------
// D/E/F. Slice paths — local and shared, with and without Drop.
// --------------------------------------------------------------------
#[test]
fn many_copy_slices_force_slow_refill() {
let arena = Arena::new();
let mut all = std::vec::Vec::new();
for i in 0..256_u32 {
let s = arena.alloc_slice_copy::<u64>(&[u64::from(i); 17]);
all.push(s);
}
for (i, s) in all.iter().enumerate() {
for &v in s.iter() {
assert_eq!(v, i as u64);
}
}
}
// --------------------------------------------------------------------
// Misc: confirm the && operator in the oversized-value gate.
// --------------------------------------------------------------------
#[test]
fn oversized_box_drop_runs_exactly_once() {
#[repr(align(64))]
struct Big {
_payload: [u64; 4 * 1024], // 32 KiB > 16 KiB max_normal_alloc
token: DropCounter,
}
let counter = StdArc::new(AtomicUsize::new(0));
{
let arena = Arena::new();
let b = arena.alloc_box(Big {
_payload: [0; 4 * 1024],
token: DropCounter(counter.clone()),
});
drop(b);
drop(arena);
}
assert_eq!(counter.load(Ordering::Relaxed), 1, "Box oversized must drop exactly once");
}
}
mod alloc_drop_behavior_2 {
#![allow(clippy::std_instead_of_core, reason = "test code")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "explicit .clone() in tests")]
#![allow(clippy::collection_is_never_read, reason = "keep allocations live")]
#![allow(clippy::doc_markdown, reason = "doc comments cite raw identifier names")]
#![allow(clippy::cast_possible_truncation, reason = "bounded indices fit")]
#![allow(clippy::items_after_statements, reason = "test-local types live near usage")]
#![allow(clippy::large_stack_arrays, reason = "test stack allocations are bounded")]
#![allow(dead_code, reason = "drop-tracking payload fields' Drop side-effects are the observable")]
#![allow(clippy::redundant_clone, reason = "tests prefer explicit clones for clarity")]
#![allow(clippy::empty_line_after_doc_comments, reason = "doc-comments cite ASCII identifiers verbatim")]
#![allow(clippy::manual_midpoint, reason = "explicit (lo+hi)/2 reads naturally for bisection")]
#![allow(clippy::ref_as_ptr, reason = "explicit `*const` cast is clearer than into()")]
#![allow(clippy::bool_assert_comparison, reason = "explicit boolean assertions are clearer")]
#![allow(clippy::assertions_on_constants, reason = "test asserts on probe results which may be constant")]
#![allow(clippy::missing_panics_doc, reason = "test functions may panic by design")]
#![allow(clippy::deref_by_slicing, reason = "tests express intent via &v[..] for clarity")]
#![allow(clippy::useless_vec, reason = "vec!! mirrors realistic user code shapes")]
#![allow(clippy::unused_unit, reason = "the explicit `()` body documents intent of the mutation we apply")]
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicUsize, Ordering};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
#[derive(Debug)]
struct DropCounter(StdArc<AtomicUsize>);
impl Drop for DropCounter {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
// ============================================================
// constants.rs mutants
// ============================================================
// ============================================================
// drop_list.rs mutants — PAD_BYTES via mem::size_of::<DropEntry>
// ============================================================
//
// DropEntry is `(fn_ptr=8) + (u16 + u16) + _pad`. With pointer-alignment
// target = 8: RAW_USED=12; PAD_BYTES=4; size_of::<DropEntry>()=16.
//
// The mutants at line 49 change RAW_USED, which changes PAD_BYTES,
// which changes size_of::<DropEntry>(). Observe by allocating a
// known number of drop-tracked values: each consumes one DropEntry
// in the chunk's back-stack. If the entry size changes, the number
// of entries that fit in a 64 KiB chunk changes, which (for
// sufficient pressure) changes the number of fresh chunks the
// arena allocates.
// ============================================================
// arena_builder.rs mutants
// ============================================================
// ============================================================
// chunk_provider.rs mutants
// ============================================================
// ============================================================
// arena.rs mutants
// ============================================================
#[test]
fn arc_drop_count_increments_on_each_alloc() {
let counter = StdArc::new(AtomicUsize::new(0));
{
let arena = multitude::Arena::new();
let mut keep: Vec<multitude::Arc<DropCounter>> = Vec::with_capacity(64);
for _ in 0..64_u32 {
keep.push(arena.alloc_arc(DropCounter(counter.clone())));
}
drop(keep);
drop(arena);
}
assert_eq!(counter.load(Ordering::Relaxed), 64);
}
#[test]
fn arc_with_non_drop_t_does_not_install_drop_entry() {
const N: u32 = 64;
let arena = multitude::Arena::builder().with_capacity(64 * 1024).build();
// 4 × 16 KiB uninit fillers walk the bump cursor to the chunk's true end.
let _fillers: Vec<multitude::Arc<core::mem::MaybeUninit<[u8; 16 * 1024]>>> =
(0..4).map(|_| arena.alloc_uninit_arc::<[u8; 16 * 1024]>()).collect();
let mut keep: Vec<multitude::Arc<u32>> = Vec::with_capacity(N as usize);
for i in 0..N {
keep.push(arena.alloc_arc(i));
}
for (i, a) in keep.iter().enumerate() {
assert_eq!(**a, i as u32);
}
}
#[test]
fn arc_with_high_align_uses_correct_needed_size() {
#[repr(align(64))]
struct Aligned64([u8; 64]);
let arena = multitude::Arena::new();
let mut keep: Vec<multitude::Arc<Aligned64>> = Vec::with_capacity(256);
for _ in 0..256_u32 {
keep.push(arena.alloc_arc(Aligned64([0; 64])));
}
for a in &keep {
let p = a.as_ref() as *const Aligned64 as usize;
assert_eq!(p % 64, 0, "alignment must be honored after refill_shared(needed)");
}
}
#[test]
fn allocate_layout_high_align_refill_uses_sum() {
use core::alloc::Layout;
use allocator_api2::alloc::Allocator;
let arena = multitude::Arena::new();
let a: &multitude::Arena = &arena;
let layout = Layout::from_size_align(4096, 64).unwrap();
let mut allocations = std::vec::Vec::new();
// Enough iterations to force chunk grows (each chunk holds
// ~15 × 4 KiB before refill at max class), but small enough that
// Miri completes promptly. A `+ → -` mutation under-refills on
// the very first high-alignment request, so a short burst still
// catches it.
for _ in 0..32 {
let ptr = a.allocate(layout).unwrap();
let addr = ptr.as_ptr() as *const u8 as usize;
assert_eq!(addr % 64, 0);
allocations.push(ptr);
}
// Deallocate so the chunks reclaim their refcounts; otherwise
// Miri (and any leak-aware allocator) would flag the chunks as
// leaked.
for ptr in allocations {
// SAFETY: ptr came from `a.allocate(layout)` with the same layout.
unsafe { a.deallocate(ptr.cast(), layout) };
}
}
#[test]
fn slice_shared_no_drop_does_not_install_entry() {
let arena = multitude::Arena::new();
let s: multitude::Arc<[u32]> = arena.alloc_slice_copy_arc(&[1u32, 2, 3, 4, 5][..]);
assert_eq!(&*s, &[1, 2, 3, 4, 5]);
}
#[test]
fn slice_shared_long_no_drop_succeeds() {
let arena = multitude::Arena::new();
let v = vec![7_u32; 70_000];
let s: multitude::Arc<[u32]> = arena.alloc_slice_copy_arc(&v[..]);
assert_eq!(s.len(), 70_000);
}
#[test]
// Skipped under Miri: needs `u16::MAX` allocations + drops (~65K
// elements) to exercise the slice-length boundary, which exceeds
// Miri's 10-minute test budget. The boundary itself is a runtime
// assertion, not a memory-safety property; native test runs verify
// it on every CI execution.
#[cfg_attr(miri, ignore)]
fn slice_shared_drop_at_u16_max_succeeds() {
let counter = StdArc::new(AtomicUsize::new(0));
#[derive(Debug)]
struct DC(StdArc<AtomicUsize>);
impl Drop for DC {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
// DC must be Send + Sync for alloc_slice_fill_with_arc.
{
let arena = multitude::Arena::new();
let c = counter.clone();
let s: multitude::Arc<[DC]> = arena.alloc_slice_fill_with_arc(u16::MAX as usize, |_| DC(c.clone()));
assert_eq!(s.len(), u16::MAX as usize);
drop(s);
}
assert_eq!(counter.load(Ordering::Relaxed), u16::MAX as usize);
}
#[test]
fn slice_shared_init_increments_guard_len() {
let counter = StdArc::new(AtomicUsize::new(0));
#[derive(Debug)]
struct DC(StdArc<AtomicUsize>);
impl Drop for DC {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let arena = multitude::Arena::new();
let c = counter.clone();
// Initialise 10 elements, then panic on the 11th.
let _s: multitude::Arc<[DC]> = arena.alloc_slice_fill_with_arc(20_usize, |i| {
assert!(i != 10, "test panic");
DC(c.clone())
});
}));
assert!(res.is_err());
// 10 elements were initialised before panic; with += -> *= the
// guard.len would stay 0 and none of those 10 would be dropped.
assert_eq!(counter.load(Ordering::Relaxed), 10);
}
#[test]
fn slice_shared_refill_uses_correct_has_drop_flag() {
let counter = StdArc::new(AtomicUsize::new(0));
#[derive(Debug)]
struct DC(StdArc<AtomicUsize>);
impl Drop for DC {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let arena = multitude::Arena::new();
let c = counter.clone();
// Many Drop slices, forcing repeated refills.
let mut keep: Vec<multitude::Arc<[DC]>> = Vec::with_capacity(256);
for _ in 0..256 {
let cc = c.clone();
keep.push(arena.alloc_slice_fill_with_arc(8_usize, |_| DC(cc.clone())));
}
drop(keep);
drop(arena);
assert_eq!(counter.load(Ordering::Relaxed), 256 * 8);
}
#[test]
fn try_bump_fit_exact_aligned_succeeds() {
let arena = multitude::Arena::new();
// Many sequential u8 allocations stress the bump cursor.
let mut keep = Vec::with_capacity(4096);
for i in 0..4096_u32 {
keep.push(arena.alloc(i as u8));
}
for (i, v) in keep.iter().enumerate() {
assert_eq!(**v, i as u8);
}
}
#[test]
fn vec_into_arc_advances_read_index() {
let arena = multitude::Arena::new();
let mut v: multitude::vec::Vec<u32, _> = arena.alloc_vec();
v.push(10);
v.push(20);
v.push(30);
let arc: multitude::Arc<[u32]> = multitude::Arc::from(v);
assert_eq!(&*arc, &[10, 20, 30]);
}
#[test]
fn vec_into_box_advances_read_index() {
// `Vec::into_box` moves the elements into a fresh shared
// allocation via `alloc_slice_fill_iter_box`, whose fill closure
// advances its read index per element. This exercises that
// advance and confirms the elements land in order.
let arena = multitude::Arena::new();
let mut v: multitude::vec::Vec<u32, _> = arena.alloc_vec_with_capacity(3);
v.push(11);
v.push(22);
v.push(33);
let b: multitude::Box<[u32]> = v.into_boxed_slice();
assert_eq!(&*b, &[11, 22, 33]);
}
}
mod alloc_hot_path_behavior {
#![allow(clippy::std_instead_of_core, reason = "test code")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "explicit .clone() in tests")]
#![allow(clippy::collection_is_never_read, reason = "keep allocations live")]
#![allow(clippy::doc_markdown, reason = "doc comments cite raw identifier names")]
#![allow(clippy::cast_possible_truncation, reason = "bounded indices fit")]
#![allow(clippy::items_after_statements, reason = "test-local types live near usage")]
#![allow(clippy::large_stack_arrays, reason = "test stack allocations are bounded")]
#![allow(dead_code, reason = "drop-tracking payload fields")]
#![allow(clippy::redundant_clone, reason = "tests prefer explicit clones")]
#![allow(clippy::empty_line_after_doc_comments, reason = "doc-comments cite ASCII identifiers")]
#![allow(clippy::missing_panics_doc, reason = "test functions may panic")]
#![allow(clippy::manual_assert, reason = "explicit if/panic preserves test intent")]
#![allow(clippy::use_self, reason = "test code")]
#![allow(clippy::ref_as_ptr, reason = "test code")]
#![allow(clippy::stable_sort_primitive, reason = "test code")]
#![allow(clippy::needless_borrows_for_generic_args, reason = "test code")]
#![allow(
clippy::used_underscore_binding,
reason = "underscore-prefixed bindings kept alive intentionally for drop ordering"
)]
#![allow(clippy::needless_range_loop, reason = "test code prefers explicit indices")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "test marker types are trivially Send/Sync")]
#![allow(clippy::redundant_closure_for_method_calls, reason = "test code")]
#![allow(unused_imports, reason = "test scope-local imports may shadow")]
#![allow(redundant_imports, reason = "test scope-local imports may shadow")]
#![allow(clippy::assertions_on_constants, reason = "test asserts on constants")]
#![allow(clippy::bool_assert_comparison, reason = "explicit boolean assertions")]
use std::cell::Cell;
use std::sync::atomic::{AtomicUsize, Ordering};
use multitude::Arena;
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
// =====================================================================
// Helper: a type that needs Drop and is Send+Sync (for Arc allocs)
// =====================================================================
thread_local! {
/// Per-test drop counter. `libtest` runs each test on its own
/// thread, and these tests perform every `DropTracker`/arena drop on
/// that same thread, so a thread-local counter is naturally isolated
/// per test. This replaces an earlier global counter plus serializing
/// mutex, which was order-sensitive: a test that dropped a
/// `DropTracker`-bearing arena *after* releasing the mutex could bump
/// the next test's count, producing flaky cross-test failures.
static DROP_COUNTER: Cell<usize> = const { Cell::new(0) };
}
/// Increment the current thread's drop counter.
fn bump_drop_counter() {
DROP_COUNTER.with(|c| c.set(c.get() + 1));
}
#[derive(Clone)]
struct DropTracker(u64);
impl Drop for DropTracker {
fn drop(&mut self) {
bump_drop_counter();
}
}
// SAFETY: DropTracker is trivially Send+Sync (just a u64).
unsafe impl Send for DropTracker {}
unsafe impl Sync for DropTracker {}
/// ZST guard returned by [`reset_drop_counter`]. The drop counter is
/// thread-local, so no cross-test serialization is required; this guard
/// exists only so existing `let _guard = reset_drop_counter();` call sites
/// keep compiling unchanged.
struct DropCounterGuard;
/// Reset the current thread's drop counter to zero.
#[must_use = "bind the guard for the test's lifetime"]
fn reset_drop_counter() -> DropCounterGuard {
DROP_COUNTER.with(|c| c.set(0));
DropCounterGuard
}
/// Read the current thread's drop count.
fn drops() -> usize {
DROP_COUNTER.with(Cell::get)
}
// =====================================================================
// arena.rs — try_alloc_inner_arc_with slow-path mutants
// =====================================================================
#[test]
fn arena_709_entry_size_gt_zero_arc_with() {
let _guard = reset_drop_counter();
let arena = Arena::new();
{
let _arc = arena.alloc_arc_with(|| DropTracker(42));
// Arc holds the value; drop it by letting it go out of scope.
}
drop(arena);
let drops = drops();
assert!(drops >= 1, "DropTracker must be dropped; got {drops} drops");
}
#[test]
#[cfg(feature = "stats")]
fn arena_728_size_eq_max_normal_alloc_arc() {
let _guard = reset_drop_counter();
// Default max_normal_alloc is large. Use a small budget to force
// the boundary. The default ArenaBuilder sets max_normal_alloc
// based on chunk size. We just allocate something and check stats.
let arena = Arena::builder().build();
// Allocate a small arc with drop — exercises the normal path
let _a1 = arena.alloc_arc_with(|| DropTracker(1));
let _a2 = arena.alloc_arc_with(|| DropTracker(2));
// Both should succeed through normal path, not oversized
let stats = arena.stats();
assert_eq!(
stats.oversized_chunks_allocated, 0,
"small arcs should use normal chunks, not oversized"
);
}
#[test]
fn arena_731_needed_computation_arc_with() {
let _guard = reset_drop_counter();
let arena = Arena::new();
// Fill several arcs to force slow-path refill
let mut keep = Vec::new();
for i in 0..100 {
keep.push(arena.alloc_arc_with(|| DropTracker(i)));
}
drop(keep);
drop(arena);
let drops = drops();
assert_eq!(drops, 100, "all 100 DropTrackers must be dropped");
}
// =====================================================================
// arena.rs — try_alloc_inner_slow_value mutants (1085, 1089, 1101)
// =====================================================================
#[test]
fn arena_1251_oversized_shared_guard_drop() {
let _guard = reset_drop_counter();
// Force oversized path for shared (arc) allocations
let arena = Arena::builder().max_normal_alloc(4096).build();
#[repr(C)]
struct LargeArcDrop {
data: [u8; 8192],
}
impl Drop for LargeArcDrop {
fn drop(&mut self) {
bump_drop_counter();
}
}
// SAFETY: just bytes + a counter
unsafe impl Send for LargeArcDrop {}
unsafe impl Sync for LargeArcDrop {}
let arc = arena.alloc_arc_with(|| LargeArcDrop { data: [0; 8192] });
drop(arc);
drop(arena);
let drops = drops();
assert!(drops >= 1, "oversized arc LargeArcDrop must drop");
}
#[test]
fn arena_1648_allocate_layout_needed() {
let arena = Arena::new();
// allocate_layout is used by alloc (borrow) path.
// Allocate many u64 values and verify they don't overlap.
let mut ptrs = Vec::new();
for i in 0u64..100 {
let r = arena.alloc(i);
ptrs.push(core::ptr::addr_of!(*r) as usize);
}
// Check no two pointers are the same
ptrs.sort();
ptrs.dedup();
assert_eq!(ptrs.len(), 100, "all 100 alloc pointers must be distinct");
// Verify values are intact
for i in 0u64..10 {
let r = arena.alloc(i + 1000);
assert_eq!(*r, i + 1000);
}
}
// =====================================================================
// arena.rs — slice allocation mutants
// =====================================================================
#[test]
fn arena_2261_slice_local_and_to_or() {
let _guard = reset_drop_counter();
let arena = Arena::new();
// Allocate a zero-length slice of a Drop type (reference)
let empty = arena.alloc_slice_fill_with(0, |_| DropTracker(0));
assert_eq!(empty.len(), 0);
// Allocate a non-empty slice of a non-Drop type (no drop_fn)
let nums = arena.alloc_slice_fill_with(10, |i| i as u32);
assert_eq!(nums.len(), 10);
for (i, v) in nums.iter().enumerate() {
assert_eq!(*v, i as u32);
}
}
#[test]
fn arena_2266_slice_len_boundary() {
let arena = Arena::new();
// Non-Drop type, len > u16::MAX — should succeed (no drop entry needed)
// Use a tiny type to avoid OOM
let big_len = u16::MAX as usize + 1;
let result = arena.try_alloc_slice_fill_with(big_len, |i| i as u8);
// This may fail due to memory, but should not fail due to the len check
// when entry_size == 0. If it fails, it's AllocError from memory, not the len check.
// Let's use a smaller test: verify that exactly u16::MAX works for Drop types.
let arena2 = Arena::new();
// len == u16::MAX with Drop type — should succeed (not > u16::MAX)
// This would be too much memory, so let's verify the boundary differently.
// Actually test len == 0 with Drop type (entry_size should be 0 when len == 0)
let empty_drop = arena2.alloc_slice_fill_with(0, |_| DropTracker(0));
assert_eq!(empty_drop.len(), 0);
// Test len == 1 with Drop type — should succeed
let _guard = reset_drop_counter();
let one_drop = arena2.alloc_slice_fill_with(1, |_| DropTracker(42));
assert_eq!(one_drop.len(), 1);
// Just verify the allocation is fine
drop(result);
}
#[test]
fn arena_2655_shared_slice_and_to_or() {
let arena = Arena::new();
// Allocate empty shared (arc) slice of Drop type
let _guard = reset_drop_counter();
let empty_arc = arena.alloc_slice_fill_with_arc(0, |_| DropTracker(0));
assert_eq!(empty_arc.len(), 0);
drop(empty_arc);
// No drops should have occurred for empty slice
let drops = drops();
assert_eq!(drops, 0, "empty arc slice should not drop any elements");
// Allocate non-empty shared slice of non-Drop type
let nums_arc = arena.alloc_slice_fill_with_arc(5, |i| i as u64);
assert_eq!(nums_arc.len(), 5);
}
#[test]
fn arena_2660_shared_slice_len_boundary() {
let arena = Arena::new();
// Drop type, len == 1 via arc — should succeed (len <= u16::MAX)
let _guard = reset_drop_counter();
let one = arena.alloc_slice_fill_with_arc(1, |_| DropTracker(99));
assert_eq!(one.len(), 1);
drop(one);
drop(arena);
let drops = drops();
assert!(drops >= 1, "single-element arc slice must drop");
}
#[test]
fn arena_2701_shared_slice_entry_size_guard() {
let _guard = reset_drop_counter();
let arena = Arena::new();
// Allocate multiple shared slices with Drop to exercise drop_back advancement
let mut keep = Vec::new();
for _ in 0..20 {
keep.push(arena.alloc_slice_fill_with_arc(3, |i| DropTracker(i as u64)));
}
drop(keep);
drop(arena);
let drops = drops();
assert_eq!(drops, 60, "20 arcs * 3 elements = 60 drops");
}
#[test]
fn arena_2719_guard_len_increment() {
let _guard = reset_drop_counter();
let arena = Arena::new();
let arc = arena.alloc_slice_fill_with_arc(5, |i| DropTracker(i as u64));
assert_eq!(arc.len(), 5);
drop(arc);
drop(arena);
let drops = drops();
assert_eq!(drops, 5, "all 5 elements must drop");
}
#[test]
fn arena_2739_refill_shared_entry_size_check() {
let _guard = reset_drop_counter();
let arena = Arena::new();
// Force the slow refill path by filling the chunk
let mut keep = Vec::new();
for _ in 0..100 {
keep.push(arena.alloc_slice_fill_with_arc(3, |i| DropTracker(i as u64)));
}
drop(keep);
drop(arena);
let drops = drops();
assert_eq!(drops, 300, "100 * 3 = 300 drops");
}
// =====================================================================
// chunk_provider.rs mutants
// =====================================================================
#[test]
fn chunk_provider_133_reserve_budget_boundary() {
// Set byte_budget so that exactly one default chunk fits.
// The first allocation should succeed. If `>` becomes `>=`,
// even the first allocation might fail.
let arena = Arena::builder().byte_budget(256 * 1024).build();
// Should succeed - within budget
let _v = arena.alloc(42u64);
}
#[test]
fn chunk_provider_441_shared_header_plus_target() {
let arena = Arena::builder().byte_budget(512 * 1024).build();
// Allocate shared (arc) values — each triggers acquire_shared
let _a1 = arena.alloc_arc(1u64);
let _a2 = arena.alloc_arc(2u64);
// If `+` became `*`, the budget would be consumed much faster
// and these allocations would likely fail or the budget check would
// prevent them.
}
// =====================================================================
// constants.rs mutants
// =====================================================================
#[test]
fn constants_76_min_class_ge_to_lt() {
// Allocating a large value forces acquire_local with a large payload
// that exercises min_class_for_bytes near MAX_CHUNK_BYTES.
let arena = Arena::new();
let big = vec![0u8; 64 * 1024];
let _alloc = arena.alloc_slice_copy(&big);
}
#[test]
#[cfg(feature = "stats")]
fn constants_87_loop_boundary() {
// Allocate a value that lands on a power-of-two class boundary.
// min_class_for_bytes(MIN_CHUNK_BYTES * 2) should return 1.
// If `<` becomes `<=`, it returns 2 instead.
// We can observe this through stats: with a tight budget,
// a higher class means a larger chunk allocation.
let arena = Arena::builder().byte_budget(128 * 1024).build();
let _v = arena.alloc(42u64);
// The allocation should succeed. If the class is wrong,
// the chunk might be too large and blow the budget.
}
// =====================================================================
// drop_list.rs mutants
// =====================================================================
// =====================================================================
// local_chunk.rs / chunk.rs mutants
// =====================================================================
#[test]
fn chunk_143_max_bump_extent() {
let arena = Arena::new();
let mut keep = Vec::new();
for i in 0u64..1000 {
keep.push(arena.alloc_arc(i));
}
for (i, v) in keep.iter().enumerate() {
assert_eq!(**v, i as u64);
}
}
#[test]
fn chunk_168_to_thin_ptr() {
let arena = Arena::new();
// Allocate and drop arcs to trigger chunk caching (which uses to_thin_ptr)
for _ in 0..5 {
let mut batch = Vec::new();
for i in 0u64..50 {
batch.push(arena.alloc_arc(i));
}
drop(batch);
}
// If to_thin_ptr returned null, the cache would be broken and
// subsequent allocations would fail or crash.
let final_arc = arena.alloc_arc(42u64);
assert_eq!(*final_arc, 42);
}
#[test]
fn chunk_186_payload_rounding() {
let _guard = reset_drop_counter();
let arena = Arena::new();
// Allocate arc values with Drop to exercise the chunk allocation
// with proper payload rounding for drop entries
let mut keep = Vec::new();
for i in 0..50 {
keep.push(arena.alloc_arc_with(|| DropTracker(i)));
}
drop(keep);
drop(arena);
let drops = drops();
assert_eq!(drops, 50, "all 50 shared DropTrackers must drop");
}
// =====================================================================
// strings/string.rs mutants
// =====================================================================
#[test]
fn string_465_try_reserve_boundary() {
let arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(10);
// Reserve exactly the remaining capacity
s.try_reserve(10).unwrap(); // needed == cap, should not grow
// Now push exactly 10 bytes
s.push_str("1234567890");
assert_eq!(s.as_str(), "1234567890");
// Reserve 0 more — should be no-op
s.try_reserve(0).unwrap();
}
// =====================================================================
// strings/utf16_string.rs mutants
// =====================================================================
#[test]
fn vec_451_resize_reserve() {
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity::<u64>(5);
v.push(1);
v.push(2);
// resize from 2 to 5 — reserve should be 3
v.resize(5, 0);
assert_eq!(v.len(), 5);
assert_eq!(v[0], 1);
assert_eq!(v[1], 2);
assert_eq!(v[2], 0);
}
#[test]
fn vec_460_461_resize_guard() {
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity::<u64>(20);
v.push(10);
v.push(20);
v.resize(8, 42);
assert_eq!(v.len(), 8);
assert_eq!(v[0], 10);
assert_eq!(v[1], 20);
for i in 2..8 {
assert_eq!(v[i], 42);
}
}
#[test]
fn vec_473_474_resize_total_new() {
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity::<u64>(10);
v.push(1);
// Resize to exactly len+1 — total_new == 1
v.resize(2, 99);
assert_eq!(v.len(), 2);
assert_eq!(v[1], 99);
// Resize to same length — total_new == 0, no-op
v.resize(2, 77);
assert_eq!(v.len(), 2);
assert_eq!(v[1], 99); // unchanged
}
#[test]
fn vec_762_into_box() {
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity::<u64>(10);
for i in 0..5 {
v.push(i * 10);
}
let boxed = v.into_boxed_slice();
assert_eq!(boxed.len(), 5);
assert_eq!(boxed[0], 0);
assert_eq!(boxed[1], 10);
assert_eq!(boxed[2], 20);
assert_eq!(boxed[3], 30);
assert_eq!(boxed[4], 40);
}
#[test]
fn vec_808_realloc_inplace_guard() {
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity::<u64>(4);
v.push(1);
v.push(2);
// Grow: new_cap > cap && cap > 0 → try in-place
v.reserve(10);
assert!(v.capacity() >= 12);
assert_eq!(v[0], 1);
assert_eq!(v[1], 2);
// From cap=0 → can't try in-place (cap > 0 is false)
let mut v2 = arena.alloc_vec_with_capacity::<u64>(0);
v2.push(42);
assert_eq!(v2[0], 42);
}
#[test]
fn vec_819_realloc_copy_guard() {
let arena = Arena::new();
// Start with cap=0, push to force realloc with len=0 initially
let mut v = arena.alloc_vec_with_capacity::<u64>(0);
assert_eq!(v.len(), 0);
v.push(1); // triggers realloc from cap=0
assert_eq!(v[0], 1);
// Now realloc with len > 0
v.reserve(100);
assert_eq!(v[0], 1);
}
#[test]
fn vec_828_realloc_relocation_tracking() {
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity::<u64>(2);
v.push(1);
v.push(2);
// Force a realloc that can't grow in place
// First alloc something else to prevent in-place growth
let _other = arena.alloc(99u64);
v.reserve(100);
assert_eq!(v[0], 1);
assert_eq!(v[1], 2);
}
#[test]
fn arena_709_entry_size_zero_arc() {
let arena = Arena::new();
// Arc<u64> — no Drop, entry_size == 0
// With `>=`, a drop entry would be written even though no space
// was reserved for it. Allocate many to make corruption likely.
let mut keep = Vec::new();
for i in 0u64..500 {
keep.push(arena.alloc_arc_with(|| i));
}
for (i, v) in keep.iter().enumerate() {
assert_eq!(**v, i as u64);
}
drop(keep);
drop(arena);
}
#[test]
fn arena_731_needed_tight_budget_arc() {
let _guard = reset_drop_counter();
// Use a tight byte budget so wrong `needed` could fail
let arena = Arena::builder().byte_budget(256 * 1024).build();
let mut keep = Vec::new();
for i in 0..200 {
keep.push(arena.alloc_arc_with(|| DropTracker(i)));
}
drop(keep);
drop(arena);
let drops = drops();
assert_eq!(drops, 200);
}
#[test]
fn arena_1251_oversized_guard_panic() {
// Use an alloc strictly larger than `MAX_CHUNK_BYTES = 64 KiB` so
// the chunk is truly oversized: after `reconcile_swap_out` the
// backing allocation is freed (and its budget released) rather
// than cached. Budget allows ONE such chunk but not two
// simultaneously. If `OversizedSharedGuard::drop` is a no-op
// (the mutant), the panicked alloc's chunk stays charged against
// the budget; the second oversized alloc then fails. We assert
// the second alloc succeeds, killing the mutant.
const N: usize = 70_000;
let arena = Arena::builder().max_normal_alloc(4096).byte_budget(N + 4096).build();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _arc: multitude::Arc<[u8; N]> = arena.alloc_arc_with(|| {
panic!("intentional panic in oversized arc closure");
});
}));
assert!(result.is_err(), "should have caught the panic");
// Only succeeds if the panicked chunk's budget was released by
// `OversizedSharedGuard::drop`. If `drop` is no-op'd, the budget
// is leaked and this `alloc_arc_with` (which calls `expect_alloc`)
// panics with "allocator returned AllocError".
let _arc2: multitude::Arc<[u8; N]> = arena.alloc_arc_with(|| [0u8; N]);
}
#[test]
fn arena_1648_high_alignment_layout() {
#[repr(align(64))]
#[derive(Clone, Copy)]
struct Aligned64 {
data: [u8; 64],
}
let arena = Arena::new();
// With `+ -> -`: needed = 64 + (64 - 8) = 120 vs 64 - (64 - 8) = 64 - 56 = 8
// The `- 56` would request only 8 bytes from refill, too small.
let mut keep = Vec::new();
for i in 0u8..100 {
let v = arena.alloc(Aligned64 { data: [i; 64] });
assert_eq!(v.data[0], i);
keep.push(core::ptr::addr_of!(*v) as usize);
}
// Verify all pointers are 64-byte aligned
for p in &keep {
assert_eq!(p % 64, 0, "pointer must be 64-byte aligned");
}
}
#[test]
fn arena_2261_empty_drop_slice() {
let _guard = reset_drop_counter();
let arena = Arena::new();
// Allocate many empty slices of Drop type
for _ in 0..500 {
let s = arena.alloc_slice_fill_with(0, |_| DropTracker(0));
assert_eq!(s.len(), 0);
}
// If entry_size was wrongly nonzero, we'd waste space and
// potentially corrupt the drop list.
// Also test non-empty non-Drop slices (drop_fn is None)
for i in 0u64..500 {
let s = arena.alloc_slice_fill_with(5, |j| i + j as u64);
assert_eq!(s[0], i);
}
drop(arena);
let drops = drops();
assert_eq!(drops, 0, "empty Drop slices should not produce drops");
}
#[test]
fn arena_2266_large_nondrop_slice() {
let arena = Arena::new();
// Try allocating a large non-Drop slice — should succeed with !=
// With == mutation, this would be rejected.
// u16::MAX + 1 = 65536 elements of u8 = 64KB
let result = arena.try_alloc_slice_fill_with(65536, |i| i as u8);
assert!(result.is_ok(), "large non-Drop slice should succeed");
let s = result.unwrap();
assert_eq!(s.len(), 65536);
assert_eq!(s[0], 0);
assert_eq!(s[65535], 255);
}
#[test]
fn arena_2655_empty_drop_shared_slice() {
let _guard = reset_drop_counter();
let arena = Arena::new();
for _ in 0..200 {
let arc = arena.alloc_slice_fill_with_arc(0, |_| DropTracker(0));
assert_eq!(arc.len(), 0);
drop(arc);
}
drop(arena);
let drops = drops();
assert_eq!(drops, 0, "empty Drop arc slices should not produce drops");
}
#[test]
fn arena_2660_large_nondrop_shared_slice() {
// Use a non-Copy, non-Drop wrapper so we go through try_alloc_slice_shared_with
// (the Copy path bypasses line 2660).
#[derive(Clone, Debug, PartialEq)]
struct W(u8);
let arena = Arena::new();
let result = arena.try_alloc_slice_fill_with_arc(65536, |i| W(i as u8));
assert!(result.is_ok(), "large non-Drop non-Copy shared slice should succeed");
let arc = result.unwrap();
assert_eq!(arc.len(), 65536);
assert_eq!(arc[0], W(0));
}
#[test]
fn arena_2701_entry_size_shared_slice() {
let arena = Arena::new();
// Allocate many non-Drop shared slices
let mut keep = Vec::new();
for i in 0u64..200 {
keep.push(arena.alloc_slice_fill_with_arc(5, |j| i + j as u64));
}
for (i, arc) in keep.iter().enumerate() {
assert_eq!(arc[0], i as u64);
assert_eq!(arc[4], i as u64 + 4);
}
}
#[test]
fn chunk_143_max_bump_many() {
let _guard = reset_drop_counter();
const N: u64 = 64;
let arena = Arena::builder().with_capacity(64 * 1024).build();
// 4 × 16 KiB uninit fillers walk the bump cursor to the chunk's true end.
let _fillers: Vec<multitude::Arc<core::mem::MaybeUninit<[u8; 16 * 1024]>>> =
(0..4).map(|_| arena.alloc_uninit_arc::<[u8; 16 * 1024]>()).collect();
let mut keep = Vec::new();
for i in 0..N {
keep.push(arena.alloc_arc_with(|| DropTracker(i)));
}
// Spot-check first/middle/last: a bump-cursor corruption affects
// every element identically.
assert_eq!(keep[0].0, 0);
assert_eq!(keep[(N / 2) as usize].0, N / 2);
assert_eq!(keep[(N - 1) as usize].0, N - 1);
drop(keep);
drop(arena);
let drops = drops();
assert_eq!(drops, N as usize);
}
#[test]
fn chunk_168_force_cache_reuse() {
let arena = Arena::new();
// Round 1: allocate arcs, fill a chunk
let mut batch1: Vec<multitude::Arc<u64>> = Vec::new();
for i in 0u64..100 {
batch1.push(arena.alloc_arc(i));
}
// Drop all arcs → chunk should be cached via to_thin_ptr
drop(batch1);
// Round 2: allocate more arcs — should reuse cached chunk
let mut batch2: Vec<multitude::Arc<u64>> = Vec::new();
for i in 0u64..100 {
batch2.push(arena.alloc_arc(i + 1000));
}
for (i, arc) in batch2.iter().enumerate() {
assert_eq!(**arc, i as u64 + 1000);
}
}
#[test]
fn chunk_186_payload_rounding_stress() {
let _guard = reset_drop_counter();
let arena = Arena::new();
// Stress test with many shared allocations of varying sizes
let mut keep = Vec::new();
for i in 0..500 {
keep.push(arena.alloc_arc_with(|| DropTracker(i)));
}
// Also test slices with varying sizes
let mut keep2 = Vec::new();
for i in 0..100 {
keep2.push(arena.alloc_slice_fill_with_arc(3, |j| DropTracker((i * 10 + j) as u64)));
}
drop(keep);
drop(keep2);
drop(arena);
let drops = drops();
assert_eq!(drops, 800, "500 singles + 300 slice elements = 800");
}
#[test]
fn string_465_reserve_exact_capacity() {
let arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(10);
s.push_str("12345");
// Reserve 5 more — total needed == cap (10), should not grow
s.try_reserve(5).unwrap();
// Reserve 6 more — total needed == 11 > cap, must grow
s.try_reserve(6).unwrap();
s.push_str("67890A");
assert_eq!(s.as_str(), "1234567890A");
}
// =====================================================================
// UTF-16 stronger tests
// =====================================================================
#[test]
fn vec_460_guard_panic_clone() {
use std::sync::atomic::AtomicUsize;
static CLONE_COUNT: AtomicUsize = AtomicUsize::new(0);
static DROP_COUNT2: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug)]
struct PanicClone(u64);
impl Clone for PanicClone {
fn clone(&self) -> Self {
let n = CLONE_COUNT.fetch_add(1, Ordering::Relaxed);
if n >= 3 {
panic!("clone panic at count {n}");
}
PanicClone(self.0)
}
}
impl Drop for PanicClone {
fn drop(&mut self) {
DROP_COUNT2.fetch_add(1, Ordering::Relaxed);
}
}
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity::<PanicClone>(20);
v.push(PanicClone(1));
v.push(PanicClone(2));
CLONE_COUNT.store(0, Ordering::SeqCst);
DROP_COUNT2.store(0, Ordering::SeqCst);
// Resize to 10 — will clone value 8 times. Panics on 4th clone (count=3).
// After 3 successful clones, len goes from 2→5 then panic.
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
v.resize(10, PanicClone(99));
}));
assert!(result.is_err(), "should panic during clone");
// Guard should have dropped 3 cloned elements.
// PanicClone(99) (the value param) is also dropped during unwind = +1.
// Total: 4 drops. With the `/` mutation, only 2 cloned + 1 value = 3.
let drops = DROP_COUNT2.load(Ordering::SeqCst);
assert_eq!(drops, 4, "guard must drop exactly 3 cloned elements + 1 value; got {drops}");
assert_eq!(v.len(), 2);
}
}
mod routing_boundary_behavior {
#![allow(clippy::std_instead_of_core, reason = "test code")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "test code")]
#![allow(clippy::cast_possible_truncation, reason = "test code")]
#![allow(clippy::panic, reason = "test code")]
#![allow(clippy::cast_lossless, reason = "test code")]
#![allow(clippy::doc_markdown, reason = "raw identifier names in docs")]
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicUsize, Ordering};
use multitude::Arena;
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
extern crate alloc;
#[test]
fn resize_uses_subtraction_for_reserve() {
let arena = Arena::new();
let mut v: multitude::vec::Vec<'_, u32> = arena.alloc_vec();
for i in 0..5 {
v.push(i);
}
assert_eq!(v.len(), 5);
let cap_before = v.capacity();
assert!(
cap_before <= 8,
"amortized growth from 0 pushes should land at cap=8, got {cap_before}"
);
v.resize(10, 0xAA);
assert_eq!(v.len(), 10);
// Original: additional = 10 - 5 = 5 ⇒ cap = max(10, 16, 4) = 16.
// Mutated `+`: additional = 10 + 5 = 15 ⇒ cap = max(20, 16, 4) = 20.
assert!(
v.capacity() <= 16,
"resize must subtract len from new_len when computing growth (cap={})",
v.capacity()
);
}
#[derive(Clone)]
#[expect(dead_code, reason = "scaffold kept for future tests")]
struct PanicAfter {
n: StdArc<AtomicUsize>,
limit: usize,
}
#[test]
fn resize_guard_drop_uses_subtraction() {
use std::panic::{set_hook, take_hook};
struct Ctor(StdArc<AtomicUsize>, usize);
impl Clone for Ctor {
fn clone(&self) -> Self {
let prev = self.0.fetch_add(1, Ordering::SeqCst);
assert!(prev + 1 < self.1, "planned clone panic at index {prev}");
Self(self.0.clone(), self.1)
}
}
let counter = StdArc::new(AtomicUsize::new(0));
// Silence the panic logger for the duration of the unwind.
let prev = take_hook();
set_hook(Box::new(|_| {}));
let result = catch_unwind(AssertUnwindSafe(|| {
let arena = Arena::new();
let mut v: multitude::vec::Vec<'_, Ctor> = arena.alloc_vec();
// Start from EMPTY vec so old_len == 0 ⇒ mutated `/ 0` div-by-zero.
// Resize to 3: clones template twice, then moves template into last slot.
// We make the SECOND clone panic.
let template = Ctor(counter.clone(), 2);
v.resize(3, template);
}));
set_hook(prev);
assert!(result.is_err(), "resize must panic via the planted clone panic");
let payload = result.unwrap_err();
let s = payload
.downcast_ref::<String>()
.cloned()
.or_else(|| payload.downcast_ref::<&'static str>().map(std::string::ToString::to_string))
.unwrap_or_default();
// Original: panic payload contains "planned clone panic".
// Mutated (`/`): the Guard drop triggers div-by-zero, aborting the
// process before catch_unwind sees a payload — process aborts.
// If we reach this assertion, the test ran without abort; the
// payload string must be the *planted* one. The mutated version
// would either abort or surface a divide-by-zero panic.
assert!(
s.contains("planned clone panic"),
"unexpected panic payload: {s:?} (mutated `/ 0` in Guard::drop would surface as divide-by-zero)"
);
}
#[test]
fn shrink_to_fit_at_full_cap_is_noop_documented() {
let arena = Arena::new();
let mut v: multitude::vec::Vec<'_, u32> = arena.alloc_vec_with_capacity(8);
for i in 0..8 {
v.push(i);
}
assert_eq!(v.len(), v.capacity());
let ptr_before = v.as_ptr();
v.shrink_to_fit();
let ptr_after = v.as_ptr();
assert_eq!(ptr_before, ptr_after);
}
}
mod misc_alloc_behavior {
#![allow(clippy::std_instead_of_core, reason = "test code")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::clone_on_ref_ptr, reason = "test code")]
#![allow(clippy::doc_markdown, reason = "raw identifier names in docs")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
use multitude::Arena;
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
#[test]
fn alloc_uninit_slice_arc_non_drop_above_u16_max_succeeds() {
let arena = Arena::new();
let arc = arena
.try_alloc_uninit_slice_arc::<u8>(u16::MAX as usize + 2)
.expect("non-Drop slice with len > u16::MAX must succeed via oversized path");
assert_eq!(arc.len(), u16::MAX as usize + 2);
}
}
mod alloc_invariants_audit {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::cast_possible_truncation, reason = "test data is small")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "tests group related unsafe ops")]
#![allow(clippy::items_after_statements, reason = "test-local helpers next to their use")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(unused_results, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::manual_assert, reason = "explicit panic message clearer in test")]
#![allow(clippy::assertions_on_result_states, reason = "tests deliberately assert error returns")]
#![allow(clippy::needless_pass_by_value, reason = "test helpers")]
#![allow(clippy::empty_drop, reason = "tests need non-trivial-drop types to exercise drop-path branches")]
#![allow(clippy::allow_attributes, reason = "test helpers use allow uniformly")]
#![allow(clippy::allow_attributes_without_reason, reason = "obvious in test context")]
use multitude::vec::Vec as ArenaVec;
use multitude::{Arc, Arena};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
// ============================================================================
// vec.rs:473 — resize Guard::drop's `if added > 0`
// vec.rs:507 — resize_with's `let added = self.vec.len - self.old_len;`
// vec.rs:515/516 — resize_with Guard::drop's `added = len - old` and `if added > 0`
//
// These are panic-safety guards. Killing requires:
// (a) panic mid-resize after some elements have been written and verify
// only the partial set is dropped (kills `> 0` → `>=` boundary because
// the added==0 case happens when init panics on the very first element);
// (b) panic before any element is written (added == 0) and verify
// len rolls back to old_len without dropping anything.
// ============================================================================
#[test]
fn resize_panic_in_middle_drops_only_added_elements() {
use core::cell::Cell;
use std::panic::AssertUnwindSafe;
struct Counter<'a>(&'a Cell<u32>);
impl Drop for Counter<'_> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
impl Clone for Counter<'_> {
fn clone(&self) -> Self {
Counter(self.0)
}
}
let drops = Cell::new(0);
let panics = Cell::new(0_u32);
let arena = Arena::new();
{
let mut v: ArenaVec<'_, Counter<'_>> = arena.alloc_vec_with_capacity(8);
v.push(Counter(&drops));
v.push(Counter(&drops));
// 2 pre-existing elements; resize pushes 5 more (4 clones + 1 move).
// We arrange the panic to fire after some clones via a cloning side
// effect — simulate by using a value whose clone panics. Easier:
// use resize_with so we can panic on the Nth call.
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
v.resize_with(7, || {
let n = panics.get();
panics.set(n + 1);
if n == 3 {
panic!("synthetic init panic");
}
Counter(&drops)
});
}));
assert!(result.is_err());
// 2 pre-existing + 3 successfully written before the panic.
// The Guard drops the 3 newly-added; len rolls back to 2.
// The 2 pre-existing get dropped when v drops at end of scope.
// After Guard runs we should have seen exactly 3 drops.
assert_eq!(drops.get(), 3, "guard should drop exactly the 3 added elements");
assert_eq!(v.len(), 2);
}
// Now the 2 originals also dropped.
drop(arena);
assert_eq!(drops.get(), 5);
}
#[test]
fn resize_panic_on_first_element_added_is_zero() {
use core::cell::Cell;
use std::panic::AssertUnwindSafe;
struct Counter<'a>(&'a Cell<u32>);
impl Drop for Counter<'_> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
let drops = Cell::new(0);
let arena = Arena::new();
{
let mut v: ArenaVec<'_, Counter<'_>> = arena.alloc_vec_with_capacity(4);
v.push(Counter(&drops));
// Panic on the very first init call: added == 0 in Guard::drop.
// Kills `if added > 0` → `if added >= 0`: with the mutant, the
// `>= 0` branch executes `from_raw_parts_mut(..., 0)` and
// `drop_in_place` over an empty slice — observationally equivalent.
// But the mutant ALSO walks `data.add(old_len)` for an empty
// slice; if old_len == cap, that's one-past-the-end which is
// legal but Miri / UBSAN would catch invalid pointer arithmetic
// if old_len exceeded cap. The mutant survives because a 0-len
// slice from a one-past pointer is well-defined — so the only
// way to kill is to verify the post-condition (len rolls back
// and zero added drops).
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
v.resize_with(3, || {
panic!("synthetic init panic");
});
}));
assert!(result.is_err());
assert_eq!(v.len(), 1);
assert_eq!(drops.get(), 0, "no added elements; nothing should be dropped by Guard");
}
drop(arena);
assert_eq!(drops.get(), 1, "the original element drops with the Vec");
}
// ============================================================================
// vec.rs:621 / vec.rs:838 — into_arena_rc/box's `if self.len > u16::MAX as usize`
//
// The boundary at `u16::MAX` (65535) is unreachable in the in-place
// branch: that branch requires the Vec data buffer to live in the arena's
// `current`, which in turn requires `len * size_of::<T>() <=
// max_normal_alloc`. With `max_normal_alloc <= max_bump_extent < 64 KiB`
// and `size_of::<T>() >= 1` (the in-place branch already short-circuits
// the ZST/empty case), `len * size_of::<T>() <= max_bump_extent <
// u16::MAX`. So `self.len > u16::MAX` cannot fire on a Vec the in-place
// path is willing to handle. The check is defensive and equivalent to
// removing it.
// ============================================================================
#[cfg(feature = "dst")]
#[allow(dead_code, reason = "helper kept after moving its consumers to dst.rs; preserved for future tests")]
struct OneByteDrop(#[allow(dead_code)] u8);
#[cfg(feature = "dst")]
impl Drop for OneByteDrop {
fn drop(&mut self) {}
}
// ============================================================================
// vec.rs:634 — into_arena_rc's `if needs_drop && len > 0`
// vec.rs:837 — into_box's `if needs_drop && self.len > 0`
//
// Mutant: `>` → `>=`. With `>= 0` (always true for usize) the empty Drop
// vec would attempt to install a slice DropEntry of len=0, which would
// panic / abort because the back-stack is full or because the helper
// rejects len==0 explicitly. Kill: empty vec of Drop type round-trips
// without abort.
// ============================================================================
// ============================================================================
// vec.rs:657 / 859 / 878 — `if cap > len` reclaim guard.
// Mutant `>=`: at cap == len the mutant tries to reclaim 0 bytes.
// `try_shrink_at_cursor(buffer_end, 0)` may decrement the cursor by 0
// (no-op) or panic on debug assertions. Kill via stats counter or by
// observing that a subsequent allocation lands on the cursor where
// the buffer ended.
// ============================================================================
// ============================================================================
// vec.rs:911 — into_box's `consumed_cell.set(idx + 1)`
// Mutant `+ → *`: at idx==0 both yield 0 → infinite loop / wrong index.
// Kill: copy at least 2 elements and verify all are present.
// ============================================================================
// ============================================================================
// vec.rs:963 — realloc's `if new_cap > self.cap && self.cap > 0`
// Mutant `>=`: with new_cap == self.cap, the mutant tries grow-in-place
// of zero bytes, which is a no-op shrink. With self.cap == 0 (fresh vec)
// the mutant calls try_grow_in_place with a dangling pointer — would
// likely abort.
// vec.rs:976 — realloc's `if self.len > 0` for memcpy of old data.
// Mutant `>=`: at len==0 the mutant copies 0 bytes from a dangling
// pointer (likely OK). To kill: verify a non-empty Vec preserves data
// across realloc.
// ============================================================================
#[test]
fn realloc_preserves_data_across_growth() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(2);
v.push(0xdead_beef);
v.push(0xcafe_babe);
// Force a realloc by pushing more than the initial capacity.
for i in 2..10_u32 {
v.push(i);
}
assert_eq!(v[0], 0xdead_beef);
assert_eq!(v[1], 0xcafe_babe);
for i in 2..10_u32 {
assert_eq!(v[i as usize], i);
}
}
#[test]
fn realloc_empty_to_nonempty_skips_memcpy() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(0);
// Cap == 0 initially, len == 0. First push triggers realloc with
// self.cap == 0 (skips the in-place branch via the second clause)
// and self.len == 0 (skips the memcpy branch).
v.push(7);
assert_eq!(v[0], 7);
assert_eq!(v.len(), 1);
}
// ============================================================================
// vec.rs:1311 — Drain TailFix::drop's `if tail_len > 0`
// Mutant `>=`: at tail_len == 0 (drained to end) the mutant tries to
// `ptr::copy(..., 0)` from one-past-the-end. Well-defined, but kill via
// the post-condition (len update is correct).
// ============================================================================
#[test]
fn drain_to_end_leaves_correct_len() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
for i in 0..6_u32 {
v.push(i);
}
{
let drained: std::vec::Vec<u32> = v.drain(2..).collect();
assert_eq!(drained, [2, 3, 4, 5]);
}
assert_eq!(v.len(), 2);
assert_eq!(v[0], 0);
assert_eq!(v[1], 1);
}
#[test]
fn drain_middle_shifts_tail_correctly() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
for i in 0..6_u32 {
v.push(i);
}
{
let drained: std::vec::Vec<u32> = v.drain(2..4).collect();
assert_eq!(drained, [2, 3]);
}
// Tail [4, 5] must shift down to indices [2, 3].
assert_eq!(v.len(), 4);
assert_eq!(v[0], 0);
assert_eq!(v[1], 1);
assert_eq!(v[2], 4);
assert_eq!(v[3], 5);
}
#[test]
fn drain_panic_in_drop_still_runs_tail_fix() {
use core::cell::Cell;
use std::panic::AssertUnwindSafe;
struct Boom<'a> {
on_drop: &'a Cell<u32>,
explodes: bool,
}
impl Drop for Boom<'_> {
fn drop(&mut self) {
self.on_drop.set(self.on_drop.get() + 1);
if self.explodes {
panic!("synthetic drop panic");
}
}
}
let drop_count = Cell::new(0);
let arena = Arena::new();
{
let mut v: ArenaVec<'_, Boom<'_>> = arena.alloc_vec_with_capacity(6);
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 0
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 1
v.push(Boom {
on_drop: &drop_count,
explodes: true,
}); // 2 - panics on drop
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 3 (drained but not yielded)
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 4 (tail)
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 5 (tail)
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
// Drain [2..4]; iterate forward consuming index 2 (which panics on drop).
let mut d = v.drain(2..4);
// Yield index 2 — Boom drops on the consumer's side.
let yielded = d.next().expect("at least one element");
// Force the drop here (panics).
drop(yielded);
}));
assert!(result.is_err(), "yielded element's drop should panic");
// Drain went out of scope (panicked). TailFix should still have run:
// tail [4,5] shifts down to indices [2,3], len == 4.
assert_eq!(v.len(), 4);
}
drop(arena);
}
/// Regression: when an unyielded element's `Drop` panics during
/// `Drain::drop`, the remaining unyielded drained elements must still
/// be dropped (panic-policy parity with `std::vec::Drain::drop`, which
/// uses `drop_in_place::<[T]>` to delegate to rustc's slice-drop guard).
/// Previously `multitude` used a per-element loop that leaked the tail
/// elements past the first panicking drop.
#[test]
fn drain_partial_consume_panic_in_drop_still_drops_remaining_unyielded() {
use core::cell::Cell;
use std::panic::AssertUnwindSafe;
struct Boom<'a> {
on_drop: &'a Cell<u32>,
explodes: bool,
}
impl Drop for Boom<'_> {
fn drop(&mut self) {
self.on_drop.set(self.on_drop.get() + 1);
assert!(!self.explodes, "synthetic drop panic");
}
}
let drop_count = Cell::new(0_u32);
let arena = Arena::new();
{
let mut v: ArenaVec<'_, Boom<'_>> = arena.alloc_vec_with_capacity(7);
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 0 (kept)
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 1 (kept)
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 2 (drained, unyielded, drops cleanly first)
v.push(Boom {
on_drop: &drop_count,
explodes: true,
}); // 3 (drained, unyielded, PANICS)
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 4 (drained, unyielded, must still drop)
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 5 (kept tail)
v.push(Boom {
on_drop: &drop_count,
explodes: false,
}); // 6 (kept tail)
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
// Drain [2..5] without consuming any elements. Drop runs at end of scope.
let _ = v.drain(2..5);
}));
assert!(result.is_err(), "drain drop must propagate the element-drop panic");
// All three drained elements must have been dropped, even though
// element 3 panicked in the middle — std::vec::Drain has the same
// contract via slice-drop glue. Plus tail shift to [2,3], len == 4.
assert_eq!(
drop_count.get(),
3,
"all 3 drained elements must drop, even with a panic in the middle"
);
assert_eq!(v.len(), 4);
}
drop(arena);
}
// ============================================================================
// arena.rs:1721 — try_alloc_slice_shared_oversized_with's
// `if entry_size != 0 && len > u16::MAX as usize { return Err(...) }`
// Mutant `>` → `<`: rejects short Drop-aware slices, accepts long ones.
// Kill: verify a short Drop-aware oversized slice succeeds; then verify
// a >u16::MAX Drop-aware oversized slice returns Err.
// ============================================================================
#[test]
fn try_alloc_slice_shared_drop_aware_short_oversized_ok() {
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicU32, Ordering};
let arena = Arena::builder().max_normal_alloc(4 * 1024).build();
let counter = StdArc::new(AtomicU32::new(0));
struct D(StdArc<AtomicU32>);
impl Drop for D {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
{
// Force oversized routing: 4 KiB max_normal_alloc; allocate
// a slice of 8 KiB (=512 D values, each 16 bytes-ish).
let arc: Arc<[D]> = arena.alloc_slice_fill_with_arc(512, |_i| D(counter.clone()));
assert_eq!(arc.len(), 512);
}
drop(arena);
assert_eq!(counter.load(Ordering::Relaxed), 512);
}
// ============================================================================
// arena.rs:1643 / 1751 — slice oversized helpers' `init_guard.len += 1;`
// Mutant `+= → *=`: with init_guard.len starting at 0, `0 *= 1` stays
// at 0 forever. Then on init panic mid-way, the SliceInitGuard drops
// 0 elements instead of N — leaks T::drop. Kill: panic mid-init in the
// oversized helper and verify the right number of drops happened.
// ============================================================================
#[test]
fn try_alloc_slice_shared_oversized_init_panic_drops_partial() {
use std::panic::AssertUnwindSafe;
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicU32, Ordering};
let drops = StdArc::new(AtomicU32::new(0));
struct D(StdArc<AtomicU32>);
impl Drop for D {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let arena = Arena::builder().max_normal_alloc(4 * 1024).build();
let drops_ref = drops.clone();
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
// 1024 * 8 bytes > max_normal_alloc(4 KiB) → oversized shared path.
let _: Arc<[D]> = arena.alloc_slice_fill_with_arc(1024, |i| {
if i == 100 {
panic!("synthetic init panic");
}
D(drops_ref.clone())
});
}));
assert!(result.is_err());
assert_eq!(drops.load(Ordering::Relaxed), 100);
drop(arena);
}
// ============================================================================
// arena.rs slice-with paths — `if layout.size() > self.provider.max_normal_alloc`
// boundary mutants: at `==` the original takes the fast path; the mutant
// `<` keeps small allocations on fast path (already, no change) but
// causes large allocations to use the fast path too, which then refills
// to an oversized chunk via worst-case-size. Net observable effect is
// best caught by allocations *just above* the boundary, where the
// original routes through the oversized helper directly.
// ============================================================================
// ============================================================================
// arena.rs:1332 / 2030 — alloc_inner_*_or_panic's `if bumped > MAX_CHUNK_BYTES`
// is intrinsically guarded — `bumped` is a compile-time-known size for the
// value paths and the safety check is unreachable for any value type a user
// can construct. Targeted by other equivalent boundary tests above.
// ============================================================================
// ============================================================================
// arena.rs refill_local/refill_shared bump_extent branch (lines 726, 1055):
// `if capacity > MAX_CHUNK_BYTES { capacity } else { capacity.min(...) }`
// Mutant `<`: would invert the condition and use `capacity` for normal
// chunks (allowing bump cursor past the first 64 KiB tile). Subsequent
// allocations would then resolve the wrong chunk header via the mask.
// Kill via stress: many allocations and a Drop type to exercise drop
// list replay.
// ============================================================================
// ============================================================================
// arena.rs:3036 / 3608 — slice paths' `if entry_size != 0 && len > u16::MAX`
// (panic-first). Mutant `!=` → `==`: with entry_size == 0 (no drop) the
// mutant runs the panic check; with entry_size != 0 it skips. The result
// is that a Copy slice longer than u16::MAX would panic. Kill: a Copy
// slice of length > u16::MAX must succeed.
// ============================================================================
// ============================================================================
// arena.rs:3039 / 3611 — slice paths' `if layout.size() > self.provider.max_normal_alloc`
// (panic-first). The "just above" check on these is the same structural
// boundary as the non-panic variants — already covered above. The "at
// exact" boundary cannot be observed because both branches eventually
// allocate via the oversized path due to compute_worst_case_size adding
// `align + entry_size` to the request, which always pushes a slice of
// exactly `max_normal_alloc` bytes past the routing threshold inside
// `acquire_local`.
// ============================================================================
// ============================================================================
// arena.rs:3097 / 3659 — slice paths' `guard.len += 1` (init guard counter)
// Mutant `+= → *=`: same as the oversized-helper variant, but for the
// fast path. Kill: panic mid-init in the fast path (small slice fits in
// a normal chunk) and verify partial-init drops are exactly N.
// ============================================================================
#[test]
fn alloc_slice_shared_fast_path_init_panic_drops_partial() {
use std::panic::AssertUnwindSafe;
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicU32, Ordering};
struct D(StdArc<AtomicU32>);
impl Drop for D {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let drops = StdArc::new(AtomicU32::new(0));
let arena = Arena::new();
let drops_ref = drops.clone();
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
let _: Arc<[D]> = arena.alloc_slice_fill_with_arc(64, |i| {
if i == 32 {
panic!("synthetic");
}
D(drops_ref.clone())
});
}));
assert!(result.is_err());
assert_eq!(drops.load(Ordering::Relaxed), 32);
drop(arena);
}
// ============================================================================
// The slice paths' drop-fn install guard (`is-Box && drop_fn && len != 0`).
// Mutants delete the `!`/swap `!=` to `==`.
//
// `delete !` mutant: skips the drop_fn install for non-Box paths.
// Without the install, the noop_drop_shim stays in the entry → elements
// leak. Kill: Rc slice of Drop type, drop the Rc, drop the arena, count
// drops.
// `!= → ==` mutant: only installs drop_fn when len == 0 — empty slices
// don't have an entry installed in the first place (entry_size == 0),
// so this mutant is observationally no-op for typical inputs. Kill:
// non-empty slice of Drop type via `Rc::from_*` should drop properly.
// ============================================================================
#[test]
fn alloc_slice_shared_arc_drop_type_runs_drop() {
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicU32, Ordering};
struct D(StdArc<AtomicU32>);
impl Drop for D {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let drops = StdArc::new(AtomicU32::new(0));
let arena = Arena::new();
{
let a: Arc<[D]> = arena.alloc_slice_fill_with_arc(8, |_| D(drops.clone()));
assert_eq!(a.len(), 8);
drop(a);
}
drop(arena);
assert_eq!(drops.load(Ordering::Relaxed), 8);
}
// ============================================================================
// arena.rs:3116 / 3676 — slice paths' refill `compute_worst_case_size(layout, entry_size != 0)`
// `!= → ==` mutant: passes `entry_size == 0` to compute_worst_case_size, which
// flips the "needs entry" flag. The downstream chunk capacity may be too
// small to fit both the slice and its drop entry. Kill: large drop-aware
// slice that needs a refill — must succeed.
// ============================================================================
// ============================================================================
// arena.rs:2076 / 941 — alloc_inner_*_or_panic's drop-count and `needed`
// arithmetic. The `+ → *` and `+ → -` mutants on
// `let needed = layout.size() + alignment + entry_size`: changing `+` to
// `*` produces wildly larger needed-size; if it stays ≤ MAX_CHUNK_BYTES,
// the chunk class still satisfies the original request — equivalent.
// Kill: at `layout.size() == max_normal_alloc - small_amount` the
// difference between `size + align + entry_size` (= max_normal_alloc-ish)
// and `size * align * entry_size` (= astronomically larger) routes the
// mutant to fail refill or fall back to oversized.
// ============================================================================
// (Hard to test via public API — covered indirectly by all the slice/value
// tests above that refill across many chunk classes.)
// ============================================================================
// Per-`Arc` reference counting removes the `u16` element-count cap on
// `Arc<[T]>` slices: a Drop-typed slice with `len > u16::MAX` now
// allocates (via the oversized path) and drops each element through
// `drop_in_place::<[T]>` in `Arc::drop`.
// ============================================================================
#[cfg(not(miri))]
#[test]
fn alloc_slice_shared_drop_aware_above_u16_max_succeeds() {
use std::sync::Arc as StdArc;
use std::sync::atomic::{AtomicU32, Ordering};
struct D(StdArc<AtomicU32>);
impl Drop for D {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let drops = StdArc::new(AtomicU32::new(0));
let arena = Arena::builder().max_normal_alloc(60 * 1024).build();
let n = 65_536_usize;
let arc = arena
.try_alloc_slice_fill_with_arc(n, |_| D(drops.clone()))
.expect("Arc slices have no u16 element-count cap");
assert_eq!(arc.len(), n);
drop(arc);
assert_eq!(drops.load(Ordering::Relaxed), n as u32);
}
// ============================================================================
// vec.rs:507:34 — `self.reserve(new_len - self.len)` in resize_with.
// Mutant `-` -> `+`: reserves `new_len + self.len`. Both work, but mutant
// over-reserves. Kill via capacity observation.
// ============================================================================
#[test]
fn resize_with_reserves_minimal_capacity() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(0);
v.push(1);
v.push(2);
v.push(3);
v.push(4);
let cap_before = v.capacity();
v.resize_with(8, || 99_u32);
let cap_after = v.capacity();
// Original: additional = 8 - 4 = 4. doubled = max(4 + 4, 4*2, 4) = 8.
// Mutant: additional = 8 + 4 = 12. doubled = max(4 + 12, 4*2, 4) = 16.
assert!(
cap_after < 16,
"resize_with from len=4 to 8 should not over-reserve (cap_before={cap_before}, cap_after={cap_after})"
);
assert_eq!(v.len(), 8);
assert_eq!(v.as_slice(), &[1, 2, 3, 4, 99, 99, 99, 99]);
}
// ============================================================================
// vec.rs:556:67 — `self.data.as_ptr().add(self.len - 1)` in pop_if.
// Mutant `-` -> `/`: passes `self.len / 1 = self.len` (one past end).
// Kill: spy on the value the predicate sees.
// ============================================================================
#[test]
fn pop_if_predicate_sees_last_element() {
use core::cell::Cell;
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(4);
v.push(0xaaaa_aaaa);
v.push(0xbbbb_bbbb);
v.push(0xcccc_cccc);
let seen = Cell::new(0_u32);
let r = v.pop_if(|x| {
seen.set(*x);
*x == 0xcccc_cccc
});
assert_eq!(seen.get(), 0xcccc_cccc, "predicate must see the final element, not past-end memory");
assert_eq!(r, Some(0xcccc_cccc));
assert_eq!(v.as_slice(), &[0xaaaa_aaaa, 0xbbbb_bbbb]);
}
// ============================================================================
// vec.rs:911:35 — `consumed_cell.set(idx + 1)` in `into_box`.
// Mutant `+` -> `*`: with idx==0, `0 * 1 == 0`; consumed_cell never
// advances, every closure invocation reads `data[0]`. Kill: route the
// buffer to an oversized chunk so install fails, then verify boxed
// elements distinctly match the source.
// ============================================================================
}
mod string_slice_utf16_behavior {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::cast_possible_truncation, reason = "test data is small")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "tests group related unsafe ops")]
#![allow(clippy::items_after_statements, reason = "test-local helpers next to their use")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(unused_results, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::manual_assert, reason = "explicit panic message clearer in test")]
#![allow(clippy::assertions_on_result_states, reason = "tests deliberately assert error returns")]
#[expect(unused_imports, reason = "documentation of test target types")]
use multitude::strings::String as ArenaString;
#[cfg(feature = "utf16")]
#[expect(unused_imports, reason = "documentation of test target types")]
use multitude::strings::Utf16String;
use multitude::vec::Vec as ArenaVec;
use multitude::{Arc, Arena};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
// ----------------------------------------------------------------------------
// vec.rs:285 — replace - with + in Vec::insert
//
// Original: `ptr::copy(ptr, ptr.add(1), self.len - idx);`
// Mutant: `ptr::copy(ptr, ptr.add(1), self.len + idx);`
//
// With `len=3, idx=1`: `len-idx=2`, `len+idx=4`. Original shifts 2
// elements (correct); mutant would copy 4 elements (UB / wrong data).
// ----------------------------------------------------------------------------
#[test]
fn vec_insert_middle_shifts_exact_tail() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
v.extend([10_u32, 20, 30, 40, 50]);
v.insert(1, 99);
assert_eq!(v.as_slice(), &[10, 99, 20, 30, 40, 50]);
}
#[test]
fn vec_insert_near_start_preserves_all_elements() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
for i in 0..7_u32 {
v.push(i);
}
v.insert(2, 100);
assert_eq!(v.as_slice(), &[0_u32, 1, 100, 2, 3, 4, 5, 6]);
}
// ----------------------------------------------------------------------------
// vec.rs:303 — `self.len - idx - 1` in Vec::remove
//
// Mutants:
// `self.len - idx + 1` (replaces second `-` with `+`)
// `self.len + idx - 1` (replaces first `-` with `+`)
// `self.len - idx / 1` (replaces second `-` with `/`)
//
// With `len=3, idx=0`: original copies 2 elements; mutants copy
// different counts → different remaining contents.
// ----------------------------------------------------------------------------
#[test]
fn vec_remove_first_shifts_all_remaining() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
v.extend([1_u32, 2, 3, 4, 5]);
let r = v.remove(0);
assert_eq!(r, 1);
assert_eq!(v.as_slice(), &[2_u32, 3, 4, 5]);
}
#[test]
fn vec_remove_middle_shifts_exact_tail() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
v.extend([1_u32, 2, 3, 4, 5]);
let r = v.remove(2);
assert_eq!(r, 3);
assert_eq!(v.as_slice(), &[1_u32, 2, 4, 5]);
}
#[test]
fn vec_remove_second_shifts_three_remaining() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
v.extend([10_u32, 20, 30, 40, 50]);
let r = v.remove(1);
assert_eq!(r, 20);
assert_eq!(v.as_slice(), &[10_u32, 30, 40, 50]);
}
// ----------------------------------------------------------------------------
// vec.rs:359 — replace < with <= in shrink_to_fit
//
// Original: `if self.len < self.cap && self.realloc(self.len).is_err()`
// Mutant: `if self.len <= self.cap && self.realloc(self.len).is_err()`
//
// When `len == cap`, original short-circuits (no realloc); mutant enters
// the branch and calls `realloc(len)` where `new_cap == self.cap`,
// triggering the `debug_assert!(new_cap != self.cap)` in `realloc`.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// vec.rs:429 — replace > with >= in try_reserve_exact
//
// Original: `if needed > self.cap { self.realloc(needed)?; }`
// Mutant: `if needed >= self.cap { self.realloc(needed)?; }`
//
// When `needed == cap`, mutant calls `realloc(cap)`, which fires the
// `debug_assert!(new_cap != self.cap)` in `realloc`.
// ----------------------------------------------------------------------------
#[test]
#[cfg(debug_assertions)]
fn vec_try_reserve_exact_at_capacity_is_noop() {
let arena = Arena::new();
let mut v: ArenaVec<'_, u32> = arena.alloc_vec_with_capacity(8);
v.extend([0_u32, 1, 2]);
// needed = 3 + 5 = 8 == cap. Original short-circuits; mutant
// realloc-call would assert-fail.
v.try_reserve_exact(5).unwrap();
assert_eq!(v.capacity(), 8);
}
// ----------------------------------------------------------------------------
// vec.rs:948 — realloc's in-place grow guards: `new_cap > self.cap && self.cap > 0`
// Mutants:
// `new_cap > self.cap || self.cap > 0` (`&&` → `||`)
// `new_cap >= self.cap && self.cap > 0` (`>` → `>=`)
// `new_cap > self.cap && self.cap >= 0` (`>` → `>=`)
// vec.rs:959 — `let Some(grown) = unsafe { ... }` then the `>` on Layout::array().is_ok()
// vec.rs:968 — `if old_cap > 0 { self.arena.bump_relocation(); }`
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// vec.rs:643 / vec.rs:644 / vec.rs:862-863 / vec.rs:895:
// `reclaim_bytes = (cap - len) * elem_size` in into_arena_rc/box paths.
// Mutants: `(cap - len) + elem_size`, `(cap + len) * elem_size`,
// `(cap - len) / elem_size`, etc.
// Detection: assert that the freeze path reclaims exactly the unused
// tail so a subsequent allocation lands in the same chunk.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// vec.rs:606 / vec.rs:619 — `needs_drop && self.len > u16::MAX as usize`
// Mutants: `&&` → `||`, `>` → `>=`.
// Detection: a `T: Drop` slice with exactly `u16::MAX` elements must
// take the in-place freeze path (not the copy fallback). A slice with
// `u16::MAX + 1` elements must take the copy fallback (the back-stack
// entry's length field is u16).
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// vec.rs:458, 502 — Guard::drop's `if added > 0 { drop_in_place(tail) }`
// Mutants: `>` → `>=`.
//
// `added == 0` => `drop_in_place(&mut [])` is a no-op. The mutant adds an
// unnecessary call but produces the same observable behavior. Mark these
// as documented-equivalent.
// ----------------------------------------------------------------------------
// (No test required — equivalent mutation. Covered by panic-recovery tests
// in arena_vec.rs::resize_panic_in_clone_drops_already_written which
// exercises the Guard::drop code path with `added > 0`.)
// ----------------------------------------------------------------------------
// vec.rs:493 — `reserve(new_len - self.len)` in resize_with
// Mutant: `reserve(new_len + self.len)` over-reserves but doesn't break behavior.
// The reservation is still sufficient — over-reservation is observable through
// `arena.stats()` chunk-allocation counts only.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// vec.rs:1293 — TailFix::drop's `if tail_len > 0 { copy ... }`
// Mutant: `>` → `>=`. With `tail_len == 0`, original skips; mutant calls
// `ptr::copy(src, dst, 0)` which is a no-op. Equivalent.
//
// (No test required — equivalent mutation by ptr::copy semantics on len=0.)
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// strings/string.rs:421 / 452 — try_push_str / try_reserve boundary
// Mutants: `needed > self.cap` → `needed >= self.cap`. At needed == cap,
// the mutant calls `try_grow_to_at_least(needed)` whose debug_assert!
// guards against `min_cap <= self.cap`.
// ----------------------------------------------------------------------------
#[test]
#[cfg(debug_assertions)]
fn string_try_push_str_at_exact_capacity_no_grow() {
let arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(8);
s.push_str("abcde");
// needed = 5 + 3 = 8 == cap; must skip grow path.
s.push_str("fgh");
assert_eq!(&*s, "abcdefgh");
}
// ----------------------------------------------------------------------------
// strings/string.rs:207 — shrink_to_fit's `if self.cap == 0 || self.len == self.cap { return; }`
// Mutant: `||` → `&&`. Then `cap == 0` allocations would attempt to grow,
// triggering the `try_grow_to_at_least` debug_assert.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// strings/string.rs:239 / 359 — insert_str / replace_range boundary `new_len > self.cap`
// Mutants: `>` → `>=`. Same kill mechanism as try_push_str.
// ----------------------------------------------------------------------------
#[test]
#[cfg(debug_assertions)]
fn string_insert_str_at_exact_capacity_no_grow() {
let arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(6);
s.push_str("abc");
// new_len = 3 + 3 = 6 == cap; must not enter grow path.
s.insert_str(0, "xyz");
assert_eq!(&*s, "xyzabc");
}
#[test]
#[cfg(debug_assertions)]
fn string_replace_range_at_exact_capacity_no_grow() {
let arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(6);
s.push_str("abc");
// Replace 1 char ('b') with 4 chars; new_len = 6 == cap.
s.replace_range(1..2, "WXYZ");
assert_eq!(&*s, "aWXYZc");
}
// ----------------------------------------------------------------------------
// strings/string.rs:268 / utf16_string.rs:348 — remove arithmetic
// `let next = idx + ch.len_utf8(); ... copy(src, dst, self.len - next)`
// Mutants: `-` → `+`, `-` → `/`.
//
// With a single-byte char and 3 chars after it, original copies 3 bytes,
// `+` mutant copies wrong count → wrong remaining string.
// ----------------------------------------------------------------------------
#[test]
fn string_remove_first_preserves_rest() {
let arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("hello");
let c = s.remove(0);
assert_eq!(c, 'h');
assert_eq!(&*s, "ello");
}
#[test]
fn string_remove_middle_preserves_split() {
let arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("abcdef");
let c = s.remove(2);
assert_eq!(c, 'c');
assert_eq!(&*s, "abdef");
}
// ----------------------------------------------------------------------------
// strings/string.rs:306 — retain's `idx_dst + n_bytes`
// Mutants: `+` → `-`, `+` → `*`.
//
// Original: bytes-from-source moved by `len-n_bytes` count.
// ----------------------------------------------------------------------------
#[test]
fn string_retain_preserves_filtered_chars() {
let arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("hello world");
s.retain(|c| !c.is_whitespace());
assert_eq!(&*s, "helloworld");
}
// ----------------------------------------------------------------------------
// strings/string.rs:366 — replace_range's `let tail = ... self.len - end_idx`
// Mutants: `-` → `+`.
// ----------------------------------------------------------------------------
#[test]
fn string_replace_range_preserves_tail() {
let arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("Hello, World!");
s.replace_range(7..12, "Rust");
assert_eq!(&*s, "Hello, Rust!");
}
#[test]
fn string_replace_range_replace_with_longer_preserves_tail() {
let arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("abcDEFghi");
s.replace_range(3..6, "WXYZ");
assert_eq!(&*s, "abcWXYZghi");
}
#[test]
fn string_replace_range_replace_with_shorter_preserves_tail() {
let arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("abcDEFghi");
s.replace_range(3..6, "X");
assert_eq!(&*s, "abcXghi");
}
// ----------------------------------------------------------------------------
// strings/string.rs:515 / utf16_string.rs:493 — try_reclaim_tail's
// `if cap >= len { let reclaim = cap - len; }` (or similar).
// Mutants: `>=` → `<`, `replace ... with ()`, `-` → `/`.
//
// `try_reclaim_tail` is called after push/grow operations to release
// unused tail capacity. To kill `replace ... with ()`, observe that the
// chunk's cursor advances by less than expected after reclaim.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// strings/utf16_string.rs:183 — truncate's `if new_len > self.len { return; }`
// Mutant: `>` → `>=`. At new_len == len, original short-circuits; mutant
// re-clamps and writes the prefix.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// strings/utf16_string.rs:195 — shrink_to_fit `cap == 0 || len == cap`
// Same as string.rs:207 mutant.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// strings/utf16_string.rs:198 / 199 — shrink_to_fit byte-arithmetic
// `reclaim_units = cap - len; reclaim_bytes = reclaim_units * 2;`
// Mutants: `-` → `/`, `*` → `+`, `*` → `/`.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// strings/utf16_string.rs:252 / 269 / 290 / 310 / 322 / 398 / 405 / 420 — many
// boundary checks; same kill mechanism as string.rs equivalents.
// ----------------------------------------------------------------------------
#[test]
#[cfg(all(debug_assertions, feature = "utf16"))]
fn utf16_try_push_str_at_exact_capacity_no_grow() {
let arena = Arena::new();
let mut s = arena.alloc_utf16_string_with_capacity(8);
s.push_from_str("abcd");
// Worst-case reservation: 4 BMP chars = 8 units; needed == cap.
s.try_push_from_str("efgh").unwrap();
assert_eq!(s.len(), 8);
}
// ----------------------------------------------------------------------------
// box.rs:209 — Box<[T]>::into_rc's `if needs_drop && len > u16::MAX as usize`
// Mutants: `&&` → `||`, `>` → `>=`.
// Same boundary as vec.rs:606 / 619.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:767 — try_alloc_inner_arc_with's `if bumped > MAX_CHUNK_BYTES`
// Mutant: `>` → `>=`. At exact equality, mutant routes to the oversized
// path even though the request fits in a normal chunk. Detection through
// stats counters.
//
// (Hard to test deterministically without exact MAX_CHUNK_BYTES; covered
// by `oversized_chunk_used_when_alloc_too_big` already.)
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:825 — `if entry_size > 0` (drop entry installation)
// Mutant: `>` → `>=`. Always falsy with usize variable; equivalent only if
// entry_size is non-zero. For `T: !Drop`, entry_size is `0`; for `T: Drop`,
// entry_size is `size_of::<InnerDropEntry>()` (>0). Both paths already
// well-tested by existing Drop-aware tests.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:848 — `if layout.size() > self.provider.max_normal_alloc`
// Same `>` → `>=` mutation; detection via oversized stats.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:851 — `let needed = layout.size() + layout.align().saturating_sub(...) + entry_size;`
// Mutants: `+` → `-`, `+` → `*`.
//
// Detection: an alignment-demanding allocation must succeed.
// ----------------------------------------------------------------------------
#[repr(align(64))]
#[derive(Debug)]
struct Align64(u32);
#[test]
fn over_aligned_arc_allocation_succeeds_with_extra_padding() {
let arena = Arena::new();
let a: Arc<Align64> = arena.alloc_arc(Align64(0xDEAD_BEEF));
assert_eq!(a.0, 0xDEAD_BEEF);
let ptr: *const Align64 = core::ptr::from_ref(&*a);
assert_eq!(ptr.align_offset(64), 0);
}
// ----------------------------------------------------------------------------
// arena.rs:5155 — check_isize_overflow: `if total > (isize::MAX as usize).saturating_sub(padding)`
// Mutant: `>` → `>=`. At exact equality, original returns Ok; mutant errors.
//
// (No deterministic boundary test feasible — covered by general alloc
// smoke tests that succeed at smaller sizes.)
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:5180 — `check_chunk_alignment -> Result<(), AllocError> with Ok(())`
// Mutant replaces the function with `Ok(())`. To kill, allocate with
// alignment >= MAX_SMART_PTR_ALIGN through a DST path and observe error.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:5339 / 5348 — try_bump_fit's range checks
// `if end > drop_back_addr { return None; }`
// `if end > payload_end_addr { return None; }`
// Mutant: `>` → `>=`. Boundary: at exact equality the original accepts.
// ----------------------------------------------------------------------------
#[test]
fn try_bump_fit_at_exact_chunk_end_succeeds() {
// Cannot exercise the boundary deterministically because the chunk
// layout is hidden. But many smoke tests would fail if `>` flipped
// to `>=` because every successful bump-fit at the exact end is now
// rejected. Covered transitively by `arena_arc.rs` / `arena_box.rs`
// tests that allocate near boundaries.
}
// ----------------------------------------------------------------------------
// arena_builder.rs:174 — `resolve_capacity`'s `cap - 1` for `next_power_of_two`-style logic.
// Mutant: `-` → `+`, `-` → `/`.
// Detection: build an arena with a specific preallocation and observe
// chunk count.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// internal/constants.rs:77 — min_class_for_bytes arithmetic
// Mutant: `bits = usize::BITS - bytes.leading_zeros()` then `-` → `+`.
// Detection: directly test `min_class_for_bytes` via integration: build
// an arena, allocate at various sizes, verify that class progression
// matches expectations through preallocate/stats.
//
// Indirectly covered by the chunk-acquisition test paths.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// internal/drop_list.rs — pad_bytes / raw_used_bytes arithmetic and replacements
// `raw_used_bytes`: `sizeof::<fn>() + 2 + 2`
// `pad_bytes`: padding to PAD_TARGET alignment.
//
// I consolidated these as constants. Mutating the underlying constants
// would change `PAD_BYTES`, causing DropEntry layout to misalign. Existing
// drop-list tests would catch this. The constants are only computed at
// compile time so the mutants are stale (constants don't exist as runtime
// functions any more).
//
// Note: these mutants may be against the previous version of the code.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// internal/local_chunk.rs:132 / chunk.rs:155 — max_bump_extent
// `capacity - drop_count * size_of::<DropEntry>()`
// Mutants: `-` → `+`, `-` → `/`. These would change the available
// space for bump allocations.
//
// Detection: many allocations exercise drop-list growth + bump fit;
// changing this arithmetic would either over- or under-estimate
// available space, causing either premature OOM or write-past-end.
//
// Covered by existing drop-aware tests.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// internal/local_chunk.rs:158 / chunk.rs:167 — entries_top_offset's
// boundary: `if drop_count < entries_top_offset(capacity) / sizeof::<DropEntry>()`
// Mutant: `<` → `<=`.
//
// Off-by-one in drop-list growth gate. Caught by tests that nearly fill
// the back-stack.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// internal/chunk_provider.rs:186 / 253 / 419 / 447 — acquire_local/shared arithmetic
// `local_header_size() + rounded_payload` / `class_to_bytes(class) - local_header_size()`
// etc. Mutants: `-` → `+`, `-` → `/`.
//
// Detection: byte_budget should be consumed by chunk-header + payload.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// internal/chunk_provider.rs:300 — preallocate_local's `if target_class > *h`
// I removed this — already addressed.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// internal/chunk_provider.rs:524 — `release_budget(shared_header_size() + cap)`
// Mutant: `+` → `*`. Misaccounting in budget release.
//
// Detection: a workload that recycles chunks must keep the budget bounded.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// internal/constants.rs:123 — `refcount_overflow_abort` impl Drop for ForceAbort
// Mutant: replace `drop` with `()`. ForceAbort is `no_std` fallback and
// the path is `#[cfg_attr(coverage_nightly, coverage(off))]`. Document as
// genuinely unreachable in tested configurations.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:446 — `Arena::builder()` returns `ArenaBuilder<Global>`.
// Mutant: `Default::default()` returns the same thing.
// EQUIVALENT — both call sites produce the same result; no test required.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:1017 / 1299 / 1388 — `match guard e <= cap.saturating_sub(entry_size) with true`
// These are inside the oversized-allocation routes where the provider's
// post-condition guarantees the chunk fits. I replaced them with
// `assert_unchecked`; the mutants are stale.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:1838 — allocate_layout's `prefix + payload + align - 1`
// Mutant: `-` → `+` in `align - 1`. Changes worst-case bytes needed.
// ----------------------------------------------------------------------------
#[test]
fn allocate_layout_handles_alignment_padding() {
let arena = Arena::new();
// Force an aligned allocation that requires padding.
let _a: Arc<Align64> = arena.alloc_arc(Align64(1));
}
// ----------------------------------------------------------------------------
// arena.rs:853 — `needed = layout.size() + layout.align().saturating_sub(...) + entry_size`
// in try_alloc_inner_arc_with. Mutating `+` to `*` makes `needed` enormous,
// forcing routing through oversized chunks for ordinary small types.
// Detection: stats should show no oversized chunks for ordinary allocs.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:1252 — try_alloc_inner_oversized_value: `match aligned.checked_add(layout.size()) { Some(e) if e <= cap.saturating_sub(entry_size) => ... }`
// Mutant `&& -> ||`: `e <= cap.saturating_sub(entry_size) || ...` always true.
//
// This branch is the post-condition guard of `provider.acquire_local`. I
// replaced it with `assert_unchecked` for fast-paths but the value-path mutant
// remains.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:5180-style — `check_chunk_alignment -> Result<(), AllocError> with Ok(())`
// Mutant: function replaced with `Ok(())`. Killed by `dst_arc_rejects_excessive_alignment_via_layout`.
// ----------------------------------------------------------------------------
// At exact equality of `layout.size()` and `max_normal_alloc`, original keeps
// going through normal chunks; mutant routes to oversized.
//
// The boundary check `size > max_normal_alloc` is mutation-resistant in
// practice because the chunk allocator must reserve header + drop-entry
// overhead, so a request of exactly `max_normal_alloc` bytes can fail
// the bump fit even on a chunk of class `max_normal_alloc`. Both
// original and mutated boundaries thus may route to oversized.
//
// Behavioral correctness is asserted by general-purpose alloc tests.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// arena.rs:448 — Arena::builder() returns ArenaBuilder<Global>, constructed
// via the crate-internal `ArenaBuilder::new()`. `ArenaBuilder` no longer
// implements `Default`, so the former `from(Default::default())` mutant is
// no longer generated. No test required.
// ----------------------------------------------------------------------------
}
mod freeze_and_box_behavior {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::cast_possible_truncation, reason = "test data is small")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "tests group related unsafe ops")]
#![allow(clippy::items_after_statements, reason = "test-local helpers next to their use")]
#![allow(clippy::collection_is_never_read, reason = "tests retain handles to keep chunks alive")]
#![allow(unused_results, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::manual_assert, reason = "explicit panic message clearer in test")]
use multitude::vec::Vec as ArenaVec;
use multitude::{Arc, Arena, Box as ArenaBox};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
// ============================================================================
// Reclaim-tail observability tests
// ----------------------------------------------------------------------------
// Many missed mutants involve `(cap - len) * elem_size` or string equivalents.
// Reclaim returns the unused tail to the chunk's bump cursor, so a subsequent
// allocation that needs that exact space MUST succeed without allocating a
// new chunk. Wrong arithmetic either reclaims too little (subsequent alloc
// spills into a new chunk) or too much (cursor moves into already-allocated
// territory and follow-up writes corrupt earlier data).
// ============================================================================
// ============================================================================
// drop_list constant-layout test
// ----------------------------------------------------------------------------
// Mutants on lines 71/75 of drop_list.rs change the computed `PAD_BYTES`,
// breaking `size_of::<DropEntry>()` and corrupting drop-list stack walks.
// Verified through observable behavior: many drop-typed allocs must each
// drop exactly once.
// ============================================================================
#[test]
fn many_drop_typed_arcs_each_drop_exactly_once() {
use core::sync::atomic::{AtomicUsize, Ordering};
static DROPPED: AtomicUsize = AtomicUsize::new(0);
struct D(#[expect(dead_code, reason = "field discriminates instances")] u32);
impl Drop for D {
fn drop(&mut self) {
DROPPED.fetch_add(1, Ordering::Relaxed);
}
}
// SAFETY: D only carries a u32 + atomic side-effect on drop.
unsafe impl Send for D {}
unsafe impl Sync for D {}
DROPPED.store(0, Ordering::SeqCst);
let arena = Arena::new();
let mut keepers: std::vec::Vec<Arc<D>> = std::vec::Vec::new();
for i in 0..64_u32 {
keepers.push(arena.alloc_arc(D(i)));
}
drop(keepers);
drop(arena);
assert_eq!(DROPPED.load(Ordering::SeqCst), 64);
}
#[test]
fn many_drop_typed_slices_drop_each_element_once() {
use core::sync::atomic::{AtomicUsize, Ordering};
static DROPPED: AtomicUsize = AtomicUsize::new(0);
struct D(#[expect(dead_code, reason = "field discriminates instances")] u32);
impl Drop for D {
fn drop(&mut self) {
DROPPED.fetch_add(1, Ordering::Relaxed);
}
}
// SAFETY: same rationale.
unsafe impl Send for D {}
unsafe impl Sync for D {}
DROPPED.store(0, Ordering::SeqCst);
let arena = Arena::new();
let mut keepers: std::vec::Vec<Arc<[D]>> = std::vec::Vec::new();
for batch in 0..8_u32 {
keepers.push(arena.alloc_slice_fill_with_arc(8, move |i| D(batch * 8 + i as u32)));
}
drop(keepers);
drop(arena);
assert_eq!(DROPPED.load(Ordering::SeqCst), 64);
}
// ============================================================================
// `cap == len` short-circuit: into_box at exact cap=len skips reclaim.
// ----------------------------------------------------------------------------
// At `cap == len`, original skips reclaim; mutant `>=` tries to reclaim 0
// bytes (no-op). Behavior observable through chunk count not changing.
// ============================================================================
// ============================================================================
// `into_box`'s ZST/empty routing (`== with !=` at line 834)
// ----------------------------------------------------------------------------
// Mutant inverts the early-return condition. Non-ZST non-empty vec must
// take the in-place path (no new chunk). With mutant, it takes the copy
// fallback which allocates fresh slice storage.
// ============================================================================
#[test]
fn vec_into_box_empty_routes_through_copy_path() {
let arena = Arena::new();
let v: ArenaVec<'_, u32> = arena.alloc_vec();
let b: ArenaBox<[u32]> = v.into_boxed_slice();
assert_eq!(b.len(), 0);
}
// ============================================================================
// `into_box`'s `consumed_cell.set(idx + 1)` (line 922)
// ----------------------------------------------------------------------------
// Mutant `+ with *`: `set(idx * 1) = idx`. Loop never advances and resulting
// slice holds N copies of element 0. Detection: copy path with distinct
// element values must preserve order.
// ============================================================================
// ============================================================================
// try_bump_fit `>` boundary (lines 5263/5272)
// ----------------------------------------------------------------------------
// Mutant `>=` rejects exact-fit allocations. Every successful allocation
// must pass this gate, so a workload that allocates many small items would
// inflate chunk turnover dramatically with the mutant.
// ============================================================================
// ============================================================================
// chunk_provider.rs:536 `+ with *` in release_budget arithmetic
// ----------------------------------------------------------------------------
// Wrong release arithmetic drifts the budget tracker and eventually fails.
// Tightly-budgeted arena cycling allocations exercises this.
// ============================================================================
// ============================================================================
// needs_drop_indirect -> true: non-drop slices must not reserve drop entries
// ----------------------------------------------------------------------------
// Mutant: function always returns true, so non-drop allocations also reserve
// a drop-entry slot. Reduces usable payload per chunk and inflates count.
// ============================================================================
// ============================================================================
// String / Utf16String shrink_to_fit reclaim arithmetic
// ----------------------------------------------------------------------------
// Mutants on `let reclaim = self.cap - self.len` or
// `reclaim_bytes = reclaim_units * 2` change the bytes returned to the chunk
// cursor. With wrong arithmetic, a follow-up allocation either spills to a new
// chunk or corrupts the preceding region (asserted by reading back the frozen
// handle).
// ============================================================================
}
mod public_surface_behavior {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain smart pointers to keep chunks alive")]
#![allow(unused_results, reason = "test code")]
#![allow(clippy::used_underscore_binding, reason = "intentional drop-after binding")]
#![allow(clippy::cast_possible_truncation, reason = "test data is small")]
#![allow(clippy::explicit_into_iter_loop, reason = "test clarity")]
#![allow(clippy::assertions_on_result_states, reason = "tests deliberately assert error returns")]
#![allow(clippy::items_after_statements, reason = "test-local statics next to their use")]
#![allow(
clippy::cast_ptr_alignment,
reason = "test writes a u32 to a u8-typed reservation we created with u32 layout"
)]
#![allow(clippy::undocumented_unsafe_blocks, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "tests group related unsafe ops")]
use core::sync::atomic::{AtomicUsize, Ordering};
#[cfg(feature = "dst")]
use multitude::Arc;
use multitude::vec::{CollectIn, Vec};
use multitude::{Arena, ArenaBuilder};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
use crate::common::{FailingAllocator, SendFailingAllocator};
#[test]
fn allocator_deallocate_triggers_teardown_when_last_ref() {
// <&Arena as Allocator>::deallocate's `if needs_teardown` branch:
// the deallocate must observe refcount → 0 and call teardown_chunk.
// Achieved by forcing many grow → relocate cycles inside a Vec
// backed by `&Arena`: each old buffer's deallocate eventually
// tears down its chunk (the chunk's only ref was the Vec's
// buffer, and after retirement the arena no longer holds it).
let arena: Arena = Arena::builder().build();
{
let mut v: allocator_api2::vec::Vec<u8, &Arena> = allocator_api2::vec::Vec::new_in(&arena);
for _ in 0..16_000_u32 {
v.push(0);
}
drop(v);
}
}
#[test]
fn builder_debug_format() {
// Drives ArenaBuilder's Debug impl.
let s = format!("{:?}", Arena::builder());
assert!(s.contains("ArenaBuilder"));
assert!(s.contains("max_normal_alloc"));
}
#[test]
fn builder_preallocate_alloc_failed() {
// Drives the AllocError return path in ArenaBuilder::try_build by
// giving the builder an allocator that refuses to allocate.
let alloc = FailingAllocator::new(0);
let result = Arena::builder().with_capacity(512).allocator_in(alloc).try_build();
assert!(result.is_err());
}
#[test]
fn arena_box_drop_unlinks_middle_of_drop_list() {
// `unlink_drop_entry` has three positions (head, middle, tail).
// The middle case is reached when the entry being removed has both
// a `prev` and a `next`. ArenaBox<T: Drop>::drop calls unlink. We
// create three drop-needing ArenaBox values, then drop the second
// one first → exercises the `Some(prev)` AND `Some(next)` branches.
let arena = Arena::new();
let mut b1 = arena.alloc_box(std::string::String::from("first"));
let mut b2 = arena.alloc_box(std::string::String::from("middle"));
let mut b3 = arena.alloc_box(std::string::String::from("last"));
// Make sure each value is reachable (touch the contents).
b1.push('!');
b2.push('!');
b3.push('!');
drop(b2); // <-- middle of doubly-linked list
assert_eq!(*b1, "first!");
assert_eq!(*b3, "last!");
}
#[test]
fn cached_local_chunk_revived_as_shared() {
// `revive_cached_chunk(chunk, Shared)` → `reinit_refcount(_, Shared, 1)`
// dispatches to the Shared branch. The deterministic way to land
// a chunk in the cache is `.preallocate(n)`, which seeds the cache
// before the first allocation. Then `alloc_arc` pops the cache and
// revives the chunk as Shared.
let arena: Arena = Arena::builder().with_capacity(1024).build();
let shared = arena.alloc_arc(99_u64);
assert_eq!(*shared, 99);
let join = std::thread::spawn(move || *shared);
assert_eq!(99, join.join().unwrap());
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_box_panics_on_failing_allocator() {
let arena: Arena<FailingAllocator> = Arena::new_in(FailingAllocator::new(0));
let _ = arena.alloc_box(0_u32);
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_box_with_panics_on_failing_allocator() {
let arena: Arena<FailingAllocator> = Arena::new_in(FailingAllocator::new(0));
let _ = arena.alloc_box_with(|| 0_u32);
}
#[test]
#[cfg(feature = "dst")]
fn alloc_dst_arc_rejects_excessive_alignment() {
let arena: Arena = Arena::new();
let huge_align = 128 * 1024_usize;
let layout = core::alloc::Layout::from_size_align(huge_align, huge_align).unwrap();
let r = unsafe {
arena.try_alloc_dst_arc::<[u8]>(layout, 0_usize, |_| {
unreachable!("init must not be called when allocation fails");
})
};
assert!(r.is_err());
}
#[test]
#[cfg(feature = "dst")]
fn alloc_dst_box_rejects_excessive_alignment() {
let arena: Arena = Arena::new();
let huge_align = 128 * 1024_usize;
let layout = core::alloc::Layout::from_size_align(huge_align, huge_align).unwrap();
let r = unsafe {
arena.try_alloc_dst_box::<[u8]>(layout, 0_usize, |_| {
unreachable!("init must not be called when allocation fails");
})
};
assert!(r.is_err());
}
// `#[repr(align(N))]` with N > CHUNK_ALIGN (64 KiB). Used by the two
// tests below to drive the `if layout.align() > CHUNK_ALIGN { return
// Err(AllocError) }` guard in `try_alloc_with` and `try_reserve_and_init`.
//
// The guard lives in a thin outer function whose frame doesn't depend
// on `T`'s alignment, so the test runs on every LLVM-backed platform —
// including Windows, whose default 1 MiB stack can't accommodate the
// 128 KiB-aligned frame the guarded body would otherwise require.
//
// Skipped under the UTC codegen backend (`--cfg utc_backend`): UTC caps
// type alignment at 8192 bytes, well below the 128 KiB this test needs.
#[cfg(not(utc_backend))]
#[repr(align(131072))]
struct HugeAlign(#[expect(dead_code, reason = "field present to give the type a non-zero size")] u8);
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_with_rejects_excessive_alignment() {
// try_alloc_with is the Alloc<T> entry point. CHUNK_ALIGN is 64 KiB;
// HugeAlign needs 128 KiB alignment, so the layout-align check
// must fire and return Err.
let arena: Arena = Arena::new();
let result: Result<multitude::Alloc<'_, HugeAlign>, _> = arena.try_alloc_with(|| HugeAlign(0));
assert!(result.is_err());
}
#[test]
fn try_alloc_string_with_capacity_huge_returns_err() {
let arena: Arena = Arena::new();
// Try a capacity that overflows when adding the prefix size.
let too_big = usize::MAX;
assert!(arena.try_alloc_string_with_capacity(too_big).is_err());
}
#[test]
fn try_alloc_string_with_capacity_isize_max_returns_err() {
// Drives the `isize::try_from(total).is_err()` guard in
// ArenaString::try_allocate_initial. Need cap such that
// `cap + PREFIX_SIZE` is between `isize::MAX + 1` and `usize::MAX`.
let arena: Arena = Arena::new();
let cap = (isize::MAX as usize) - 4; // cap + 8 > isize::MAX, and < usize::MAX
assert!(arena.try_alloc_string_with_capacity(cap).is_err());
}
// Note: the `align > CHUNK_ALIGN` guard inside the typed alloc paths
// (`Arena::try_alloc_with`, `Arena::try_reserve_and_init`) cannot be
// exercised from a test that names a `#[repr(align(N))]` `T` with
// `N > CHUNK_ALIGN` — even though the closure / value would never be
// constructed, the compiled function's stack frame inherits `T`'s
// alignment, producing a STATUS_ACCESS_VIOLATION on call. The
// equivalent guard is exercised through the layout-based path in
// `alloc_uninit_dst_rejects_excessive_alignment` above (which uses
// `Layout::from_size_align` directly without naming a `T`).
#[test]
fn arena_string_grow_through_chunk_rotation() {
// Drives the `if needs_teardown { teardown_chunk(chunk, true); }`
// branch in `Arena::grow_for_string` — when the OLD string buffer's
// chunk has only the string as a holder (refcount==1 → after dec
// it's 0 → teardown).
let arena: Arena = Arena::builder().build();
let mut s = arena.alloc_string();
// Push enough text to force the string to grow into a fresh chunk;
// the old chunk had ONLY this string (no other allocations) so its
// refcount drops to 0 on grow → triggers teardown_chunk.
let chunk = "x".repeat(64);
for _ in 0..200 {
s.push_str(&chunk);
}
assert_eq!(s.len(), 200 * 64);
}
#[test]
fn arena_vec_deref_mut_modifies_in_place() {
let arena = Arena::new();
let mut v: Vec<u32, _> = arena.alloc_vec();
v.push(1);
v.push(2);
v.push(3);
// Modify via DerefMut (not via push).
let slice: &mut [u32] = &mut v;
slice[0] = 99;
assert_eq!(v.as_slice(), &[99, 2, 3]);
}
#[test]
fn collect_in_empty_iterator_uses_new_in() {
// An iterator with `size_hint().0 == 0` should take the `new_in`
// path (no `with_capacity_in(0)` detour). Easiest: filter that
// discards everything but advertises `(0, _)`.
let arena = Arena::new();
let v: Vec<u32, _> = (0..10_u32).filter(|_| false).collect_in(&arena);
assert!(v.is_empty());
}
#[test]
#[cfg(feature = "dst")]
fn alloc_dst_arc_runs_drop_on_chunk_teardown() {
use core::sync::atomic::{AtomicUsize, Ordering as Ord};
static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
DROP_COUNT.store(0, Ord::SeqCst);
struct Tracked(#[expect(dead_code, reason = "field exists only for size")] u32);
impl Drop for Tracked {
fn drop(&mut self) {
let _ = DROP_COUNT.fetch_add(1, Ord::SeqCst);
}
}
let arena: Arena = Arena::new();
{
let layout = core::alloc::Layout::array::<Tracked>(1).unwrap();
// SAFETY: layout matches [Tracked; 1]; init writes one Tracked.
let arc: Arc<[Tracked]> = unsafe {
arena.alloc_dst_arc::<[Tracked]>(layout, 1_usize, |fat: *mut [Tracked]| {
fat.cast::<Tracked>().write(Tracked(0xCAFE_F00D));
})
};
assert_eq!(arc.len(), 1);
// Move arc to another thread (exercises the Send+Sync path).
let h = std::thread::spawn(move || arc.len());
let val = h.join().unwrap();
assert_eq!(val, 1);
}
drop(arena);
assert_eq!(DROP_COUNT.load(Ord::SeqCst), 1, "drop must run exactly once");
}
// Use ArenaBuilder type (covered by allocator_in test) to silence
// unused-import warnings if any of the above tests change.
#[test]
fn builder_type_is_constructible() {
let _: ArenaBuilder = Arena::builder();
}
// Infallible Arc / Box slice constructors and the strait-`alloc_arc` family.
// These wrap their `try_*` cousins with `unwrap_or_else(panic_alloc)`; the
// happy path was previously uncovered.
#[test]
fn arena_try_alloc_str_arc_succeeds() {
use multitude::Arc;
let arena: Arena = Arena::new();
let s: Arc<str> = arena.try_alloc_str_arc("hello arc").unwrap();
assert_eq!(s.as_str(), "hello arc");
}
#[test]
fn arena_try_alloc_str_box_succeeds() {
use multitude::Box;
let arena: Arena = Arena::new();
let s: Box<str> = arena.try_alloc_str_box("hello box").unwrap();
assert_eq!(s.as_str(), "hello box");
}
#[test]
fn arena_box_str_as_mut_via_trait() {
let arena: Arena = Arena::new();
let mut s = arena.alloc_str_box("abc");
let m: &mut str = AsMut::<str>::as_mut(&mut s);
// SAFETY: ASCII bytes; in-place uppercase preserves UTF-8.
unsafe { m.as_bytes_mut()[0] = b'A' };
assert_eq!(s.as_str(), "Abc");
}
// ArenaString::with_capacity_in (cap > 0) — exercises allocate_initial path
// (line 102 / 324) and into_arena_str slack reclamation (line 258).
#[test]
fn alloc_string_with_capacity_allocates_buffer() {
let arena: Arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(64);
assert!(s.capacity() >= 64);
s.push_str("hello world");
assert_eq!(s.as_str(), "hello world");
}
#[test]
fn try_alloc_vec_with_capacity_succeeds() {
let arena: Arena = Arena::new();
let mut v = arena.try_alloc_vec_with_capacity::<u32>(16).unwrap();
assert!(v.capacity() >= 16);
v.push(1);
v.push(2);
assert_eq!(&*v, &[1, 2]);
}
// `panic_alloc` closure paths for the Arc/Box variants of slice / value
// constructors. These mirror the existing tests for the Rc variants;
// each drives the `unwrap_or_else(|_| panic_alloc())` closure body so it
// shows as covered.
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_arc_panics_on_failing_allocator() {
let arena: Arena<SendFailingAllocator> = Arena::new_in(SendFailingAllocator::new(0));
let _ = arena.alloc_arc(0_u32);
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_arc_with_panics_on_failing_allocator() {
let arena: Arena<SendFailingAllocator> = Arena::new_in(SendFailingAllocator::new(0));
let _ = arena.alloc_arc_with(|| 0_u32);
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_slice_copy_arc_panics_on_failing_allocator() {
let arena: Arena<SendFailingAllocator> = Arena::new_in(SendFailingAllocator::new(0));
let _ = arena.alloc_slice_copy_arc([0_u8; 4]);
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_slice_clone_arc_panics_on_failing_allocator() {
let arena: Arena<SendFailingAllocator> = Arena::new_in(SendFailingAllocator::new(0));
let _ = arena.alloc_slice_clone_arc([1_u32, 2]);
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_slice_fill_with_arc_panics_on_failing_allocator() {
let arena: Arena<SendFailingAllocator> = Arena::new_in(SendFailingAllocator::new(0));
let _ = arena.alloc_slice_fill_with_arc::<u32, _>(4, |i| i as u32);
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_slice_fill_iter_arc_panics_on_failing_allocator() {
let arena: Arena<SendFailingAllocator> = Arena::new_in(SendFailingAllocator::new(0));
let _ = arena.alloc_slice_fill_iter_arc([1_u32, 2, 3]);
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_str_panics_on_failing_allocator() {
let arena: Arena<FailingAllocator> = Arena::new_in(FailingAllocator::new(0));
let _ = arena.alloc_str("hi");
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_str_arc_panics_on_failing_allocator() {
let arena: Arena<SendFailingAllocator> = Arena::new_in(SendFailingAllocator::new(0));
let _ = arena.alloc_str_arc("hi");
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_str_box_panics_on_failing_allocator() {
let arena: Arena<FailingAllocator> = Arena::new_in(FailingAllocator::new(0));
let _ = arena.alloc_str_box("hi");
}
#[test]
#[should_panic(expected = "multitude: allocator returned AllocError")]
fn alloc_string_with_capacity_panics_on_failing_allocator() {
let arena: Arena<FailingAllocator> = Arena::new_in(FailingAllocator::new(0));
let _ = arena.alloc_string_with_capacity(64);
}
// Drive `build`'s `unwrap_or_else(panic_build)` closure for each
// allocator monomorphization so the per-instantiation region count
// reaches 100% in the coverage report.
#[test]
#[should_panic(expected = "multitude::ArenaBuilder::build")]
fn build_panics_on_failing_allocator() {
let _: Arena<FailingAllocator> = Arena::builder().allocator_in(FailingAllocator::new(0)).with_capacity(512).build();
}
#[test]
#[should_panic(expected = "multitude::ArenaBuilder::build")]
fn build_panics_on_send_failing_allocator() {
let _: Arena<SendFailingAllocator> = Arena::builder()
.allocator_in(SendFailingAllocator::new(0))
.with_capacity(512)
.build();
}
// Distinct type from `HugeAlign` above so we don't perturb the caller's frame
// alignment and trigger the issue noted in the comment near
// `try_alloc_with_rejects_excessive_alignment`. The `MaybeUninit<T>` returned
// by the uninit-family entry points never materializes a real `T` on the
// stack, so the test compiles and runs safely on every platform.
#[cfg(not(utc_backend))]
#[repr(align(131072))]
struct HugeAlignBox(#[expect(dead_code, reason = "field gives the type a non-zero size")] u8);
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_uninit_box_rejects_excessive_alignment() {
let arena: Arena = Arena::new();
let r = arena.try_alloc_uninit_box::<HugeAlignBox>();
assert!(r.is_err());
}
#[test]
fn arena_string_replace_range_excluded_start() {
use core::ops::Bound;
let arena: Arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("hello");
// Excluded(0) -> start = 1, Excluded(3) -> end = 3 -> replace bytes 1..3 ("el") with "X"
s.replace_range((Bound::Excluded(0_usize), Bound::Excluded(3_usize)), "X");
assert_eq!(&*s, "hXlo");
}
#[test]
fn arena_string_replace_range_grow_path() {
let arena: Arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("ab");
// Replacement is much longer than what's removed, forcing a grow
// (`new_len > self.cap` branch in replace_range).
s.replace_range(0..1, "lots of replacement text");
assert_eq!(&*s, "lots of replacement textb");
}
#[test]
fn arena_string_replace_range_added_gt_removed_no_grow() {
// Drives the `added > removed` arm of replace_range with the
// `new_len > self.cap` check evaluating to false (the buffer
// already has enough capacity for the larger replacement).
let arena: Arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(64);
s.push_str("abc");
s.replace_range(0..1, "XY"); // removed=1, added=2 -> grows by 1; cap (64) suffices
assert_eq!(&*s, "XYbc");
}
#[test]
fn arena_string_try_reserve_additional_overflow_returns_err() {
let arena: Arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("a");
// self.len (1) + usize::MAX overflows -> Err.
let r = s.try_reserve(usize::MAX);
assert!(r.is_err());
}
#[test]
fn arena_string_try_reserve_within_existing_capacity_is_noop() {
// Drives the `needed <= self.cap` branch of `try_reserve`
// (cap already suffices, so try_grow_to_at_least is not called).
let arena: Arena = Arena::new();
let mut s = arena.alloc_string_with_capacity(64);
s.push_str("hi");
s.try_reserve(8).unwrap();
assert!(s.capacity() >= 64);
let mut exact = arena.alloc_string_with_capacity(8);
exact.push_str("abc");
exact.try_reserve(5).unwrap();
assert_eq!(exact.capacity(), 8);
}
#[test]
fn arena_string_try_reserve_grow_path_succeeds() {
// Drives the success-fall-through past `try_grow_to_at_least(needed)?`
// in `try_reserve` (cap>0, needed>cap, grow succeeds).
let arena: Arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("seed");
let prior = s.capacity();
s.try_reserve(prior * 4).unwrap();
assert!(s.capacity() >= prior * 4 + s.len());
}
#[test]
fn arena_string_try_reserve_grow_path_overflow_returns_err() {
// Drives `try_grow_to_at_least`'s `PREFIX_SIZE.checked_add(new_cap)` /
// `isize::try_from(new_total)` failure paths. We need cap > 0 first
// (so we hit the grow path, not initial allocate), then ask for an
// additional that pushes total past isize::MAX.
let arena: Arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("seed"); // cap > 0
// additional fits in usize but new_total overflows isize.
let additional = (isize::MAX as usize) - 4;
let r = s.try_reserve(additional);
assert!(r.is_err());
}
// Failure-driven coverage tests — drive `?` Err propagation and panicking
// `unwrap_or_else(|_| panic_alloc())` lambda bodies via FailingAllocator.
use std::panic::AssertUnwindSafe;
fn expect_panic<F: FnOnce()>(f: F) {
let r = std::panic::catch_unwind(AssertUnwindSafe(f));
assert!(r.is_err(), "expected panic but call returned");
}
fn fail_arena() -> Arena<FailingAllocator> {
Arena::new_in(FailingAllocator::new(0))
}
fn send_fail_arena() -> Arena<SendFailingAllocator> {
Arena::new_in(SendFailingAllocator::new(0))
}
// Panicking method bodies (every `unwrap_or_else(|_| panic_alloc())` lambda).
#[test]
fn panic_alloc_with() {
expect_panic(|| {
let a = fail_arena();
let _ = a.alloc_with(|| 42);
});
}
#[test]
fn panic_alloc_str() {
expect_panic(|| {
let a = fail_arena();
let _ = a.alloc_str("hi");
});
}
#[test]
fn panic_alloc_slice_fill_iter() {
expect_panic(|| {
let a = fail_arena();
let _ = a.alloc_slice_fill_iter([1_u32, 2, 3]);
});
}
#[test]
fn panic_alloc_uninit_box() {
expect_panic(|| {
let a = fail_arena();
let _ = a.alloc_uninit_box::<u32>();
});
}
#[test]
fn panic_alloc_zeroed_box() {
expect_panic(|| {
let a = fail_arena();
let _ = a.alloc_zeroed_box::<u32>();
});
}
#[test]
fn panic_alloc_uninit_arc() {
expect_panic(|| {
let a = send_fail_arena();
let _ = a.alloc_uninit_arc::<u32>();
});
}
#[test]
fn panic_alloc_zeroed_arc() {
expect_panic(|| {
let a = send_fail_arena();
let _ = a.alloc_zeroed_arc::<u32>();
});
}
#[test]
fn panic_alloc_uninit_slice_arc() {
expect_panic(|| {
let a = send_fail_arena();
let _ = a.alloc_uninit_slice_arc::<u32>(4);
});
}
#[test]
fn panic_alloc_zeroed_slice_arc() {
expect_panic(|| {
let a = send_fail_arena();
let _ = a.alloc_zeroed_slice_arc::<u32>(4);
});
}
// `try_*` Err-propagation branches (the `?` lines).
#[test]
fn try_alloc_str_err() {
let a = fail_arena();
assert!(a.try_alloc_str("hi").is_err());
}
#[test]
fn try_alloc_uninit_box_err() {
let a = fail_arena();
assert!(a.try_alloc_uninit_box::<u32>().is_err());
}
#[test]
fn try_alloc_zeroed_box_err() {
let a = fail_arena();
assert!(a.try_alloc_zeroed_box::<u32>().is_err());
}
#[test]
fn try_alloc_uninit_arc_err() {
let a = send_fail_arena();
assert!(a.try_alloc_uninit_arc::<u32>().is_err());
}
#[test]
fn try_alloc_zeroed_arc_err() {
let a = send_fail_arena();
assert!(a.try_alloc_zeroed_arc::<u32>().is_err());
}
#[test]
fn try_alloc_uninit_slice_arc_err() {
let a = send_fail_arena();
assert!(a.try_alloc_uninit_slice_arc::<u32>(4).is_err());
}
#[test]
fn try_alloc_zeroed_slice_arc_err() {
let a = send_fail_arena();
assert!(a.try_alloc_zeroed_slice_arc::<u32>(4).is_err());
}
// Uninit slice with T: Drop drives the register_drop=true `?` propagation
// in reserve_slice (line 1625) under failure.
// ArenaString grow-path failures.
#[test]
fn arena_string_try_push_str_initial_alloc_err() {
let a = fail_arena();
let mut s = a.alloc_string();
assert!(s.try_push_str("hello").is_err());
}
#[test]
fn arena_string_try_grow_to_at_least_grow_path_err() {
// Allow the initial chunk alloc, fail the grow's new-chunk alloc by
// requesting a capacity that exceeds the chunk_size.
let a = Arena::builder().allocator_in(FailingAllocator::new(1)).build();
let mut s = a.try_alloc_string_with_capacity(4).unwrap();
s.try_push_str("abcd").unwrap();
// Forces grow_for_string → needs new (oversized) chunk → allocator fails.
assert!(s.try_reserve(64 * 1024).is_err());
}
#[test]
fn panic_arena_string_grow_to_at_least() {
expect_panic(|| {
let a = Arena::builder().allocator_in(FailingAllocator::new(1)).build();
let mut s = a.try_alloc_string_with_capacity(4).unwrap();
s.try_push_str("abcd").unwrap();
// grow_to_at_least asks for a new chunk; allocator is exhausted.
s.push_str("x".repeat(64 * 1024));
});
}
// grow_for_string slow path: relocate succeeds, old chunk's refcount goes
// to 0 (drives lines 1815/1820/1822-1823 in arena.rs).
#[test]
fn grow_for_string_old_chunk_torn_down() {
let a = Arena::builder().build();
let mut s = a.alloc_string();
// Force at least one grow_for_string call. Initial cap == 16.
s.push_str("x".repeat(64));
// Multiple grows to ensure we exercise the slow-path relocate.
s.push_str("y".repeat(8 * 1024));
drop(s);
}
// Oversized + needs_drop=false branch in ChunkHeader::oversized_layout
// (lines 188, 189). Default max_normal_alloc = chunk_size/4. We allocate
// a chunk-sized payload to force the oversized path with a Copy type.
#[test]
fn oversized_no_drop_branch() {
let a = Arena::builder().max_normal_alloc(4 * 1024).build();
// 1500 bytes of u8 (Copy, no Drop) > max_normal_alloc(4 * 1024).
let _s = a.alloc_slice_copy(&[0_u8; 1500][..]);
}
#[test]
fn panic_alloc_slice_fill_with() {
expect_panic(|| {
let a = fail_arena();
let _ = a.alloc_slice_fill_with(4, |i| i as u32);
});
}
#[test]
fn vec_try_reserve_no_growth_needed() {
// Line 182: try_reserve when capacity already sufficient → Ok(()) without growing.
let arena = Arena::new();
let mut v: Vec<u32> = arena.alloc_vec();
v.push(1);
v.push(2);
// capacity should be >= 4 after the initial growth; reserve 1 more (already have room).
assert!(v.try_reserve(1).is_ok());
assert_eq!(v.len(), 2);
}
#[test]
fn vec_try_reserve_exact_realloc_and_overflow() {
// Lines 432-436: try_reserve_exact that needs realloc.
let arena = Arena::new();
let mut v: Vec<u32> = arena.alloc_vec();
v.push(1);
// Force exact reserve beyond current capacity.
assert!(v.try_reserve_exact(100).is_ok());
assert!(v.capacity() >= 101);
// Line 436: try_reserve_exact when capacity is already sufficient (no growth).
assert!(v.try_reserve_exact(1).is_ok());
// Overflow: len + additional > usize::MAX.
let err = v.try_reserve_exact(usize::MAX);
assert!(err.is_err());
}
#[test]
fn vec_resize_with_shrink() {
// Lines 473-475: resize_with to a smaller size calls truncate.
let arena = Arena::new();
let mut v: Vec<u32> = arena.alloc_vec();
for i in 0..10 {
v.push(i);
}
v.resize_with(3, || unreachable!());
assert_eq!(v.len(), 3);
assert_eq!(&*v, &[0, 1, 2]);
}
#[test]
fn vec_drain_with_exclusive_start_and_inclusive_end() {
use core::ops::Bound;
// Lines 512-513: Excluded start bound and Unbounded start.
// Lines 516-518: Included end bound and Unbounded end.
let arena = Arena::new();
let mut v: Vec<u32> = arena.alloc_vec();
for i in 0..10 {
v.push(i);
}
// drain((Excluded(0), Included(3))) → start=1, end=4
let drained: std::vec::Vec<_> = v.drain((Bound::Excluded(0), Bound::Included(3))).collect();
assert_eq!(drained, vec![1, 2, 3]);
assert_eq!(v.len(), 7);
// drain(..) → Unbounded start, Unbounded end → start=0, end=len
let arena2 = Arena::new();
let mut v2: Vec<u32> = arena2.alloc_vec();
for i in 0..5 {
v2.push(i);
}
let drained2: std::vec::Vec<_> = v2.drain(..).collect();
assert_eq!(drained2, vec![0, 1, 2, 3, 4]);
assert_eq!(v2.len(), 0);
}
#[test]
fn vec_zst_operations() {
// Lines 360, 586, 594-596: ZST Vec realloc and shrink_to_fit.
let arena = Arena::new();
let mut v: Vec<()> = arena.alloc_vec();
for _ in 0..100 {
v.push(());
}
assert_eq!(v.len(), 100);
v.shrink_to_fit();
// ZST shrink_to_fit is a no-op (line 360: size_of::<T>() == 0 → return).
assert_eq!(v.len(), 100);
}
#[test]
fn vec_drain_debug_and_next_back() {
// Lines 833-835: Drain Debug format.
// Lines 865-875: Drain::next_back (DoubleEndedIterator).
let arena = Arena::new();
let mut v: Vec<u32> = arena.alloc_vec();
for i in 0..5 {
v.push(i);
}
let mut drain = v.drain(1..4);
let s = std::format!("{drain:?}");
assert!(s.contains("Drain"), "Debug output: {s}");
assert!(s.contains("remaining"), "Debug output: {s}");
// next_back
assert_eq!(drain.next_back(), Some(3));
assert_eq!(drain.next_back(), Some(2));
assert_eq!(drain.next(), Some(1));
assert_eq!(drain.next_back(), None);
}
#[test]
fn vec_insert_triggers_growth() {
// Line 284: insert when len == cap forces grow_one.
let arena = Arena::new();
let mut v: Vec<u32> = arena.alloc_vec();
// Fill to capacity (initial growth is 4).
for i in 0..4 {
v.push(i);
}
assert_eq!(v.capacity(), 4);
// Insert forces growth.
v.insert(2, 99);
assert_eq!(v[2], 99);
assert!(v.capacity() > 4);
}
#[test]
fn vec_push_panics_on_alloc_failure() {
// Line 126: grow_one → panic_alloc.
expect_panic(|| {
let arena = Arena::new_in(FailingAllocator::new(1)); // 1 alloc for initial chunk
let mut v: Vec<u64, _> = arena.alloc_vec();
// First pushes may succeed using the chunk, but growth will fail.
// FailingAllocator(1) gives exactly one chunk; the second
// realloc-on-grow trips the panic well before 100 pushes —
// the loop bound is just a defensive cap.
for _ in 0..100 {
v.push(0);
}
});
}
#[test]
fn vec_reserve_panics_on_alloc_failure() {
// Line 168: reserve → panic_alloc.
expect_panic(|| {
let arena = Arena::new_in(FailingAllocator::new(0));
let mut v: Vec<u64, _> = arena.alloc_vec();
v.reserve(1);
});
}
#[test]
fn vec_reserve_exact_panics_on_alloc_failure() {
// Line 422: reserve_exact → panic_alloc.
expect_panic(|| {
let arena = Arena::new_in(FailingAllocator::new(0));
let mut v: Vec<u64, _> = arena.alloc_vec();
v.reserve_exact(1);
});
}
#[test]
fn shared_bump_fast_path_bail_on_oversize() {
// Line 385: the current-chunk bump path returns None for oversize request.
// Lines 569-570: try_get_chunk_for_shared creates oversized chunk.
let arena = Arena::builder().max_normal_alloc(4096).build();
// This is larger than max_normal_alloc(4096), so fast path bails → oversized chunk.
let arc = arena.alloc_arc([0_u64; 1024]); // 8192 bytes > 4096
assert_eq!(arc[0], 0);
}
#[test]
fn shared_bump_fit_in_current_chunk() {
// Lines 593-594: try_get_chunk_for_shared fits in current chunk.
// This exercises the shared slow-path fit check that returns the current chunk.
let arena = Arena::new();
// Allocate many small Arcs to fill the current chunk, then one that
// might go through the slow path on a second chunk. The first Arc establishes
// the chunk.
let _a1 = arena.alloc_arc(1_u32);
let _a2 = arena.alloc_arc(2_u32);
// Both fit in the same chunk → shared bump fit path is exercised.
}
#[test]
fn shared_oversized_inc_ref_on_non_normal_chunk() {
// Lines 799-802: inc_ref_shared_deferred for non-Normal (oversized) chunk.
// The oversized shared alloc path: alloc_slice_copy_arc with slice > max_normal_alloc.
let arena = Arena::builder().max_normal_alloc(4096).build();
let data = [42_u8; 8192]; // > max_normal_alloc(4096)
let arc_slice = arena.alloc_slice_copy_arc(&data[..]);
assert_eq!(arc_slice.len(), 8192);
assert_eq!(arc_slice[0], 42);
}
#[test]
fn shared_eviction_of_pinned_chunk() {
// Line 603: push_pinned when evicting a pinned chunk.
// Use the smallest possible chunk so a modest number of pushes
// reliably overflows the current chunk and triggers eviction of
// the pinned (string-builder-owned) chunk on refill.
let arena = Arena::builder().with_capacity(512).build();
// String builders use chunks with pin_for_bump=true.
let mut s = arena.alloc_string();
// 600 ASCII chars > 512-byte chunk capacity guarantees the refill
// path runs while the chunk is pinned, exercising the
// pinned-eviction branch regardless of host/Miri.
let n = 600;
for _ in 0..n {
s.push('A'); // This grows the string builder, pinning the chunk.
}
// If the chunk was pinned, it goes to the pinned list (line 603 equivalent in local path).
assert!(s.len() >= n);
}
#[test]
#[cfg(all(not(target_os = "windows"), not(utc_backend)))]
// See note on `acquire_slice_slot_rejects_overaligned`: naming a
// `T` with `align(131072)` aborts on Windows before the guard runs.
fn try_alloc_slice_copy_rejects_overaligned() {
// Line 1441: layout.align() >= CHUNK_ALIGN → Err(AllocError).
#[repr(align(131072))]
#[derive(Clone, Copy)]
#[expect(dead_code, reason = "field needed for alignment/size but not read")]
struct HugeAlign(u8);
let arena = Arena::new();
let data = [HugeAlign(0)];
let result = arena.try_alloc_slice_copy(&data[..]);
assert!(result.is_err());
}
#[test]
fn try_alloc_slice_copy_rejects_overflow() {
// Line 1436: total > isize::MAX → Err(AllocError).
let arena = Arena::new();
// Fabricate a slice reference with huge length via unsafe.
// Can't actually create such a large allocation, but we can test the
// overflow branch by checking try_alloc_slice_fill_with with a huge len.
let result = arena.try_alloc_slice_fill_with::<u64, _>(usize::MAX / 4, |_| 0);
assert!(result.is_err());
}
#[test]
#[cfg(all(not(target_os = "windows"), not(utc_backend)))]
// See note on `acquire_slice_slot_rejects_overaligned`: naming a
// `T` with `align(131072)` aborts on Windows before the guard runs.
fn try_alloc_slice_fill_with_rejects_overaligned() {
// Line 1505: layout.align() >= CHUNK_ALIGN → Err(AllocError).
#[repr(align(131072))]
struct HugeAlignDrop(#[expect(dead_code, reason = "field needed for alignment/size but not read")] u8);
#[expect(clippy::empty_drop, reason = "Drop impl makes needs_drop::<T>() true for test")]
impl Drop for HugeAlignDrop {
fn drop(&mut self) {}
}
let arena = Arena::new();
let result = arena.try_alloc_slice_fill_with::<HugeAlignDrop, _>(1, |_| HugeAlignDrop(0));
assert!(result.is_err());
}
#[test]
fn try_alloc_slice_fill_with_no_drop_fast_path() {
// Line 1509: no-drop type takes the fast-path branch in try_alloc_slice_fill_with.
let arena = Arena::new();
let result = arena.try_alloc_slice_fill_with::<u32, _>(10, |i| i as u32);
assert!(result.is_ok());
let slice = result.unwrap();
assert_eq!(slice.len(), 10);
assert_eq!(slice[5], 5);
}
#[test]
fn try_alloc_slice_fill_with_overflow() {
// Line 1500: total > isize::MAX for non-drop type.
let arena = Arena::new();
let result = arena.try_alloc_slice_fill_with::<u64, _>(usize::MAX / 4, |_| 0);
assert!(result.is_err());
}
#[test]
fn alloc_slice_fill_with_overflow() {
// Line 1500: try_alloc_slice_fill_with overflow (total > isize::MAX - (align-1)).
let arena = Arena::new();
let len = (isize::MAX as usize) / 8 + 1;
let result = arena.try_alloc_slice_fill_with::<u64, _>(len, |_| 0);
assert!(result.is_err());
}
#[test]
fn alloc_slice_fill_with_non_drop_fast_path() {
// Line 1509: fast-path ptr.cast::<T>() for non-Drop types in try_alloc_slice_fill_with.
let arena = Arena::new();
// First call: allocates a chunk via slow path.
let _ = arena.alloc_slice_fill_with::<u32, _>(4, |i| i as u32);
// Second call: fast path in current chunk (line 1509).
let slice = arena.alloc_slice_fill_with::<u32, _>(4, |i| (i + 10) as u32);
assert_eq!(&*slice, &[10, 11, 12, 13]);
}
#[test]
fn slice_init_guard_drops_prefix_on_panic() {
// Lines 3767-3772: SliceInitGuard drops initialized elements on panic.
// SliceInitGuard is used in try_alloc_slice_fill_with (non-Rc) for both
// Drop and non-Drop types. Exercise via alloc_slice_fill_with (the non-Rc version)
// with a type that has Drop.
static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
#[derive(Clone)]
#[expect(dead_code, reason = "field needed for alignment/size but not read")]
struct Tracked(u32);
impl Drop for Tracked {
fn drop(&mut self) {
DROP_COUNT.fetch_add(1, Ordering::Relaxed);
}
}
DROP_COUNT.store(0, Ordering::Relaxed);
let arena = Arena::new();
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
let _ = arena.alloc_slice_fill_with::<Tracked, _>(5, |i| {
assert!(i != 3, "deliberate panic at index 3");
Tracked(i as u32)
});
}));
assert!(result.is_err());
// Elements 0, 1, 2 were initialized before the panic at index 3.
// SliceInitGuard should have dropped them.
assert!(DROP_COUNT.load(Ordering::Relaxed) >= 3);
}
/// Exercises the fast path in `try_alloc_slice_copy` where
/// `the current-chunk bump path` succeeds on an already-populated chunk.
/// A first small allocation populates `current`, then a second
/// `alloc_slice_copy` fits in the same chunk without needing the slow path.
#[test]
fn alloc_slice_copy_fast_path_bump() {
let arena = Arena::new();
// First allocation populates current with a fresh chunk.
let _x = arena.alloc(42_u8);
// Second allocation is small enough to bump within the same chunk,
// hitting the `the current-chunk bump path` success path.
let s = arena.alloc_slice_copy([1_u8, 2, 3, 4, 5, 6, 7, 8]);
assert_eq!(&*s, &[1, 2, 3, 4, 5, 6, 7, 8]);
}
//
// All smart-pointer alloc paths reject `align >= 32 KiB` because, with
// the co-allocated `DropEntry` taking 32 bytes immediately before the
// payload, an `align == 32 KiB` payload lands at chunk offset
// `CHUNK_ALIGN`. `header_for(value_ptr)` masks the low 16 bits of the
// pointer to recover the chunk header — for that offset, the mask
// returns the *next* chunk's address. The guard exists to make this
// failure mode unreachable from safe code.
//
// These tests pin the boundary: a sized `T` with `repr(align(32768))`
// must be rejected by every smart-pointer entry point. The companion
// tests in `dst.rs` cover the unsafe DST paths.
//
// Skipped on Windows: naming a type with `align(32768)` on stack inside
// `try_alloc_*_with` materializes a stack frame Windows' default 1 MiB
// stack cannot satisfy on entry, aborting with STATUS_STACK_OVERFLOW
// before the guard runs. The MaybeUninit/uninit-family tests only hold
// the type *inside* `MaybeUninit`, so they're safe everywhere.
#[cfg(all(not(target_os = "windows"), not(utc_backend)))]
#[repr(align(32768))]
#[derive(Clone, Copy)]
struct HalfChunkAlignNoDrop(#[expect(dead_code, reason = "field gives the type a non-zero size")] u8);
#[cfg(not(utc_backend))]
#[repr(align(32768))]
struct HalfChunkAlignDrop(#[expect(dead_code, reason = "field gives the type a non-zero size")] u8);
#[cfg(not(utc_backend))]
#[expect(clippy::empty_drop, reason = "Drop impl makes needs_drop::<T>() true for the test")]
impl Drop for HalfChunkAlignDrop {
fn drop(&mut self) {}
}
#[test]
#[cfg(all(not(target_os = "windows"), not(utc_backend)))]
fn try_alloc_arc_with_rejects_half_chunk_alignment() {
let arena: Arena = Arena::new();
let r: Result<multitude::Arc<HalfChunkAlignDrop>, _> = arena.try_alloc_arc_with(|| HalfChunkAlignDrop(0));
assert!(r.is_err());
}
#[test]
#[cfg(all(not(target_os = "windows"), not(utc_backend)))]
fn try_alloc_box_with_rejects_half_chunk_alignment() {
let arena: Arena = Arena::new();
let r: Result<multitude::Box<HalfChunkAlignDrop>, _> = arena.try_alloc_box_with(|| HalfChunkAlignDrop(0));
assert!(r.is_err());
}
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_uninit_box_rejects_half_chunk_alignment() {
// Holding T inside MaybeUninit means no stack frame needs T's
// alignment, so this test is portable to Windows.
let arena: Arena = Arena::new();
let r = arena.try_alloc_uninit_box::<HalfChunkAlignDrop>();
assert!(r.is_err());
}
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_uninit_arc_rejects_half_chunk_alignment() {
let arena: Arena = Arena::new();
let r = arena.try_alloc_uninit_arc::<HalfChunkAlignDrop>();
assert!(r.is_err());
}
#[test]
#[cfg(all(not(target_os = "windows"), not(utc_backend)))]
fn try_alloc_slice_fill_with_arc_rejects_half_chunk_alignment() {
let arena: Arena = Arena::new();
let r = arena.try_alloc_slice_fill_with_arc::<HalfChunkAlignDrop, _>(1, |_| HalfChunkAlignDrop(0));
assert!(r.is_err());
}
#[test]
#[cfg(all(not(target_os = "windows"), not(utc_backend)))]
fn try_alloc_slice_fill_with_box_rejects_half_chunk_alignment() {
let arena: Arena = Arena::new();
let r = arena.try_alloc_slice_fill_with_box::<HalfChunkAlignDrop, _>(1, |_| HalfChunkAlignDrop(0));
assert!(r.is_err());
}
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_uninit_slice_arc_rejects_half_chunk_alignment() {
let arena: Arena = Arena::new();
let r = arena.try_alloc_uninit_slice_arc::<HalfChunkAlignDrop>(1);
assert!(r.is_err());
}
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_uninit_slice_box_rejects_half_chunk_alignment() {
let arena: Arena = Arena::new();
let r = arena.try_alloc_uninit_slice_box::<HalfChunkAlignDrop>(1);
assert!(r.is_err());
}
#[test]
#[cfg(all(not(target_os = "windows"), not(utc_backend)))]
fn try_alloc_slice_copy_arc_allows_half_chunk_align_for_copy_t() {
let arena: Arena = Arena::new();
let data = [HalfChunkAlignNoDrop(0), HalfChunkAlignNoDrop(1)];
let r = arena.try_alloc_slice_copy_arc(&data[..]);
assert!(r.is_err());
}
//
// Each `alloc_*_with` reserves a slot, takes a protective `+1` chunk
// refcount, then runs the user-supplied `f`. If `f` panics, the
// `RefcountReleaseGuard` releases that `+1` so the chunk reclaims
// normally; no `DropEntry` is linked (so `T::drop` does not run on the
// half-built value), and the bump bytes leak in-chunk. The arena must
// remain usable after the panic.
#[test]
fn alloc_arc_with_closure_panic_releases_refcount() {
use std::panic::AssertUnwindSafe;
let arena: Arena = Arena::new();
let _stable = arena.alloc_arc(0_u32);
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
let _: multitude::Arc<u64> = arena.alloc_arc_with(|| panic!("deliberate panic in alloc_arc_with"));
}));
assert!(result.is_err());
let after = arena.alloc_arc(99_u32);
assert_eq!(*after, 99);
}
#[test]
fn alloc_box_with_closure_panic_releases_refcount() {
use std::panic::AssertUnwindSafe;
let arena: Arena = Arena::new();
let _stable = arena.alloc_box(0_u32);
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
let _: multitude::Box<u64> = arena.alloc_box_with(|| panic!("deliberate panic in alloc_box_with"));
}));
assert!(result.is_err());
let after = arena.alloc_box(99_u32);
assert_eq!(*after, 99);
}
#[test]
fn vec_resize_clones_exactly_extra_minus_one() {
use core::sync::atomic::{AtomicUsize, Ordering};
static CLONE_COUNT: AtomicUsize = AtomicUsize::new(0);
CLONE_COUNT.store(0, Ordering::Relaxed);
#[derive(Default)]
struct CloneCounter;
impl Clone for CloneCounter {
fn clone(&self) -> Self {
CLONE_COUNT.fetch_add(1, Ordering::Relaxed);
Self
}
}
let arena: Arena = Arena::new();
let mut v: multitude::vec::Vec<CloneCounter> = arena.alloc_vec();
v.push(CloneCounter);
v.push(CloneCounter);
assert_eq!(CLONE_COUNT.load(Ordering::Relaxed), 0);
v.resize(5, CloneCounter);
assert_eq!(v.len(), 5);
assert_eq!(CLONE_COUNT.load(Ordering::Relaxed), 2);
}
}
mod public_surface_behavior_2 {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain smart pointers to keep chunks alive")]
#![allow(unused_results, reason = "test code")]
#![allow(clippy::used_underscore_binding, reason = "intentional drop-after binding")]
#![allow(clippy::cast_possible_truncation, reason = "test data is small")]
#![allow(clippy::explicit_into_iter_loop, reason = "test clarity")]
#![allow(clippy::assertions_on_result_states, reason = "tests deliberately assert error returns")]
#![allow(clippy::items_after_statements, reason = "test-local statics next to their use")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "tests group related unsafe ops")]
use core::alloc::Layout;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering};
use allocator_api2::alloc::Allocator;
use multitude::strings::String as ArenaString;
use multitude::vec::Vec as ArenaVec;
use multitude::{Arc, Arena, FromIn as _};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
use crate::common::{FailingAllocator, SendFailingAllocator};
#[derive(Clone, Debug, Eq, PartialEq)]
struct Droppy(&'static str);
impl Drop for Droppy {
fn drop(&mut self) {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
}
#[derive(Clone)]
struct DropZst;
impl Drop for DropZst {
fn drop(&mut self) {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
}
// ---- src/arc.rs / src/rc.rs gaps ----
#[test]
fn arc_from_arena_vec_uses_into_arc() {
let arena = Arena::new();
let mut v: ArenaVec<'_, i32> = arena.alloc_vec();
v.push(1);
v.push(2);
let a: Arc<[i32]> = v.into();
assert_eq!(&*a, &[1, 2]);
}
// ---- src/internal/chunk_provider.rs gaps ----
#[test]
fn builder_preallocate_shared_releases_budget_on_allocator_error() {
assert!(
Arena::builder_in(SendFailingAllocator::new(0))
.with_capacity(512)
.try_build()
.is_err()
);
}
#[test]
fn oversized_shared_alloc_error_releases_budget() {
let arena = Arena::builder_in(SendFailingAllocator::new(0)).max_normal_alloc(4096).build();
let src = std::vec![7_u8; 5000];
assert!(arena.try_alloc_slice_copy_arc(src).is_err());
}
#[test]
fn cache_discards_too_small_chunk_before_large_request() {
let arena = Arena::builder().with_capacity(512).build();
let big = std::vec![3_u8; 4096];
let a = arena.alloc_slice_copy_arc(&big);
assert_eq!(a.len(), big.len());
}
#[test]
fn preallocate_local_updates_high_water_on_larger_class() {
let arena = Arena::builder().with_capacity(1024).build();
let value = arena.alloc(42_u32);
assert_eq!(*value, 42);
}
// ---- src/internal/local_chunk.rs gaps ----
// ---- src/strings/string.rs gaps ----
#[test]
fn string_retain_panic_restores_guard_len() {
let arena = Arena::new();
let mut s = ArenaString::from_in("abcd", &arena);
let result = panic::catch_unwind(AssertUnwindSafe(|| {
s.retain(|ch| {
assert_ne!(ch, 'd', "retain must stop at the panic");
assert!(ch != 'c', "predicate panic");
ch != 'b'
});
}));
assert!(result.is_err());
assert_eq!(s.as_str(), "a");
}
/// Regression: `Vec::retain`/`Vec::retain_mut`/`Vec::dedup*` used to
/// silently wipe ALL elements when the predicate panicked (because
/// `with_apivec` zeroed the raw parts before delegating, and the
/// panicking `ApiVec::Drop` then freed the whole buffer). They now
/// match `std::Vec::retain`'s contract: the kept prefix is preserved.
#[test]
fn vec_retain_panic_preserves_kept_prefix() {
use std::cell::Cell;
let arena = Arena::new();
let mut v: multitude::vec::Vec<'_, i32> = arena.alloc_vec();
v.extend([1_i32, 2, 3, 4, 5]);
// Predicate: keep odd numbers; panic on element `3`. After panic,
// the kept prefix `[1]` must remain (matches std::Vec::retain).
let seen = Cell::new(0_i32);
let result = panic::catch_unwind(AssertUnwindSafe(|| {
v.retain(|x| {
seen.set(seen.get() + 1);
assert!(*x != 3, "predicate panic at element 3");
*x % 2 == 1
});
}));
assert!(result.is_err());
// Element 1 passed the predicate (kept), element 2 was dropped,
// element 3 panicked → ApiVec leaves [1] + leak-amplification of
// unprocessed tail (3, 4, 5) is acceptable per std semantics.
// Whatever ApiVec leaves, it must NOT be empty when the predicate
// managed to keep at least one element.
assert!(
!v.is_empty(),
"kept prefix [1, ...] must survive the panic; std::Vec::retain has the same contract"
);
assert_eq!(v[0], 1, "element 1 must be retained");
}
#[test]
fn vec_dedup_panic_preserves_kept_prefix() {
let arena = Arena::new();
let mut v: multitude::vec::Vec<'_, i32> = arena.alloc_vec();
v.extend([1_i32, 1, 2, 2, 3, 3]);
let result = panic::catch_unwind(AssertUnwindSafe(|| {
v.dedup_by(|a, _b| {
assert!(*a != 3, "dedup panic");
false
});
}));
assert!(result.is_err());
// At least one element must survive the panic; the all-elements-wiped
// bug would leave the vector completely empty.
assert!(!v.is_empty(), "Vec must not be fully wiped on dedup-predicate panic");
}
#[test]
#[should_panic(expected = "allocator returned AllocError")]
fn string_push_panics_on_allocator_error() {
let arena = Arena::builder_in(FailingAllocator::new(0)).build();
let mut s = arena.alloc_string();
s.push('x');
}
#[test]
#[should_panic(expected = "allocator returned AllocError")]
fn string_reserve_panics_on_allocator_error() {
let arena = Arena::builder_in(FailingAllocator::new(0)).build();
let mut s = arena.alloc_string();
s.reserve(128);
}
#[test]
#[should_panic(expected = "allocator returned AllocError")]
fn string_replace_range_panics_from_grow_to_at_least() {
let arena = Arena::builder_in(FailingAllocator::new(1)).build();
let mut s = ArenaString::from_in("a", &arena);
// `FailingAllocator` denies every allocation after the first
// regardless of size; a moderate replacement (well past the
// initial small chunk's residual capacity) is sufficient.
let replacement = "x".repeat(1024);
s.replace_range(0..1, &replacement);
}
#[test]
fn string_reserve_zero_on_nonempty_string_is_noop() {
let arena = Arena::new();
let mut s = ArenaString::from_in("already allocated", &arena);
let cap = s.capacity();
s.reserve(0);
assert_eq!(s.capacity(), cap);
assert_eq!(s.as_str(), "already allocated");
}
// ---- src/strings/utf16_string.rs gaps ----
// ---- src/vec/vec.rs gaps ----
#[test]
#[should_panic(expected = "allocator returned AllocError")]
fn vec_with_capacity_panics_on_allocator_error() {
let arena = Arena::builder_in(FailingAllocator::new(0)).build();
let _v: ArenaVec<'_, u8, _> = arena.alloc_vec_with_capacity(8);
}
#[test]
#[should_panic(expected = "allocator returned AllocError")]
fn vec_into_arc_panics_on_shared_allocator_error() {
let arena = Arena::builder_in(SendFailingAllocator::new(1)).build();
// Fill most of the first chunk, then split: the tail has no freeze
// prefix of its own, so freezing it into an `Arc` (a copy of equal
// size) cannot reuse the buffer in place and must acquire a second
// chunk, which the failing allocator rejects.
let mut v: ArenaVec<'_, u8, _> = arena.alloc_vec_with_capacity(400);
v.extend((0..400).map(|_| 0u8));
let tail = v.split_off(200);
let _arc = multitude::Arc::from(tail);
}
#[test]
fn vec_into_box_handles_zst_fallback() {
let arena = Arena::new();
let mut v = arena.alloc_vec::<()>();
for _ in 0..16 {
v.push(());
}
let b = v.into_boxed_slice();
assert_eq!(b.len(), 16);
}
#[test]
#[should_panic(expected = "allocator returned AllocError")]
fn vec_into_box_panics_on_zst_drop_alloc_error() {
let arena = Arena::builder_in(FailingAllocator::new(0)).build();
let mut v = arena.alloc_vec::<DropZst>();
v.extend([DropZst, DropZst, DropZst]);
let _ = v.into_boxed_slice();
}
#[test]
fn vec_into_box_falls_back_when_drop_entry_install_misses() {
let arena = Arena::new();
let mut v = arena.alloc_vec::<Droppy>();
v.extend([Droppy("a"), Droppy("b")]);
let _decoy = arena.alloc_slice_fill_with(70_000, |i| i as u8);
let b = v.into_boxed_slice();
assert_eq!(b.len(), 2);
}
#[test]
// Skipped under Miri: building + dropping `u16::MAX + 1` elements
// (~65K) exceeds Miri's test budget. The lifted restriction is a
// runtime property, not a memory-safety one, so native + cargo-careful
// runs cover it.
#[cfg_attr(miri, ignore)]
fn vec_into_box_drop_slice_longer_than_u16_succeeds() {
// `Box<[T]>` drops via `drop_in_place::<[T]>` (no `u16`-counted
// drop entry), so a `T: Drop` slice longer than `u16::MAX` freezes
// into a `Box` without rejection.
let arena = Arena::new();
let mut v = arena.alloc_vec::<Droppy>();
let len = (u16::MAX as usize) + 1;
v.extend((0..len).map(|_| Droppy("many")));
let b = v.into_boxed_slice();
assert_eq!(b.len(), len);
}
#[test]
fn vec_resize_moves_final_clone_source_into_last_slot() {
let arena = Arena::new();
let mut v = arena.alloc_vec::<std::string::String>();
v.resize(3, "x".to_owned());
assert_eq!(&*v, &["x", "x", "x"]);
}
#[test]
fn vec_realloc_edge_cases_are_observable_through_public_api() {
let arena = Arena::new();
let mut v = arena.alloc_vec_with_capacity(8);
v.extend([1_u32, 2, 3, 4]);
v.reserve_exact(0);
assert!(v.capacity() >= 8);
v.reserve(32);
assert_eq!(&*v, &[1, 2, 3, 4]);
v.clear();
v.shrink_to_fit();
assert_eq!(v.capacity(), 0);
}
#[test]
fn vec_shrink_to_fit_oversized_chunk_is_a_noop() {
// Buffers allocated in oversized chunks (cap > MAX_NORMAL_ALLOC)
// are never at the `current` bump cursor, so
// `shrink_to_fit` must no-op rather than allocate-copy-deallocate
// (which would just churn fresh chunks for no semantic benefit).
// Verify the no-op path under a one-shot allocator that would
// refuse any subsequent allocation, demonstrating that no
// allocator call is made.
let arena = Arena::builder_in(FailingAllocator::new(1)).max_normal_alloc(4096).build();
let mut v = arena.alloc_vec_with_capacity(70_000);
let cap_before = v.capacity();
v.extend([1_u32, 2, 3, 4]);
v.shrink_to_fit();
assert_eq!(v.capacity(), cap_before);
assert_eq!(v.len(), 4);
}
// ---- src/allocator_impl.rs gaps ----
#[test]
fn arena_allocator_grow_falls_back_when_in_place_growth_is_ineligible() {
let arena = Arena::new();
let alloc = &arena;
let old = Layout::from_size_align(8, 8).unwrap();
let ptr = alloc.allocate(old).unwrap().cast::<u8>();
let different_align = Layout::from_size_align(16, 16).unwrap();
let grown = unsafe { Allocator::grow(&alloc, ptr, old, different_align) }.unwrap();
unsafe { Allocator::deallocate(&alloc, grown.cast(), different_align) };
let old = Layout::from_size_align(16, 8).unwrap();
let ptr = alloc.allocate(old).unwrap().cast::<u8>();
let smaller = Layout::from_size_align(8, 8).unwrap();
let shrunk = unsafe { Allocator::grow(&alloc, ptr, old, smaller) }.unwrap();
unsafe { Allocator::deallocate(&alloc, shrunk.cast(), smaller) };
}
// ---- src/arena.rs gaps ----
#[test]
fn arena_slice_clone_no_drop_branch() {
let arena = Arena::new();
let values = [10_u32, 20, 30];
let cloned = arena.alloc_slice_clone(values);
assert_eq!(&*cloned, &[10, 20, 30]);
}
#[test]
fn shared_refill_preserves_reentrant_drop_allocation() {
static REENTERED: AtomicUsize = AtomicUsize::new(0);
REENTERED.store(0, Ordering::SeqCst);
struct ReentrantDrop {
arena: *const Arena,
}
unsafe impl Send for ReentrantDrop {}
unsafe impl Sync for ReentrantDrop {}
impl Drop for ReentrantDrop {
fn drop(&mut self) {
let arena = unsafe { &*self.arena };
let value = arena.alloc_arc(0xCAFE_u64);
assert_eq!(*value, 0xCAFE);
REENTERED.fetch_add(1, Ordering::SeqCst);
}
}
let arena = Arena::new();
let arena_ptr: *const Arena = &raw const arena;
let reentrant = arena.alloc_arc(ReentrantDrop { arena: arena_ptr });
drop(reentrant);
// Drain the current chunk in one bulk allocation so the next
// outer alloc forces a refill. A single 64 KiB uninit Arc
// takes the entire chunk's worth of bytes; cheaper than the
// prior 16 × 4 KiB fillers (16× fewer atomic ops under Miri).
let filler = arena.alloc_uninit_arc::<[u8; 60 * 1024]>();
drop(filler);
let outer = arena.alloc_arc([0x55_u8; 4096]);
assert_eq!(outer[0], 0x55);
assert_eq!(REENTERED.load(Ordering::SeqCst), 1);
}
}
mod public_surface_behavior_3 {
#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::large_stack_arrays, reason = "test allocations are intentional")]
#![allow(clippy::collection_is_never_read, reason = "tests retain smart pointers to keep chunks alive")]
#![allow(unused_results, reason = "test code")]
#![allow(clippy::used_underscore_binding, reason = "intentional drop-after binding")]
#![allow(clippy::cast_possible_truncation, reason = "test data is small")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "test code")]
#![allow(clippy::multiple_unsafe_ops_per_block, reason = "tests group related unsafe ops")]
#![allow(clippy::items_after_statements, reason = "test-local statics next to their use")]
#![allow(clippy::assertions_on_result_states, reason = "tests deliberately assert Err returns")]
#![allow(clippy::ptr_as_ptr, reason = "test code uses `as` casts for raw pointers")]
use multitude::vec::Vec as ArenaVec;
use multitude::{Arc, Arena};
use crate::common;
#[derive(Clone)]
struct Droppy(&'static str);
impl Drop for Droppy {
fn drop(&mut self) {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}
}
// ---- arc.rs / rc.rs: drop-list retarget loop must traverse past first entry. ----
//
// The drop list is a back-stack: index 0 is the OLDEST entry, index drop_count-1
// the NEWEST. `assume_init` scans from i=0 forward and breaks on the matching
// `value_offset`. To exercise the i>0 iterations we allocate a "decoy" drop
// entry first, then allocate the `MaybeUninit` to be assume_init'd, so the
// match happens at the LAST slot, not the first.
#[test]
fn arc_single_assume_init_loop_traverses_past_first_drop_entry() {
let arena = Arena::new();
let arc_uninit = arena.alloc_uninit_arc::<Droppy>();
let _decoy: Arc<Droppy> = arena.alloc_arc(Droppy("decoy"));
unsafe {
Arc::as_ptr(&arc_uninit)
.cast_mut()
.write(core::mem::MaybeUninit::new(Droppy("target")));
}
let arc = unsafe { arc_uninit.assume_init() };
assert_eq!(arc.0, "target");
}
#[test]
fn arc_slice_assume_init_loop_traverses_past_first_drop_entry() {
let arena = Arena::new();
let arc_uninit = arena.alloc_uninit_slice_arc::<Droppy>(2);
let _decoy: Arc<Droppy> = arena.alloc_arc(Droppy("decoy"));
unsafe {
let base = Arc::as_ptr(&arc_uninit).cast::<core::mem::MaybeUninit<Droppy>>().cast_mut();
(*base.add(0)).write(Droppy("a"));
(*base.add(1)).write(Droppy("b"));
}
let arc = unsafe { arc_uninit.assume_init() };
assert_eq!(arc[1].0, "b");
}
// ---- vec.rs: cleanup_after_partial_move fires when an IntoIter is dropped mid-way. ----
#[test]
fn vec_swap_remove_last_index_skips_copy() {
// Drives the `idx == self.len` branch of `swap_remove` where no
// element copy is performed.
let arena = Arena::new();
let mut v = arena.alloc_vec::<u32>();
v.extend([1_u32, 2, 3]);
let last = v.swap_remove(2);
assert_eq!(last, 3);
assert_eq!(v.as_slice(), &[1, 2]);
}
#[test]
fn vec_into_iter_partial_drop_compacts_tail() {
use core::sync::atomic::{AtomicUsize, Ordering};
static DROPPED: AtomicUsize = AtomicUsize::new(0);
struct Tracked(#[expect(dead_code, reason = "field only exists to make Tracked non-ZST")] u32);
impl Drop for Tracked {
fn drop(&mut self) {
DROPPED.fetch_add(1, Ordering::Relaxed);
}
}
DROPPED.store(0, Ordering::Relaxed);
let arena = Arena::new();
let mut v: ArenaVec<'_, Tracked> = arena.alloc_vec_with_capacity(4);
for i in 0..4_u32 {
v.push(Tracked(i));
}
let mut it = v.into_iter();
// Consume 2 of 4; the IntoIter's Drop must then compact and drop the
// remaining 2, which exercises `cleanup_after_partial_move`.
let _a = it.next().unwrap();
let _b = it.next().unwrap();
drop(_a);
drop(_b);
// At this point DROPPED == 2.
assert_eq!(DROPPED.load(Ordering::Relaxed), 2);
drop(it);
// Dropping the iter compacts the surviving tail (2 elements) and drops them.
assert_eq!(DROPPED.load(Ordering::Relaxed), 4);
}
// ---- vec.rs: realloc same-cap is unreachable (callers gate); see realloc's debug_assert. ----
// ---- arena.rs: MAX_SMART_PTR_ALIGN guard in `try_alloc_slice_shared_no_drop_with`. ----
#[cfg(not(utc_backend))]
#[repr(align(32768))]
#[derive(Clone, Copy)]
struct OverAligned32K;
// SAFETY: zero-sized POD; no drop.
#[cfg(not(utc_backend))]
unsafe impl Send for OverAligned32K {}
// SAFETY: zero-sized POD; no drop.
#[cfg(not(utc_backend))]
unsafe impl Sync for OverAligned32K {}
#[cfg(not(utc_backend))]
#[test]
fn try_alloc_slice_fill_with_arc_rejects_over_aligned() {
let arena = Arena::new();
// `try_alloc_slice_fill_with_arc` for `T: !needs_drop` routes through
// `try_alloc_slice_shared_no_drop_with`, which checks
// `align >= MAX_SMART_PTR_ALIGN` and errors.
let result = arena.try_alloc_slice_fill_with_arc::<OverAligned32K, _>(2, |_| OverAligned32K);
assert!(result.is_err());
}
// ---- arena.rs: DST allocator metadata size check ----
//
// `metadata_to_u16` checks that `T::Metadata` is `usize`-sized. For sized `T`
// the metadata is `()` (zero bytes), so the check returns `Err` immediately.
// ---- chunk_provider.rs CAS contention paths: spin up two threads that push to
// the shared-cache concurrently to force the CAS retry arm. ----
#[test]
fn cache_push_pop_contention_drives_cas_retries() {
use std::sync::Barrier;
use std::thread;
// Force CAS contention on cache push/pop and reserve_budget by
// hammering the same arena from many threads simultaneously.
let arena: Arena = Arena::builder().max_normal_alloc(4096).byte_budget(128 * 1024 * 1024).build();
// Pre-allocate Arcs grouped per thread so all the work happens during
// the dropping phase (cross-thread chunk releases).
//
// The test makes no assertions: it only exists to give coverage to
// the CAS retry branches in `push` / `pop`.
// A few dozen concurrent drops per thread is more than enough to
// exercise those branches; the bottleneck for finding races is
// scheduler-interleaving variety (covered by Miri's many-seeds race
// sweep and by the OS scheduler under native runs), not raw op
// count.
let nthreads = 8;
let per_thread = 32;
let mut sets: Vec<Vec<multitude::Arc<u64>>> = (0..nthreads).map(|_| Vec::with_capacity(per_thread)).collect();
for set in &mut sets {
for _ in 0..per_thread {
set.push(arena.alloc_arc(42));
}
}
let barrier = std::sync::Arc::new(Barrier::new(nthreads));
let mut handles = Vec::new();
for set in sets {
let b = barrier.clone();
handles.push(thread::spawn(move || {
// Synchronize the drop storm so threads race on the
// Treiber-stack push CAS in `push`.
b.wait();
for a in set {
drop(a);
}
}));
}
for h in handles {
let _ = h.join();
}
}
// ============================================================================
// ============================================================================
// vec.rs — shrink_to_fit no-ops when the buffer is not at the bump cursor
// ============================================================================
#[test]
fn vec_shrink_to_fit_is_a_noop_when_not_at_cursor() {
// `shrink_to_fit` no longer allocates: when the buffer is not at
// the current bump cursor (because intervening allocations have
// moved the cursor past the buffer), the arena cannot reclaim
// partial allocations, so the call no-ops instead of churning
// chunk space via allocate-copy-deallocate. This test exercises
// the no-op path under a one-shot allocator that would refuse a
// refill, demonstrating that no allocator call is made.
let alloc = common::FailingAllocator::new(2);
let arena = Arena::new_in(alloc);
let mut v = arena.alloc_vec::<u8>();
v.reserve(100);
let cap_before = v.capacity();
// Consume the rest of the chunk so the vec's buffer is no longer
// at the bump cursor.
let _filler = arena.alloc_slice_fill_with::<u8, _>(400, |_| 0);
// SAFETY: u8 is valid for any bit pattern and `cap >= 50` after `reserve(100)`.
unsafe { v.set_len(50) };
v.shrink_to_fit();
// Capacity unchanged: shrink was a no-op.
assert_eq!(v.capacity(), cap_before);
assert_eq!(v.len(), 50);
}
// ============================================================================
// vec.rs:731-734 — into_arena_rc copy-fallback error path
// ============================================================================
}
mod public_surface_behavior_4 {
#![allow(clippy::std_instead_of_core, reason = "test code uses std")]
#![allow(clippy::missing_panics_doc, reason = "test code")]
#![allow(clippy::unwrap_used, reason = "test code")]
use multitude::{Arc, Arena};
#[expect(unused_imports, reason = "merged test module re-exports common helpers")]
use crate::common;
// ---------------------------------------------------------------------------
// arc.rs:162-164, rc.rs:167-169, box.rs (From<Handle<T,A>> for Pin<Handle<T,A>>).
// ---------------------------------------------------------------------------
#[test]
fn arc_into_pin_via_from_impl() {
let arena = Arena::new();
let arc: Arc<u32> = arena.alloc_arc(42_u32);
let pinned: core::pin::Pin<Arc<u32>> = arc.into();
assert_eq!(*pinned, 42);
}
#[test]
fn box_into_pin_via_from_impl() {
let arena = Arena::new();
let b: multitude::Box<u32> = arena.alloc_box(42_u32);
let pinned: core::pin::Pin<multitude::Box<u32>> = b.into();
assert_eq!(*pinned, 42);
}
// ---------------------------------------------------------------------------
// zero_init_macros.rs:58/85/115/142/172/202/232/265/289/290 — the
// `panic_alloc()` arms in `BytemuckView` / `ZerocopyView` allocation methods.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// strings/string.rs / utf16_string.rs — insert at end + replace_range tail.
// ---------------------------------------------------------------------------
#[test]
fn string_insert_str_at_end_of_string() {
let arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("hi");
s.insert_str(s.len(), "!");
assert_eq!(s.as_str(), "hi!");
}
#[test]
fn string_replace_range_empty_at_end() {
let arena = Arena::new();
let mut s = arena.alloc_string();
s.push_str("abc");
let n = s.len();
s.replace_range(n..n, "xyz");
assert_eq!(s.as_str(), "abcxyz");
}
// ---------------------------------------------------------------------------
// vec/vec.rs:471 — `resize_with` panic-rollback Guard's drop_in_place tail.
// ---------------------------------------------------------------------------
#[test]
fn vec_resize_with_clone_panic_drops_partial() {
use std::cell::Cell;
use std::panic::AssertUnwindSafe;
struct Tracker<'a> {
clones_made: &'a Cell<usize>,
clones_dropped: &'a Cell<usize>,
panic_after: usize,
}
impl Clone for Tracker<'_> {
fn clone(&self) -> Self {
let n = self.clones_made.get() + 1;
self.clones_made.set(n);
assert!(n != self.panic_after, "clone #{n} panics by design");
Tracker {
clones_made: self.clones_made,
clones_dropped: self.clones_dropped,
panic_after: self.panic_after,
}
}
}
impl Drop for Tracker<'_> {
fn drop(&mut self) {
self.clones_dropped.set(self.clones_dropped.get() + 1);
}
}
let clones_made = Cell::new(0);
let clones_dropped = Cell::new(0);
let arena = Arena::new();
{
let mut v: multitude::vec::Vec<'_, Tracker<'_>> = arena.alloc_vec_with_capacity(8);
v.push(Tracker {
clones_made: &clones_made,
clones_dropped: &clones_dropped,
panic_after: 3,
});
let seed = Tracker {
clones_made: &clones_made,
clones_dropped: &clones_dropped,
panic_after: 3,
};
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
v.resize(6, seed);
}));
assert!(result.is_err(), "panicking clone in resize must propagate");
}
drop(arena);
// 2 successful clones happened (#1, #2) before #3 panicked. Resize's
// panic-recovery Guard must have dropped those 2 already-written
// elements before unwinding; the initial v[0] is dropped on `drop(v)`.
// So total drops counted: 2 (rolled-back clones) + 1 (v[0]) + 1 (seed
// — never moved into the Vec because the panic happened before the
// final move).
assert!(
clones_dropped.get() >= 2,
"Guard must drop the 2 successful clones rolled back by the resize panic; got {}",
clones_dropped.get()
);
}
// ---------------------------------------------------------------------------
// arena/alloc_utf16.rs:26, :65 — the oversized-utf16 branches that route
// requests larger than `max_normal_alloc` to the oversized allocator.
// Default `max_normal_alloc` is 16 KiB == 8192 u16 elements; allocate
// well above that to force entry into the oversized fork.
// ---------------------------------------------------------------------------
}