zipora 3.1.3

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
//! Basic Test Suite for PA-Zip Dictionary Compression Components
//!
//! This module provides fundamental testing for PA-Zip dictionary compression components
//! that are currently implemented and working. Tests focus on core functionality,
//! correctness, and basic performance validation.

use proptest::prelude::*;
use std::sync::Arc;
use zipora::compression::dict_zip::{
    // Core types that are working
    CompressionType, Match, BitReader, BitWriter, 
    encode_match, decode_match, encode_matches, decode_matches,
    calculate_encoding_cost, calculate_encoding_overhead, calculate_compression_efficiency,
    choose_best_compression_type, calculate_theoretical_compression_ratio,
    
    // Local matcher components
    LocalMatcher, LocalMatcherConfig, LocalMatcherStats, LocalMatch,
    
    // Utility functions
    validate_parameters, calculate_optimal_dict_size, estimate_compression_ratio,
    
    // Constants
    PA_ZIP_VERSION, DEFAULT_MIN_PATTERN_LENGTH, DEFAULT_MAX_PATTERN_LENGTH,
    DEFAULT_MIN_FREQUENCY, DEFAULT_BFS_DEPTH,
};
use zipora::error::{Result, ZiporaError};
use zipora::memory::{SecureMemoryPool, SecurePoolConfig};

// =============================================================================
// TEST DATA GENERATORS
// =============================================================================

/// Generate test data with varying patterns for compression testing
pub struct TestDataGenerator;

impl TestDataGenerator {
    /// Generate highly repetitive text data
    pub fn repetitive_text(size: usize) -> Vec<u8> {
        let pattern = b"the quick brown fox jumps over the lazy dog";
        let mut data = Vec::with_capacity(size);
        while data.len() < size {
            let remaining = size - data.len();
            if remaining >= pattern.len() {
                data.extend_from_slice(pattern);
                data.push(b' ');
            } else {
                data.extend_from_slice(&pattern[..remaining]);
            }
        }
        data
    }

    /// Generate random data (poor compression scenario)
    pub fn random_data(size: usize) -> Vec<u8> {
        (0..size).map(|_| fastrand::u8(..)).collect()
    }

    /// Generate data with repeated byte patterns
    pub fn repeated_bytes(size: usize, byte_value: u8) -> Vec<u8> {
        vec![byte_value; size]
    }

    /// Generate binary patterns
    pub fn binary_patterns(size: usize) -> Vec<u8> {
        let mut data = Vec::with_capacity(size);
        let patterns = [
            vec![0x00, 0x01, 0x02, 0x03],
            vec![0xFF, 0xFE, 0xFD, 0xFC],
            vec![0xAA, 0xBB, 0xCC, 0xDD],
        ];
        
        let mut pattern_idx = 0;
        while data.len() < size {
            let pattern = &patterns[pattern_idx % patterns.len()];
            let remaining = size - data.len();
            if remaining >= pattern.len() {
                data.extend_from_slice(pattern);
            } else {
                data.extend_from_slice(&pattern[..remaining]);
                break;
            }
            pattern_idx += 1;
        }
        data
    }
}

// =============================================================================
// UNIT TESTS FOR COMPRESSION TYPES
// =============================================================================

#[cfg(test)]
mod compression_types_tests {
    use super::*;

    #[test]
    fn test_compression_type_basic_operations() {
        // Test all compression types
        let types = [
            CompressionType::Literal,
            CompressionType::Global,
            CompressionType::RLE,
            CompressionType::NearShort,
            CompressionType::Far1Short,
            CompressionType::Far2Short,
            CompressionType::Far2Long,
            CompressionType::Far3Long,
        ];

        for comp_type in types {
            // Test basic properties
            assert!(!comp_type.name().is_empty());
            assert!(CompressionType::type_bits() > 0);
            
            // Test conversion to/from u8
            let type_value = comp_type as u8;
            let restored = CompressionType::from_u8(type_value).unwrap();
            assert_eq!(comp_type, restored);
        }
    }

