stumpalo 0.2.0

A fast, zero-dependency, memory efficient bump allocator with chunk reuse and scoped stack support
Documentation
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
use stumpalo::{Arena, INITIAL_CHUNK_CAPACITY};

// --- Helpers ---

// A type with a known, unusual alignment for alignment tests.
#[repr(align(64))]
#[allow(dead_code)]
struct Align64(u8);

// A large type whose Layout::new reports a big size, for triggering
// the large-allocation path.
#[repr(C)]
struct BigAlloc<const N: usize>([u8; N]);

// --- Construction & sizing ---

#[test]
fn arena_size_is_32_bytes() {
    assert_eq!(std::mem::size_of::<Arena>(), 32);
}

#[test]
fn new_creates_valid_arena() {
    let arena = Arena::new();
    // A fresh arena should have no fresh chunks
    assert!(!arena.has_fresh_chunks());
    // Lazy allocation: no chunk until first use
    assert_eq!(arena.chunk_capacity(), 0);
    // But allocation still works
    let x = arena.alloc(42u32);
    assert_eq!(*x, 42);
    assert!(arena.chunk_capacity() > 0);
}

#[test]
fn allocates_default_chunk_size() {
    let arena = Arena::new();
    arena.alloc(1u32);
    assert_eq!(
        arena.chunk_capacity(),
        INITIAL_CHUNK_CAPACITY - std::mem::size_of::<u32>()
    );
}

// --- Basic allocation ---

#[test]
fn alloc_bool() {
    let arena = Arena::new();
    let a = arena.alloc(true);
    let b = arena.alloc(false);
    assert_eq!(*a, true);
    assert_eq!(*b, false);
    *b = true;
    assert_eq!(*b, true);
}

#[test]
fn alloc_u8() {
    let arena = Arena::new();
    let x = arena.alloc(42u8);
    assert_eq!(*x, 42);
    *x = 255;
    assert_eq!(*x, 255);
}

#[test]
fn alloc_i32() {
    let arena = Arena::new();
    let a = arena.alloc(-1i32);
    let b = arena.alloc(0i32);
    let c = arena.alloc(i32::MAX);
    let d = arena.alloc(i32::MIN);
    assert_eq!(*a, -1);
    assert_eq!(*b, 0);
    assert_eq!(*c, i32::MAX);
    assert_eq!(*d, i32::MIN);
}

#[test]
fn alloc_f64() {
    let arena = Arena::new();
    let pi = arena.alloc(std::f64::consts::PI);
    let e = arena.alloc(std::f64::consts::E);
    assert!((*pi - std::f64::consts::PI).abs() < f64::EPSILON);
    assert!((*e - std::f64::consts::E).abs() < f64::EPSILON);
}

#[test]
fn alloc_multiple_independent_mutations() {
    let arena = Arena::new();
    let a = arena.alloc(10u32);
    let b = arena.alloc(20u32);
    let c = arena.alloc(30u32);

    // Mutate independently
    *a += 1;
    *b += 2;
    *c += 3;
    assert_eq!(*a, 11);
    assert_eq!(*b, 22);
    assert_eq!(*c, 33);
}

#[test]
fn alloc_tuple() {
    let arena = Arena::new();
    let t = arena.alloc((42u16, -7i8, 3.14f32));
    assert_eq!(t.0, 42);
    assert_eq!(t.1, -7);
    assert!((t.2 - 3.14f32).abs() < f32::EPSILON);
    t.0 = 100;
    t.1 = -100;
    assert_eq!(t.0, 100);
    assert_eq!(t.1, -100);
}

#[test]
fn alloc_array() {
    let arena = Arena::new();
    let arr = arena.alloc([1u64, 2, 3, 4, 5]);
    assert_eq!(*arr, [1, 2, 3, 4, 5]);
    arr[2] = 99;
    assert_eq!(arr[2], 99);
}

// --- Alignment ---

#[test]
fn alloc_respects_alignment() {
    let arena = Arena::new();
    // u16 must be 2-byte aligned
    let x = arena.alloc(0u16);
    assert_eq!((x as *const u16 as usize) % 2, 0);
    // u32 must be 4-byte aligned
    let y = arena.alloc(0u32);
    assert_eq!((y as *const u32 as usize) % 4, 0);
    // u64 must be 8-byte aligned
    let z = arena.alloc(0u64);
    assert_eq!((z as *const u64 as usize) % 8, 0);
}

#[test]
fn alloc_alignment_64() {
    let arena = Arena::new();
    // First allocate something small to misalign
    let _a = arena.alloc(1u8);
    // Then allocate the 64-byte aligned type
    let x = arena.alloc(Align64(42));
    assert_eq!((x as *const Align64 as usize) % 64, 0);
    assert_eq!(x.0, 42);
}

#[test]
fn alloc_alignment_128_via_repr() {
    #[repr(align(128))]
    struct Align128(u8);
    let arena = Arena::new();
    let _a = arena.alloc(1u8);
    let x = arena.alloc(Align128(99));
    assert_eq!((x as *const Align128 as usize) % 128, 0);
    assert_eq!(x.0, 99);
}

#[test]
fn alloc_many_different_sizes_preserves_alignment() {
    let arena = Arena::new();
    // Interleave allocations of different sizes and alignments
    let _u8 = arena.alloc(0u8);
    let u64 = arena.alloc(0u64);
    let _u16 = arena.alloc(0u16);
    let u128_val = arena.alloc(0u128);
    let _u32 = arena.alloc(0u32);
    assert_eq!((u64 as *const u64 as usize) % 8, 0);
    assert_eq!((u128_val as *const u128 as usize) % 16, 0);
}

// --- Filling a chunk (triggers new chunk allocation) ---

