flashsieve 0.1.3

Storage-level pre-filtering for pattern matching, skip blocks that can't contain matches
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
#![allow(
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::expect_used,
    clippy::float_cmp,
    clippy::items_after_statements,
    clippy::panic,
    clippy::uninlined_format_args,
    clippy::unwrap_used
)]
//! Oneshot adversarial tests for flashsieve.
//!
//! This module contains comprehensive edge-case and adversarial tests
//! to ensure robustness against unexpected inputs and usage patterns.

use flashsieve::{
    BlockIndex, BlockIndexBuilder, ByteFilter, ByteHistogram, NgramBloom, NgramFilter,
};
use rand::rngs::StdRng;
use rand::{Rng, RngCore, SeedableRng};
use std::collections::HashSet;

// ============================================================================
// 1. Empty Filter Tests
// ============================================================================

#[test]
fn empty_filter_no_inserts_all_queries_false() {
    let bloom = NgramBloom::new(1024).unwrap();
    // Query all possible n-grams
    for a in 0u8..=255 {
        for b in 0u8..=255 {
            assert!(
                !bloom.maybe_contains(a, b),
                "Empty filter should return false for ({}, {})",
                a,
                b
            );
        }
    }
}

#[test]
fn empty_histogram_all_counts_zero() {
    let hist = ByteHistogram::new();
    for byte in 0u8..=255 {
        assert_eq!(
            hist.count(byte),
            0,
            "Empty histogram count for {} should be 0",
            byte
        );
    }
}

#[test]
fn empty_block_produces_empty_bloom() {
    let bloom = NgramBloom::from_block(&[], 1024).unwrap();
    assert!(!bloom.maybe_contains(b'a', b'b'));
}

// ============================================================================
// 2. No False Negatives Tests
// ============================================================================

#[test]
fn no_false_negatives_10000_inserts() {
    let mut bloom = NgramBloom::new(65536).unwrap();
    let mut inserted = Vec::new();
    let mut rng = StdRng::seed_from_u64(0xDEAD_BEEF);

    // Insert 10000 random n-grams
    for _ in 0..10000 {
        let a = rng.random();
        let b = rng.random();
        bloom.insert_ngram(a, b);
        inserted.push((a, b));
    }

    // Verify all inserted items are found
    for (a, b) in inserted {
        assert!(
            bloom.maybe_contains(a, b),
            "False negative for inserted n-gram ({}, {})",
            a,
            b
        );
    }
}

#[test]
fn no_false_negatives_all_65536_pairs() {
    let mut bloom = NgramBloom::new(65536).unwrap();

    // Insert all possible 2-byte combinations
    for a in 0u8..=255 {
        for b in 0u8..=255 {
            bloom.insert_ngram(a, b);
        }
    }

    // Verify all are found
    for a in 0u8..=255 {
        for b in 0u8..=255 {
            assert!(
                bloom.maybe_contains(a, b),
                "False negative for ({}, {})",
                a,
                b
            );
        }
    }
}

// ============================================================================
// 3. FPR Measurement Tests
// ============================================================================

#[test]
fn fpr_within_theoretical_bound() {
    let num_bits = 32768;
    let num_inserts = 1000;
    let num_tests = 10000;

    let mut bloom = NgramBloom::new(num_bits).unwrap();
    let mut rng = StdRng::seed_from_u64(0xF1A5_510E);
    let mut inserted = HashSet::new();

    // Insert random items
    for _ in 0..num_inserts {
        let a = rng.random();
        let b = rng.random();
        bloom.insert_ngram(a, b);
        inserted.insert((a, b));
    }

    // Test random absent items
    let mut false_positives = 0;
    let mut trials = 0;
    for _ in 0..num_tests {
        let a = rng.random();
        let b = rng.random();
        if !inserted.contains(&(a, b)) {
            trials += 1;
            if bloom.maybe_contains(a, b) {
                false_positives += 1;
            }
        }
    }

    let measured_fpr = false_positives as f64 / trials as f64;

    // Theoretical FPR with k=3: (1 - e^(-kn/m))^k
    let n = num_inserts as f64;
    let m = num_bits as f64;
    let k = 3.0;
    let theoretical_fpr = (1.0 - (-k * n / m).exp()).powf(k);

    // Allow 50% margin above theoretical bound
    let max_acceptable_fpr = theoretical_fpr * 1.5;

    assert!(
        measured_fpr < max_acceptable_fpr,
        "FPR {} exceeds theoretical bound {} (with margin)",
        measured_fpr,
        max_acceptable_fpr
    );
}