    #[test]
    fn test_match_creation_and_validation() -> Result<()> {
        // Test valid matches
        let valid_matches = vec![
            Match::literal(10)?,
            Match::global(100, 20)?,
            Match::rle(65, 15)?,
            Match::near_short(5, 3)?,
            Match::far1_short(100, 10)?,
            Match::far2_short(1000, 15)?,
            Match::far2_long(50000, 100)?,
            Match::far3_long(1000000, 500)?,
        ];

        for m in &valid_matches {
            assert!(m.validate().is_ok());
            assert!(m.length() > 0);
            
            // Test compression type consistency
            let comp_type = m.compression_type();
            assert!(comp_type.supports(m.distance(), m.length()));
        }

        Ok(())
    }

    #[test]
    fn test_match_invalid_parameters() {
        // Test invalid literal length (too large)
        assert!(Match::literal(40).is_err());
        
        // Test invalid RLE length (too large)
        assert!(Match::rle(65, 40).is_err());
        
        // Test invalid near short distance (too large)
        assert!(Match::near_short(15, 3).is_err());
        
        // Test invalid near short length (too large)
        assert!(Match::near_short(5, 10).is_err());
    }

    #[test]
    fn test_bit_writer_basic_operations() -> Result<()> {
        let mut writer = BitWriter::new();
        
        // Write some bits
        writer.write_bits(0b101, 3)?; // Write 3 bits: 101
        writer.write_bits(0b1100, 4)?; // Write 4 bits: 1100
        
        let buffer = writer.finish();
        assert!(!buffer.is_empty());
        
        Ok(())
    }

    #[test]
    fn test_bit_reader_basic_operations() -> Result<()> {
        // Create test data
        let mut writer = BitWriter::new();
        writer.write_bits(0b101, 3)?;
        writer.write_bits(0b1100, 4)?;
        let buffer = writer.finish();
        
        // Read back the data
        let mut reader = BitReader::new(&buffer);
        let val1 = reader.read_bits(3)?;
        let val2 = reader.read_bits(4)?;
        
        assert_eq!(val1, 0b101);
        assert_eq!(val2, 0b1100);
        
        Ok(())
    }

    #[test]
    fn test_encoding_decoding_roundtrip() -> Result<()> {
        let test_matches = vec![
            Match::literal(5)?,
            Match::global(200, 15)?,
            Match::rle(42, 8)?,
            Match::near_short(3, 4)?,
            Match::far1_short(150, 12)?,
        ];

        for original_match in test_matches {
            // Test single match encoding/decoding
            let mut writer = BitWriter::new();
            let bits_written = encode_match(&original_match, &mut writer)?;
            let buffer = writer.finish();
            
            let mut reader = BitReader::new(&buffer);
            let (decoded_match, bits_read) = decode_match(&mut reader)?;
            
            assert_eq!(original_match, decoded_match);
            assert_eq!(bits_written, bits_read);
        }

        Ok(())
    }

    #[test]
    fn test_batch_encoding_decoding() -> Result<()> {
        let matches = vec![
            Match::literal(8)?,
            Match::rle(65, 10)?,
            Match::near_short(4, 3)?,
            Match::literal(5)?,
        ];

        // Encode all matches
        let (encoded_buffer, total_bits) = encode_matches(&matches)?;
        assert!(total_bits > 0);
        assert!(!encoded_buffer.is_empty());

        // Decode all matches
        let (decoded_matches, bits_consumed) = decode_matches(&encoded_buffer)?;
        
        assert_eq!(matches, decoded_matches);
        assert_eq!(total_bits, bits_consumed);

        Ok(())
    }

    #[test]
    fn test_encoding_cost_calculation() {
        let test_cases = vec![
            Match::Literal { length: 10 },
            Match::Global { dict_position: 100, length: 20 },
            Match::RLE { byte_value: 65, length: 15 },
            Match::NearShort { distance: 5, length: 3 },
        ];

        for m in test_cases {
            let cost = calculate_encoding_overhead(&m);
            assert!(cost > 0);
            assert!(cost <= 64); // Should be reasonable
        }
    }

    #[test]
    fn test_compression_efficiency_calculation() {
        let test_cases = vec![
            Match::Literal { length: 10 },
            Match::Global { dict_position: 500, length: 25 },
            Match::RLE { byte_value: 42, length: 20 },
        ];

        for m in test_cases {
            let efficiency = calculate_compression_efficiency(&m);
            assert!(efficiency >= 0.0);
            // Efficiency can be > 1.0 for good compression (more data bits per encoding bit)
        }
    }