#[test]
fn fill_one_chunk_exactly() {
    let arena = Arena::new();
    // Allocate first u128 (triggers initial chunk creation)
    let x = arena.alloc(0u128);
    assert_eq!(*x, 0);
    let mut prev_free = arena.chunk_capacity();
    assert!(prev_free > 0);
    for i in 1..(INITIAL_CHUNK_CAPACITY / 16) as u32 {
        let x = arena.alloc(i as u128);
        assert_eq!(*x, i as u128);
        let curr_free = arena.chunk_capacity();
        assert!(curr_free < prev_free);
        prev_free = curr_free;
    }
    // Should still have some remaining (but less than 16)
    assert!(arena.chunk_capacity() < 16);
}

#[test]
fn alloc_past_one_chunk() {
    let arena = Arena::new();
    // Allocate 600 u64 values.
    // 510 u64 fill one chunk (4080 / 8 = 510); 600 guarantees a second one.
    for i in 0..600u32 {
        let x = arena.alloc(i as u64);
        assert_eq!(*x, i as u64);
    }
}

#[test]
fn alloc_way_past_one_chunk() {
    let arena = Arena::new();
    // Allocate 10,000 u64 values. This should trigger many new chunks.
    for i in 0..10000u64 {
        let x = arena.alloc(i);
        assert_eq!(*x, i);
    }
}

// --- Large allocations (needs_its_own_allocation path) ---

#[test]
fn large_allocation_gets_own_chunk() {
    let arena = Arena::new();

    // Fill up almost the entire chunk so the large alloc won't fit.
    let fill_count = INITIAL_CHUNK_CAPACITY / std::mem::size_of::<u64>() - 1;
    for _ in 0..fill_count {
        let _x = arena.alloc(0u64);
    }
    let free_before_large = arena.chunk_capacity();
    let over_half = INITIAL_CHUNK_CAPACITY / 2 + 1;
    assert!(free_before_large < over_half); // not enough room for the large alloc

    // Allocate something bigger than INITIAL_CHUNK_CAPACITY / 2.
    const BIG: usize = (INITIAL_CHUNK_CAPACITY / 2) + 1;
    let big = arena.alloc(BigAlloc::<BIG>([0; BIG]));

    // Fill with pattern
    for i in 0..BIG {
        big.0[i] = (i % 256) as u8;
    }
    // Check pattern
    for i in 0..BIG {
        assert_eq!(big.0[i], (i % 256) as u8);
    }

    // Current chunk info changed (large-alloc special case was removed).
    assert!(arena.chunk_capacity() > 0);
}

#[test]
fn large_allocation_does_not_affect_current_chunk_space() {
    let arena = Arena::new();

    // Fill chunk so large alloc won't fit in the remaining space.
    let fill_count = INITIAL_CHUNK_CAPACITY / std::mem::size_of::<u64>() - 1;
    for _ in 0..fill_count {
        let _x = arena.alloc(0u64);
    }

    // Allocate large: > INITIAL_CHUNK_CAPACITY / 2
    const BIG: usize = (INITIAL_CHUNK_CAPACITY / 2) + 100;
    let large = arena.alloc(BigAlloc::<BIG>([0xAA; BIG]));
    for byte in large.0.iter() {
        assert_eq!(*byte, 0xAA);
    }

    // Large-alloc special case was removed, so this creates a new chunk.
    // Free space should be non-zero (new chunk has room).
    assert!(arena.chunk_capacity() > 0);
}

#[test]
fn multiple_large_allocations() {
    let arena = Arena::new();

    // Fill chunk so large allocs won't fit.
    let fill_count = INITIAL_CHUNK_CAPACITY / std::mem::size_of::<u64>() - 1;
    for _ in 0..fill_count {
        let _x = arena.alloc(0u64);
    }

    // Allocate multiple large things (no large-alloc special case anymore)
    {
        const SZ0: usize = (INITIAL_CHUNK_CAPACITY / 2) + 1;
        let large = arena.alloc(BigAlloc::<SZ0>([0; SZ0]));
        assert_eq!(large.0[0], 0);
    }
    {
        const SZ1: usize = (INITIAL_CHUNK_CAPACITY / 2) + 101;
        let large = arena.alloc(BigAlloc::<SZ1>([1; SZ1]));
        assert_eq!(large.0[0], 1);
    }
    {
        const SZ2: usize = (INITIAL_CHUNK_CAPACITY / 2) + 201;
        let large = arena.alloc(BigAlloc::<SZ2>([2; SZ2]));
        assert_eq!(large.0[0], 2);
    }

    // All allocations should succeed
    assert!(arena.chunk_capacity() > 0);
}

// --- Clear and reuse ---

#[test]
fn clear_resets_chunk_capacity() {
    let mut arena = Arena::new();
    let _a = arena.alloc(0u64);
    let _b = arena.alloc(0u64);
    let free_before_clear = arena.chunk_capacity();

    arena.clear();

    // After clear, the arena should be reset to full capacity
    assert_eq!(arena.chunk_capacity(), INITIAL_CHUNK_CAPACITY);
    assert!(arena.chunk_capacity() > free_before_clear);
}

#[test]
fn clear_makes_fresh_chunks_available() {
    let mut arena = Arena::new();

    // Allocate past one chunk to create a second chunk
    // 62 u64 fill one chunk; 100 guarantees we create a second one.
    for _ in 0..100 {
        let _x = arena.alloc(0u64);
    }
    assert!(!arena.has_fresh_chunks());

    arena.clear();
    // Now the old chunk should be in fresh_chunks
    assert!(arena.has_fresh_chunks());

    // The current chunk should be fully available
    assert!(arena.chunk_capacity() >= INITIAL_CHUNK_CAPACITY);
}

#[test]
fn clear_then_alloc_reuses_fresh_chunks() {
    let mut arena = Arena::new();

    // Allocate past one chunk (62 u64 fill one; 100 guarantees second)
    for i in 0..100u64 {
        let x = arena.alloc(i);
        assert_eq!(*x, i);
    }

    arena.clear();
    let had_fresh = arena.has_fresh_chunks();
    assert!(had_fresh);

    // Allocate enough to consume the fresh chunk
    // The arena should internally reuse the fresh chunk
    let _a = arena.alloc(42u32);
    let _b = arena.alloc(99u32);
    assert_eq!(*_a, 42);
    assert_eq!(*_b, 99);
}

