masstree 0.9.5

A high-performance concurrent ordered map (trie of B+trees)
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
use std::cmp::Ordering;
use std::iter as StdIter;
use std::mem as StdMem;

use super::{INITIAL_CAPACITY, InlineSuffixBag, PermutationProvider, SuffixBag, SuffixSidecar};
use crate::permuter::Permuter15;
// Note: AllocError removed - allocations are now infallible

// ========================================================================
//  Basic Tests
// ========================================================================

#[test]
fn test_new_suffix_bag() {
    let bag: SuffixBag = SuffixBag::new();

    assert_eq!(bag.count(), 0);
    assert!(bag.capacity() >= INITIAL_CAPACITY);
    assert_eq!(bag.used(), 0);
}

#[test]
fn test_with_capacity() {
    let bag: SuffixBag = SuffixBag::with_capacity(256);

    assert!(bag.capacity() >= 256);
    assert_eq!(bag.count(), 0);
}

#[test]
fn test_default() {
    let bag: SuffixBag = SuffixBag::default();

    assert_eq!(bag.count(), 0);
}

// ========================================================================
//  Assign and Get Tests
// ========================================================================

#[test]
fn test_assign_and_get() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"hello");
    bag.assign(5, b"world");
    bag.assign(10, b"!");

    assert_eq!(bag.get(0), Some(b"hello".as_slice()));
    assert_eq!(bag.get(5), Some(b"world".as_slice()));
    assert_eq!(bag.get(10), Some(b"!".as_slice()));
    assert_eq!(bag.get(1), None);
    assert_eq!(bag.count(), 3);
}

#[test]
fn test_empty_suffix() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"");

    assert_eq!(bag.get(0), Some(b"".as_slice()));
    assert!(bag.has_suffix(0));
}

#[test]
fn test_get_or_empty() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"hello");

    assert_eq!(bag.get_or_empty(0), b"hello".as_slice());
    assert_eq!(bag.get_or_empty(1), b"".as_slice());
}

#[test]
fn test_overwrite_suffix() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"hello");
    assert_eq!(bag.get(0), Some(b"hello".as_slice()));

    bag.assign(0, b"goodbye");
    assert_eq!(bag.get(0), Some(b"goodbye".as_slice()));

    // Old data still in buffer (not compacted)
    assert!(bag.used() >= "hello".len() + "goodbye".len());
}

// ========================================================================
//  Clear Tests
// ========================================================================

#[test]
fn test_clear_suffix() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"hello");
    assert!(bag.has_suffix(0));

    bag.clear(0);

    assert!(!bag.has_suffix(0));
    assert_eq!(bag.get(0), None);
}

#[test]
fn test_clear_already_empty() {
    let mut bag: SuffixBag = SuffixBag::new();

    // Clearing an empty slot should not panic
    bag.clear(0);

    assert!(!bag.has_suffix(0));
}

// ========================================================================
//  Compact Tests
// ========================================================================

#[test]
fn test_compact() {
    let mut bag: SuffixBag = SuffixBag::new();

    // Add several suffixes
    bag.assign(0, b"aaaa");
    bag.assign(1, b"bbbb");
    bag.assign(2, b"cccc");
    bag.assign(3, b"dddd");

    let before: usize = bag.used();
    assert_eq!(before, 16);

    // Compact keeping only slots 0 and 2
    let reclaimed: usize = bag.compact([0, 2].into_iter());

    assert!(reclaimed > 0);
    assert_eq!(bag.get(0), Some(b"aaaa".as_slice()));
    assert_eq!(bag.get(2), Some(b"cccc".as_slice()));
    assert_eq!(bag.get(1), None);
    assert_eq!(bag.get(3), None);
    assert_eq!(bag.used(), 8);
}

#[test]
fn test_compact_empty() {
    let mut bag: SuffixBag = SuffixBag::new();

    // Compact with no active slots should work
    let reclaimed: usize = bag.compact(StdIter::empty());

    assert_eq!(reclaimed, 0);
    assert_eq!(bag.count(), 0);
}

#[test]
fn test_compact_all() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"test");
    bag.assign(1, b"data");

    // Compact keeping all
    let reclaimed: usize = bag.compact([0, 1].into_iter());

    // No garbage to collect
    assert_eq!(reclaimed, 0);
    assert_eq!(bag.get(0), Some(b"test".as_slice()));
    assert_eq!(bag.get(1), Some(b"data".as_slice()));
}

