structured-zstd 0.0.54

Pure-Rust Zstandard (zstd) compression and decompression: all levels, streaming, dictionaries, no_std and WebAssembly ready — no FFI, no cmake
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
use super::{
    FseTableMode, LastUsedTable, RawSequence, choose_table, emit_single_sequence_block,
    encode_literal_length, encode_match_len, encode_offset_with_history, min_gain,
    min_literals_to_compress, previous_table, remember_last_used_tables,
};
use crate::encoding::frame_compressor::{CompressState, FseTables, PreviousFseTable};
use crate::encoding::strategy::StrategyTag;
use crate::fse::fse_encoder::build_table_from_symbol_counts;
use crate::huff0::huff0_encoder;
use alloc::vec::Vec;

fn tables_match(
    lhs: &crate::fse::fse_encoder::FSETable,
    rhs: &crate::fse::fse_encoder::FSETable,
) -> bool {
    lhs.table_size == rhs.table_size
        && (0..=255u8)
            .all(|symbol| lhs.symbol_probability(symbol) == rhs.symbol_probability(symbol))
}

#[test]
fn repeat_offset_codes_follow_rfc_mapping() {
    let mut hist = [10, 20, 30];
    assert_eq!(encode_offset_with_history(10, 5, &mut hist), 1);
    assert_eq!(hist, [10, 20, 30]);

    let mut hist = [10, 20, 30];
    assert_eq!(encode_offset_with_history(20, 5, &mut hist), 2);
    assert_eq!(hist, [20, 10, 30]);

    let mut hist = [10, 20, 30];
    assert_eq!(encode_offset_with_history(30, 5, &mut hist), 3);
    assert_eq!(hist, [30, 10, 20]);

    let mut hist = [10, 20, 30];
    assert_eq!(encode_offset_with_history(20, 0, &mut hist), 1);
    assert_eq!(hist, [20, 10, 30]);

    let mut hist = [10, 20, 30];
    assert_eq!(encode_offset_with_history(30, 0, &mut hist), 2);
    assert_eq!(hist, [30, 10, 20]);

    let mut hist = [10, 20, 30];
    assert_eq!(encode_offset_with_history(9, 0, &mut hist), 3);
    assert_eq!(hist, [9, 10, 20]);
}

#[test]
fn min_literals_to_compress_returns_per_strategy_floor() {
    for strat in [
        StrategyTag::Fast,
        StrategyTag::Dfast,
        StrategyTag::Greedy,
        StrategyTag::Lazy,
        StrategyTag::Btlazy2,
    ] {
        assert_eq!(min_literals_to_compress(strat, false), 64);
        assert_eq!(min_literals_to_compress(strat, true), 6);
    }
    assert_eq!(min_literals_to_compress(StrategyTag::BtOpt, false), 32);
    assert_eq!(min_literals_to_compress(StrategyTag::BtOpt, true), 6);
    assert_eq!(min_literals_to_compress(StrategyTag::BtUltra, false), 16);
    assert_eq!(min_literals_to_compress(StrategyTag::BtUltra, true), 6);
    assert_eq!(min_literals_to_compress(StrategyTag::BtUltra2, false), 8);
    assert_eq!(min_literals_to_compress(StrategyTag::BtUltra2, true), 6);
}

#[test]
fn min_gain_returns_per_strategy_margin() {
    let src = 4096usize;
    for strat in [
        StrategyTag::Fast,
        StrategyTag::Dfast,
        StrategyTag::Greedy,
        StrategyTag::Lazy,
        StrategyTag::Btlazy2,
        StrategyTag::BtOpt,
    ] {
        assert_eq!(min_gain(src, strat), (src >> 6) + 2);
    }
    assert_eq!(min_gain(src, StrategyTag::BtUltra), (src >> 7) + 2);
    assert_eq!(min_gain(src, StrategyTag::BtUltra2), (src >> 8) + 2);
    assert_eq!(min_gain(0, StrategyTag::Fast), 2);
    assert_eq!(min_gain(63, StrategyTag::Fast), 2);
    assert_eq!(min_gain(64, StrategyTag::Fast), 3);
}