// ============================================================================
// 4. Serialization Roundtrip Tests
// ============================================================================

#[test]
fn serialization_roundtrip_empty_index() {
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 256])
        .unwrap();

    let bytes = index.to_bytes();
    let recovered = BlockIndex::from_bytes(&bytes).unwrap();

    assert_eq!(index.block_size(), recovered.block_size());
    assert_eq!(index.block_count(), recovered.block_count());
}

#[test]
fn serialization_roundtrip_multiple_blocks() {
    let mut data = vec![0u8; 1024];
    let mut rng = StdRng::seed_from_u64(0xCAFE_BABE);
    rng.fill_bytes(&mut data);

    let index = BlockIndexBuilder::new()
        .block_size(256)
        .bloom_bits(1024)
        .build(&data)
        .unwrap();

    let bytes = index.to_bytes();
    let recovered = BlockIndex::from_bytes(&bytes).unwrap();

    // Query results should be identical
    let filter = ByteFilter::from_patterns(&[b"\x00".as_slice()]);
    assert_eq!(
        index.candidate_blocks_byte(&filter),
        recovered.candidate_blocks_byte(&filter)
    );
}

#[test]
fn truncated_deserialize_fails_gracefully() {
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 512])
        .unwrap();

    let bytes = index.to_bytes();

    // Try various truncation points
    for truncate_at in [1, 10, 20, 29, 100, bytes.len() - 1] {
        if truncate_at < bytes.len() {
            let truncated = &bytes[..truncate_at];
            assert!(
                BlockIndex::from_bytes(truncated).is_none(),
                "Should fail for truncation at {}",
                truncate_at
            );
        }
    }
}

#[test]
fn corrupted_crc_detected() {
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 256])
        .unwrap();

    let mut bytes = index.to_bytes();

    // Corrupt a byte in the middle
    let mid = bytes.len() / 2;
    bytes[mid] = bytes[mid].wrapping_add(1);

    let result = BlockIndex::from_bytes_checked(&bytes);
    assert!(result.is_err());
}

#[test]
fn corrupted_data_detected_by_crc() {
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 1024])
        .unwrap();

    let mut bytes = index.to_bytes();

    // Corrupt multiple bytes
    for i in (0..bytes.len() - 4).step_by(100) {
        bytes[i] ^= 0xFF;
    }

    assert!(BlockIndex::from_bytes(&bytes).is_none());
}

// ============================================================================
// 5. Edge Case Tests
// ============================================================================

#[test]
fn zero_size_filter_rejected() {
    assert!(NgramBloom::new(0).is_err());
}

#[test]
fn huge_item_count_handled() {
    // A 1e9-item / 1% FPR bloom would need ~12.3 billion bits (~1.5 GB). The
    // builder caps bloom size and *rejects* such requests with BloomBitsTooLarge
    // rather than attempting the allocation -- "handled" means failing closed, not
    // OOMing. Assert the safety cap fires.
    let result = NgramBloom::with_target_fpr(0.01, 1_000_000_000);
    let was_ok = result.is_ok();
    assert!(
        matches!(result, Err(flashsieve::Error::BloomBitsTooLarge { .. })),
        "expected BloomBitsTooLarge for an absurd item count (ok={was_ok})"
    );
}

#[test]
fn single_byte_block() {
    // Block with only 1 byte has no n-grams
    let bloom = NgramBloom::from_block(b"x", 1024).unwrap();
    assert!(!bloom.maybe_contains(b'x', b'x'));
}