#[test]
fn test_compact_with_permuter() {
    // Create a mock permuter
    struct MockPerm {
        slots: Vec<usize>,
    }

    impl PermutationProvider for MockPerm {
        fn size(&self) -> usize {
            self.slots.len()
        }

        fn get(&self, i: usize) -> usize {
            self.slots[i]
        }
    }

    let mut bag: SuffixBag = SuffixBag::new();
    bag.assign(0, b"keep0");
    bag.assign(1, b"drop1");
    bag.assign(2, b"keep2");

    let perm = MockPerm {
        slots: vec![0, 2], // Only slots 0 and 2 are active
    };

    bag.compact_with_permuter(&perm, None);

    assert_eq!(bag.get(0), Some(b"keep0".as_slice()));
    assert_eq!(bag.get(1), None);
    assert_eq!(bag.get(2), Some(b"keep2".as_slice()));
}

#[test]
fn test_compact_with_exclude() {
    struct MockPerm {
        slots: Vec<usize>,
    }

    impl PermutationProvider for MockPerm {
        fn size(&self) -> usize {
            self.slots.len()
        }

        fn get(&self, i: usize) -> usize {
            self.slots[i]
        }
    }

    let mut bag: SuffixBag = SuffixBag::new();
    bag.assign(0, b"keep");
    bag.assign(1, b"exclude");
    bag.assign(2, b"keep2");

    let perm = MockPerm {
        slots: vec![0, 1, 2],
    };

    // Exclude slot 1 from compaction
    bag.compact_with_permuter(&perm, Some(1));

    assert_eq!(bag.get(0), Some(b"keep".as_slice()));
    assert_eq!(bag.get(1), None); // Excluded
    assert_eq!(bag.get(2), Some(b"keep2".as_slice()));
}

// ========================================================================
//  Growth Tests
// ========================================================================

#[test]
fn test_growth() {
    let mut bag: SuffixBag = SuffixBag::with_capacity(16);

    // Fill past capacity
    for i in 0..15 {
        bag.assign(i, b"12345678"); // 8 bytes each = 120 bytes total
    }

    assert!(bag.capacity() > 16);
    assert_eq!(bag.used(), 120);

    // All suffixes should still be accessible
    for i in 0..15 {
        assert_eq!(bag.get(i), Some(b"12345678".as_slice()));
    }
}

#[test]
fn test_long_suffix() {
    let mut bag: SuffixBag = SuffixBag::new();

    let long_suffix: Vec<u8> = vec![b'x'; 1000];
    bag.assign(0, &long_suffix);

    assert_eq!(bag.get(0), Some(long_suffix.as_slice()));
}

// ========================================================================
//  Comparison Tests
// ========================================================================

#[test]
fn test_suffix_equals() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"hello");

    assert!(bag.suffix_equals(0, b"hello"));
    assert!(!bag.suffix_equals(0, b"world"));
    assert!(!bag.suffix_equals(0, b"hell"));
    assert!(!bag.suffix_equals(1, b"hello")); // No suffix at slot 1
}

#[test]
fn test_suffix_compare() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"hello");

    assert_eq!(bag.suffix_compare(0, b"hello"), Some(Ordering::Equal));
    assert_eq!(bag.suffix_compare(0, b"hella"), Some(Ordering::Greater));
    assert_eq!(bag.suffix_compare(0, b"hellz"), Some(Ordering::Less));
    assert_eq!(bag.suffix_compare(1, b"hello"), None);
}

// ========================================================================
//  Clone Tests
// ========================================================================

#[test]
fn test_clone() {
    let mut bag: SuffixBag = SuffixBag::new();
    bag.assign(0, b"hello");
    bag.assign(5, b"world");

    let cloned: SuffixBag = bag.clone();

    assert_eq!(cloned.get(0), Some(b"hello".as_slice()));
    assert_eq!(cloned.get(5), Some(b"world".as_slice()));
    assert_eq!(cloned.count(), 2);
}

// ========================================================================
//  In-Place Assignment Tests
// ========================================================================