#[test]
fn use_raw_literal_fallback_uses_payload_vs_srcsize_threshold() {
    use super::{compressed_literals_header_bytes, use_raw_literal_fallback};
    // Upstream zstd formula: `huf_section_size >= literals_len - min_gain`,
    // payload-vs-srcSize (no headers on either side). Verify the
    // gate is symmetric in header overhead by hitting the boundary
    // where the old on-wire `total >= raw_section - mg` form would
    // have disagreed.
    let strategy = StrategyTag::Fast; // min_gain(20, Fast) = (20>>6)+2 = 2

    // literals_len = 20: raw_header = 1, compressed_header = 3.
    // New threshold (payload-vs-srcSize):
    //   keep huf iff huf_section_size <  20 - 2 = 18
    //   fallback   iff huf_section_size >= 18
    //
    // Old (regressed) threshold on-wire-vs-on-wire:
    //   total = huf_section_size + 3
    //   fallback iff total >= (20 + 1) - 2 = 19
    //                iff huf_section_size >= 16
    //
    // Gap where formulas disagree: huf_section_size in [16, 18).
    // The new formula MUST keep huf in this gap.
    let literals_len = 20usize;
    // Sanity-check the literals-header constants the math relies on.
    assert_eq!(compressed_literals_header_bytes(literals_len), 3);
    assert_eq!(super::uncompressed_literals_header_bytes(literals_len), 1);

    // Inside the gap — new keeps, old would have rejected:
    assert!(!use_raw_literal_fallback(16, literals_len, strategy));
    assert!(!use_raw_literal_fallback(17, literals_len, strategy));

    // At/above new threshold — both new and old fall back:
    assert!(use_raw_literal_fallback(18, literals_len, strategy));
    assert!(use_raw_literal_fallback(19, literals_len, strategy));

    // Below the old threshold — both keep:
    assert!(!use_raw_literal_fallback(15, literals_len, strategy));
    assert!(!use_raw_literal_fallback(0, literals_len, strategy));
}

#[test]
fn prefer_repeat_eligible_applies_gate() {
    use super::prefer_repeat_eligible;
    // Upstream zstd `zstd_compress_literals.c:165`:
    //   strategy < ZSTD_lazy && srcSize <= 1024 -> HUF_flags_preferRepeat
    // ZSTD_lazy == 4 in upstream zstd enum; our `< Lazy` set is
    // {Fast, Dfast, Greedy}. Verify the gate fires for each
    // and stays off for the rest.
    for lit_len in [0usize, 1, 64, 256, 1024] {
        assert!(
            prefer_repeat_eligible(StrategyTag::Fast, lit_len),
            "Fast/{lit_len}"
        );
        assert!(
            prefer_repeat_eligible(StrategyTag::Dfast, lit_len),
            "Dfast/{lit_len}"
        );
        assert!(
            prefer_repeat_eligible(StrategyTag::Greedy, lit_len),
            "Greedy/{lit_len}"
        );
        assert!(
            !prefer_repeat_eligible(StrategyTag::Lazy, lit_len),
            "Lazy/{lit_len}"
        );
        assert!(
            !prefer_repeat_eligible(StrategyTag::Btlazy2, lit_len),
            "Btlazy2/{lit_len}"
        );
        assert!(
            !prefer_repeat_eligible(StrategyTag::BtOpt, lit_len),
            "BtOpt/{lit_len}"
        );
        assert!(
            !prefer_repeat_eligible(StrategyTag::BtUltra, lit_len),
            "BtUltra/{lit_len}"
        );
        assert!(
            !prefer_repeat_eligible(StrategyTag::BtUltra2, lit_len),
            "BtUltra2/{lit_len}"
        );
    }
    // Above the 1024-byte size threshold the gate stays off
    // for ALL strategies (upstream zstd `srcSize <= 1024` is the
    // closed upper bound).
    for lit_len in [1025usize, 2048, 16384] {
        assert!(!prefer_repeat_eligible(StrategyTag::Fast, lit_len));
        assert!(!prefer_repeat_eligible(StrategyTag::Dfast, lit_len));
        assert!(!prefer_repeat_eligible(StrategyTag::Greedy, lit_len));
        assert!(!prefer_repeat_eligible(StrategyTag::Btlazy2, lit_len));
    }
}

