fastpfor 0.9.0

FastPFOR lib with C++ Rust wrapper and pure Rust implementation
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
use std::io::Cursor;

use bytemuck::{cast_slice, cast_slice_mut};

use crate::codec::AnyLenCodec;
use crate::helpers::AsUsize;
use crate::rust::cursor::IncrementCursor;
use crate::{FastPForError, FastPForResult};

/// Variable-byte encoding codec for integer compression.
#[derive(Debug)]
pub struct VariableByte;

// Helper functions with const generics for extracting 7-bit chunks
impl VariableByte {
    /// Extract 7 bits from position i (with masking)
    const fn extract_7bits<const I: u32>(val: u32) -> u8 {
        ((val >> (7 * I)) & ((1 << 7) - 1)) as u8
    }

    /// Extract 7 bits from position i (without masking, for last byte)
    const fn extract_7bits_maskless<const I: u32>(val: u32) -> u8 {
        (val >> (7 * I)) as u8
    }
}

// Implemented for consistency with other codecs
impl VariableByte {
    /// Creates a new instance
    #[must_use]
    pub fn new() -> VariableByte {
        VariableByte
    }

    /// Compress `input_length` u32 values from `input[input_offset..]` into
    /// `output[output_offset..]` as packed variable-byte u8 values (stored in
    /// u32 words, padded to 4-byte alignment with `0xFF`).
    #[allow(clippy::unnecessary_wraps)]
    fn compress_into_slice(
        input: &[u32],
        input_length: u32,
        input_offset: &mut Cursor<u32>,
        output: &mut [u32],
        output_offset: &mut Cursor<u32>,
    ) -> FastPForResult<()> {
        if input_length == 0 {
            return Ok(());
        }

        let output_start = output_offset.position() as usize;
        let output_bytes: &mut [u8] = &mut cast_slice_mut::<u32, u8>(output)[output_start * 4..];

        // Lemire format: last byte has high bit set (c >= 128 means end of value).
        let mut byte_pos = 0;
        for k in input_offset.position()..(input_offset.position() + u64::from(input_length)) {
            let val = input[k as usize];
            if val < (1 << 7) {
                output_bytes[byte_pos] = Self::extract_7bits::<0>(val) | (1 << 7);
                byte_pos += 1;
            } else if val < (1 << 14) {
                output_bytes[byte_pos] = Self::extract_7bits::<0>(val);
                output_bytes[byte_pos + 1] = Self::extract_7bits_maskless::<1>(val) | (1 << 7);
                byte_pos += 2;
            } else if val < (1 << 21) {
                output_bytes[byte_pos] = Self::extract_7bits::<0>(val);
                output_bytes[byte_pos + 1] = Self::extract_7bits::<1>(val);
                output_bytes[byte_pos + 2] = Self::extract_7bits_maskless::<2>(val) | (1 << 7);
                byte_pos += 3;
            } else if val < (1 << 28) {
                output_bytes[byte_pos] = Self::extract_7bits::<0>(val);
                output_bytes[byte_pos + 1] = Self::extract_7bits::<1>(val);
                output_bytes[byte_pos + 2] = Self::extract_7bits::<2>(val);
                output_bytes[byte_pos + 3] = Self::extract_7bits_maskless::<3>(val) | (1 << 7);
                byte_pos += 4;
            } else {
                output_bytes[byte_pos] = Self::extract_7bits::<0>(val);
                output_bytes[byte_pos + 1] = Self::extract_7bits::<1>(val);
                output_bytes[byte_pos + 2] = Self::extract_7bits::<2>(val);
                output_bytes[byte_pos + 3] = Self::extract_7bits::<3>(val);
                output_bytes[byte_pos + 4] = Self::extract_7bits_maskless::<4>(val) | (1 << 7);
                byte_pos += 5;
            }
        }

        // Pad to 4-byte alignment with 0 (lemire uses 0, not 0xFF)
        while byte_pos % 4 != 0 {
            output_bytes[byte_pos] = 0;
            byte_pos += 1;
        }

        output_offset.add(byte_pos as u32 / 4);
        input_offset.add(input_length);

        Ok(())
    }