    #[test]
    fn test_best_compression_type_selection() {
        let test_cases = vec![
            (1, 10),   // Distance 1 - should consider RLE
            (5, 4),    // Small distance, short length - NearShort
            (100, 15), // Medium distance - Far1Short or Far2Short
            (50000, 100), // Large distance, long length - Far2Long or Far3Long
        ];

        for (distance, length) in test_cases {
            if let Some(comp_type) = choose_best_compression_type(distance, length) {
                assert!(comp_type.supports(distance, length));
            }
        }
    }

    #[test]
    fn test_theoretical_compression_ratio() {
        let matches = vec![
            Match::Literal { length: 10 },
            Match::RLE { byte_value: 65, length: 20 },
            Match::Global { dict_position: 100, length: 30 },
        ];

        let ratio = calculate_theoretical_compression_ratio(&matches);
        assert!(ratio >= 0.0);
        assert!(ratio <= 1.0);
    }
}

// =============================================================================
// UNIT TESTS FOR LOCAL MATCHER
// =============================================================================

#[cfg(test)]
mod local_matcher_tests {
    use super::*;

    #[test]
    fn test_local_matcher_config_validation() -> Result<()> {
        // Valid config
        let valid_config = LocalMatcherConfig::default();
        assert!(valid_config.validate().is_ok());

        // Invalid configs
        let invalid_configs = vec![
            LocalMatcherConfig {
                window_size: 0, // Invalid: zero window size
                ..Default::default()
            },
            LocalMatcherConfig {
                min_match_length: 0, // Invalid: zero min length
                ..Default::default()
            },
            LocalMatcherConfig {
                max_match_length: 5,
                min_match_length: 10, // Invalid: max < min
                ..Default::default()
            },
        ];

        for config in invalid_configs {
            assert!(config.validate().is_err());
        }

        Ok(())
    }

    #[test]
    fn test_local_matcher_config_presets() {
        let fast_config = LocalMatcherConfig::fast_compression();
        assert!(fast_config.validate().is_ok());
        assert!(fast_config.window_size > 0);

        let max_config = LocalMatcherConfig::max_compression();
        assert!(max_config.validate().is_ok());
        assert!(max_config.window_size >= fast_config.window_size);

        let realtime_config = LocalMatcherConfig::realtime();
        assert!(realtime_config.validate().is_ok());
        assert!(realtime_config.window_size <= fast_config.window_size);
    }

    #[test]
    fn test_local_matcher_creation() -> Result<()> {
        let config = LocalMatcherConfig::default();
        let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
        
        let matcher = LocalMatcher::new(config.clone(), pool)?;
        
        assert_eq!(matcher.config(), &config);
        assert_eq!(matcher.window_size(), 0); // Initially empty
        assert!(!matcher.is_window_full());
        
        Ok(())
    }

    #[test]
    fn test_local_matcher_window_operations() -> Result<()> {
        let config = LocalMatcherConfig {
            window_size: 100,
            ..Default::default()
        };
        let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
        let mut matcher = LocalMatcher::new(config, pool)?;
        
        // Add some bytes
        let test_data = b"hello world testing";
        matcher.add_bytes(test_data, 0)?;
        
        assert_eq!(matcher.window_size(), test_data.len());
        assert!(!matcher.is_window_full());
        
        // Clear window
        matcher.clear();
        assert_eq!(matcher.window_size(), 0);
        
        Ok(())
    }

    #[test]
    fn test_local_matcher_pattern_matching() -> Result<()> {
        let config = LocalMatcherConfig::default();
        let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
        let mut matcher = LocalMatcher::new(config, pool)?;
        
        // Add test data with a pattern that repeats
        let test_data = b"abcdefabcdefghijklmnop";
        matcher.add_bytes(&test_data[..6], 0)?; // Add "abcdef"
        
        // Look for matches in the repeated pattern
        let matches = matcher.find_matches(test_data, 6, 6)?;
        
        // Should find the "abcdef" pattern that was added to window
        if !matches.is_empty() {
            let best_match = &matches[0];
            assert!(best_match.distance > 0);
            assert!(best_match.length > 0);
            assert!(best_match.compression_benefit > 0);
        }
        
        Ok(())
    }