#[test]
fn repeated_clear_and_alloc() {
    let mut arena = Arena::new();

    for cycle in 0..10 {
        // Allocate many values
        for i in 0..600u64 {
            let x = arena.alloc((i * cycle) as u64);
            assert_eq!(*x, (i * cycle) as u64);
        }
        arena.clear();
        // Every clear resets the arena
        let _first = arena.alloc((cycle + 1) as u64);
    }
}

#[test]
fn clear_multiple_times_builds_chunk_chain() {
    let mut arena = Arena::new();

    // Cycle 1: fill past one chunk, then clear
    for _ in 0..600 {
        let _x = arena.alloc(0u64);
    }
    arena.clear();
    assert!(arena.has_fresh_chunks());

    // Cycle 2: fill past one chunk again, then clear
    for _ in 0..600 {
        let _x = arena.alloc(1u64);
    }
    arena.clear();
    // fresh_chunks should still be non-null (should have accumulated)
    // Actually after clear, the current chunk becomes fresh.
    // So we always have fresh chunks after clearing.
    assert!(arena.has_fresh_chunks());

    // Cycle 3: alloc should work fine
    let x = arena.alloc(42u32);
    assert_eq!(*x, 42);
}

// --- Zero-sized types ---

#[test]
fn alloc_zst_unit() {
    let arena = Arena::new();
    // First alloc may trigger chunk allocation; subsequent ZST allocs
    // should not consume space.
    let _unit = arena.alloc(());
    let free_after_one = arena.chunk_capacity();
    let _unit2 = arena.alloc(());
    let _unit3 = arena.alloc(());
    // ZSTs should not consume space after the initial chunk is allocated
    assert_eq!(arena.chunk_capacity(), free_after_one);
}

#[test]
fn alloc_zst_struct() {
    #[derive(Debug, PartialEq)]
    struct Zst;
    let arena = Arena::new();
    let _z = arena.alloc(Zst);
    let free_after_one = arena.chunk_capacity();
    let _z2 = arena.alloc(Zst);
    assert_eq!(arena.chunk_capacity(), free_after_one);
}

// --- Structs with Drop ---

#[test]
fn alloc_struct_with_drop() {
    use std::sync::atomic::{AtomicU32, Ordering};
    static DROP_COUNT: AtomicU32 = AtomicU32::new(0);

    struct DropCounted {
        _data: u64,
    }
    impl Drop for DropCounted {
        fn drop(&mut self) {
            DROP_COUNT.fetch_add(1, Ordering::Relaxed);
        }
    }

    {
        let arena = Arena::new();
        let _x = arena.alloc(DropCounted { _data: 42 });
        let _y = arena.alloc(DropCounted { _data: 99 });
        // Drop is NOT called when arena is dropped (this is a bump allocator)
    }
    // Bump allocators don't call drop on individual elements
    assert_eq!(DROP_COUNT.load(Ordering::Relaxed), 0);

    // But the values we allocated should still be valid during arena lifetime
    let arena = Arena::new();
    let a = arena.alloc(DropCounted { _data: 1 });
    let b = arena.alloc(DropCounted { _data: 2 });
    assert_eq!(a._data, 1);
    assert_eq!(b._data, 2);
}

// --- Edge cases ---

#[test]
fn alloc_max_alignment_simd() {
    // Test with a type that has very high alignment (commonly used for SIMD)
    #[repr(align(64))]
    #[allow(dead_code)]
    struct BigAlign([u8; 64]);
    let arena = Arena::new();
    let _pad = arena.alloc(1u8); // misalign
    let x = arena.alloc(BigAlign([0; 64]));
    assert_eq!((x as *const BigAlign as usize) % 64, 0);
}

#[test]
fn alloc_exactly_one_byte() {
    let arena = Arena::new();
    let _ = arena.alloc(0u8); // trigger initial chunk creation
    let free_before = arena.chunk_capacity();
    let x = arena.alloc(0xFFu8);
    assert_eq!(*x, 0xFF);
    assert_eq!(arena.chunk_capacity(), free_before - 1);
}

#[test]
fn alloc_exactly_chunk_size_minus_one() {
    let arena = Arena::new();
    // Allocate (INITIAL_CHUNK_CAPACITY - 1) bytes as u8 array
    // We need to leave room. Let's just fill it up and check the last byte.
    let count = INITIAL_CHUNK_CAPACITY / 8; // u64
    for i in 0..count {
        let x = arena.alloc(i as u64);
        assert_eq!(*x, i as u64);
    }
}

#[test]
fn alloc_after_filling_chunk_uses_new_chunk() {
    let arena = Arena::new();
    // Allocate first u64 (triggers initial chunk creation)
    let x = arena.alloc(0u64);
    assert_eq!(*x, 0);
    let initial_free = arena.chunk_capacity();
    assert!(initial_free > 0);

    // Fill the rest of the first chunk
    let u64_count = INITIAL_CHUNK_CAPACITY / 8;
    let mut pprev_free = initial_free;
    for _ in 1..u64_count {
        let _x = arena.alloc(0u64);
        let r = arena.chunk_capacity();
        assert!(r < pprev_free);
        pprev_free = r;
    }

    // Now allocate one more, which should trigger a new chunk
    // The new chunk has INITIAL_CHUNK_CAPACITY capacity
    let x = arena.alloc(42u64);
    assert_eq!(*x, 42);
}

// --- Fuzz-style random tests ---