#[test]
fn decide_huff_reuse_prefer_repeat_forces_reuse_for_fast_band() {
    use super::{decide_huff_reuse_like_encoder, huff0_encoder};
    // Fixture chosen so size-comparison heuristic and the
    // preferRepeat short-circuit DISAGREE: `prev` is built
    // from a broad uniform sweep so it can encode any byte;
    // the literals payload is heavily skewed (240 zeros + 16
    // outliers) so a freshly-built `new_tbl` would compress
    // strictly better than `prev`. Without preferRepeat, the
    // heuristic picks `new` (returns true); WITH preferRepeat
    // the fast-band gate forces reuse (returns false).
    // Removing the short-circuit flips Fast/Dfast/Greedy to
    // true and breaks this test — that's the regression gate.
    let prev_training: Vec<u8> = (0..1024u32).map(|i| (i % 256) as u8).collect();
    let prev = huff0_encoder::HuffmanTable::build_from_data(&prev_training);
    let mut skewed_literals: Vec<u8> = Vec::with_capacity(256);
    skewed_literals.extend(core::iter::repeat_n(0u8, 240));
    skewed_literals.extend((0..16u8).map(|i| 200 + i));
    let mut new_tbl = huff0_encoder::HuffmanTable::build_from_data(&skewed_literals);
    let new_desc = new_tbl
        .writeable_table_description_size()
        .expect("non-empty table emits a description");

    // The decision reads its sizes off the histogram of the very literals it
    // is deciding for, so build one per fixture here as the encoder does.
    let counts_of = |bytes: &[u8]| -> [usize; 256] {
        let mut counts = [0usize; 256];
        for &b in bytes {
            counts[b as usize] += 1;
        }
        counts
    };
    let skewed_counts = counts_of(&skewed_literals);

    // Distinguishing precondition: WITHOUT preferRepeat the
    // size comparison must prefer new (else the test isn't
    // exercising the override). Verify by running with a
    // strategy outside the eligible band: Lazy returns
    // true (=new) on this fixture.
    assert!(
        decide_huff_reuse_like_encoder(
            &new_tbl,
            Some(&prev),
            new_desc,
            &skewed_literals,
            &skewed_counts,
            StrategyTag::Lazy,
        ),
        "fixture precondition: size-comparison must prefer new for Lazy on skewed literals"
    );

    // Eligible fast band: preferRepeat forces reuse (=false)
    // despite size-comparison preferring new.
    for strategy in [StrategyTag::Fast, StrategyTag::Dfast, StrategyTag::Greedy] {
        assert!(
            !decide_huff_reuse_like_encoder(
                &new_tbl,
                Some(&prev),
                new_desc,
                &skewed_literals,
                &skewed_counts,
                strategy,
            ),
            "{strategy:?} <= 1024 must short-circuit to reuse despite size-comparison favouring new"
        );
    }

    // Above the 1024-byte threshold the gate stays off even
    // for Fast/Dfast/Greedy — falls through to the size
    // heuristic, which on this fixture prefers new.
    let mut big_skewed = skewed_literals.clone();
    big_skewed.extend(core::iter::repeat_n(0u8, 1024));
    assert!(
        big_skewed.len() > 1024,
        "fixture must exceed 1024 to disable preferRepeat"
    );
    assert!(
        decide_huff_reuse_like_encoder(
            &new_tbl,
            Some(&prev),
            new_desc,
            &big_skewed,
            &counts_of(&big_skewed),
            StrategyTag::Fast,
        ),
        "Fast at len > 1024 must NOT short-circuit (gate disabled), falls through to size heuristic"
    );
}

#[test]
fn estimator_literals_section_mirrors_emit_for_short_inputs() {
    use super::{
        CompressedBlockScratch, EntropyOnlyMatcher, EstimatorWorkspace, encode_block_parts,
        estimate_block_parts_size,
    };
    // For each strategy at boundary literal lengths around `min_lits`
    // and across the all-identical RLE pre-check (fires for any
    // non-empty all-identical input under every strategy/HUF state),
    // estimator's predicted size MUST equal the bytes the emitter
    // actually writes. Cases include: (a) fresh state with
    // `last_huff_table: None` covering the strategy-specific
    // `min_lits` band (8/16/32/64), (b) seeded HUF-reuse state at
    // the lowered floor of 6 — under the prior hardcoded `len >= 8`
    // gate 6/7-byte all-identical sections went raw, now they pass
    // `min_lits == 6` and the upstream zstd-parity HUF+`cLitSize==1` path
    // would route them to RLE; the pre-check shortcuts that path,
    // (c) sub-`min_lits` all-identical sections that take the
    // RLE pre-check regardless of strategy.
    type Inputs = &'static [(usize, bool)];
    let cases: &[(StrategyTag, bool, Inputs)] = &[
        // (strategy, seed_huff_reuse, [(len, all_identical)])
        (
            StrategyTag::Fast,
            false,
            &[
                (1, true), // sub-min_lits all-identical → RLE
                (5, true), // sub-min_lits all-identical → RLE
                (8, true),
                (8, false),
                (63, true),
                (63, false),
                (64, false),
            ],
        ),
        (
            StrategyTag::BtUltra2,
            false,
            &[(7, true), (7, false), (8, true), (8, false), (16, false)],
        ),
        (
            StrategyTag::BtOpt,
            false,
            &[(8, true), (31, true), (32, false)],
        ),
        // HUF reuse path: floor drops to 6. Under the prior
        // hardcoded `len >= 8` gate 6/7-byte sections went raw;
        // post-fix, all-identical 6/7-byte sections take the RLE
        // pre-check and stay byte-equivalent estimator-vs-emit
        // (upstream zstd parity would route them HUF→cLitSize==1→RLE).
        // Also exercise non-identical 6-byte raw fallback and
        // 16-byte HUF reuse path.
        (
            StrategyTag::Lazy,
            true,
            &[(6, true), (7, true), (6, false), (16, false)],
        ),
    ];

    for (strat, seed_huff, inputs) in cases {
        for (len, identical) in *inputs {
            let literals: Vec<u8> = if *identical {
                alloc::vec![0x5Au8; *len]
            } else {
                (0..*len as u8).collect()
            };
            // Seed both estimator and emit state with the same
            // synthetic HUF table when `seed_huff` is true so the
            // reuse path's `min_lits == 6` floor is exercised.
            // Counts from a varied byte sequence give a valid
            // (writeable) table that survives the `decide_huff_reuse`
            // decision when literals are large enough to consider it.
            let seed_table = if *seed_huff {
                let mut counts = [0usize; 256];
                for b in (0..=63u8).chain(64..=127u8) {
                    counts[b as usize] = 1;
                }
                Some(huff0_encoder::HuffmanTable::build_from_counts(&counts))
            } else {
                None
            };
            let mut est_state = CompressState::<EntropyOnlyMatcher> {
                matcher: EntropyOnlyMatcher,
                copy_tier: crate::decoding::simd_copy::ExactCopyTier::resolve(),
                last_huff_table: seed_table.clone(),
                huff_table_spare: None,
                huff_rollback: None,
                huff_weights: Default::default(),
                seen_content: Default::default(),
                fse_tables: FseTables::new(),
                block_scratch: CompressedBlockScratch::new(),
                offset_hist: [1, 4, 8],
                strategy_tag: *strat,
                pre_split: None,
                huf_optimal_search: true,
                literal_compression_disabled: false,
            };
            let mut emit_state = CompressState::<EntropyOnlyMatcher> {
                matcher: EntropyOnlyMatcher,
                copy_tier: crate::decoding::simd_copy::ExactCopyTier::resolve(),
                last_huff_table: seed_table,
                huff_table_spare: None,
                huff_rollback: None,
                huff_weights: Default::default(),
                seen_content: Default::default(),
                fse_tables: FseTables::new(),
                block_scratch: CompressedBlockScratch::new(),
                offset_hist: [1, 4, 8],
                strategy_tag: *strat,
                pre_split: None,
                huf_optimal_search: true,
                literal_compression_disabled: false,
            };
            let mut workspace = EstimatorWorkspace::default();
            let est = estimate_block_parts_size(&mut est_state, &literals, &[], &mut workspace);
            let mut emitted: Vec<u8> = Vec::new();
            encode_block_parts(
                &mut emit_state,
                &literals,
                &mut [],
                &mut Vec::new(),
                &mut emitted,
            );
            assert_eq!(
                est,
                emitted.len(),
                "estimator/emit parity broken: strategy={:?} seed_huff={} len={} identical={} est={} emit={}",
                strat,
                seed_huff,
                len,
                identical,
                est,
                emitted.len(),
            );
        }
    }
}

