compcol 0.4.3

A no_std collection of compression algorithms behind a uniform streaming trait, gated per-algorithm by Cargo features.
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
//! Integration tests for the LZMA encoder and decoder.
//!
//! The decoder tests use pre-generated `.lzma` fixtures produced by Python's
//! stdlib `lzma` module (which uses XZ Utils internally) via
//! `lzma.compress(payload, format=lzma.FORMAT_ALONE)`.
//!
//! The encoder tests verify round-trip against our own decoder, plus a
//! handful of decoder-only edge cases. Canonical v0.3 port: every call
//! returns `(Progress, Status)` and the loop dispatches on `Status` rather
//! than inferring from byte counts.

#![cfg(feature = "lzma")]

use compcol::lzma::{Decoder, Encoder, EncoderConfig, Lzma};
use compcol::{Algorithm, Decoder as _, Encoder as _, Error, Status};

fn hex(s: &str) -> Vec<u8> {
    let s: String = s.chars().filter(|c| !c.is_whitespace()).collect();
    (0..s.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
        .collect()
}

fn decode_one_shot(compressed: &[u8]) -> Result<Vec<u8>, Error> {
    decode_chunked(compressed, compressed.len().max(1), 65536)
}

/// Drive a fresh decoder to completion. Feeds the input in `in_chunk`-sized
/// slices and drains via an `out_chunk`-sized buffer.
fn decode_chunked(compressed: &[u8], in_chunk: usize, out_chunk: usize) -> Result<Vec<u8>, Error> {
    let mut dec = Decoder::new();
    decode_chunked_with(&mut dec, compressed, in_chunk, out_chunk)
}

/// Same as `decode_chunked`, but operates on a caller-supplied decoder so the
/// reset-and-reuse test can hit the same instance with two streams.
fn decode_chunked_with(
    dec: &mut Decoder,
    compressed: &[u8],
    in_chunk: usize,
    out_chunk: usize,
) -> Result<Vec<u8>, Error> {
    let mut out = Vec::new();
    let mut buf = vec![0u8; out_chunk.max(1)];
    let mut i = 0;

    while i < compressed.len() {
        let end = (i + in_chunk).min(compressed.len());
        let chunk = &compressed[i..end];
        let mut consumed = 0;
        while consumed < chunk.len() {
            let (p, status) = dec.decode(&chunk[consumed..], &mut buf)?;
            out.extend_from_slice(&buf[..p.written]);
            consumed += p.consumed;
            match status {
                Status::StreamEnd => return Ok(out),
                Status::InputEmpty => break,
                Status::OutputFull => continue,
            }
        }
        i = end;
    }

    // Drain any output the decoder can still produce from internally
    // buffered bytes.
    loop {
        let (p, status) = dec.decode(&[], &mut buf)?;
        out.extend_from_slice(&buf[..p.written]);
        if matches!(status, Status::StreamEnd) {
            return Ok(out);
        }
        if p.written == 0 {
            break;
        }
    }

    loop {
        let (p, status) = dec.finish(&mut buf)?;
        out.extend_from_slice(&buf[..p.written]);
        match status {
            Status::StreamEnd => break,
            Status::OutputFull | Status::InputEmpty => {
                if p.written == 0 {
                    panic!("decoder finish stalled");
                }
            }
        }
    }
    Ok(out)
}

// ─── decoder fixtures (FORMAT_ALONE, "alone" / .lzma legacy) ─────────────

/// `python3 -c "import lzma; print(lzma.compress(b'', format=lzma.FORMAT_ALONE).hex())"`
const FIX_EMPTY: &str = "5d00008000ffffffffffffffff0083fffbffffc0000000";

/// `lzma.compress(b'hello world', format=lzma.FORMAT_ALONE)`
const FIX_HELLO: &str = "5d00008000ffffffffffffffff00341949ee8de917893a336005f7cf64fffb782000";

/// `lzma.compress(b'A' * 4096, format=lzma.FORMAT_ALONE)`
const FIX_REP4K: &str =
    "5d00008000ffffffffffffffff0020effbbffea3b15ee5f83fb2aa2655f868704170150ee40930ffffb52c0000";

#[test]
fn decode_empty() {
    let out = decode_one_shot(&hex(FIX_EMPTY)).unwrap();
    assert!(
        out.is_empty(),
        "empty fixture decoded to {} bytes",
        out.len()
    );
}

#[test]
fn decode_hello_world() {
    let out = decode_one_shot(&hex(FIX_HELLO)).unwrap();
    assert_eq!(out, b"hello world");
}

#[test]
fn decode_hello_world_chunked() {
    let stream = hex(FIX_HELLO);
    for in_chunk in [1, 2, 3, 5, 8, 16] {
        let out = decode_chunked(&stream, in_chunk, 7).unwrap();
        assert_eq!(out, b"hello world", "in_chunk={in_chunk}");
    }
}

#[test]
fn decode_4kib_repeating_bytes() {
    let out = decode_one_shot(&hex(FIX_REP4K)).unwrap();
    assert_eq!(out.len(), 4096);
    assert!(out.iter().all(|&b| b == b'A'));
}

#[test]
fn decode_4kib_chunked_tiny_output() {
    let stream = hex(FIX_REP4K);
    let out = decode_chunked(&stream, 7, 13).unwrap();
    assert_eq!(out.len(), 4096);
    assert!(out.iter().all(|&b| b == b'A'));
}

#[test]
fn decode_lorem_16kib() {
    // 16 KiB of repeating Lorem ipsum (well past one dictionary refresh).
    let fix = concat!(
        "5d00008000ffffffffffffffff00261bca46675af277b87d86d841db0535cd",
        "83a57c12a505db90bd2f14d3717296a88a7d8456718d6a2298ab9e3dc355ef",
        "cca5c3dd5b8ebf03812140d6269102454f92a178bb8a00af902a26920223e5",
        "5cb32de3e85c2cfb3221c66f6a37b16620cdb7527d66a42108d1441495affc",
        "58cfe5db354c05b89327ad7fe5fcbd0afbe2eda9e4d660d61c60112bf411e2",
        "9134c192bd8d4ac7c3c84aef9b3dda35640dd2db8ac9fd8cacc0",
    );
    let out = decode_one_shot(&hex(fix)).unwrap();
    let lorem_chunk = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
    let expected: Vec<u8> = lorem_chunk
        .repeat(200)
        .into_bytes()
        .into_iter()
        .take(16384)
        .collect();
    assert_eq!(out.len(), 16384);
    assert_eq!(out, expected);
}

#[test]
fn decode_known_uncompressed_size_header() {
    // Same payload as FIX_HELLO but with the uncompressed-size field set
    // to 11 instead of u64::MAX. The decoder should stop after producing
    // exactly 11 bytes.
    let mut stream = hex(FIX_HELLO);
    stream[5..13].copy_from_slice(&11u64.to_le_bytes());
    let out = decode_one_shot(&stream).unwrap();
    assert_eq!(out, b"hello world");
}

#[test]
fn bad_header_props_rejected() {
    // properties byte 0xFF (>= 9*5*5 = 225) is illegal.
    let mut stream = hex(FIX_HELLO);
    stream[0] = 0xFF;
    let mut dec = Decoder::new();
    let mut buf = [0u8; 64];
    let err = dec.decode(&stream, &mut buf).unwrap_err();
    assert_eq!(err, Error::BadHeader);
}

#[test]
fn corrupt_first_init_byte_rejected() {
    // The first byte of the range-coder payload (offset 13) must be 0x00.
    let mut stream = hex(FIX_HELLO);
    stream[13] = 0x01;
    let mut dec = Decoder::new();
    let mut buf = [0u8; 64];
    let err = dec.decode(&stream, &mut buf).unwrap_err();
    assert_eq!(err, Error::Corrupt);
}

#[test]
fn unexpected_eof_on_finish() {
    let stream = hex(FIX_HELLO);
    let truncated = &stream[..stream.len() - 4]; // chop the EOS marker
    let mut dec = Decoder::new();
    let mut buf = vec![0u8; 64];
    let _ = dec.decode(truncated, &mut buf).unwrap();
    // finish should now realise we're stuck without input.
    let err = dec.finish(&mut buf).unwrap_err();
    assert_eq!(err, Error::UnexpectedEnd);
}

// ─── algorithm metadata ─────────────────────────────────────────────────

#[test]
fn name_is_lzma() {
    assert_eq!(<Lzma as Algorithm>::NAME, "lzma");
}

#[test]
fn default_config_is_level_6() {
    assert_eq!(EncoderConfig::default().level, 6);
}

// ─── encoder round-trip tests ────────────────────────────────────────────

fn encode_one_shot(payload: &[u8]) -> Vec<u8> {
    let mut enc = Encoder::new();
    encode_with(&mut enc, payload)
}

fn encode_at_level(payload: &[u8], level: u8) -> Vec<u8> {
    let mut enc = Encoder::with_config(EncoderConfig { level });
    encode_with(&mut enc, payload)
}

fn encode_with(enc: &mut Encoder, payload: &[u8]) -> Vec<u8> {
    // The encoder buffers all input internally and emits nothing until
    // `finish`, so a small scratch buffer is fine for the `encode` calls.
    let mut scratch = [0u8; 64];
    let mut consumed = 0;
    while consumed < payload.len() {
        let (p, status) = enc.encode(&payload[consumed..], &mut scratch).unwrap();
        consumed += p.consumed;
        // Output should always be empty from encode() for LZMA.
        assert_eq!(p.written, 0);
        match status {
            Status::InputEmpty | Status::StreamEnd => break,
            Status::OutputFull => {
                // Shouldn't happen — encode() doesn't write anything.
                if p.consumed == 0 {
                    panic!("encoder stalled mid-input");
                }
            }
        }
    }

    let mut out = Vec::new();
    let mut buf = vec![0u8; 4096];
    loop {
        let (p, status) = enc.finish(&mut buf).unwrap();
        out.extend_from_slice(&buf[..p.written]);
        match status {
            Status::StreamEnd => break,
            Status::OutputFull | Status::InputEmpty => {
                if p.written == 0 {
                    panic!("encoder finish stalled");
                }
            }
        }
    }
    out
}

fn round_trip(payload: &[u8]) {
    let compressed = encode_one_shot(payload);
    let recovered = decode_one_shot(&compressed).expect("decoding our own output failed");
    assert_eq!(
        recovered,
        payload,
        "round-trip mismatch (input len {})",
        payload.len()
    );
}

#[test]
fn encode_empty_round_trip() {
    let compressed = encode_one_shot(b"");
    assert!(
        compressed.len() >= 13,
        "encoder must always emit a header, got {} bytes",
        compressed.len()
    );
    assert_eq!(
        compressed[0], 0x5d,
        "props byte = (pb=2)*5*9 + (lp=0)*9 + (lc=3)"
    );
    // Uncompressed size sentinel = u64::MAX.
    for &b in &compressed[5..13] {
        assert_eq!(b, 0xFF);
    }
    let recovered = decode_one_shot(&compressed).unwrap();
    assert!(recovered.is_empty());
}

#[test]
fn encode_single_byte_round_trip() {
    for b in [0u8, 1, 0x7F, 0xFE, 0xFF, b'A'] {
        let compressed = encode_one_shot(&[b]);
        let recovered = decode_one_shot(&compressed).unwrap();
        assert_eq!(recovered, vec![b], "byte 0x{:02x}", b);
    }
}

#[test]
fn encode_hello_world_round_trip() {
    round_trip(b"hello world");
}

#[test]
fn encode_small_text_round_trip() {
    round_trip(b"hello world! hello world! hello world!");
}

#[test]
fn encode_4kib_repeating_byte_round_trip() {
    // All-A's: dominated by rep matches.
    let payload = vec![b'A'; 4096];
    let compressed = encode_one_shot(&payload);
    // Highly compressible: should be well under 100 bytes for this case.
    assert!(
        compressed.len() < 100,
        "expected strong compression on repeating byte, got {} bytes",
        compressed.len()
    );
    let recovered = decode_one_shot(&compressed).unwrap();
    assert_eq!(recovered, payload);
}

#[test]
fn encode_byte_value_coverage() {
    // Every byte value 0..=255 in sequence, to exercise every literal path.
    let payload: Vec<u8> = (0u8..=255).collect();
    round_trip(&payload);
}

#[test]
fn encode_streaming_one_byte_chunks_round_trip() {
    let payload = b"The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.";

    let mut enc = Encoder::new();
    let mut scratch = [0u8; 4];
    for byte in payload {
        let (p, _status) = enc
            .encode(core::slice::from_ref(byte), &mut scratch)
            .unwrap();
        assert_eq!(p.consumed, 1);
        assert_eq!(p.written, 0);
    }
    let mut compressed = Vec::new();
    let mut buf = [0u8; 1];
    loop {
        let (p, status) = enc.finish(&mut buf).unwrap();
        compressed.extend_from_slice(&buf[..p.written]);
        match status {
            Status::StreamEnd => break,
            Status::OutputFull | Status::InputEmpty => {
                if p.written == 0 {
                    panic!("encoder finish stalled in single-byte streaming mode");
                }
            }
        }
    }

    // Also stream the decode side one byte at a time on input and output.
    let recovered = decode_chunked(&compressed, 1, 1).unwrap();
    assert_eq!(recovered, payload);
}

// ─── ≥64 KiB mixed corpus ───────────────────────────────────────────────

/// Build a ≥64 KiB corpus that compresses well but is not the
/// random-data pathology documented in BENCH.md (high-distance-slot path
/// is slow on incompressible input). Mixes a small alphabet with
/// long recurring phrases that benefit from match-finder depth.
fn mixed_corpus() -> Vec<u8> {
    let mut state: u32 = 0xC0FFEE_u32;
    let mut out = Vec::with_capacity(80 * 1024);
    let alphabet = b"abcdef";
    let phrases: &[&[u8]] = &[
        b"the_quick_brown_fox_jumps_over_the_lazy_dog_xxxxxxxxxxxxxxxxxxxxxxxx",
        b"lorem_ipsum_dolor_sit_amet_consectetur_adipiscing_elit_yyyyyyyyyyyyyy",
        b"compcol_streaming_codec_test_corpus_for_level_differentiation_zzzzz",
    ];
    let mut phrase_idx = 0usize;
    while out.len() < 64 * 1024 {
        for _ in 0..64 {
            state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
            out.push(alphabet[(state as usize) % alphabet.len()]);
        }
        out.extend_from_slice(phrases[phrase_idx % phrases.len()]);
        phrase_idx += 1;
    }
    out
}

#[test]
fn round_trip_mixed_corpus_default_level() {
    let input = mixed_corpus();
    assert!(input.len() >= 64 * 1024);
    round_trip(&input);
}

// ─── level-specific tests ───────────────────────────────────────────────

/// Repeated "Lorem ipsum" buffer of at least 16 KiB — well past the point
/// where lzma at higher levels can find very long matches the lower-level
/// chain budget walks past.
fn lorem_corpus(min_len: usize) -> Vec<u8> {
    let chunk = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
    let mut out = Vec::with_capacity(min_len + chunk.len());
    while out.len() < min_len {
        out.extend_from_slice(chunk.as_bytes());
    }
    out
}

#[test]
fn round_trip_level_1() {
    for input in [
        &b""[..],
        b"hello world",
        &b"abcabcabcabcabc".repeat(100)[..],
    ] {
        let compressed = encode_at_level(input, 1);
        let recovered = decode_one_shot(&compressed).unwrap();
        assert_eq!(recovered, input);
    }
}

#[test]
fn round_trip_level_9() {
    for input in [
        &b""[..],
        b"hello world",
        &b"abcabcabcabcabc".repeat(100)[..],
    ] {
        let compressed = encode_at_level(input, 9);
        let recovered = decode_one_shot(&compressed).unwrap();
        assert_eq!(recovered, input);
    }
}

#[test]
fn level_9_no_worse_than_level_1_on_compressible_corpus() {
    // The whole point of having levels: max-effort must produce output
    // at least as small as min-effort on a compressible corpus. We use a
    // ≥16 KiB lorem corpus so the chain-depth difference has room to
    // produce a measurable size delta — on tiny inputs LZMA's greedy
    // parser converges and the levels collapse.
    let input = lorem_corpus(16 * 1024);
    let lo = encode_at_level(&input, 1);
    let hi = encode_at_level(&input, 9);
    assert!(
        hi.len() <= lo.len(),
        "level 9 ({} bytes) was bigger than level 1 ({} bytes)",
        hi.len(),
        lo.len(),
    );
    // Sanity: both must roundtrip.
    assert_eq!(decode_one_shot(&lo).unwrap(), input);
    assert_eq!(decode_one_shot(&hi).unwrap(), input);
}

#[test]
fn out_of_range_level_is_clamped() {
    // Level 250 should produce a valid stream (clamped to 9) — we don't
    // expose a fallible constructor.
    let input = b"the rain in spain falls mainly on the plain";
    let compressed = encode_at_level(input, 250);
    assert_eq!(decode_one_shot(&compressed).unwrap(), input);
}

// ─── reset / reuse ──────────────────────────────────────────────────────

#[test]
fn reset_preserves_level_and_allows_reuse() {
    let input_a = b"alpha alpha alpha alpha alpha".as_slice();
    let input_b = b"bravo bravo bravo bravo bravo".as_slice();

    let mut enc = Encoder::with_config(EncoderConfig { level: 9 });
    let encoded_a = encode_with(&mut enc, input_a);
    enc.reset();
    let encoded_b = encode_with(&mut enc, input_b);

    assert_eq!(decode_one_shot(&encoded_a).unwrap(), input_a);
    assert_eq!(decode_one_shot(&encoded_b).unwrap(), input_b);

    // After reset, an encoder configured at level 9 should still be at
    // level 9. A fresh level-9 encoder on the same input must produce
    // an identical byte stream — this is the contract that reset
    // preserves configuration.
    let mut fresh = Encoder::with_config(EncoderConfig { level: 9 });
    let fresh_b = encode_with(&mut fresh, input_b);
    assert_eq!(encoded_b, fresh_b, "reset must preserve compression level");
}

#[test]
fn decoder_reset_allows_reuse() {
    let encoded_a = encode_one_shot(b"hello");
    let encoded_b = encode_one_shot(b"world");

    let mut dec = Decoder::new();
    assert_eq!(
        decode_chunked_with(&mut dec, &encoded_a, 64, 64).unwrap(),
        b"hello"
    );
    dec.reset();
    assert_eq!(
        decode_chunked_with(&mut dec, &encoded_b, 64, 64).unwrap(),
        b"world"
    );
}

// ─── algorithm-trait entry points ───────────────────────────────────────

#[test]
fn algorithm_encoder_decoder_round_trip() {
    let mut enc = <Lzma as Algorithm>::encoder();
    let input = b"compcol Algorithm trait roundtrip!";

    // Encode.
    let mut encoded = Vec::new();
    let mut buf = vec![0u8; 256];
    let mut consumed = 0;
    while consumed < input.len() {
        let (p, status) = enc.encode(&input[consumed..], &mut buf).unwrap();
        encoded.extend_from_slice(&buf[..p.written]);
        consumed += p.consumed;
        if matches!(status, Status::InputEmpty) {
            break;
        }
    }
    loop {
        let (p, status) = enc.finish(&mut buf).unwrap();
        encoded.extend_from_slice(&buf[..p.written]);
        if matches!(status, Status::StreamEnd) {
            break;
        }
    }

    // Decode through Algorithm::decoder() as well.
    let mut dec = <Lzma as Algorithm>::decoder();
    let decoded = decode_chunked_with(&mut dec, &encoded, encoded.len(), 256).unwrap();
    assert_eq!(decoded, input);
}

#[test]
fn algorithm_encoder_with_uses_config() {
    let input = lorem_corpus(16 * 1024);
    let mut enc_lo = <Lzma as Algorithm>::encoder_with(EncoderConfig { level: 1 });
    let mut enc_hi = <Lzma as Algorithm>::encoder_with(EncoderConfig { level: 9 });
    let lo = encode_with(&mut enc_lo, &input);
    let hi = encode_with(&mut enc_hi, &input);
    assert!(
        hi.len() <= lo.len(),
        "encoder_with(level=9) was bigger than encoder_with(level=1): hi={} lo={}",
        hi.len(),
        lo.len(),
    );
    assert_eq!(decode_one_shot(&lo).unwrap(), input);
    assert_eq!(decode_one_shot(&hi).unwrap(), input);
}

// ─── factory lookup ─────────────────────────────────────────────────────

#[cfg(feature = "factory")]
mod factory {
    use compcol::Status;
    use compcol::factory;

    #[test]
    fn lookup_known() {
        assert!(factory::encoder_by_name("lzma").is_some());
        assert!(factory::decoder_by_name("lzma").is_some());
    }

    #[test]
    fn names_contains_lzma() {
        assert!(factory::names().contains(&"lzma"));
    }

    #[test]
    fn boxed_round_trip() {
        let mut enc = factory::encoder_by_name("lzma").unwrap();
        let mut dec = factory::decoder_by_name("lzma").unwrap();
        let input = b"hello hello hello world world world!";

        let mut encoded = Vec::new();
        let mut buf = vec![0u8; 256];
        let mut consumed = 0;
        while consumed < input.len() {
            let (p, status) = enc.encode(&input[consumed..], &mut buf).unwrap();
            encoded.extend_from_slice(&buf[..p.written]);
            consumed += p.consumed;
            if matches!(status, Status::InputEmpty) {
                break;
            }
        }
        loop {
            let (p, status) = enc.finish(&mut buf).unwrap();
            encoded.extend_from_slice(&buf[..p.written]);
            if matches!(status, Status::StreamEnd) {
                break;
            }
        }

        // Decode — feed the whole thing in one go (the decoder's loop
        // tolerates partial draws), then drain anything still buffered,
        // then finish.
        let mut decoded = Vec::new();
        let mut consumed = 0;
        while consumed < encoded.len() {
            let (p, status) = dec.decode(&encoded[consumed..], &mut buf).unwrap();
            decoded.extend_from_slice(&buf[..p.written]);
            consumed += p.consumed;
            if matches!(status, Status::StreamEnd) {
                break;
            }
            if matches!(status, Status::InputEmpty) {
                break;
            }
        }
        loop {
            let (p, status) = dec.decode(&[], &mut buf).unwrap();
            decoded.extend_from_slice(&buf[..p.written]);
            if matches!(status, Status::StreamEnd) {
                break;
            }
            if p.written == 0 {
                break;
            }
        }
        loop {
            let (p, status) = dec.finish(&mut buf).unwrap();
            decoded.extend_from_slice(&buf[..p.written]);
            if matches!(status, Status::StreamEnd) {
                break;
            }
            if p.written == 0 {
                panic!("decoder finish stalled");
            }
        }
        assert_eq!(&decoded[..], input);
    }
}

// ─── interop with system xz --format=lzma (issue #14) ───────────────────

#[test]
fn xz_format_lzma_round_trips_via_vec_helper() {
    // Reporter's exact path: xz --format=lzma → vec::decompress_to_vec.
    // We don't depend on `xz` being on PATH; instead we hard-code a
    // small fixture produced offline by `echo "hello lzma alone" |
    // xz --format=lzma -c | xxd -i` (echo appends a trailing newline).
    const FIXTURE: &[u8] = &[
        0x5d, 0x00, 0x00, 0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x34,
        0x19, 0x49, 0xee, 0x8d, 0xe9, 0x14, 0x8a, 0x6a, 0xa5, 0xd6, 0xb6, 0x11, 0x0a, 0xd7, 0x39,
        0x16, 0x6a, 0x19, 0x15, 0x45, 0xff, 0xfe, 0x66, 0xec, 0x00,
    ];
    let decoded = compcol::vec::decompress_to_vec::<compcol::lzma::Lzma>(FIXTURE).unwrap();
    assert_eq!(decoded, b"hello lzma alone\n");
}

#[test]
fn limited_decoder_at_exact_budget_terminates_cleanly() {
    // Used to fail with Error::OutputLimitExceeded because the decoder's
    // raw_finish couldn't recognize EOS once the budget exhausted the
    // output slice. The decoder produced exactly N bytes correctly but
    // never returned StreamEnd — the EOS packet (which emits zero
    // output bytes) wasn't being decoded inside drain_output. Fixed by
    // restructuring drain_output so EOS / packet-with-no-output paths
    // run regardless of remaining output capacity.
    use compcol::Algorithm;
    use compcol::limit::LimitedDecoder;
    let original = vec![b'A'; 65536];
    let compressed = compcol::vec::compress_to_vec::<compcol::lzma::Lzma>(&original).unwrap();

    let mut dec = LimitedDecoder::new(compcol::lzma::Lzma::decoder(), original.len() as u64);
    let mut buf = vec![0u8; 4096];
    let mut decoded = Vec::new();
    let mut consumed = 0;
    while consumed < compressed.len() {
        let (p, s) = dec.decode(&compressed[consumed..], &mut buf).unwrap();
        decoded.extend_from_slice(&buf[..p.written]);
        consumed += p.consumed;
        if matches!(s, compcol::Status::StreamEnd) {
            break;
        }
        if matches!(s, compcol::Status::InputEmpty) && consumed == compressed.len() {
            break;
        }
        if p.consumed == 0 && p.written == 0 {
            break;
        }
    }
    loop {
        let (p, s) = dec.finish(&mut buf).unwrap();
        decoded.extend_from_slice(&buf[..p.written]);
        if matches!(s, compcol::Status::StreamEnd) {
            break;
        }
    }
    assert_eq!(decoded, original);
}