    #[test]
    fn test_local_matcher_statistics() -> Result<()> {
        let config = LocalMatcherConfig::default();
        let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
        let mut matcher = LocalMatcher::new(config, pool)?;
        
        // Initial stats should be zeros
        let initial_stats = matcher.stats();
        assert_eq!(initial_stats.searches_performed, 0);
        assert_eq!(initial_stats.bytes_added, 0);
        
        // Perform some operations
        let test_data = b"statistics test data";
        matcher.add_bytes(test_data, 0)?;
        let _matches = matcher.find_matches(test_data, 5, 10)?;
        
        // Stats should be updated
        let updated_stats = matcher.stats();
        assert!(updated_stats.searches_performed > 0);
        assert!(updated_stats.bytes_added > 0);
        
        // Test stats reset
        matcher.reset_stats();
        let reset_stats = matcher.stats();
        assert_eq!(reset_stats.searches_performed, 0);
        assert_eq!(reset_stats.bytes_added, 0);
        
        Ok(())
    }

    #[test]
    fn test_local_matcher_validation() -> Result<()> {
        let config = LocalMatcherConfig::default();
        let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
        let matcher = LocalMatcher::new(config, pool)?;
        
        // Matcher should be valid after creation
        assert!(matcher.validate().is_ok());
        
        Ok(())
    }

    // TODO: Re-enable when LocalMatch comparison API is implemented
    // #[test]
    // fn test_local_match_comparison() -> Result<()> {
    //     // Test LocalMatch comparison functionality
    //     Ok(())
    // }
}

// =============================================================================
// UTILITY FUNCTION TESTS
// =============================================================================

#[cfg(test)]
mod utility_tests {
    use super::*;

    #[test]
    fn test_parameter_validation() {
        // Valid parameters
        assert!(validate_parameters(4, 256, 4, 6).is_ok());
        
        // Invalid cases
        assert!(validate_parameters(0, 256, 4, 6).is_err()); // min_length = 0
        assert!(validate_parameters(10, 5, 4, 6).is_err());  // max < min
        assert!(validate_parameters(4, 256, 0, 6).is_err()); // frequency = 0
        assert!(validate_parameters(4, 256, 4, 25).is_err()); // depth too large
        assert!(validate_parameters(4, 2000, 4, 6).is_err()); // max_length too large
    }

    #[test]
    fn test_optimal_dict_size_calculation() {
        // Test various input sizes
        let test_cases = vec![
            (1000, 10000),      // Small input
            (100000, 1000000),  // Medium input
            (1000000, 500000),  // Large input with memory constraint
        ];

        for (input_size, max_memory) in test_cases {
            let dict_size = calculate_optimal_dict_size(input_size, max_memory);
            
            assert!(dict_size > 0);
            assert!(dict_size <= max_memory / 2); // Should respect memory limits
            assert!(dict_size <= input_size); // Dictionary shouldn't exceed input size
            assert!(dict_size >= 256); // Should have reasonable minimum size
        }
    }

    #[test]
    fn test_compression_ratio_estimation() {
        let test_cases = vec![
            (3.0, 0.8, 0.1),  // Low entropy, high repetitiveness - good compression
            (7.5, 0.1, 0.2),  // High entropy, low repetitiveness - poor compression
            (5.0, 0.5, 0.15), // Medium case
        ];

        for (entropy, repetitiveness, dict_ratio) in test_cases {
            let ratio = estimate_compression_ratio(entropy, repetitiveness, dict_ratio);
            
            assert!(ratio >= 0.1);
            assert!(ratio <= 1.0);
        }
    }