#[test]
fn a_section_with_flat_ends_costs_what_the_emitter_writes_for_it() {
    use super::{
        CompressedBlockScratch, EntropyOnlyMatcher, EstimatorWorkspace, encode_block_parts,
        estimate_block_parts_size,
    };
    // The shape the end-sample shortcut exists for, and the one where the
    // estimator and the emitter can disagree: both ends look random, the
    // interior is heavily biased. A histogram of the whole section says
    // "compresses well"; the two 4 KiB end samples say "flat". The emitter
    // trusts the samples and writes a raw section, so the estimator has to
    // reach the same decision at the same point in the branch order. If it
    // prices this as Huffman-compressed instead, the splitter picks a
    // partition on a price nothing can produce.
    //
    // Sized past the shortcut's own floor (it declines to sample sections
    // smaller than ratio x sample), with each end cycling all 256 values so
    // the largest per-end count is far under the flatness bound.
    let mut literals: Vec<u8> = (0..4096u32).map(|i| (i % 256) as u8).collect();
    literals.extend(core::iter::repeat_n(0u8, 40_000));
    literals.extend((0..4096u32).map(|i| (i % 256) as u8));

    let make_state = || CompressState::<EntropyOnlyMatcher> {
        matcher: EntropyOnlyMatcher,
        copy_tier: crate::decoding::simd_copy::ExactCopyTier::resolve(),
        last_huff_table: None,
        huff_table_spare: None,
        huff_rollback: None,
        huff_weights: Default::default(),
        seen_content: Default::default(),
        fse_tables: FseTables::new(),
        block_scratch: CompressedBlockScratch::new(),
        offset_hist: [1, 4, 8],
        strategy_tag: StrategyTag::Lazy,
        pre_split: None,
        huf_optimal_search: true,
        literal_compression_disabled: false,
    };
    let mut est_state = make_state();
    let mut emit_state = make_state();

    let mut workspace = EstimatorWorkspace::default();
    let est = estimate_block_parts_size(&mut est_state, &literals, &[], &mut workspace);
    let mut emitted: Vec<u8> = Vec::new();
    encode_block_parts(
        &mut emit_state,
        &literals,
        &mut [],
        &mut Vec::new(),
        &mut emitted,
    );

    assert_eq!(
        est,
        emitted.len(),
        "estimator priced a flat-ended section at {est} bytes, emitter wrote {}",
        emitted.len(),
    );
    // Pin that this really is the raw outcome, not parity reached by both
    // sides compressing: a shortcut that stopped firing on both would keep
    // the equality above while losing what the test is about.
    assert!(
        emitted.len() >= literals.len(),
        "expected a raw literals section, got {} bytes for {} literals",
        emitted.len(),
        literals.len(),
    );
    // The shortcut clears any carried table on both sides.
    assert!(est_state.last_huff_table.is_none());
    assert!(emit_state.last_huff_table.is_none());
}