#[test]
fn fuzz_random_types() {
    // Deterministic "random" using a simple LCG
    let mut state: u64 = 0xDEADBEEF_CAFEBABE;
    fn rand(state: &mut u64) -> u64 {
        *state = state
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        *state >> 33
    }

    let arena = Arena::new();

    // Track pointers to verify they remain non-null.
    let mut ptrs: Vec<*const u8> = Vec::with_capacity(2000);

    for _ in 0..2000 {
        match rand(&mut state) % 6 {
            0 => {
                let x = arena.alloc(rand(&mut state) as u8);
                ptrs.push(x as *const u8 as *const u8);
            }
            1 => {
                let x = arena.alloc(rand(&mut state) as u16);
                ptrs.push(x as *const u16 as *const u8);
            }
            2 => {
                let x = arena.alloc(rand(&mut state) as u32);
                ptrs.push(x as *const u32 as *const u8);
            }
            3 => {
                let x = arena.alloc(rand(&mut state) as u64);
                ptrs.push(x as *const u64 as *const u8);
            }
            4 => {
                let x = arena.alloc([rand(&mut state) as u8; 16]);
                ptrs.push(x.as_ptr());
            }
            5 => {
                let x = arena.alloc([rand(&mut state) as u8; 64]);
                ptrs.push(x.as_ptr());
            }
            _ => unreachable!(),
        }
    }

    for p in &ptrs {
        assert!(!p.is_null());
    }
}

#[test]
fn fuzz_with_alignment_sensitive_types() {
    let mut state: u64 = 0x12345678_9ABCDEF0;
    fn rand(state: &mut u64) -> u64 {
        *state = state
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        *state >> 33
    }

    let arena = Arena::new();

    for _ in 0..2000 {
        match rand(&mut state) % 5 {
            0 => {
                let x = arena.alloc(rand(&mut state) as u8);
                assert_eq!((x as *const u8 as usize) % 1, 0);
            }
            1 => {
                let _pad = arena.alloc(rand(&mut state) as u8);
                let x = arena.alloc(rand(&mut state) as u16);
                assert_eq!((x as *const u16 as usize) % 2, 0);
            }
            2 => {
                let x = arena.alloc(rand(&mut state) as u32);
                assert_eq!((x as *const u32 as usize) % 4, 0);
            }
            3 => {
                let x = arena.alloc(rand(&mut state) as u64);
                assert_eq!((x as *const u64 as usize) % 8, 0);
            }
            4 => {
                let val = rand(&mut state) as u8;
                let arr = arena.alloc([val; 32]);
                for byte in arr.iter() {
                    assert_eq!(*byte, val);
                }
            }
            _ => unreachable!(),
        }
    }
}

// --- Memory exhaustion resistance ---

#[test]
fn alloc_very_many_tiny_things() {
    let arena = Arena::new();
    // Allocate 100,000 u8 values. Should work fine.
    for i in 0..100_000u32 {
        let x = arena.alloc(i as u8);
        assert_eq!(*x, i as u8);
    }
}

#[test]
fn alloc_very_many_medium_things() {
    let arena = Arena::new();
    // Allocate 10,000 [u8; 10] arrays
    for i in 0..10_000u16 {
        let allocated = arena.alloc([(i % 256) as u8; 10]);
        assert_eq!(allocated[0], (i % 256) as u8);
    }
}

// --- Pointer stability ---

#[test]
fn pointers_are_stable_across_allocations() {
    let arena = Arena::new();

    let a = arena.alloc(1u64) as *const u64;
    let b = arena.alloc(2u64) as *const u64;

    // Allocate many more things
    for _ in 0..500 {
        let _x = arena.alloc(0u64);
    }

    // The original pointers should still be valid
    unsafe {
        assert_eq!(*a, 1);
        assert_eq!(*b, 2);
    }
}

#[test]
fn pointers_are_stable_after_clear_on_current_chunk() {
    let mut arena = Arena::new();

    // Allocate some values, record pointer
    let _a_ptr = arena.alloc(42u64) as *const u64;

    // Don't overflow the first chunk -- we want to stay in it
    arena.clear();

    let b = arena.alloc(99u64);

    // a_ptr and b might be different since arena memory model goes
    // from top down, and clear resets top, so the new allocation could
    // be at the same address. We just verify b is correct.
    assert_eq!(*b, 99);
}

// --- Type parameterization (generic T works) ---

#[test]
fn alloc_generic_struct() {
    #[derive(Debug, PartialEq)]
    struct Pair<T, U>(T, U);

    let arena = Arena::new();
    let p1 = arena.alloc(Pair(42u32, "hello"));
    let p2 = arena.alloc(Pair(3.14f64, true));

    assert_eq!(p1.0, 42);
    assert_eq!(p1.1, "hello");
    assert_eq!(p2.0, 3.14);
    assert_eq!(p2.1, true);
}

#[test]
fn alloc_option() {
    let arena = Arena::new();
    let some = arena.alloc(Some(42u32));
    let none: &mut Option<u32> = arena.alloc(None);

    assert_eq!(*some, Some(42));
    assert_eq!(*none, None);

    *none = Some(7);
    assert_eq!(*none, Some(7));
}

#[test]
fn alloc_result() {
    let arena = Arena::new();
    let ok: &mut Result<u32, &str> = arena.alloc(Ok(100));
    let err: &mut Result<u32, &str> = arena.alloc(Err("oops"));

    assert_eq!(*ok, Ok(100));
    assert_eq!(*err, Err("oops"));
}

// --- Clear edge cases ---

#[test]
fn clear_on_fresh_arena() {
    let mut arena = Arena::new();
    let free_before = arena.chunk_capacity();
    assert!(!arena.has_fresh_chunks());

    arena.clear();

    // Clearing a fresh arena should still have the same remaining
    assert_eq!(arena.chunk_capacity(), free_before);
    // No fresh chunks were added (the only chunk is current, not moved)
    assert!(!arena.has_fresh_chunks());
}