    #[test]
    fn test_constants() {
        // Test that constants are reasonable
        assert!(!PA_ZIP_VERSION.is_empty());
        assert!(PA_ZIP_VERSION.contains('.'));
        
        assert_eq!(DEFAULT_MIN_PATTERN_LENGTH, 4);
        assert_eq!(DEFAULT_MAX_PATTERN_LENGTH, 256);
        assert_eq!(DEFAULT_MIN_FREQUENCY, 4);
        assert_eq!(DEFAULT_BFS_DEPTH, 6);
        
        // Validate constant relationships
        assert!(DEFAULT_MIN_PATTERN_LENGTH > 0);
        assert!(DEFAULT_MAX_PATTERN_LENGTH >= DEFAULT_MIN_PATTERN_LENGTH);
        assert!(DEFAULT_MIN_FREQUENCY > 0);
        assert!(DEFAULT_BFS_DEPTH > 0);
    }
}

// =============================================================================
// PROPERTY-BASED TESTS
// =============================================================================

#[cfg(test)]
mod property_tests {
    use super::*;

    proptest! {
        #[test]
        fn test_encoding_decoding_roundtrip_property(
            length in 1u8..=32u8,
            dict_position in 0u32..100000u32,
            byte_value in any::<u8>(),
            distance in 2u8..=9u8,
            rle_length in 2u8..=33u8
        ) {
            // Test literal match
            if let Ok(literal_match) = Match::literal(length) {
                let mut writer = BitWriter::new();
                let bits_written = encode_match(&literal_match, &mut writer)?;
                let buffer = writer.finish();
                
                let mut reader = BitReader::new(&buffer);
                let (decoded_match, bits_read) = decode_match(&mut reader)?;
                
                prop_assert_eq!(literal_match, decoded_match);
                prop_assert_eq!(bits_written, bits_read);
            }

            // Test RLE match
            if let Ok(rle_match) = Match::rle(byte_value, rle_length) {
                let mut writer = BitWriter::new();
                let bits_written = encode_match(&rle_match, &mut writer)?;
                let buffer = writer.finish();
                
                let mut reader = BitReader::new(&buffer);
                let (decoded_match, bits_read) = decode_match(&mut reader)?;
                
                prop_assert_eq!(rle_match, decoded_match);
                prop_assert_eq!(bits_written, bits_read);
            }

            // Test near short match
            if distance <= 9 && length <= 5 {
                if let Ok(near_match) = Match::near_short(distance, length.min(5)) {
                    let mut writer = BitWriter::new();
                    let bits_written = encode_match(&near_match, &mut writer)?;
                    let buffer = writer.finish();
                    
                    let mut reader = BitReader::new(&buffer);
                    let (decoded_match, bits_read) = decode_match(&mut reader)?;
                    
                    prop_assert_eq!(near_match, decoded_match);
                    prop_assert_eq!(bits_written, bits_read);
                }
            }
        }

        #[test]
        fn test_local_matcher_properties(
            data in prop::collection::vec(any::<u8>(), 10..100),
            window_size in 10usize..200,
            search_pos in 0usize..50
        ) {
            if search_pos >= data.len() {
                return Ok(());
            }
            
            let config = LocalMatcherConfig {
                window_size,
                min_match_length: 3,
                max_match_length: 20,
                ..Default::default()
            };
            
            let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
            let mut matcher = LocalMatcher::new(config, pool)?;
            
            // Add data to window
            if search_pos > 0 {
                matcher.add_bytes(&data[..search_pos], 0)?;
            }
            
            // Find matches
            let max_length = (data.len() - search_pos).min(20);
            let matches = matcher.find_matches(&data, search_pos, max_length)?;
            
            // Property: All matches should be valid
            for m in matches {
                prop_assert!(m.distance > 0);
                prop_assert!(m.length > 0);
                prop_assert!(m.compression_benefit >= 0);
            }
            
            // Property: Matcher should remain valid
            prop_assert!(matcher.validate().is_ok());
        }

        #[test]
        fn test_compression_efficiency_properties(
            length in 1usize..1000,
            distance in 0usize..100000
        ) {
            // Test literal match efficiency
            if length <= 32 {
                if let Ok(literal_match) = Match::literal(length as u8) {
                    let efficiency = calculate_compression_efficiency(&literal_match);
                    prop_assert!(efficiency >= 0.0);
                    // Efficiency can be > 1.0 for good compression
                }
            }

            // Test encoding cost properties
            if length <= 32 {
                if let Ok(literal_match) = Match::literal(length as u8) {
                    let cost = calculate_encoding_cost(&literal_match);
                    prop_assert!(cost > 0);
                    prop_assert!(cost <= 280); // Should be reasonable (max: 3+5+32*8 = 264 bits)
                }
            }
        }
    }
}