#[test]
fn encode_match_len_uses_correct_upper_range_base() {
    assert_eq!(encode_match_len(65539), (52, 0, 16));
    assert_eq!(encode_match_len(65540), (52, 1, 16));
    assert_eq!(encode_match_len(131074), (52, 65535, 16));
}

/// The scratch is taken and put back around a block, so every buffer it keeps
/// is retained allocation a context reports through `ZSTD_sizeof_CCtx`. The
/// per-sequence code buffer is one of them, and a caller budgeting memory sees
/// whatever this sum leaves out.
#[test]
fn retained_heap_size_counts_the_sequence_code_buffer() {
    let mut scratch = super::CompressedBlockScratch::new();
    let before = scratch.retained_heap_size();
    let codes = 1024;
    scratch.sequence_codes.reserve_exact(codes);
    let after = scratch.retained_heap_size();
    assert!(
        after - before >= codes * core::mem::size_of::<u32>(),
        "reserving {codes} sequence codes grew the reported retained size by only {} bytes",
        after - before,
    );
}

/// The estimator prices a block the splitter is thinking about; the emitter
/// writes the one it chose. They walk the sequences by different routes: the
/// emitter derives each offset code, packs it with the extra-bit widths and
/// runs the FSE writer, while the estimator counts from a scratch history and
/// prices the streams from a per-symbol cost model. The parity tests above use
/// empty sequence arrays; this one carries sequences through both.
///
/// The two agree EXACTLY on the literals section, whose Huffman code lengths
/// are known per symbol. They cannot on the sequences section, and are not
/// meant to: the FSE cost model prices a symbol at its average width from the
/// normalised probability (upstream does the same in `ZSTD_fseBitCost`), where
/// the writer pays what the state trajectory actually costs. What must hold is
/// that the two stay within that model's rounding of each other, and that they
/// walked the same sequences — a wrong offset code on either side moves the
/// histogram, the table choice and the price by far more than rounding.
#[test]
fn estimator_and_emitter_agree_on_a_block_with_sequences() {
    use super::{
        CompressedBlockScratch, EntropyOnlyMatcher, EstimatorWorkspace, encode_block_parts,
        estimate_block_parts_size,
    };
    // Enough sequences for the section to carry real FSE tables rather than
    // the degenerate single-symbol shapes, with repeats among the offsets so
    // the repeat-offset codes are exercised beside the explicit ones.
    let literals: Vec<u8> = (0..512u32).map(|i| (i % 251) as u8).collect();
    let sequences: Vec<RawSequence> = (0..64u32)
        .map(|i| RawSequence {
            ll: i % 8,
            ml: 4 + i % 13,
            off_base: 1 + (i % 5) * 7,
        })
        .collect();

    for strat in [StrategyTag::Fast, StrategyTag::Lazy, StrategyTag::BtUltra2] {
        let make_state = || CompressState::<EntropyOnlyMatcher> {
            matcher: EntropyOnlyMatcher,
            copy_tier: crate::decoding::simd_copy::ExactCopyTier::resolve(),
            last_huff_table: None,
            huff_table_spare: None,
            huff_rollback: None,
            huff_weights: Default::default(),
            seen_content: Default::default(),
            fse_tables: FseTables::new(),
            block_scratch: CompressedBlockScratch::new(),
            offset_hist: [1, 4, 8],
            strategy_tag: strat,
            pre_split: None,
            huf_optimal_search: true,
            literal_compression_disabled: false,
        };
        let mut est_state = make_state();
        let mut emit_state = make_state();

        let mut workspace = EstimatorWorkspace::default();
        let est = estimate_block_parts_size(&mut est_state, &literals, &sequences, &mut workspace);

        let mut emitted: Vec<u8> = Vec::new();
        let mut to_emit = sequences.clone();
        encode_block_parts(
            &mut emit_state,
            &literals,
            &mut to_emit,
            &mut Vec::new(),
            &mut emitted,
        );

        // Both sides must have advanced the repeat-offset history the same
        // way. This is the assertion that catches a wrong offset code: the
        // byte counts could coincide, the histories cannot.
        assert_eq!(
            est_state.offset_hist, emit_state.offset_hist,
            "estimator and emitter disagree on the repeat-offset history at {strat:?}",
        );
        // The block really did carry a sequence section — otherwise the rest
        // of this test would be passing on the literals path alone.
        assert!(emitted.len() > literals.len() / 2);
        // One byte per sixteen sequences plus four, against a model that
        // rounds each symbol's width down from a 256-scale log table. The
        // measured divergence for this fixture is two bytes on every strategy;
        // the bound leaves room for the rounding to accumulate without letting
        // a real divergence through, which moves the price by tens of bytes.
        let allowed = 4 + sequences.len() / 16;
        let diff = est.abs_diff(emitted.len());
        assert!(
            diff <= allowed,
            "estimator priced {est} bytes against {} emitted for {} sequences at {strat:?}: \
             off by {diff}, more than the {allowed} the cost model can round away",
            emitted.len(),
            sequences.len(),
        );
    }
}

