@@ -373,8 +373,8 @@ pub fn checksum(algorithm: CrcAlgorithm, buf: &[u8]) -> u64 {
373
373
/// use crc_fast::{checksum_file, CrcAlgorithm::Crc32IsoHdlc};
374
374
///
375
375
/// // for example/test purposes only, use your own file path
376
- /// let binding = env::current_dir().expect("missing working dir").join("crc-check.txt");
377
- /// let file_on_disk = binding .to_str().unwrap();
376
+ /// let file_path = env::current_dir().expect("missing working dir").join("crc-check.txt");
377
+ /// let file_on_disk = file_path .to_str().unwrap();
378
378
///
379
379
/// let checksum = checksum_file(Crc32IsoHdlc, file_on_disk, None);
380
380
///
@@ -547,68 +547,52 @@ mod lib {
547
547
548
548
#[ test]
549
549
fn test_small_all_lengths ( ) {
550
- let mut rng = rng ( ) ;
551
-
552
- // Test each CRC-64 variant
553
550
for config in TEST_ALL_CONFIGS {
554
551
// Test each length from 1 to 255
555
552
for len in 1 ..=255 {
556
- // Generate random data for this length
557
- let mut data = vec ! [ 0u8 ; len] ;
558
- rng. fill ( & mut data[ ..] ) ;
559
-
560
- // Calculate expected CRC using the reference implementation
561
- let expected = config. checksum_with_reference ( & data) ;
562
-
563
- let result = checksum ( config. get_algorithm ( ) , & data) ;
564
-
565
- assert_eq ! ( result, expected) ;
553
+ test_length ( len, config) ;
566
554
}
567
555
}
568
556
}
569
557
570
558
#[ test]
571
559
fn test_medium_lengths ( ) {
572
- let mut rng = rng ( ) ;
573
-
574
- // Test each CRC-64 variant
575
560
for config in TEST_ALL_CONFIGS {
576
561
// Test each length from 256 to 1024, which should fold and include handling remainders
577
562
for len in 256 ..=1024 {
578
- // Generate random data for this length
579
- let mut data = vec ! [ 0u8 ; len] ;
580
- rng. fill ( & mut data[ ..] ) ;
581
-
582
- // Calculate expected CRC using the reference implementation
583
- let expected = config. checksum_with_reference ( & data) ;
584
-
585
- let result = checksum ( config. get_algorithm ( ) , & data) ;
586
-
587
- assert_eq ! ( result, expected) ;
563
+ test_length ( len, config) ;
588
564
}
589
565
}
590
566
}
591
567
592
568
#[ test]
593
569
fn test_large_lengths ( ) {
594
- let mut rng = rng ( ) ;
595
-
596
- // Test each CRC-64 variant
597
570
for config in TEST_ALL_CONFIGS {
598
571
// Test 1 MiB just before, at, and just after the folding boundaries
599
572
for len in 1048575 ..1048577 {
600
- // Generate random data for this length
601
- let mut data = vec ! [ 0u8 ; len] ;
602
- rng. fill ( & mut data[ ..] ) ;
573
+ test_length ( len, config) ;
574
+ }
575
+ }
576
+ }
603
577
604
- // Calculate expected CRC using the reference implementation
605
- let expected = config. checksum_with_reference ( & data) ;
578
+ fn test_length ( length : usize , config : & AnyCrcTestConfig ) {
579
+ let mut data = vec ! [ 0u8 ; length] ;
580
+ rng ( ) . fill ( & mut data[ ..] ) ;
606
581
607
- let result = checksum ( config. get_algorithm ( ) , & data) ;
582
+ // Calculate expected CRC using the reference implementation
583
+ let expected = config. checksum_with_reference ( & data) ;
608
584
609
- assert_eq ! ( result, expected) ;
610
- }
611
- }
585
+ let result = checksum ( config. get_algorithm ( ) , & data) ;
586
+
587
+ assert_eq ! (
588
+ result,
589
+ expected,
590
+ "Failed for algorithm: {:?}, length: {}, expected: {:#x}, got: {:#x}" ,
591
+ config. get_algorithm( ) ,
592
+ length,
593
+ expected,
594
+ result
595
+ ) ;
612
596
}
613
597
614
598
#[ test]
@@ -759,57 +743,53 @@ mod lib {
759
743
return Ok ( ( ) ) ;
760
744
}
761
745
762
- #[ cfg( not( target_os = "windows" ) ) ]
763
- {
764
- const HEADER : & str = "libcrc_fast.h" ;
765
-
766
- let crate_dir =
767
- std:: env:: var ( "CARGO_MANIFEST_DIR" ) . map_err ( |error| error. to_string ( ) ) ?;
768
-
769
- let mut expected = Vec :: new ( ) ;
770
- cbindgen:: Builder :: new ( )
771
- . with_crate ( crate_dir)
772
- . with_include_guard ( "CRC_FAST_H" )
773
- . with_header ( "/* crc_fast library C/C++ API - Copyright 2025 Don MacAskill */\n /* This header is auto-generated. Do not edit directly. */\n " )
774
- // exclude internal implementation functions
775
- . exclude_item ( "crc32_iscsi_impl" )
776
- . exclude_item ( "crc32_iso_hdlc_impl" )
777
- . exclude_item ( "get_iscsi_target" )
778
- . exclude_item ( "get_iso_hdlc_target" )
779
- . exclude_item ( "ISO_HDLC_TARGET" )
780
- . exclude_item ( "ISCSI_TARGET" )
781
- . exclude_item ( "CrcParams" )
782
- . rename_item ( "Digest" , "CrcFastDigest" )
783
- . with_style ( Both )
784
- // generate C header
785
- . with_language ( C )
786
- // with C++ compatibility
787
- . with_cpp_compat ( true )
788
- . generate ( )
789
- . map_err ( |error| error. to_string ( ) ) ?
790
- . write ( & mut expected) ;
791
-
792
- // Convert the expected bytes to string for pattern replacement, since cbindgen
793
- // generates an annoying amount of empty contiguous newlines
794
- let header_content = String :: from_utf8 ( expected) . map_err ( |error| error. to_string ( ) ) ?;
795
-
796
- // Replace excessive newlines (3 or more consecutive newlines) with 2 newlines
797
- let regex = regex:: Regex :: new ( r"\n{3,}" ) . map_err ( |error| error. to_string ( ) ) ?;
798
- let cleaned_content = regex. replace_all ( & header_content, "\n \n " ) . to_string ( ) ;
799
-
800
- // Convert back to bytes
801
- expected = cleaned_content. into_bytes ( ) ;
802
-
803
- let actual = read ( HEADER ) . map_err ( |error| error. to_string ( ) ) ?;
804
-
805
- if expected != actual {
806
- write ( HEADER , expected) . map_err ( |error| error. to_string ( ) ) ?;
807
- return Err ( format ! (
808
- "{HEADER} is not up-to-date, commit the generated file and try again"
809
- ) ) ;
810
- }
811
-
812
- Ok ( ( ) )
746
+ const HEADER : & str = "libcrc_fast.h" ;
747
+
748
+ let crate_dir = std:: env:: var ( "CARGO_MANIFEST_DIR" ) . map_err ( |error| error. to_string ( ) ) ?;
749
+
750
+ let mut expected = Vec :: new ( ) ;
751
+ cbindgen:: Builder :: new ( )
752
+ . with_crate ( crate_dir)
753
+ . with_include_guard ( "CRC_FAST_H" )
754
+ . with_header ( "/* crc_fast library C/C++ API - Copyright 2025 Don MacAskill */\n /* This header is auto-generated. Do not edit directly. */\n " )
755
+ // exclude internal implementation functions
756
+ . exclude_item ( "crc32_iscsi_impl" )
757
+ . exclude_item ( "crc32_iso_hdlc_impl" )
758
+ . exclude_item ( "get_iscsi_target" )
759
+ . exclude_item ( "get_iso_hdlc_target" )
760
+ . exclude_item ( "ISO_HDLC_TARGET" )
761
+ . exclude_item ( "ISCSI_TARGET" )
762
+ . exclude_item ( "CrcParams" )
763
+ . rename_item ( "Digest" , "CrcFastDigest" )
764
+ . with_style ( Both )
765
+ // generate C header
766
+ . with_language ( C )
767
+ // with C++ compatibility
768
+ . with_cpp_compat ( true )
769
+ . generate ( )
770
+ . map_err ( |error| error. to_string ( ) ) ?
771
+ . write ( & mut expected) ;
772
+
773
+ // Convert the expected bytes to string for pattern replacement, since cbindgen
774
+ // generates an annoying amount of empty contiguous newlines
775
+ let header_content = String :: from_utf8 ( expected) . map_err ( |error| error. to_string ( ) ) ?;
776
+
777
+ // Replace excessive newlines (3 or more consecutive newlines) with 2 newlines
778
+ let regex = regex:: Regex :: new ( r"\n{3,}" ) . map_err ( |error| error. to_string ( ) ) ?;
779
+ let cleaned_content = regex. replace_all ( & header_content, "\n \n " ) . to_string ( ) ;
780
+
781
+ // Convert back to bytes
782
+ expected = cleaned_content. into_bytes ( ) ;
783
+
784
+ let actual = read ( HEADER ) . map_err ( |error| error. to_string ( ) ) ?;
785
+
786
+ if expected != actual {
787
+ write ( HEADER , expected) . map_err ( |error| error. to_string ( ) ) ?;
788
+ return Err ( format ! (
789
+ "{HEADER} is not up-to-date, commit the generated file and try again"
790
+ ) ) ;
813
791
}
792
+
793
+ Ok ( ( ) )
814
794
}
815
795
}
0 commit comments