#[test]
fn test_try_assign_in_place_fresh_bag() {
    let mut bag: SuffixBag = SuffixBag::new();

    // Fresh bag has capacity, should succeed
    assert!(bag.try_assign_in_place(0, b"hello"));
    assert_eq!(bag.get(0), Some(b"hello".as_slice()));
}

#[test]
fn test_try_assign_in_place_reuse_slot() {
    let mut bag: SuffixBag = SuffixBag::new();

    // Assign a longer suffix first
    bag.assign(0, b"hello world");
    let used_before: usize = bag.used();

    // Assign a shorter suffix - should reuse the slot
    assert!(bag.try_assign_in_place(0, b"hi"));
    assert_eq!(bag.get(0), Some(b"hi".as_slice()));

    // Used bytes should not increase (reused existing space)
    assert_eq!(bag.used(), used_before);
}

#[test]
fn test_try_assign_in_place_append() {
    let mut bag: SuffixBag = SuffixBag::new();

    // Assign to slot 0
    assert!(bag.try_assign_in_place(0, b"first"));

    // Assign to slot 1 - should append
    assert!(bag.try_assign_in_place(1, b"second"));

    assert_eq!(bag.get(0), Some(b"first".as_slice()));
    assert_eq!(bag.get(1), Some(b"second".as_slice()));
}

#[test]
fn test_try_assign_in_place_fails_when_full() {
    // Create a bag with very small capacity
    let mut bag: SuffixBag = SuffixBag::with_capacity(10);

    // First assignment should succeed
    assert!(bag.try_assign_in_place(0, b"12345"));

    // Second assignment that exceeds capacity should fail
    assert!(!bag.try_assign_in_place(1, b"678901234567890"));

    // First slot should still be valid
    assert_eq!(bag.get(0), Some(b"12345".as_slice()));
    // Second slot should not exist
    assert_eq!(bag.get(1), None);
}

#[test]
fn test_try_assign_in_place_same_length() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"hello");
    let used_before: usize = bag.used();

    // Same length should reuse slot
    assert!(bag.try_assign_in_place(0, b"world"));
    assert_eq!(bag.get(0), Some(b"world".as_slice()));
    assert_eq!(bag.used(), used_before);
}

#[test]
fn test_try_assign_in_place_longer_suffix_needs_append() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"hi");
    let used_before: usize = bag.used();

    // Longer suffix can't reuse slot, needs append
    assert!(bag.try_assign_in_place(0, b"hello world"));
    assert_eq!(bag.get(0), Some(b"hello world".as_slice()));

    // Used bytes should increase
    assert!(bag.used() > used_before);
}

#[test]
fn test_try_assign_in_place_mixed_usage() {
    let mut bag: SuffixBag = SuffixBag::new();

    // Fill several slots
    for i in 0..5 {
        assert!(bag.try_assign_in_place(i, b"test"));
    }

    // Reuse slot 2 with shorter suffix
    assert!(bag.try_assign_in_place(2, b"ab"));
    assert_eq!(bag.get(2), Some(b"ab".as_slice()));

    // Other slots unchanged
    assert_eq!(bag.get(0), Some(b"test".as_slice()));
    assert_eq!(bag.get(1), Some(b"test".as_slice()));
    assert_eq!(bag.get(3), Some(b"test".as_slice()));
    assert_eq!(bag.get(4), Some(b"test".as_slice()));
}

// ========================================================================
//  InlineSuffixBag Tests
// ========================================================================

#[test]
fn test_inline_new() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    #[cfg(not(feature = "small-suffix-capacity"))]
    {
        assert_eq!(bag.capacity(), 512);
        assert_eq!(bag.remaining(), 512);
    }

    #[cfg(feature = "small-suffix-capacity")]
    {
        assert_eq!(bag.capacity(), 256);
        assert_eq!(bag.remaining(), 256);
    }

    assert_eq!(bag.used(), 0);
    assert_eq!(bag.count(), 0);
}

#[test]
fn test_inline_default() {
    let bag: InlineSuffixBag = InlineSuffixBag::default();

    assert_eq!(bag.count(), 0);
    assert_eq!(bag.used(), 0);
}

#[test]
fn test_inline_try_assign_basic() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    assert!(bag.try_assign(0, b"hello"));
    assert!(bag.try_assign(5, b"world"));

    assert_eq!(bag.get(0), Some(b"hello".as_slice()));
    assert_eq!(bag.get(5), Some(b"world".as_slice()));
    assert_eq!(bag.get(1), None);
    assert_eq!(bag.count(), 2);
    assert_eq!(bag.used(), 10);
}