#[test]
fn raw_partition_fallback_restores_repeat_offset_history() {
    let mut state = CompressState {
        matcher: super::EntropyOnlyMatcher,
        copy_tier: crate::decoding::simd_copy::ExactCopyTier::resolve(),
        last_huff_table: None,
        huff_table_spare: None,
        huff_rollback: None,
        huff_weights: Default::default(),
        seen_content: Default::default(),
        fse_tables: FseTables::new(),
        block_scratch: super::CompressedBlockScratch::new(),
        offset_hist: [10, 20, 30],
        strategy_tag: crate::encoding::strategy::StrategyTag::Fast,
        pre_split: None,
        huf_optimal_search: true,
        literal_compression_disabled: false,
    };
    let source = [0xA5; 8];
    let mut sequences = [RawSequence {
        ll: 0,
        ml: 5,
        off_base: 20,
    }];
    let mut output = Vec::new();
    let mut compressed_scratch = Vec::new();

    let mut code_scratch = Vec::new();
    let mut emit_buffers = super::SingleSequenceEmitBuffers {
        output: &mut output,
        compressed: &mut compressed_scratch,
        codes: &mut code_scratch,
    };
    let emitted_raw = emit_single_sequence_block(
        &mut state,
        true,
        source.len(),
        &[],
        &mut sequences,
        &mut emit_buffers,
    );
    if emitted_raw {
        output.extend_from_slice(&source);
    }

    assert_eq!(
        state.offset_hist,
        [10, 20, 30],
        "raw post-split fallback must not advance decoder repeat-offset history"
    );
    assert_eq!(
        (output[0] >> 1) & 0b11,
        0,
        "fixture should force the partition to fall back to a Raw block"
    );
}

/// A block that goes predefined or RLE must park the custom table it replaces,
/// not drop it.
///
/// Distributions move between custom and predefined from block to block, and
/// dropping the handle on the way out means the next custom block builds into a
/// freshly allocated table. That is the per-block allocation the two-buffer
/// design removes, coming back through the one transition that does not go
/// through the swap.
#[test]
fn a_predefined_or_rle_block_parks_the_custom_table_it_replaces() {
    use crate::encoding::frame_compressor::SharedFseTable;
    use crate::fse::fse_encoder::FSETable;

    for decision in [LastUsedTable::Default, LastUsedTable::Rle(7)] {
        let mut previous = Some(PreviousFseTable::Custom(SharedFseTable::new(
            FSETable::blank(),
        )));
        let mut next = None;
        super::commit_last_used_table(&mut previous, &mut next, decision);
        assert!(
            next.is_some(),
            "{decision:?}: the displaced custom table is the next block's buffer",
        );
    }
}

/// A block that loses to a raw one must leave the axis exactly as it found
/// it: the restored table unique, and a buffer free to build into.
///
/// The caller holds a CLONE of what `previous` was, so restoring only that
/// leaves the same table in both slots — `previous` restored and `next` where
/// confirmation displaced it — and the next block finds its buffer shared and
/// allocates a fresh eleven-kilobyte table. On a run of blocks that keep
/// falling back to raw that is a per-axis allocation per block, which is the
/// cost this pair of slots exists to remove.
#[test]
fn a_block_that_falls_back_to_raw_leaves_its_axis_reusable() {
    use crate::encoding::frame_compressor::SharedFseTable;
    use crate::fse::fse_encoder::FSETable;

    let mut fse_tables = FseTables::new();
    let before = SharedFseTable::new(FSETable::blank());
    fse_tables.ll_previous = Some(PreviousFseTable::Custom(before));
    // What the caller keeps across the attempt.
    let saved = fse_tables.ll_previous.clone();

    // The attempt builds into the axis's spare slot, then confirms it, which
    // displaces the old table into that slot.
    fse_tables.ll_next = Some(SharedFseTable::new(FSETable::blank()));
    super::commit_last_used_table(
        &mut fse_tables.ll_previous,
        &mut fse_tables.ll_next,
        LastUsedTable::Encoded,
    );

    fse_tables.roll_back_confirmation([saved, None, None]);

    let Some(PreviousFseTable::Custom(restored)) = fse_tables.ll_previous.as_ref() else {
        panic!("the restored table must be the custom one the axis started with");
    };
    assert_eq!(
        SharedFseTable::strong_count(restored),
        1,
        "the restored table must be unique, or the next block cannot build into its slot",
    );
    assert!(
        fse_tables.ll_next.is_some(),
        "the discarded table is exactly the buffer the next block wants",
    );
}