#[test]
fn two_byte_block_single_ngram() {
    // Block with exactly 2 bytes has exactly 1 n-gram
    let bloom = NgramBloom::from_block(b"ab", 1024).unwrap();
    assert!(bloom.maybe_contains(b'a', b'b'));
    assert!(!bloom.maybe_contains(b'b', b'a'));
}

#[test]
fn binary_data_with_nulls() {
    let data = vec![0u8, 1, 2, 0, 3, 0, 0, 255];
    let bloom = NgramBloom::from_block(&data, 1024).unwrap();

    // Should handle null bytes correctly
    assert!(bloom.maybe_contains(0, 1));
    assert!(bloom.maybe_contains(0, 0));
    assert!(bloom.maybe_contains(0, 3));
}

// ============================================================================
// 6. Duplicate Insert Tests
// ============================================================================

#[test]
fn duplicate_inserts_idempotent() {
    let mut bloom = NgramBloom::new(1024).unwrap();

    // Insert same n-gram many times
    for _ in 0..1000 {
        bloom.insert_ngram(b'x', b'y');
    }

    // Should still be found
    assert!(bloom.maybe_contains(b'x', b'y'));

    // FPR estimate should account for actual fill, not insert count
    let fpr = bloom.estimated_false_positive_rate();
    assert!(fpr < 0.01, "FPR after duplicate inserts should be low");
}

#[test]
fn all_same_byte_repeated() {
    let data = vec![b'A'; 10000];
    let bloom = NgramBloom::from_block(&data, 1024).unwrap();

    // Only one distinct n-gram: "AA"
    assert!(bloom.maybe_contains(b'A', b'A'));
}

// ============================================================================
// 7. Hash Distribution Tests
// ============================================================================

#[test]
fn hash_distribution_spread() {
    let mut bloom = NgramBloom::new(4096).unwrap();

    // Insert sequential pairs
    for i in 0..256u16 {
        let a = (i >> 8) as u8;
        let b = i as u8;
        bloom.insert_ngram(a, b);
    }

    // Check that bits are spread (not all concentrated)
    let (_, bits) = bloom.raw_parts();
    let ones: u64 = bits.iter().map(|w| u64::from(w.count_ones())).sum();

    // With 256 inserts and k=3, expect roughly 768 bits set
    // Allow for variance but ensure reasonable spread
    assert!(
        ones > 100,
        "Bits should be well distributed, got {} ones",
        ones
    );
    assert!(ones < 4000, "Too many bits set: {}", ones);
}

// ============================================================================
// 8. CRC-32 Reference Values
// ============================================================================

/// Compute CRC-32 for verification
fn crc32_reference(data: &[u8]) -> u32 {
    let table: [u32; 256] = {
        let mut t = [0u32; 256];
        for (i, slot) in t.iter_mut().enumerate() {
            let mut crc = i as u32;
            for _ in 0..8 {
                if crc & 1 != 0 {
                    crc = (crc >> 1) ^ 0xEDB8_8320;
                } else {
                    crc >>= 1;
                }
            }
            *slot = crc;
        }
        t
    };

    let mut crc = 0xFFFF_FFFFu32;
    for &byte in data {
        let idx = ((crc ^ u32::from(byte)) & 0xFF) as usize;
        crc = (crc >> 8) ^ table[idx];
    }
    !crc
}

#[test]
fn crc32_reference_values() {
    // Known CRC-32 values for test strings
    let test_cases = [
        (b"" as &[u8], 0x0000_0000),
        (b"\x00\x00\x00\x00", 0x2144_df1c),
        (b"123456789", 0xcbf4_3926),
        (b"FSBX", 0x563e_bc55),
    ];

    for (input, expected) in &test_cases {
        let computed = crc32_reference(input);
        assert_eq!(computed, *expected, "CRC-32 mismatch for input {:?}", input);
    }
}

// ============================================================================
// 9. FNV-64 Reference Values
// ============================================================================