#[test]
fn test_inline_try_assign_reuse_slot() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    // Assign longer suffix first
    assert!(bag.try_assign(0, b"hello world"));
    let used_before = bag.used();

    // Shorter suffix should reuse slot's space
    assert!(bag.try_assign(0, b"hi"));
    assert_eq!(bag.get(0), Some(b"hi".as_slice()));

    // Used bytes should not increase
    assert_eq!(bag.used(), used_before);
}

#[test]
fn test_inline_try_assign_append() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    assert!(bag.try_assign(0, b"first"));
    assert!(bag.try_assign(1, b"second"));

    assert_eq!(bag.used(), 11); // 5 + 6
    assert_eq!(bag.get(0), Some(b"first".as_slice()));
    assert_eq!(bag.get(1), Some(b"second".as_slice()));
}

#[test]
fn test_inline_clear() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    bag.try_assign(0, b"hello");
    assert!(bag.has_suffix(0));

    bag.clear(0);

    assert!(!bag.has_suffix(0));
    assert_eq!(bag.get(0), None);
    // Used bytes NOT reclaimed by clear
    assert_eq!(bag.used(), 5);
}

#[test]
fn test_inline_clear_all() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    bag.try_assign(0, b"hello");
    bag.try_assign(1, b"world");

    bag.clear_all();

    assert_eq!(bag.count(), 0);
    assert_eq!(bag.used(), 0);
    assert!(!bag.has_suffix(0));
    assert!(!bag.has_suffix(1));
}

#[test]
fn test_inline_get_or_empty() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    bag.try_assign(0, b"hello");

    assert_eq!(bag.get_or_empty(0), b"hello".as_slice());
    assert_eq!(bag.get_or_empty(1), b"".as_slice());
}

#[test]
fn test_inline_suffix_equals() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    bag.try_assign(0, b"hello");

    assert!(bag.suffix_equals(0, b"hello"));
    assert!(!bag.suffix_equals(0, b"world"));
    assert!(!bag.suffix_equals(1, b"hello"));
}

#[test]
fn test_inline_suffix_compare() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    bag.try_assign(0, b"hello");

    assert_eq!(bag.suffix_compare(0, b"hello"), Some(Ordering::Equal));
    assert_eq!(bag.suffix_compare(0, b"hella"), Some(Ordering::Greater));
    assert_eq!(bag.suffix_compare(1, b"hello"), None);
}

#[test]
fn test_inline_drain_to_external() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    // Fill inline bag
    bag.try_assign(0, b"suffix0");
    bag.try_assign(1, b"suffix1");
    bag.try_assign(2, b"suffix2");

    // Create a permutation with 3 sorted entries (slots 0, 1, 2)
    let perm = Permuter15::make_sorted(3);

    // Drain to external with a new suffix for slot 3
    let external = bag.drain_to_external(&perm, 3, b"new_suffix");

    // Inline bag state is PRESERVED (orphaned inline optimization).
    // This is intentional - clearing inline before external publication
    // creates a race where readers see neither inline nor external data.
    // The inline data becomes "orphaned" but is still valid for readers.
    assert_eq!(bag.count(), 3);

    // External bag should have all suffixes
    assert_eq!(external.get(0), Some(b"suffix0".as_slice()));
    assert_eq!(external.get(1), Some(b"suffix1".as_slice()));
    assert_eq!(external.get(2), Some(b"suffix2".as_slice()));
    assert_eq!(external.get(3), Some(b"new_suffix".as_slice()));
}

#[test]
fn test_inline_drain_replaces_slot() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    bag.try_assign(0, b"old_suffix");
    bag.try_assign(1, b"keep_this");

    // Create a permutation with 2 sorted entries (slots 0, 1)
    let perm = Permuter15::make_sorted(2);

    // Replace slot 0's suffix during drain
    let external = bag.drain_to_external(&perm, 0, b"new_suffix");

    // External should have new suffix for slot 0
    assert_eq!(external.get(0), Some(b"new_suffix".as_slice()));
    assert_eq!(external.get(1), Some(b"keep_this".as_slice()));
}