/// A frame start replaces the previous slots, and the table that was there is
/// the next frame's buffer — dropping it costs a reused compressor an
/// allocation per axis on every frame that builds a custom table.
#[test]
fn a_frame_start_keeps_the_last_frames_table_as_a_buffer() {
    use crate::encoding::frame_compressor::SharedFseTable;
    use crate::fse::fse_encoder::FSETable;

    let mut fse_tables = FseTables::new();
    fse_tables.ll_previous = Some(PreviousFseTable::Custom(SharedFseTable::new(
        FSETable::blank(),
    )));
    // Shared with the dictionary entropy cache: parking it would hand the axis
    // a buffer it cannot build into.
    let shared = SharedFseTable::new(FSETable::blank());
    let _cache_holds_it = shared.clone();
    fse_tables.ml_previous = Some(PreviousFseTable::Custom(shared));

    fse_tables.park_previous_before_frame();

    assert!(
        fse_tables.ll_next.is_some(),
        "the frame's own table is what the next one builds into",
    );
    assert!(
        fse_tables.ml_next.is_none(),
        "a table the dictionary cache still holds is not a free buffer",
    );
}

#[test]
fn remember_last_used_tables_keeps_predefined_and_repeat_modes() {
    let mut fse_tables = FseTables::new();

    remember_last_used_tables(
        &mut fse_tables,
        [
            LastUsedTable::Default,
            LastUsedTable::Default,
            LastUsedTable::Default,
        ],
    );

    assert!(tables_match(
        previous_table(fse_tables.ll_previous.as_ref(), fse_tables.ll_default_ref()).unwrap(),
        fse_tables.ll_default_ref()
    ));
    assert!(tables_match(
        previous_table(fse_tables.ml_previous.as_ref(), fse_tables.ml_default_ref()).unwrap(),
        fse_tables.ml_default_ref()
    ));
    assert!(tables_match(
        previous_table(fse_tables.of_previous.as_ref(), fse_tables.of_default_ref()).unwrap(),
        fse_tables.of_default_ref()
    ));

    let sample_codes = [0u8, 1u8];
    // Lazy is a non-fast-band strategy, so this exercises the cost-based
    // repeat decision (not the fast-band shortcut).
    let strat = crate::encoding::strategy::StrategyTag::Lazy;
    // Slots for a table these calls are expected NOT to build; the assertions
    // below are that every axis repeats instead.
    let mut ll_slot = None;
    let mut ml_slot = None;
    let mut of_slot = None;
    let ll_repeat = choose_table(
        fse_tables.ll_previous.as_ref(),
        fse_tables.ll_default_ref(),
        sample_codes.iter().copied(),
        9,
        strat,
        &mut ll_slot,
    );
    let ml_repeat = choose_table(
        fse_tables.ml_previous.as_ref(),
        fse_tables.ml_default_ref(),
        sample_codes.iter().copied(),
        9,
        strat,
        &mut ml_slot,
    );
    let of_repeat = choose_table(
        fse_tables.of_previous.as_ref(),
        fse_tables.of_default_ref(),
        sample_codes.iter().copied(),
        8,
        strat,
        &mut of_slot,
    );

    assert!(matches!(ll_repeat, FseTableMode::RepeatLast(_)));
    assert!(matches!(ml_repeat, FseTableMode::RepeatLast(_)));
    assert!(matches!(of_repeat, FseTableMode::RepeatLast(_)));
}

/// Fast-band strategies (fast/dfast/greedy) reuse a covering previous FSE
/// table without building a new one (upstream zstd `preferRepeat`), even on a
/// fresh distribution where the cost-based path could pick a new table.
/// A non-fast-band strategy on an identical distribution takes the
/// cost-based path instead.
#[test]
fn fast_band_strategies_prefer_repeat_fse_table() {
    use crate::encoding::strategy::StrategyTag;
    let prev = build_table_from_symbol_counts(&[8, 1], 9, false);
    let previous =
        PreviousFseTable::Custom(crate::encoding::frame_compressor::SharedFseTable::new(prev));
    let fse_tables = FseTables::new();
    // Distribution over symbols {0,1}, both covered by `previous`.
    let mut counts = [0usize; 256];
    counts[0] = 4;
    counts[1] = 6;
    let total = 10;

    // All fast-band strategies (Fast, Dfast, Greedy) unconditionally
    // reuse the covering previous table; cover every eligible arm so an
    // enum-arm regression in the implementation branch is caught.
    for strategy in [StrategyTag::Fast, StrategyTag::Dfast, StrategyTag::Greedy] {
        // A slot the reuse path must leave alone.
        let mut slot = None;
        let mode = super::choose_table_from_counts(
            Some(&previous),
            fse_tables.ll_default_ref(),
            &mut counts,
            total,
            1, // highest non-zero code in {0,1}
            9,
            strategy,
            None,
            &mut slot,
        );
        assert!(
            matches!(mode, FseTableMode::RepeatLast(_)),
            "fast-band {strategy:?} must reuse the covering previous table",
        );
    }
}