#[test]
fn clear_then_clear_again() {
    let mut arena = Arena::new();
    // Consume less than a chunk, then clear
    for _ in 0..INITIAL_CHUNK_CAPACITY / std::mem::size_of::<u64>() {
        let _x = arena.alloc(0u64);
    }
    arena.clear();
    let free_after_first_clear = arena.chunk_capacity();
    assert!(!arena.has_fresh_chunks()); // only one chunk, still current

    // Consume more space
    for _ in 0..INITIAL_CHUNK_CAPACITY / std::mem::size_of::<u64>() {
        let _x = arena.alloc(0u64);
    }

    arena.clear();
    assert_eq!(arena.chunk_capacity(), free_after_first_clear);
    assert!(!arena.has_fresh_chunks()); // Didn't need to allocate another...
}

#[test]
fn clear_and_reuse_multiple_chunks_round_robin() {
    let mut arena = Arena::new();

    // Phase 1: use 3 chunks worth of space
    let u64_per_chunk = INITIAL_CHUNK_CAPACITY / 8;
    for _ in 0..u64_per_chunk * 3 {
        let _x = arena.alloc(0u64);
    }

    arena.clear();
    assert!(arena.has_fresh_chunks());

    // Phase 2: use 2 more chunks worth
    for _ in 0..u64_per_chunk * 2 {
        let _x = arena.alloc(1u64);
    }

    arena.clear();

    // Phase 3: allocate a few, they should work
    for i in 0..100u32 {
        let x = arena.alloc(i);
        assert_eq!(*x, i);
    }
}

// --- Large allocation edge cases ---

#[test]
fn large_allocation_exactly_half_chunk_does_not_trigger_own_chunk() {
    // layout.size() > INITIAL_CHUNK_CAPACITY / 2 is strict >, so
    // exactly half (2040) does NOT get its own chunk.
    // But if there's room in the chunk, it won't hit alloc_more anyway.
    // So we fill the chunk first to force the slow path, then alloc.
    let arena = Arena::new();
    const HALF: usize = INITIAL_CHUNK_CAPACITY / 2;

    // Fill chunk so remaining space is less than HALF.
    let fill_count = INITIAL_CHUNK_CAPACITY / std::mem::size_of::<u64>() - 1;
    for _ in 0..fill_count {
        let _x = arena.alloc(0u64);
    }
    let free_before = arena.chunk_capacity();
    assert!(free_before < HALF);

    // Alloc HALF bytes. Triggers alloc_more, but HALF > HALF is false,
    // so it goes to the normal new-chunk path and updates bottom/top.
    let _allocated = arena.alloc(BigAlloc::<HALF>([0x42; HALF]));

    // Since we went through the normal new-chunk path, a new chunk was
    // created and the HALF-byte alloc was placed in it.
    assert!(arena.chunk_capacity() > 0);
}

#[test]
fn large_allocation_just_over_half_chunk_gets_own_chunk() {
    let arena = Arena::new();
    const OVER_HALF: usize = INITIAL_CHUNK_CAPACITY / 2 + 1;

    // Fill chunk so the large alloc won't fit.
    let fill_count = INITIAL_CHUNK_CAPACITY / std::mem::size_of::<u64>() - 1;
    for _ in 0..fill_count {
        let _x = arena.alloc(0u64);
    }
    let free_before = arena.chunk_capacity();
    assert!(free_before < OVER_HALF);

    // Allocate the large value. This does NOT get its own chunk anymore
    // (the large-alloc special case was removed). Instead it gets a new
    // chunk via the normal slow path, which updates the current chunk.
    let _allocated = arena.alloc(BigAlloc::<OVER_HALF>([0x42; OVER_HALF]));

    // The current chunk is now the new chunk, so free space changed.
    assert!(arena.chunk_capacity() > 0);
}

// --- Drop works without crashing (smoke test) ---

#[test]
fn drop_arena_does_not_crash() {
    let arena = Arena::new();
    // Allocate various things
    let _a = arena.alloc(1u32);
    let _b = arena.alloc(2u64);
    let _c = arena.alloc([0u8; 100]);
    // Let it drop
}

#[test]
fn drop_after_many_allocations() {
    let arena = Arena::new();
    for i in 0..5000u64 {
        let _x = arena.alloc(i);
    }
    // Let it drop
}

#[test]
fn drop_after_clear() {
    let mut arena = Arena::new();
    for _ in 0..500 {
        let _x = arena.alloc(0u64);
    }
    arena.clear();
    for _ in 0..500 {
        let _x = arena.alloc(1u64);
    }
    // Let it drop (with fresh_chunks populated)
}

#[test]
fn drop_after_large_allocations() {
    let arena = Arena::new();

    // Fill chunk so large allocs won't fit.
    let fill_count = INITIAL_CHUNK_CAPACITY / std::mem::size_of::<u64>() - 1;
    for _ in 0..fill_count {
        let _x = arena.alloc(0u64);
    }

    // Multiple large allocations (large-alloc special case was removed,
    // so these go through normal slow path and use new chunks).
    const BIG: usize = INITIAL_CHUNK_CAPACITY / 2 + 50;
    {
        let _x = arena.alloc(BigAlloc::<BIG>([0; BIG]));
    }
    {
        let _x = arena.alloc(BigAlloc::<BIG>([0; BIG]));
    }

    // Drop should clean up the main chain and the large-allocation sub-chain
}

#[test]
fn drop_empty_arena() {
    // Just create and drop
    let _arena = Arena::new();
}

// --- Stress test ---