#[test]
fn test_inline_drain_to_external_init() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    // Simulate split initialization: slots 0, 1, 2 filled sequentially
    bag.try_assign(0, b"suffix_0");
    bag.try_assign(1, b"suffix_1");
    bag.try_assign(2, b"suffix_2");

    // Drain to external for slot 3 (slots 0..3 are filled)
    let external = bag.drain_to_external_init(3, b"new_suffix_3");

    // Inline bag state is PRESERVED (orphaned inline optimization).
    // See drain_to_external test for rationale.
    assert_eq!(bag.count(), 3);

    // External should have all suffixes
    assert_eq!(external.get(0), Some(b"suffix_0".as_slice()));
    assert_eq!(external.get(1), Some(b"suffix_1".as_slice()));
    assert_eq!(external.get(2), Some(b"suffix_2".as_slice()));
    assert_eq!(external.get(3), Some(b"new_suffix_3".as_slice()));
}

#[test]
fn test_inline_drain_to_external_init_replaces_slot() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    // Simulate: slots 0, 1 filled, now replacing slot 1
    bag.try_assign(0, b"keep_this");
    bag.try_assign(1, b"old_suffix");

    // Drain with new_slot=1 means slots 0..1 are "filled" (just slot 0)
    // and we're assigning to slot 1
    let external = bag.drain_to_external_init(1, b"new_suffix");

    // External should have slot 0 preserved and slot 1 with new suffix
    assert_eq!(external.get(0), Some(b"keep_this".as_slice()));
    assert_eq!(external.get(1), Some(b"new_suffix".as_slice()));
}

#[test]
fn test_inline_clone() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();
    bag.try_assign(0, b"hello");
    bag.try_assign(5, b"world");

    let cloned = bag.clone();

    assert_eq!(cloned.get(0), Some(b"hello".as_slice()));
    assert_eq!(cloned.get(5), Some(b"world".as_slice()));
    assert_eq!(cloned.count(), 2);
    assert_eq!(cloned.used(), bag.used());
}

#[test]
fn test_inline_empty_suffix() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    // Empty suffix should work
    assert!(bag.try_assign(0, b""));
    assert_eq!(bag.get(0), Some(b"".as_slice()));
    assert!(bag.has_suffix(0));
    assert_eq!(bag.used(), 0);
}

#[test]
fn test_inline_size_calculation() {
    // InlineSuffixBag layout: 15 * 4 (slots) + 2 (size) + 1 (count) + 1 (pad) + CAPACITY
    // Default (256):  64 + 256 = 320 bytes
    // Large (512):    64 + 512 = 576 bytes
    #[cfg(not(feature = "small-suffix-capacity"))]
    assert_eq!(StdMem::size_of::<InlineSuffixBag>(), 576);

    #[cfg(feature = "small-suffix-capacity")]
    assert_eq!(StdMem::size_of::<InlineSuffixBag>(), 320);
}

// ============================================================================
//  Tests for suffix_count maintenance
// ============================================================================

#[test]
fn test_suffix_count_assign_clear() {
    let mut bag: SuffixBag = SuffixBag::new();
    assert_eq!(bag.count(), 0);

    bag.assign(0, b"hello");
    assert_eq!(bag.count(), 1);

    bag.assign(1, b"world");
    assert_eq!(bag.count(), 2);

    // Reassign to same slot, count should stay unchanged
    bag.assign(0, b"hi");
    assert_eq!(bag.count(), 2);

    bag.clear(0);
    assert_eq!(bag.count(), 1);

    bag.clear(1);
    assert_eq!(bag.count(), 0);

    // Clear already empty slot - count stays 0
    bag.clear(0);
    assert_eq!(bag.count(), 0);
}

#[test]
fn test_suffix_count_try_assign_in_place() {
    let mut bag: SuffixBag = SuffixBag::new();
    assert_eq!(bag.count(), 0);

    assert!(bag.try_assign_in_place(0, b"hello"));
    assert_eq!(bag.count(), 1);

    assert!(bag.try_assign_in_place(1, b"world"));
    assert_eq!(bag.count(), 2);

    // Reassign to same slot (shorter suffix reuses space) - count unchanged
    assert!(bag.try_assign_in_place(0, b"hi"));
    assert_eq!(bag.count(), 2);

    // Reassign to same slot (longer suffix appends) - count unchanged
    assert!(bag.try_assign_in_place(1, b"hello world"));
    assert_eq!(bag.count(), 2);
}