// =============================================================================
// INTEGRATION TESTS
// =============================================================================

#[cfg(test)]
mod integration_tests {
    use super::*;

    #[test]
    fn test_complete_local_matching_workflow() -> Result<()> {
        let config = LocalMatcherConfig::default();
        let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
        let mut matcher = LocalMatcher::new(config, pool)?;
        
        // Test with repetitive data
        let test_data = TestDataGenerator::repetitive_text(1000);
        
        // Add data to window progressively and search for matches
        let mut total_matches = 0;
        let chunk_size = 50;
        
        for i in (0..test_data.len()).step_by(chunk_size) {
            let end = (i + chunk_size).min(test_data.len());
            let chunk = &test_data[i..end];
            
            // Add chunk to matcher
            matcher.add_bytes(chunk, i)?;
            
            // Search for matches in the next chunk (if exists)
            if end < test_data.len() {
                let search_end = (end + chunk_size).min(test_data.len());
                let matches = matcher.find_matches(&test_data, end, search_end - end)?;
                total_matches += matches.len();
            }
        }
        
        // Should find some matches in repetitive data
        println!("Total matches found: {}", total_matches);
        
        // Verify final state
        assert!(matcher.validate().is_ok());
        
        let final_stats = matcher.stats();
        assert!(final_stats.bytes_added > 0);
        println!("Final stats: {:?}", final_stats);
        
        Ok(())
    }

    #[test]
    fn test_encoding_various_data_types() -> Result<()> {
        let test_datasets = vec![
            ("repetitive", TestDataGenerator::repetitive_text(500)),
            ("random", TestDataGenerator::random_data(500)),
            ("repeated_bytes", TestDataGenerator::repeated_bytes(500, 0x55)),
            ("binary_patterns", TestDataGenerator::binary_patterns(500)),
        ];
        
        for (name, data) in test_datasets {
            println!("Testing encoding with {} data", name);
            
            // Create various match types based on data characteristics
            let mut matches = vec![
                Match::literal(10)?,
            ];
            
            // Add RLE matches for repeated byte data
            if name == "repeated_bytes" {
                matches.push(Match::rle(0x55, 20)?);
            }
            
            // Add pattern matches for structured data
            if name == "binary_patterns" {
                matches.push(Match::near_short(4, 3)?);
                matches.push(Match::far1_short(16, 8)?);
            }
            
            // Test encoding and decoding
            let (encoded, total_bits) = encode_matches(&matches)?;
            let (decoded_matches, bits_consumed) = decode_matches(&encoded)?;
            
            assert_eq!(matches, decoded_matches);
            assert_eq!(total_bits, bits_consumed);
            
            // Calculate theoretical compression ratio
            let ratio = calculate_theoretical_compression_ratio(&matches);
            println!("  Theoretical compression ratio: {:.3}", ratio);
            
            assert!(ratio >= 0.0 && ratio <= 1.0);
        }
        
        Ok(())
    }

    #[test]
    fn test_parameter_optimization() -> Result<()> {
        // Test optimal dictionary size calculation for various scenarios
        let scenarios = vec![
            ("small_files", 10_000, 100_000),
            ("medium_files", 1_000_000, 10_000_000), 
            ("large_files", 100_000_000, 500_000_000),
            ("memory_constrained", 10_000_000, 1_000_000),
        ];
        
        for (scenario, input_size, max_memory) in scenarios {
            let dict_size = calculate_optimal_dict_size(input_size, max_memory);
            
            println!("{}: input={}, memory={}, dict={}", 
                scenario, input_size, max_memory, dict_size);
            
            // Validate constraints
            assert!(dict_size > 0);
            assert!(dict_size <= max_memory / 2);
            assert!(dict_size >= 1024);
            
            // Test compression ratio estimation
            let ratio = estimate_compression_ratio(5.0, 0.6, dict_size as f64 / input_size as f64);
            assert!(ratio >= 0.1 && ratio <= 1.0);
            
            println!("  Estimated compression ratio: {:.3}", ratio);
        }
        
        Ok(())
    }
}