fn fnv1a_64(data: &[u8]) -> u64 {
    const FNV_OFFSET_BASIS: u64 = 0xCBF2_9CE4_8422_2325;
    const FNV_PRIME: u64 = 0x0000_0100_0000_01B3;

    let mut hash = FNV_OFFSET_BASIS;
    for &byte in data {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(FNV_PRIME);
    }
    hash
}

#[test]
fn fnv1a_64_reference_values() {
    // Known FNV-1a 64-bit test vectors from the FNV spec
    let test_cases = [
        (b"" as &[u8], 0xCBF2_9CE4_8422_2325u64),
        (b"a", 0xAF63_DC4C_8601_EC8C),
        (b"foobar", 0x8594_4171_F739_67E8),
    ];

    for (input, expected) in &test_cases {
        let computed = fnv1a_64(input);
        assert_eq!(
            computed, *expected,
            "FNV-1a 64 mismatch for input {:?}",
            input
        );
    }
}

// ============================================================================
// 10. Concurrent Read Tests
// ============================================================================

#[test]
fn concurrent_reads_thread_safe() {
    use std::sync::Arc;
    use std::thread;

    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 2048])
        .unwrap();

    let index = Arc::new(index);
    let mut handles = vec![];

    // Spawn 8 threads doing concurrent reads
    for thread_id in 0..8 {
        let idx = Arc::clone(&index);
        let handle = thread::spawn(move || {
            let filter = ByteFilter::from_patterns(&[b"test".as_slice()]);
            for _ in 0..1000 {
                let candidates = idx.candidate_blocks_byte(&filter);
                assert_eq!(candidates.len(), 0); // No matches expected
            }
            thread_id
        });
        handles.push(handle);
    }

    // Wait for all threads
    for handle in handles {
        handle.join().unwrap();
    }
}

// ============================================================================
// 11. Filter Edge Cases
// ============================================================================

#[test]
fn empty_pattern_filter_never_matches() {
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 256])
        .unwrap();

    let filter = ByteFilter::from_patterns(&[]);
    let candidates = index.candidate_blocks_byte(&filter);
    assert!(candidates.is_empty());
}

#[test]
fn single_byte_pattern_matches_all_with_byte() {
    let mut data = vec![0u8; 256];
    data[100] = b'x';

    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&data)
        .unwrap();

    let filter = ByteFilter::from_patterns(&[b"x".as_slice()]);
    let candidates = index.candidate_blocks_byte(&filter);
    assert_eq!(candidates.len(), 1);
}

#[test]
fn pattern_longer_than_block() {
    // Use minimum valid block size (256) for testing
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 256])
        .unwrap();

    // Pattern longer than block should still be queryable
    let filter = NgramFilter::from_patterns(&[
        b"this is a very long pattern that exceeds block size".as_slice(),
    ]);
    let _ = index.candidate_blocks_ngram(&filter);
    // Should not panic
}

// ============================================================================
// 12. Block Size Edge Cases
// ============================================================================

#[test]
fn minimum_block_size_256() {
    // 256 is minimum valid block size
    let result = BlockIndexBuilder::new().block_size(256).build(&[0u8; 256]);
    assert!(result.is_ok());
}

#[test]
fn block_size_not_power_of_two_rejected() {
    let result = BlockIndexBuilder::new()
        .block_size(300) // Not a power of two
        .build(&[0u8; 300]);
    assert!(result.is_err());
}

#[test]
fn block_size_too_small_rejected() {
    let result = BlockIndexBuilder::new()
        .block_size(128) // Less than 256
        .build(&[0u8; 128]);
    assert!(result.is_err());
}

// ============================================================================
// 13. Builder Edge Cases
// ============================================================================

#[test]
fn builder_zero_bloom_bits_rejected() {
    let result = BlockIndexBuilder::new().bloom_bits(0).build(&[0u8; 256]);
    assert!(result.is_err());
}

#[test]
fn streaming_build_empty_iterator() {
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build_streaming(std::iter::empty::<Vec<u8>>())
        .unwrap();

    assert_eq!(index.block_count(), 0);
    assert_eq!(index.total_data_length(), 0);
}