#[test]
fn test_suffix_count_after_compact() {
    let mut bag: SuffixBag = SuffixBag::new();
    bag.assign(0, b"hello");
    bag.assign(1, b"world");
    bag.assign(2, b"test");
    assert_eq!(bag.count(), 3);

    bag.clear(1);
    assert_eq!(bag.count(), 2);

    // Compact keeping only slots 0 and 2
    bag.compact([0, 2].into_iter());
    assert_eq!(bag.count(), 2);

    // Compact keeping only slot 0
    bag.compact(StdIter::once(0));
    assert_eq!(bag.count(), 1);

    // Compact with empty iterator
    bag.compact(StdIter::empty());
    assert_eq!(bag.count(), 0);
}

#[test]
fn test_suffix_count_clone() {
    let mut bag: SuffixBag = SuffixBag::new();
    bag.assign(0, b"hello");
    bag.assign(1, b"world");
    bag.clear(1); // Clear but don't compact - garbage in data

    let cloned = bag.clone();
    assert_eq!(cloned.count(), 1);
    assert_eq!(cloned.get(0), Some(b"hello".as_slice()));
    assert_eq!(cloned.get(1), None);

    // Clone should have compacted - used bytes should be minimal
    assert_eq!(cloned.used(), 5); // Only "hello"
}

#[test]
fn test_inline_suffix_count_try_assign() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();
    assert_eq!(bag.count(), 0);

    assert!(bag.try_assign(0, b"hello"));
    assert_eq!(bag.count(), 1);

    assert!(bag.try_assign(1, b"world"));
    assert_eq!(bag.count(), 2);

    // Reassign shorter suffix (reuses space) - count unchanged
    assert!(bag.try_assign(0, b"hi"));
    assert_eq!(bag.count(), 2);
}

#[test]
fn test_inline_suffix_count_clear() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    bag.try_assign(0, b"hello");
    bag.try_assign(1, b"world");
    assert_eq!(bag.count(), 2);

    bag.clear(0);
    assert_eq!(bag.count(), 1);

    bag.clear(1);
    assert_eq!(bag.count(), 0);

    // Clear already empty slot
    bag.clear(0);
    assert_eq!(bag.count(), 0);
}

#[test]
fn test_inline_suffix_count_clear_all() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    bag.try_assign(0, b"hello");
    bag.try_assign(1, b"world");
    assert_eq!(bag.count(), 2);

    bag.clear_all();
    assert_eq!(bag.count(), 0);
}

// ============================================================================
//  Tests for try_assign with compaction
// ============================================================================

#[test]
fn test_try_assign_compacts_before_grow() {
    // Create a bag with INITIAL_CAPACITY to avoid capacity changes from compaction
    let mut bag: SuffixBag = SuffixBag::with_capacity(INITIAL_CAPACITY);

    // Fill most of it
    for i in 0..12 {
        bag.assign(i, b"1234567890"); // 10 bytes each = 120 bytes
    }

    // Clear half the slots, creating 60 bytes of garbage
    for i in (0..12).step_by(2) {
        bag.clear(i);
    }
    // Now: 60 bytes active (slots 1,3,5,7,9,11), 60 bytes garbage

    let old_used: usize = bag.used();
    assert_eq!(old_used, 120); // All data still in buffer

    // Try to assign - should compact first (reclaiming 60 bytes)
    // Returns false if buffer grew (which it shouldn't since compaction freed space)
    let _did_not_grow: bool = bag.try_assign(12, b"newdata!!!");

    // After compaction + new assignment: 60 + 10 = 70 bytes
    assert!(bag.used() < old_used); // Compaction happened
    assert_eq!(bag.count(), 7); // slots 1,3,5,7,9,11,12

    // Verify data integrity
    assert_eq!(bag.get(12), Some(b"newdata!!!".as_slice()));
    assert_eq!(bag.get(1), Some(b"1234567890".as_slice()));
}