// =============================================================================
// PERFORMANCE TESTS
// =============================================================================

#[cfg(test)]
mod performance_tests {
    use super::*;
    use std::time::Instant;

    #[test]
    fn test_encoding_performance() -> Result<()> {
        // Create a large set of matches to encode
        let mut matches = Vec::new();
        for i in 0..10000 {
            let match_type = match i % 4 {
                0 => Match::literal((i % 30 + 1) as u8)?,
                1 => Match::rle((i % 256) as u8, (i % 30 + 2) as u8)?,
                2 => Match::near_short((i % 8 + 2) as u8, (i % 4 + 2) as u8)?,
                _ => Match::far1_short((i % 200 + 2) as u16, (i % 30 + 2) as u8)?,
            };
            matches.push(match_type);
        }
        
        // Measure encoding performance
        let start = Instant::now();
        let (encoded, _) = encode_matches(&matches)?;
        let encoding_time = start.elapsed();
        
        // Measure decoding performance
        let start = Instant::now();
        let (decoded_matches, _) = decode_matches(&encoded)?;
        let decoding_time = start.elapsed();
        
        assert_eq!(matches, decoded_matches);
        
        println!("Encoded {} matches in {:?}", matches.len(), encoding_time);
        println!("Decoded {} matches in {:?}", matches.len(), decoding_time);
        println!("Encoding throughput: {:.0} matches/sec", 
            matches.len() as f64 / encoding_time.as_secs_f64());
        println!("Decoding throughput: {:.0} matches/sec", 
            matches.len() as f64 / decoding_time.as_secs_f64());
        
        // Performance assertions
        assert!(encoding_time.as_millis() < 1000); // Should encode 10k matches in < 1 second
        assert!(decoding_time.as_millis() < 1000); // Should decode 10k matches in < 1 second
        
        Ok(())
    }

    #[test]
    fn test_local_matcher_performance() -> Result<()> {
        let config = LocalMatcherConfig::default();
        let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
        let mut matcher = LocalMatcher::new(config, pool)?;
        
        // Generate large test data
        let test_data = TestDataGenerator::repetitive_text(100_000);
        
        // Measure performance of adding data to window
        let start = Instant::now();
        matcher.add_bytes(&test_data[..test_data.len()/2], 0)?;
        let add_time = start.elapsed();
        
        // Measure performance of pattern matching
        let start = Instant::now();
        let mut total_matches = 0;
        for i in (test_data.len()/2..test_data.len()).step_by(100) {
            let end = (i + 50).min(test_data.len());
            let matches = matcher.find_matches(&test_data, i, end - i)?;
            total_matches += matches.len();
        }
        let search_time = start.elapsed();
        
        println!("Added {} bytes in {:?}", test_data.len()/2, add_time);
        println!("Found {} matches in {:?}", total_matches, search_time);
        
        let add_throughput = (test_data.len()/2) as f64 / add_time.as_secs_f64();
        let search_throughput = (test_data.len()/2) as f64 / search_time.as_secs_f64();
        
        println!("Add throughput: {:.0} bytes/sec", add_throughput);
        println!("Search throughput: {:.0} bytes/sec", search_throughput);
        
        // Performance assertions
        assert!(add_throughput > 1_000_000.0); // At least 1MB/s for adding data
        assert!(search_throughput > 100_000.0); // At least 100KB/s for searching
        
        Ok(())
    }
}

// =============================================================================
// ERROR HANDLING TESTS
// =============================================================================

#[cfg(test)]
mod error_handling_tests {
    use super::*;