#[test]
fn streaming_build_wrong_block_size() {
    let blocks = vec![vec![0u8; 128]]; // Wrong size
    let result = BlockIndexBuilder::new()
        .block_size(256)
        .build_streaming(blocks.into_iter());
    assert!(result.is_err());
}

// ============================================================================
// 14. CandidateRange Tests
// ============================================================================

#[test]
fn merge_adjacent_empty() {
    let merged = BlockIndex::merge_adjacent(&[]);
    assert!(merged.is_empty());
}

#[test]
fn merge_adjacent_single() {
    use flashsieve::index::CandidateRange;

    let ranges = vec![CandidateRange {
        offset: 0,
        length: 256,
    }];
    let merged = BlockIndex::merge_adjacent(&ranges);
    assert_eq!(merged.len(), 1);
    assert_eq!(merged[0].offset, 0);
    assert_eq!(merged[0].length, 256);
}

#[test]
fn merge_adjacent_non_adjacent() {
    use flashsieve::index::CandidateRange;

    let ranges = vec![
        CandidateRange {
            offset: 0,
            length: 256,
        },
        CandidateRange {
            offset: 512,
            length: 256,
        }, // Gap of 256
    ];
    let merged = BlockIndex::merge_adjacent(&ranges);
    assert_eq!(merged.len(), 2);
}

// ============================================================================
// 15. Random Stress Test
// ============================================================================

#[test]
fn random_data_no_panic() {
    let mut rng = StdRng::seed_from_u64(0xBAD_C0DE);

    for _ in 0..100 {
        let block_size = 256;
        let num_blocks = rng.random_range(1..10);
        let mut data = vec![0u8; block_size * num_blocks];
        rng.fill_bytes(&mut data);

        let index = BlockIndexBuilder::new()
            .block_size(block_size)
            .bloom_bits(1024)
            .build(&data)
            .unwrap();

        // Generate random patterns
        for _ in 0..10 {
            let pattern_len = rng.random_range(1..20);
            let pattern: Vec<u8> = (0..pattern_len).map(|_| rng.random()).collect();

            let byte_filter = ByteFilter::from_patterns(&[pattern.as_slice()]);
            let ngram_filter = NgramFilter::from_patterns(&[pattern.as_slice()]);

            let candidates = index.candidate_blocks(&byte_filter, &ngram_filter);
            assert!(candidates.len() <= num_blocks);
        }
    }
}

// ============================================================================
// 16. Serialization Version Tests
// ============================================================================

#[test]
fn deserialize_version_1_format() {
    // Manually construct a version 1 (no CRC) serialized index
    // magic + version + block_size + total_len + block_count + histogram + bloom
    let mut data = Vec::new();
    data.extend_from_slice(b"FSBX"); // magic
    data.extend_from_slice(&1u32.to_le_bytes()); // version 1 (no CRC)
    data.extend_from_slice(&256u64.to_le_bytes()); // block_size
    data.extend_from_slice(&256u64.to_le_bytes()); // total_len
    data.extend_from_slice(&1u64.to_le_bytes()); // block_count

    // Histogram: 256 u32s (all zeros)
    for _ in 0..256 {
        data.extend_from_slice(&0u32.to_le_bytes());
    }

    // Bloom: num_bits + word_count + words
    data.extend_from_slice(&64u64.to_le_bytes()); // num_bits
    data.extend_from_slice(&1u64.to_le_bytes()); // word_count
    data.extend_from_slice(&0u64.to_le_bytes()); // single word

    // Version 1 should still parse (no CRC check)
    let result = BlockIndex::from_bytes(&data);
    assert!(result.is_some());
}

#[test]
fn deserialize_invalid_version_rejected() {
    let mut data = vec![0u8; 100];
    data[0..4].copy_from_slice(b"FSBX");
    data[4..8].copy_from_slice(&255u32.to_le_bytes()); // Invalid version

    assert!(BlockIndex::from_bytes(&data).is_none());
}

// ============================================================================
// 17. Composite Filter Tests
// ============================================================================