    /// Decompress `input_length` u32 words of variable-byte data from
    /// `input[input_offset..]` into `output[output_offset..]`.
    fn decompress_from_u32_slice(
        input: &[u32],
        input_length: u32,
        input_offset: &mut Cursor<u32>,
        output: &mut [u32],
        output_offset: &mut Cursor<u32>,
    ) -> FastPForResult<()> {
        if input_length == 0 {
            return Ok(());
        }

        let byte_length = input_length.as_usize() * 4;
        let input_start = input_offset.position() as usize;

        let input_bytes: &[u8] =
            &cast_slice::<u32, u8>(input)[input_start * 4..input_start * 4 + byte_length];

        let mut byte_pos = 0;
        let mut tmp_outpos = output_offset.position() as usize;

        // Lemire format: high bit set (c >= 128) means last byte of value.
        // Fast path: process while we have at least 10 bytes remaining
        while byte_pos + 10 <= byte_length {
            let mut v: u32 = 0;
            let mut bytes_read = 0;

            for i in 0..5 {
                let c = input_bytes[byte_pos + i];

                if i < 4 {
                    v |= u32::from(c & 0x7F) << (i * 7);
                    if c >= 128 {
                        bytes_read = i + 1;
                        break;
                    }
                } else {
                    // For byte 4, use only 4 bits (total: 7*4 + 4 = 32 bits)
                    v |= u32::from(c & 0x0F) << 28;
                    bytes_read = 5;
                }
            }

            byte_pos += bytes_read;
            if tmp_outpos >= output.len() {
                return Err(FastPForError::OutputBufferTooSmall);
            }
            output[tmp_outpos] = v;
            tmp_outpos += 1;
        }

        // Slow path: process remaining bytes (lemire: c >= 128 = last byte)
        while byte_pos < byte_length {
            let mut v: u32 = 0;
            let mut decoded = false;
            for i in 0..5usize {
                if byte_pos >= byte_length {
                    break;
                }
                let c = input_bytes[byte_pos];
                byte_pos += 1;
                if i < 4 {
                    v |= u32::from(c & 0x7F) << (i * 7);
                    if c >= 128 {
                        decoded = true;
                        break;
                    }
                } else {
                    // 5th byte: only 4 bits contribute (7*4 bits already used)
                    v |= u32::from(c & 0x0F) << 28;
                    decoded = true;
                }
            }
            if decoded {
                if tmp_outpos >= output.len() {
                    return Err(FastPForError::OutputBufferTooSmall);
                }
                output[tmp_outpos] = v;
                tmp_outpos += 1;
            }
        }

        output_offset.set_position(tmp_outpos as u64);
        input_offset.add(input_length);

        Ok(())
    }