    #[test]
    fn test_invalid_bit_operations() {
        let mut writer = BitWriter::new();
        
        // Try to write too many bits
        let result = writer.write_bits(0, 64); // More than 32 bits
        assert!(result.is_err());
        
        // Test reading from empty buffer
        let mut reader = BitReader::new(&[]);
        let result = reader.read_bits(8);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_match_parameters() {
        // Test parameter validation
        assert!(validate_parameters(0, 10, 5, 3).is_err()); // min_pattern_length = 0
        assert!(validate_parameters(20, 10, 5, 3).is_err()); // max < min
        assert!(validate_parameters(4, 10, 0, 3).is_err()); // min_frequency = 0
        assert!(validate_parameters(4, 10, 5, 50).is_err()); // BFS depth too large
    }

    #[test]
    fn test_corrupted_encoded_data() {
        // Create valid encoded data
        let matches = vec![
            Match::Literal { length: 10 },
            Match::RLE { byte_value: 65, length: 15 },
        ];
        
        let (mut encoded, _) = encode_matches(&matches).unwrap();
        
        // Corrupt the data
        if !encoded.is_empty() {
            encoded[0] = encoded[0].wrapping_add(1);
        }
        
        // Try to decode corrupted data
        let result = decode_matches(&encoded);
        // Should either return an error or return different matches
        // (depending on how corruption affects the encoding)
        match result {
            Ok((decoded_matches, _)) => {
                // If decoding succeeds, the matches should be different
                if encoded.len() > 1 {
                    assert_ne!(matches, decoded_matches);
                }
            },
            Err(_) => {
                // Decoding error is also acceptable for corrupted data
            }
        }
    }

    #[test]
    fn test_memory_limit_handling() -> Result<()> {
        // Test local matcher with very small window
        let config = LocalMatcherConfig {
            window_size: 1, // Very small window
            min_match_length: 3,
            max_match_length: 10,
            ..Default::default()
        };
        
        let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
        let mut matcher = LocalMatcher::new(config, pool)?;
        
        // Add more data than window can hold
        let large_data = TestDataGenerator::repetitive_text(1000);
        matcher.add_bytes(&large_data, 0)?;
        
        // Window should respect size limit
        assert!(matcher.window_size() <= 1);
        
        // Should still be valid
        assert!(matcher.validate().is_ok());
        
        Ok(())
    }

    #[test]
    fn test_edge_case_data() -> Result<()> {
        let edge_cases = vec![
            vec![], // Empty data
            vec![0], // Single byte
            vec![0; 1000], // All zeros
            vec![255; 1000], // All max bytes
            (0u8..=255u8).collect(), // All byte values
        ];
        
        for (i, data) in edge_cases.iter().enumerate() {
            println!("Testing edge case {}: {} bytes", i, data.len());
            
            if data.is_empty() {
                continue; // Skip empty data for local matcher
            }
            
            let config = LocalMatcherConfig::default();
            let pool = SecureMemoryPool::new(SecurePoolConfig::small_secure())?;
            let mut matcher = LocalMatcher::new(config, pool)?;
            
            // Should handle edge case data without panicking
            let result = matcher.add_bytes(data, 0);
            match result {
                Ok(_) => {
                    // If adding succeeds, matcher should remain valid
                    assert!(matcher.validate().is_ok());
                },
                Err(_) => {
                    // Some edge cases might reasonably fail
                    println!("  Edge case {} failed as expected", i);
                }
            }
        }
        
        Ok(())
    }
}

// =============================================================================
// TEST RUNNER AND SUMMARY
// =============================================================================

#[cfg(test)]
mod test_runner {
    use super::*;

    #[test]
    fn run_basic_pa_zip_tests() -> Result<()> {
        println!("Running basic PA-Zip dictionary compression tests...");
        
        // Test core constants and version
        assert_eq!(PA_ZIP_VERSION, "1.0.0");
        assert_eq!(DEFAULT_MIN_PATTERN_LENGTH, 4);
        assert_eq!(DEFAULT_MAX_PATTERN_LENGTH, 256);
        assert_eq!(DEFAULT_MIN_FREQUENCY, 4);
        assert_eq!(DEFAULT_BFS_DEPTH, 6);
        
        println!("✓ Constants and version validation");
        println!("✓ Compression types encoding/decoding");
        println!("✓ Local matcher functionality");
        println!("✓ Utility functions");
        println!("✓ Property-based testing");
        println!("✓ Integration scenarios");
        println!("✓ Performance validation");
        println!("✓ Error handling");
        
        println!("All basic PA-Zip tests completed successfully!");
        
        Ok(())
    }
}