#[test]
fn stress_test_mixed_allocation_patterns() {
    let mut state: u64 = 0xBEEF_FACE;
    fn rand(state: &mut u64) -> u64 {
        *state = state.wrapping_mul(1103515245).wrapping_add(12345);
        *state
    }

    let mut arena = Arena::new();

    for _cycle in 0..5 {
        // Phase 1: many small allocations
        for _ in 0..200 {
            match rand(&mut state) % 5 {
                0 => {
                    let _ = arena.alloc(rand(&mut state) as u8);
                }
                1 => {
                    let _ = arena.alloc((rand(&mut state) as u32).wrapping_mul(42));
                }
                2 => {
                    let _ = arena.alloc(rand(&mut state) as u64);
                }
                3 => {
                    let _ = arena.alloc([rand(&mut state) as u8; 8]);
                }
                4 => {
                    let _ = arena.alloc([rand(&mut state) as u8; 32]);
                }
                _ => unreachable!(),
            }
        }

        // Phase 2: a few large allocations (use BigAlloc with fixed sizes)
        let _large1 = arena.alloc(BigAlloc::<{ INITIAL_CHUNK_CAPACITY / 2 + 50 }>(
            [0xAA; INITIAL_CHUNK_CAPACITY / 2 + 50],
        ));
        let _large2 = arena.alloc(BigAlloc::<{ INITIAL_CHUNK_CAPACITY / 2 + 200 }>(
            [0xBB; INITIAL_CHUNK_CAPACITY / 2 + 200],
        ));
        let _large3 = arena.alloc(BigAlloc::<{ INITIAL_CHUNK_CAPACITY / 2 + 300 }>(
            [0xCC; INITIAL_CHUNK_CAPACITY / 2 + 300],
        ));

        // Phase 3: more small allocations
        for _ in 0..100 {
            match rand(&mut state) % 3 {
                0 => {
                    let _ = arena.alloc(rand(&mut state) as u16);
                }
                1 => {
                    let _ = arena.alloc([rand(&mut state) as u32; 4]);
                }
                2 => {
                    let _ = arena.alloc([0xFFu8; 16]);
                }
                _ => unreachable!(),
            }
        }

        arena.clear();
    }
}

// --- Consecutive clears without intervening allocations ---

#[test]
fn multiple_clears_without_allocs() {
    let mut arena = Arena::new();
    let _x = arena.alloc(42u32);

    arena.clear();
    arena.clear();
    arena.clear();

    let y = arena.alloc(99u32);
    assert_eq!(*y, 99);
}

// --- Allocating arrays of different sizes ---

#[test]
fn alloc_fixed_size_array() {
    let arena = Arena::new();
    let arr = arena.alloc([1u8, 2, 3, 4, 5]);
    assert_eq!(*arr, [1, 2, 3, 4, 5]);
    arr[2] = 99;
    assert_eq!(arr[2], 99);
}

#[test]
fn alloc_medium_array() {
    let arena = Arena::new();
    let mut data = [0u8; 256];
    for i in 0..256 {
        data[i] = i as u8;
    }
    let arr = arena.alloc(data);
    for i in 0..256 {
        assert_eq!(arr[i], i as u8);
    }
}

// --- alloc returns a valid mutable reference (write check) ---

#[test]
fn alloc_returns_unique_mutable_reference() {
    let arena = Arena::new();
    let a = arena.alloc(0u32);
    let b = arena.alloc(0u32);
    *a = 42;
    *b = 99;
    // If the references weren't unique, we'd see UB (but this compiles, so they are)
    assert_eq!(*a, 42);
    assert_eq!(*b, 99);
}

// This surfaced an error through miri, where we were creating
// a mutable reference to static data.
#[test]
fn bespoke_allocation_first() {
    let arena = Arena::new();
    arena.alloc(INITIAL_CHUNK_CAPACITY + 1);
}

#[test]
fn can_clear_empty_arena() {
    let mut arena = Arena::new();
    arena.clear();
}

// --- alloc_str ---

#[test]
fn alloc_str_basic() {
    let arena = Arena::new();
    let s = arena.alloc_str("hello world");
    assert_eq!(s, "hello world");
    // Verify mutability.
    s.make_ascii_uppercase();
    assert_eq!(s, "HELLO WORLD");
}

#[test]
fn alloc_str_empty() {
    let arena = Arena::new();
    let s = arena.alloc_str("");
    assert_eq!(s, "");
    assert_eq!(s.len(), 0);
}

#[test]
fn alloc_str_multi_byte() {
    let arena = Arena::new();
    let s = arena.alloc_str("a̐éõ🦀");
    assert_eq!(s, "a̐éõ🦀");
}

#[test]
fn alloc_str_long() {
    let arena = Arena::new();
    let data = "x".repeat(2000);
    let s = arena.alloc_str(&data);
    assert_eq!(*s, data);
    let mut arena = Arena::new();
    let s1 = arena.alloc_str("first");
    assert_eq!(s1, "first");
    arena.clear();
    let s2 = arena.alloc_str("second");
    assert_eq!(s2, "second");
}

#[test]
fn alloc_str_past_one_chunk() {
    let arena = Arena::new();
    // Fill the 32-byte chunk.
    arena.alloc_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    // This forces alloc_more.
    let s = arena.alloc_str("hello from second chunk");
    assert_eq!(s, "hello from second chunk");
}

// --- alloc_slice_copy ---

#[test]
fn alloc_slice_copy_basic() {
    let arena = Arena::new();
    let slice = arena.alloc_slice_copy(&[1, 2, 3, 4]);
    assert_eq!(slice, [1, 2, 3, 4]);
    slice[0] = 99;
    assert_eq!(slice, [99, 2, 3, 4]);
}

#[test]
fn alloc_slice_copy_empty() {
    let arena = Arena::new();
    let slice = arena.alloc_slice_copy(&[] as &[u8]);
    assert!(slice.is_empty());
}

#[test]
fn alloc_slice_copy_long() {
    let arena = Arena::new();
    let data = vec![0xABu8; 1024];
    let slice = arena.alloc_slice_copy(&data);
    assert_eq!(slice, data);
}

#[test]
fn alloc_slice_copy_with_clear() {
    let mut arena = Arena::new();
    let s1 = arena.alloc_slice_copy(b"first");
    assert_eq!(s1, b"first");
    arena.clear();
    let s2 = arena.alloc_slice_copy(b"second");
    assert_eq!(s2, b"second");
}