#[test]
fn test_try_assign_grows_when_compact_insufficient() {
    let mut bag: SuffixBag = SuffixBag::with_capacity(16);

    // Fill completely with no garbage
    bag.assign(0, b"12345678"); // 8 bytes
    bag.assign(1, b"12345678"); // 8 bytes = 16 total, no garbage

    let old_capacity = bag.capacity();

    // Try to assign - compaction won't help, must grow
    // Returns false because buffer grew
    let did_grow: bool = !bag.try_assign(2, b"newdata!");
    assert!(did_grow);

    // Capacity should have grown
    assert!(bag.capacity() > old_capacity);
    assert_eq!(bag.count(), 3);
}

// ============================================================================
//  Tests for compacting clone
// ============================================================================

#[test]
fn test_clone_compacts_garbage() {
    let mut bag: SuffixBag = SuffixBag::new();

    // Add and remove several suffixes to create garbage
    bag.assign(0, b"aaaaaaaaaa"); // 10 bytes
    bag.assign(1, b"bbbbbbbbbb"); // 10 bytes
    bag.assign(2, b"cccccccccc"); // 10 bytes
    bag.assign(0, b"ddddd"); // 5 bytes (old 10 bytes now garbage)
    bag.clear(1); // 10 more bytes of garbage

    // Original has garbage
    let original_used: usize = bag.used();
    assert!(original_used >= 35); // At least all the data written

    let cloned: SuffixBag = bag.clone();

    // Clone should only have active data
    assert_eq!(cloned.used(), 15); // 5 (slot 0) + 10 (slot 2)
    assert_eq!(cloned.count(), 2);
    assert_eq!(cloned.get(0), Some(b"ddddd".as_slice()));
    assert_eq!(cloned.get(1), None);
    assert_eq!(cloned.get(2), Some(b"cccccccccc".as_slice()));
}

#[test]
fn test_clone_empty_bag() {
    let bag: SuffixBag = SuffixBag::new();
    let cloned: SuffixBag = bag;

    assert_eq!(cloned.count(), 0);
    assert_eq!(cloned.used(), 0);
}

#[test]
fn test_compact_dedup_duplicates() {
    let mut bag: SuffixBag = SuffixBag::new();

    bag.assign(0, b"aaaa");
    bag.assign(1, b"bbbb");
    bag.assign(2, b"cccc");
    assert_eq!(bag.count(), 3);

    // Includes duplicates; `compact()` should deduplicate.
    let reclaimed: usize = bag.compact([0, 0, 2, 2].into_iter());

    assert!(reclaimed > 0);
    assert_eq!(bag.count(), 2);
    assert_eq!(bag.get(0), Some(b"aaaa".as_slice()));
    assert_eq!(bag.get(1), None);
    assert_eq!(bag.get(2), Some(b"cccc".as_slice()));
}

#[test]
fn test_inline_drain_to_external_with_vec_reserves_enough() {
    let bag: InlineSuffixBag = InlineSuffixBag::new();

    // Fill a few slots.
    assert!(bag.try_assign(0, b"suffix0"));
    assert!(bag.try_assign(1, b"suffix1_long"));
    assert!(bag.try_assign(2, b"suffix2"));

    let perm: Permuter15 = Permuter15::make_sorted(3);

    let new_slot: usize = 3;
    let new_suffix: &[u8] = b"new_suffix";

    // Required capacity = sum(existing) + new.
    let required_capacity: usize =
        b"suffix0".len() + b"suffix1_long".len() + b"suffix2".len() + new_suffix.len();

    // Deliberately pick a non-zero but insufficient capacity to catch the
    // "reserve vs capacity" bug via the debug_assert in the implementation.
    let buffer: Vec<u8> = Vec::with_capacity(16);

    let external: SuffixBag = bag.drain_to_external_with_vec(&perm, new_slot, new_suffix, buffer);

    assert!(external.capacity() >= required_capacity);
    assert_eq!(external.get(0), Some(b"suffix0".as_slice()));
    assert_eq!(external.get(1), Some(b"suffix1_long".as_slice()));
    assert_eq!(external.get(2), Some(b"suffix2".as_slice()));
    assert_eq!(external.get(3), Some(new_suffix));
}

// ============================================================================
//  SuffixSidecar Tests (Miri-friendly - no iterations, tests Drop correctness)
// ============================================================================