    /// Compress `input_length` u32 values into an `i8` slice using sign-bit
    /// continuation encoding (negative i8 = more bytes follow).
    #[cfg(test)]
    #[allow(clippy::unnecessary_wraps)]
    fn compress_to_i8_slice(
        input: &[u32],
        input_length: u32,
        input_offset: &mut Cursor<u32>,
        output: &mut [i8],
        output_offset: &mut Cursor<u32>,
    ) -> FastPForResult<()> {
        if input_length == 0 {
            return Ok(());
        }
        let mut out_pos_tmp = output_offset.position();
        for k in input_offset.position() as u32..(input_offset.position() as u32 + input_length) {
            let val = input[k.as_usize()];
            if val < (1 << 7) {
                output[out_pos_tmp as usize] = Self::extract_7bits::<0>(val) as i8;
                out_pos_tmp += 1;
            } else if val < (1 << 14) {
                output[out_pos_tmp as usize] = (Self::extract_7bits::<0>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = Self::extract_7bits_maskless::<1>(val) as i8;
                out_pos_tmp += 1;
            } else if val < (1 << 21) {
                output[out_pos_tmp as usize] = (Self::extract_7bits::<0>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = (Self::extract_7bits::<1>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = Self::extract_7bits_maskless::<2>(val) as i8;
                out_pos_tmp += 1;
            } else if val < (1 << 28) {
                output[out_pos_tmp as usize] = (Self::extract_7bits::<0>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = (Self::extract_7bits::<1>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = (Self::extract_7bits::<2>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = Self::extract_7bits_maskless::<3>(val) as i8;
                out_pos_tmp += 1;
            } else {
                output[out_pos_tmp as usize] = (Self::extract_7bits::<0>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = (Self::extract_7bits::<1>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = (Self::extract_7bits::<2>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = (Self::extract_7bits::<3>(val) | (1 << 7)) as i8;
                out_pos_tmp += 1;
                output[out_pos_tmp as usize] = Self::extract_7bits_maskless::<4>(val) as i8;
                out_pos_tmp += 1;
            }
        }
        output_offset.set_position(out_pos_tmp + u64::from(input_length));
        input_offset.add(input_length);
        Ok(())
    }

    /// Decompress `input_length` i8 values (sign-bit continuation encoding)
    /// into u32 output.
    #[cfg(test)]
    #[allow(clippy::unnecessary_wraps)]
    fn decompress_from_i8_slice(
        input: &[i8],
        input_length: u32,
        input_offset: &mut Cursor<u32>,
        output: &mut [u32],
        output_offset: &mut Cursor<u32>,
    ) -> FastPForResult<()> {
        let mut p = input_offset.position() as u32;
        let final_p = input_offset.position() as u32 + input_length;
        let mut tmp_outpos = output_offset.position();

        while p < final_p {
            let mut v = i32::from(input[p.as_usize()] & 0x7F);
            if input[p.as_usize()] >= 0 {
                p += 1;
                output[tmp_outpos as usize] = v as u32;
                tmp_outpos += 1;
                continue;
            }

            v |= i32::from(input[p.as_usize() + 1] & 0x7F) << 7;
            if input[p.as_usize() + 1] >= 0 {
                p += 2;
                output[tmp_outpos as usize] = v as u32;
                tmp_outpos += 1;
                continue;
            }

            v |= i32::from(input[p.as_usize() + 2] & 0x7F) << 14;
            if input[p.as_usize() + 2] >= 0 {
                p += 3;
                output[tmp_outpos as usize] = v as u32;
                tmp_outpos += 1;
                continue;
            }

            v |= i32::from(input[p.as_usize() + 3] & 0x7F) << 21;
            if input[p.as_usize() + 3] >= 0 {
                p += 4;
                output[tmp_outpos as usize] = v as u32;
                tmp_outpos += 1;
                continue;
            }

            v |= i32::from(input[p.as_usize() + 4] & 0x0F) << 28;
            p += 5;
            output[tmp_outpos as usize] = v as u32;
            tmp_outpos += 1;
        }
        output_offset.set_position(tmp_outpos);
        input_offset.add(input_length);
        Ok(())
    }
}

impl Default for VariableByte {
    fn default() -> Self {
        VariableByte::new()
    }
}

impl AnyLenCodec for VariableByte {
    fn encode(&mut self, input: &[u32], out: &mut Vec<u32>) -> FastPForResult<()> {
        let capacity = input.len() * 2 + 4;
        let start = out.len();
        out.resize(start + capacity, 0);
        let mut in_off = Cursor::new(0u32);
        let mut out_off = Cursor::new(0u32);
        VariableByte::compress_into_slice(
            input,
            input.len() as u32,
            &mut in_off,
            &mut out[start..],
            &mut out_off,
        )?;
        let written = out_off.position() as usize;
        out.truncate(start + written);
        Ok(())
    }

    fn decode(
        &mut self,
        input: &[u32],
        out: &mut Vec<u32>,
        expected_len: Option<u32>,
    ) -> FastPForResult<()> {
        let capacity = if let Some(expected) = expected_len {
            expected.is_valid_expected(Self::max_decompressed_len(input.len()))?
        } else {
            input.len() * 4
        };
        let start = out.len();
        out.reserve(capacity);
        out.resize(start + capacity, 0);
        let mut in_off = Cursor::new(0u32);
        let mut out_off = Cursor::new(0u32);
        VariableByte::decompress_from_u32_slice(
            input,
            input.len() as u32,
            &mut in_off,
            &mut out[start..],
            &mut out_off,
        )?;
        let written = out_off.position() as usize;
        out.truncate(start + written);
        if let Some(n) = expected_len {
            written.is_decoded_mismatch(n)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::hash_map::RandomState;
    use std::hash::{BuildHasher, Hasher};

    use super::*;
    use crate::test_utils::{compress, decompress, roundtrip};

    fn verify_u32_roundtrip(input: &[u32]) {
        let mut encoded: Vec<u32> = vec![0; input.len() * 2 + 1];
        let mut input_offset = Cursor::new(0);
        let mut output_offset = Cursor::new(0);

        VariableByte::compress_into_slice(
            input,
            input.len() as u32,
            &mut input_offset,
            &mut encoded,
            &mut output_offset,
        )
        .expect("Failed to compress");

        let encoded_len = output_offset.position() as u32;
        let mut decoded: Vec<u32> = vec![0; input.len()];
        let mut input_offset = Cursor::new(0);
        let mut output_offset = Cursor::new(0);

        VariableByte::decompress_from_u32_slice(
            &encoded,
            encoded_len,
            &mut input_offset,
            &mut decoded,
            &mut output_offset,
        )
        .expect("Failed to uncompress");

        assert_eq!(
            input.len(),
            output_offset.position() as usize,
            "Decoded length mismatch"
        );
        assert_eq!(input, &decoded[..input.len()], "Decoded data mismatch");
    }

    fn verify_i8_roundtrip(input: &[u32]) {
        let mut encoded: Vec<i8> = vec![0; input.len() * 10];
        let mut input_offset = Cursor::new(0);
        let mut output_offset = Cursor::new(0);

        VariableByte::compress_to_i8_slice(
            input,
            input.len() as u32,
            &mut input_offset,
            &mut encoded,
            &mut output_offset,
        )
        .expect("Failed to compress");

        let encoded_len = (output_offset.position() - input.len() as u64) as u32;
        let mut decoded: Vec<u32> = vec![0; input.len()];
        let mut input_offset = Cursor::new(0);
        let mut output_offset = Cursor::new(0);

        VariableByte::decompress_from_i8_slice(
            &encoded,
            encoded_len,
            &mut input_offset,
            &mut decoded,
            &mut output_offset,
        )
        .expect("Failed to uncompress");

        assert_eq!(
            input.len(),
            output_offset.position() as usize,
            "Decoded length mismatch"
        );
        assert_eq!(input, &decoded[..input.len()], "Decoded data mismatch");
    }

    #[test]
    fn test_empty_int_array() {
        verify_u32_roundtrip(&[]);
    }

    #[test]
    fn test_empty_byte_array() {
        verify_i8_roundtrip(&[]);
    }

    #[test]
    fn test_single_small_value() {
        verify_u32_roundtrip(&[5]);
        verify_i8_roundtrip(&[5]);
    }

    #[test]
    fn test_single_large_value() {
        verify_u32_roundtrip(&[10_878_508]);
        verify_i8_roundtrip(&[10_878_508]);
    }

    #[test]
    fn test_boundary_values_7bit() {
        verify_u32_roundtrip(&[0, 127]);
        verify_i8_roundtrip(&[0, 127]);
    }

    #[test]
    fn test_boundary_values_14bit() {
        verify_u32_roundtrip(&[128, 16383]);
        verify_i8_roundtrip(&[128, 16383]);
    }

    #[test]
    fn test_boundary_values_21bit() {
        verify_u32_roundtrip(&[16384, 2_097_151]);
        verify_i8_roundtrip(&[16384, 2_097_151]);
    }

    #[test]
    fn test_boundary_values_28bit() {
        verify_u32_roundtrip(&[2_097_152, 268_435_455]);
        verify_i8_roundtrip(&[2_097_152, 268_435_455]);
    }

    #[test]
    fn test_boundary_values_32bit() {
        verify_u32_roundtrip(&[268_435_456, u32::MAX]);
        verify_i8_roundtrip(&[268_435_456, u32::MAX]);
    }

    #[test]
    fn test_increasing_sequence() {
        let input: Vec<u32> = (0..1000).collect();
        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_max_and_min() {
        verify_u32_roundtrip(&[0, u32::MAX]);
        verify_i8_roundtrip(&[0, u32::MAX]);
    }

    #[test]
    fn test_powers_of_two() {
        let input: Vec<u32> = (0..31).map(|i| 1u32 << i).collect();
        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_mixed_sizes() {
        let input = vec![
            5,           // 1 byte
            200,         // 2 bytes
            20_000,      // 3 bytes
            2_000_000,   // 4 bytes
            200_000_000, // 5 bytes
        ];
        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_all_same_value() {
        let input = vec![42; 100];
        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_alternating_small_large() {
        let mut input = Vec::new();
        for i in 0..50 {
            if i % 2 == 0 {
                input.push(1);
            } else {
                input.push(u32::MAX);
            }
        }
        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_random_numbers_small() {
        let seed = RandomState::new().build_hasher().finish();
        let mut rng = seed;
        let mut input = Vec::new();

        for _ in 0..1000 {
            rng = rng.wrapping_mul(6364136223846793005).wrapping_add(1);
            input.push((rng % u64::from(u32::MAX)) as u32);
        }

        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_fuzz_case_regression() {
        // Regression test from fuzzing: input [0x00a6002c]
        let input = vec![0x00a6002c];
        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_sequential_values() {
        let input: Vec<u32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_sparse_values() {
        let input = vec![0, 1000000, 2000000, 3000000, 4000000];
        verify_u32_roundtrip(&input);
        verify_i8_roundtrip(&input);
    }

    #[test]
    fn test_variable_byte_default() {
        let data = vec![1u32, 2, 3];
        roundtrip::<VariableByte>(&data);
    }

    /// `decompress_from_u32_slice` returns `OutputBufferTooSmall` when the
    /// output buffer is exhausted mid-stream (fast path, ≥10 bytes remaining).
    #[test]
    fn test_decompress_output_too_small_fast_path() {
        // Encode 16 values so the fast path (≥10 bytes) is exercised.
        let input: Vec<u32> = (0..16).collect();
        let mut encoded: Vec<u32> = vec![0; input.len() * 2 + 1];
        let mut in_off = Cursor::new(0u32);
        let mut out_off = Cursor::new(0u32);
        VariableByte::compress_into_slice(
            &input,
            input.len() as u32,
            &mut in_off,
            &mut encoded,
            &mut out_off,
        )
        .unwrap();
        let encoded_len = out_off.position() as u32;

        // Output buffer with room for only 4 values — must error.
        let mut tiny_out = vec![0u32; 4];
        let result = VariableByte::decompress_from_u32_slice(
            &encoded,
            encoded_len,
            &mut Cursor::new(0u32),
            &mut tiny_out,
            &mut Cursor::new(0u32),
        );
        assert!(
            matches!(result, Err(FastPForError::OutputBufferTooSmall)),
            "expected OutputBufferTooSmall, got {result:?}"
        );
    }

    /// `decompress_from_u32_slice` returns `OutputBufferTooSmall` when the
    /// output buffer is exhausted in the slow path (<10 bytes remaining).
    #[test]
    fn test_decompress_output_too_small_slow_path() {
        // Encode 2 values so only the slow path is exercised (< 10 bytes).
        let input = vec![1u32, 2];
        let mut encoded: Vec<u32> = vec![0; input.len() * 2 + 1];
        let mut in_off = Cursor::new(0u32);
        let mut out_off = Cursor::new(0u32);
        VariableByte::compress_into_slice(
            &input,
            input.len() as u32,
            &mut in_off,
            &mut encoded,
            &mut out_off,
        )
        .unwrap();
        let encoded_len = out_off.position() as u32;

        // Zero-capacity output — must error.
        let result = VariableByte::decompress_from_u32_slice(
            &encoded,
            encoded_len,
            &mut Cursor::new(0u32),
            &mut [],
            &mut Cursor::new(0u32),
        );
        assert!(
            matches!(result, Err(FastPForError::OutputBufferTooSmall)),
            "expected OutputBufferTooSmall, got {result:?}"
        );
    }

    #[test]
    fn test_anylen_decode_with_expected_len_ok() {
        let data = vec![1u32, 2, 3];
        let encoded = compress::<VariableByte>(&data).unwrap();
        let decoded = decompress::<VariableByte>(&encoded, Some(3)).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn test_anylen_decode_expected_len_mismatch_errors() {
        // expected_len must be >= actual to avoid OutputBufferTooSmall; use a larger
        // value to exercise the is_decoded_mismatch path.
        let encoded = compress::<VariableByte>(&[1u32, 2, 3]).unwrap();
        decompress::<VariableByte>(&encoded, Some(10)).unwrap_err();
    }
}