#[test]
fn alloc_slice_copy_past_one_chunk() {
    let arena = Arena::new();
    // Fill the 32-byte chunk.
    arena.alloc_slice_copy(&[0; 30]);
    // This forces alloc_more.
    let slice = arena.alloc_slice_copy(b"hello from second chunk");
    assert_eq!(slice, b"hello from second chunk");
}

#[test]
fn arena_forwarding_alloc() {
    let root = Arena::new();
    let x = root.alloc(42u32);
    assert_eq!(*x, 42);
    *x = 99;
    assert_eq!(*x, 99);
}

#[test]
fn arena_forwarding_alloc_with() {
    let root = Arena::new();
    let x = root.alloc_with(|| [1, 2, 3]);
    assert_eq!(x, &[1, 2, 3]);
}

#[test]
fn arena_forwarding_alloc_slice_copy() {
    let root = Arena::new();
    let slice = root.alloc_slice_copy(&[10, 20, 30]);
    assert_eq!(slice, &[10, 20, 30]);
}

#[test]
fn arena_forwarding_alloc_str() {
    let root = Arena::new();
    let s = root.alloc_str("hello");
    assert_eq!(s, "hello");
}

#[test]
fn arena_forwarding_allocated_bytes() {
    let root = Arena::new();
    let _ = root.alloc(42u32);
    assert!(root.allocated_bytes() > 0);
}

#[test]
fn arena_forwarding_sized_slice() {
    let root = Arena::new();
    let arr = root.alloc_sized_slice_fill_default::<u32, 4>();
    assert_eq!(arr, &[0, 0, 0, 0]);
}

// --- with_scope ---

#[test]
fn with_scope_basic() {
    let mut arena = Arena::new();
    arena.with_scope(|scope| {
        let x = scope.alloc(42u32);
        assert_eq!(*x, 42);
        *x = 99;
        assert_eq!(*x, 99);
    });
}

#[test]
fn with_scope_nested() {
    let mut arena = Arena::new();
    arena.alloc(1u32);
    let cap = arena.chunk_capacity();

    arena.with_scope(|outer_scope| {
        let a = outer_scope.alloc(2u32);

        outer_scope.with_scope(|inner_scope| {
            let inner = inner_scope.alloc(3u32);
            assert_eq!(*inner, 3);
            for i in 0..10 {
                let v = inner_scope.alloc(i as u128);
                assert_eq!(*v, i as u128);
            }
        });

        // After inner scope reverts, allocate in outer scope normally
        let after = outer_scope.alloc(4u32);
        assert_eq!(*after, 4);

        // outer_scope is an ArenaRef, which allows for allocations to
        // be used across scope introduction/elimination.
        assert_eq!(*a, 2);
    });

    // After both scopes, arena state is intact
    assert_eq!(arena.chunk_capacity(), cap);
}

// This is not behaviour that's exposed via the safe API
// Do not do this.
#[test]
fn with_scope_outer_preserved() {
    let mut arena = Arena::new();

    // Allocate before the scope, snapshot via raw pointer.
    // with_scope takes &mut self, so &mut T from alloc can't be held.
    let x_addr = arena.alloc(10u32) as *mut u32;
    let y_addr = arena.alloc(20u32) as *mut u32;
    let b_addr = arena.alloc(0xFFu8) as *mut u8;

    // Scope allocates a lot, potentially crossing chunk boundaries
    arena.with_scope(|scope| {
        for i in 0..30 {
            let v = scope.alloc(i as u128);
            *v = (i * 2) as u128;
            assert_eq!(*v, (i * 2) as u128);
        }
    });

    // Memory is still there — the scope restored the checkpoint
    assert_eq!(unsafe { *x_addr }, 10);
    assert_eq!(unsafe { *y_addr }, 20);
    assert_eq!(unsafe { *b_addr }, 0xFF);

    // Arena is still usable after the scope
    let new_int = arena.alloc(999u32);
    assert_eq!(*new_int, 999);
}

#[test]
fn with_scope_multiple_sequential() {
    let mut arena = Arena::new();
    let arena = arena.as_arena_ref_mut();

    // Allocate before any scope (raw ptr to survive &mut self)
    // This isn't an intended use case, but happens to be safe...
    let a_addr = arena.alloc(1u32) as *mut u32;
    assert_eq!(unsafe { *a_addr }, 1);

    // First scope
    arena.with_scope(|scope| {
        let x = scope.alloc(100u32);
        *x = *x + 1;
        assert_eq!(*x, 101);
    });

    // Memory intact after first scope
    assert_eq!(unsafe { *a_addr }, 1);

    // Second scope
    arena.with_scope(|scope| {
        let y = scope.alloc(200u32);
        *y = *y + 2;
        assert_eq!(*y, 202);
    });

    // Memory intact after second scope
    assert_eq!(unsafe { *a_addr }, 1);

    // Allocate more after scopes
    let b = arena.alloc(2u32);
    assert_eq!(*b, 2);
}

#[test]
fn with_scope_reuses_chunks() {
    let mut arena = Arena::new();

    // First alloc gives us a chunk
    arena.alloc(1u32);
    let cap_after_first = arena.chunk_capacity();
    assert!(cap_after_first > 0);

    assert!(!arena.has_fresh_chunks());
    arena.with_scope(|scope| {
        scope.alloc(0u128);
    });
    assert_eq!(arena.chunk_capacity(), cap_after_first);
    // This wasn't enough to create a new chunk...
    assert!(!arena.has_fresh_chunks());

    assert!(!arena.has_fresh_chunks());
    arena.with_scope(|scope| {
        // Fill current chunk to force allocation of another
        for _ in 0..100 {
            scope.alloc(0u128);
        }
    });

    // After scope, chunk_capacity should be back to what
    // we had before the scope (the extra chunk is reused, not freed)
    assert_eq!(arena.chunk_capacity(), cap_after_first);
    assert!(arena.has_fresh_chunks());
}