/// Test sidecar creation and drop with only inline suffixes.
/// Miri will detect any memory leaks or invalid drops.
#[test]
fn test_sidecar_drop_inline_only() {
    let sidecar: SuffixSidecar = SuffixSidecar::new();

    // Add a few inline suffixes
    sidecar.inline.try_assign(0, b"hello");
    sidecar.inline.try_assign(1, b"world");
    sidecar.inline.try_assign(2, b"test");

    assert!(sidecar.has_suffix(0));
    assert!(sidecar.has_suffix(1));
    assert!(sidecar.has_suffix(2));
    assert!(!sidecar.has_suffix(3));

    // Verify data before drop
    assert_eq!(sidecar.get(0), Some(b"hello".as_slice()));
    assert_eq!(sidecar.get(1), Some(b"world".as_slice()));

    // Drop happens here - Miri verifies no leaks
}

/// Test sidecar creation and drop with external overflow allocation.
/// This exercises the Drop implementation that frees the external `SuffixBagCell`.
/// Miri will detect any memory leaks or double-frees.
#[test]
fn test_sidecar_drop_with_external() {
    let sidecar: SuffixSidecar = SuffixSidecar::new();

    // Force external allocation (infallible - aborts on OOM)
    // SAFETY: Test-only, no concurrent access
    let external_bag = unsafe { sidecar.ensure_external() };

    // Write to external bag (we have exclusive access in this test)
    external_bag.assign(0, b"external_suffix_0");
    external_bag.assign(1, b"external_suffix_1");

    // Verify external is allocated
    assert!(
        !sidecar
            .external
            .load(std::sync::atomic::Ordering::Relaxed)
            .is_null()
    );

    // Verify we can read from external
    assert_eq!(sidecar.get(0), Some(b"external_suffix_0".as_slice()));
    assert_eq!(sidecar.get(1), Some(b"external_suffix_1".as_slice()));

    // Drop happens here - Miri verifies external bag is freed without leaks
}

/// Test sidecar with both inline and external data.
/// Exercises the full Drop path.
#[test]
fn test_sidecar_drop_mixed_inline_external() {
    let sidecar: SuffixSidecar = SuffixSidecar::new();

    // Add inline suffixes
    sidecar.inline.try_assign(0, b"inline_0");
    sidecar.inline.try_assign(1, b"inline_1");

    // Force external allocation (infallible - aborts on OOM)
    // SAFETY: Test-only, no concurrent access
    let external_bag = unsafe { sidecar.ensure_external() };

    // Write to external bag for different slots (we have exclusive access)
    external_bag.assign(5, b"external_5");
    external_bag.assign(10, b"external_10");

    // Verify inline takes precedence for slots 0, 1
    assert_eq!(sidecar.get(0), Some(b"inline_0".as_slice()));
    assert_eq!(sidecar.get(1), Some(b"inline_1".as_slice()));

    // Verify external is used for slots 5, 10
    assert_eq!(sidecar.get(5), Some(b"external_5".as_slice()));
    assert_eq!(sidecar.get(10), Some(b"external_10".as_slice()));

    // Drop happens here - Miri verifies both inline and external cleanup
}

/// Test sidecar default and new are equivalent.
#[test]
fn test_sidecar_default() {
    let s1: SuffixSidecar = SuffixSidecar::new();
    let s2: SuffixSidecar = SuffixSidecar::default();

    assert!(
        s1.external
            .load(std::sync::atomic::Ordering::Relaxed)
            .is_null()
    );
    assert!(
        s2.external
            .load(std::sync::atomic::Ordering::Relaxed)
            .is_null()
    );
    assert_eq!(s1.inline.count(), 0);
    assert_eq!(s2.inline.count(), 0);
}

/// Test `ensure_external` is idempotent (returns same backing allocation).
#[test]
fn test_sidecar_ensure_external_idempotent() {
    let sidecar: SuffixSidecar = SuffixSidecar::new();

    // SAFETY: Test-only, no concurrent access. Allocation is infallible.
    let ptr1: *const SuffixBag = unsafe { sidecar.ensure_external() };
    let ptr2: *const SuffixBag = unsafe { sidecar.ensure_external() };
    let ptr3: *const SuffixBag = unsafe { sidecar.ensure_external() };

    // All calls should return references into the same allocation
    assert_eq!(ptr1, ptr2);
    assert_eq!(ptr2, ptr3);
    assert!(!ptr1.is_null());

    // Drop frees exactly one allocation - Miri verifies no double-free
}