#[test]
fn composite_filter_and_semantics() {
    use flashsieve::filter::{CompositeFilter, FilterOp};

    let a = ByteFilter::from_patterns(&[b"abc".as_slice()]);
    let b = ByteFilter::from_patterns(&[b"def".as_slice()]);
    let combined = CompositeFilter::combine_byte(a, b, FilterOp::And);

    let hist_abc = ByteHistogram::from_block(b"abc");
    let hist_def = ByteHistogram::from_block(b"def");
    let hist_abcdef = ByteHistogram::from_block(b"abcdef");

    let bloom = NgramBloom::new(1024).unwrap();

    // AND: neither alone should match
    assert!(!combined.matches(&hist_abc, &bloom));
    assert!(!combined.matches(&hist_def, &bloom));

    // But together they should
    assert!(combined.matches(&hist_abcdef, &bloom));
}

#[test]
fn composite_filter_or_semantics() {
    use flashsieve::filter::{CompositeFilter, FilterOp};

    let a = ByteFilter::from_patterns(&[b"abc".as_slice()]);
    let b = ByteFilter::from_patterns(&[b"def".as_slice()]);
    let combined = CompositeFilter::combine_byte(a, b, FilterOp::Or);

    let hist_abc = ByteHistogram::from_block(b"abc");
    let bloom = NgramBloom::new(1024).unwrap();

    // OR: either should match
    assert!(combined.matches(&hist_abc, &bloom));
}

// ============================================================================
// 18. Pattern Matching Edge Cases
// ============================================================================

#[test]
fn empty_pattern_matches_all() {
    // Empty patterns have no requirements, so they match everything
    let bloom = NgramBloom::from_block(b"hello", 1024).unwrap();
    assert!(bloom.maybe_contains_pattern(b""));
}

#[test]
fn single_byte_pattern_matches_all() {
    // Single byte patterns have no n-grams, so they match
    let bloom = NgramBloom::from_block(b"hello", 1024).unwrap();
    assert!(bloom.maybe_contains_pattern(b"x"));
}

#[test]
fn pattern_exactly_at_block_boundary() {
    let mut data = vec![b'x'; 512];
    // Pattern fully contained in block 0 (not spanning boundary for simplicity)
    data[100..106].copy_from_slice(b"needle");

    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&data)
        .unwrap();

    // Test with byte filter - should find block 0
    let byte_filter = ByteFilter::from_patterns(&[b"needle".as_slice()]);
    let candidates = index.candidate_blocks_byte(&byte_filter);

    // Should find only block 0 (pattern is fully contained there)
    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].offset, 0);

    // Test with ngram filter
    let ngram_filter = NgramFilter::from_patterns(&[b"needle".as_slice()]);
    let candidates = index.candidate_blocks_ngram(&ngram_filter);

    // Should also find block 0
    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].offset, 0);
}

// ============================================================================
// 19. Selectivity Tests
// ============================================================================

#[test]
fn selectivity_zero_for_empty_candidates() {
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 256])
        .unwrap();

    let candidates = vec![];
    assert_eq!(index.selectivity(&candidates), 0.0);
}

#[test]
fn selectivity_full_coverage() {
    let index = BlockIndexBuilder::new()
        .block_size(256)
        .build(&[0u8; 256])
        .unwrap();

    use flashsieve::index::CandidateRange;
    let candidates = vec![CandidateRange {
        offset: 0,
        length: 256,
    }];
    assert_eq!(index.selectivity(&candidates), 1.0);
}

// ============================================================================
// 20. Bloom Filter Size Tests
// ============================================================================

#[test]
fn very_small_bloom_filter() {
    // Minimum practical size (will be rounded up)
    let bloom = NgramBloom::new(1).unwrap();
    let (num_bits, _) = bloom.raw_parts();
    assert!(num_bits >= 64);
}

#[test]
fn very_large_bloom_filter() {
    // Large filter that triggers exact_pairs table
    let bloom = NgramBloom::new(65536).unwrap();
    let (num_bits, _) = bloom.raw_parts();
    assert_eq!(num_bits, 65536);
}