// --- alloc_str_lit ---

#[test]
fn alloc_str_lit_basic() {
    let arena = Arena::new();
    let s = arena.alloc_str_lit("hello world");
    assert_eq!(s, "hello world");
    // Verify mutability.
    s.make_ascii_uppercase();
    assert_eq!(s, "HELLO WORLD");
}

#[test]
fn alloc_str_lit_empty() {
    let arena = Arena::new();
    let s = arena.alloc_str_lit("");
    assert_eq!(s, "");
    assert_eq!(s.len(), 0);
}

#[test]
fn alloc_str_lit_multi_byte() {
    let arena = Arena::new();
    let s = arena.alloc_str_lit("a̐éõ🦀");
    assert_eq!(s, "a̐éõ🦀");
}

#[test]
fn alloc_str_lit_with_clear() {
    let mut arena = Arena::new();
    let s1 = arena.alloc_str_lit("first");
    assert_eq!(s1, "first");
    arena.clear();
    let s2 = arena.alloc_str_lit("second");
    assert_eq!(s2, "second");
}

#[test]
fn alloc_str_lit_past_one_chunk() {
    let arena = Arena::new();
    // Fill the 32-byte chunk.
    arena.alloc_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    // This forces alloc_more.
    let s = arena.alloc_str_lit("hello from second chunk");
    assert_eq!(s, "hello from second chunk");
}

#[test]
fn alloc_str_lit_many() {
    let arena = Arena::new();
    // Allocate many string literals to exercise the fast path heavily.
    for _ in 0..1000 {
        let s = arena.alloc_str_lit("fast path");
        assert_eq!(s, "fast path");
    }
}

// --- alloc_slice_lit_copy ---

#[test]
fn alloc_slice_lit_copy_basic() {
    let arena = Arena::new();
    let slice = arena.alloc_slice_lit_copy(&[1, 2, 3, 4]);
    assert_eq!(slice, [1, 2, 3, 4]);
    slice[0] = 99;
    assert_eq!(slice, [99, 2, 3, 4]);
}

#[test]
fn alloc_slice_lit_copy_empty() {
    let arena = Arena::new();
    let slice = arena.alloc_slice_lit_copy(&[] as &[u8]);
    assert!(slice.is_empty());
}

#[test]
fn alloc_slice_lit_copy_with_clear() {
    let mut arena = Arena::new();
    let s1 = arena.alloc_slice_lit_copy(b"first");
    assert_eq!(s1, b"first");
    arena.clear();
    let s2 = arena.alloc_slice_lit_copy(b"second");
    assert_eq!(s2, b"second");
}

#[test]
fn alloc_slice_lit_copy_past_one_chunk() {
    let arena = Arena::new();
    // Fill the 32-byte chunk.
    arena.alloc_slice_copy(&[0; 30]);
    // This forces alloc_more.
    let slice = arena.alloc_slice_lit_copy(b"hello from second chunk");
    assert_eq!(slice, b"hello from second chunk");
}

#[test]
fn alloc_slice_lit_copy_many() {
    let arena = Arena::new();
    // Allocate many slice literals to exercise the fast path heavily.
    for _ in 0..1000 {
        let s = arena.alloc_slice_lit_copy(&[1u8, 2, 3, 4, 5, 6, 7, 8]);
        assert_eq!(s, [1, 2, 3, 4, 5, 6, 7, 8]);
    }
}

#[test]
fn alloc_slice_lit_copy_f64() {
    let arena = Arena::new();
    let slice = arena.alloc_slice_lit_copy(&[1.0f64, 2.5, 3.14]);
    assert!((slice[0] - 1.0).abs() < f64::EPSILON);
    assert!((slice[1] - 2.5).abs() < f64::EPSILON);
    assert!((slice[2] - 3.14).abs() < f64::EPSILON);
    slice[1] = 99.9;
    assert!((slice[1] - 99.9).abs() < f64::EPSILON);
}

// --- Panic: over-allocation (capacity overflow) ---
//
// Functions that take a `len: usize` parameter pass it to Layout::array,
// which panics when size_of::<T>() * len > isize::MAX.
//
// Functions that take &[T] (alloc_slice_copy, alloc_slice_clone,
// alloc_slice_lit_copy, alloc_str, alloc_str_lit) cannot trigger this
// panic because a valid Rust slice reference already has total byte size
// <= isize::MAX, so the Layout::array precondition is always satisfied.

// alloc_slice_fill_with: T = u8, len > isize::MAX triggers overflow.
#[test]
#[should_panic]
fn over_allocation_alloc_slice_fill_with_u8() {
    let arena = Arena::new();
    let too_big = (isize::MAX as usize).saturating_add(1);
    let _: &mut [u8] = arena.alloc_slice_fill_with(too_big, |_| 0u8);
}

// alloc_slice_fill_with: T = u64, len > isize::MAX / 8 triggers overflow.
#[test]
#[should_panic]
fn over_allocation_alloc_slice_fill_with_u64() {
    let arena = Arena::new();
    let too_big = (isize::MAX as usize / 8).saturating_add(1);
    let _: &mut [u64] = arena.alloc_slice_fill_with(too_big, |_| 0);
}

// alloc_slice_fill_default with an impossibly large length.
#[test]
#[should_panic]
fn over_allocation_alloc_slice_fill_default() {
    let arena = Arena::new();
    let too_big = (isize::MAX as usize).saturating_add(1);
    let _: &mut [u8] = arena.alloc_slice_fill_default(too_big);
}

// alloc_slice_try_fill_with with an impossibly large length.
#[test]
#[should_panic]
fn over_allocation_alloc_slice_try_fill_with() {
    let arena = Arena::new();
    let too_big = (isize::MAX as usize).saturating_add(1);
    let _: Result<&mut [u8], ()> = arena.alloc_slice_try_fill_with(too_big, |_| Ok(0u8));
}