#[test]
fn remember_last_used_tables_reuses_existing_custom_slot_for_repeat() {
    let mut fse_tables = FseTables::new();
    let custom = build_table_from_symbol_counts(&[1, 1], 5, false);
    fse_tables.ll_previous = Some(PreviousFseTable::Custom(
        crate::encoding::frame_compressor::SharedFseTable::new(custom),
    ));

    let before = core::ptr::from_ref(
        previous_table(fse_tables.ll_previous.as_ref(), fse_tables.ll_default_ref()).unwrap(),
    );

    remember_last_used_tables(
        &mut fse_tables,
        [
            LastUsedTable::Keep,
            LastUsedTable::Default,
            LastUsedTable::Default,
        ],
    );

    let after = core::ptr::from_ref(
        previous_table(fse_tables.ll_previous.as_ref(), fse_tables.ll_default_ref()).unwrap(),
    );

    assert_eq!(before, after);
    assert!(matches!(
        fse_tables.ll_previous.as_ref(),
        Some(PreviousFseTable::Custom(_))
    ));
}

#[test]
fn choose_table_handles_single_symbol_distribution() {
    let fse_tables = FseTables::new();
    let mut slot = None;
    let mode = choose_table(
        None,
        fse_tables.ll_default_ref(),
        core::iter::repeat_n(0u8, 32),
        9,
        crate::encoding::strategy::StrategyTag::Lazy,
        &mut slot,
    );
    assert!(matches!(mode, FseTableMode::Rle(0)));
}

/// The range-match form both length coders had before they became table
/// lookups, kept as an oracle. It spells out every code's baseline and width
/// explicitly, so it is the readable statement of the format and an
/// independent check that the tables and the extra-bit masking agree with it.
fn literal_length_ranges(len: u32) -> (u8, u32, usize) {
    match len {
        0..=15 => (len as u8, 0, 0),
        16..=17 => (16, len - 16, 1),
        18..=19 => (17, len - 18, 1),
        20..=21 => (18, len - 20, 1),
        22..=23 => (19, len - 22, 1),
        24..=27 => (20, len - 24, 2),
        28..=31 => (21, len - 28, 2),
        32..=39 => (22, len - 32, 3),
        40..=47 => (23, len - 40, 3),
        48..=63 => (24, len - 48, 4),
        64..=127 => (25, len - 64, 6),
        128..=255 => (26, len - 128, 7),
        256..=511 => (27, len - 256, 8),
        512..=1023 => (28, len - 512, 9),
        1024..=2047 => (29, len - 1024, 10),
        2048..=4095 => (30, len - 2048, 11),
        4096..=8191 => (31, len - 4096, 12),
        8192..=16383 => (32, len - 8192, 13),
        16384..=32767 => (33, len - 16384, 14),
        32768..=65535 => (34, len - 32768, 15),
        65536..=131071 => (35, len - 65536, 16),
        131072.. => unreachable!(),
    }
}

fn match_len_ranges(len: u32) -> (u8, u32, usize) {
    match len {
        0..=2 => unreachable!(),
        3..=34 => (len as u8 - 3, 0, 0),
        35..=36 => (32, len - 35, 1),
        37..=38 => (33, len - 37, 1),
        39..=40 => (34, len - 39, 1),
        41..=42 => (35, len - 41, 1),
        43..=46 => (36, len - 43, 2),
        47..=50 => (37, len - 47, 2),
        51..=58 => (38, len - 51, 3),
        59..=66 => (39, len - 59, 3),
        67..=82 => (40, len - 67, 4),
        83..=98 => (41, len - 83, 4),
        99..=130 => (42, len - 99, 5),
        131..=258 => (43, len - 131, 7),
        259..=514 => (44, len - 259, 8),
        515..=1026 => (45, len - 515, 9),
        1027..=2050 => (46, len - 1027, 10),
        2051..=4098 => (47, len - 2051, 11),
        4099..=8194 => (48, len - 4099, 12),
        8195..=16386 => (49, len - 8195, 13),
        16387..=32770 => (50, len - 16387, 14),
        32771..=65538 => (51, len - 32771, 15),
        65539..=131074 => (52, len - 65539, 16),
        131075.. => unreachable!(),
    }
}

#[test]
fn literal_length_coding_agrees_with_the_ranges_over_every_length() {
    for len in 0..131_072u32 {
        assert_eq!(
            encode_literal_length(len),
            literal_length_ranges(len),
            "literal length {len}",
        );
    }
}

#[test]
fn match_length_coding_agrees_with_the_ranges_over_every_length() {
    for len in 3..131_075u32 {
        assert_eq!(
            encode_match_len(len),
            match_len_ranges(len),
            "match length {len}"
        );
    }
}

#[test]
fn choose_table_without_previous_does_not_unwrap_none() {
    let only_zero_one_table = build_table_from_symbol_counts(&[1, 1], 5, false);
    let mut slot = None;
    let mode = choose_table(
        None,
        &only_zero_one_table,
        [1u8, 2].into_iter().cycle().take(32),
        5,
        crate::encoding::strategy::StrategyTag::Lazy,
        &mut slot,
    );
    assert!(matches!(mode, FseTableMode::Encoded(_)));
}