diff --git a/src/concat/mod.rs b/src/concat/mod.rs index d4c3dc77..93ac76ac 100644 --- a/src/concat/mod.rs +++ b/src/concat/mod.rs @@ -575,9 +575,10 @@ impl BroCatli { } } +#[cfg(test)] mod test { - #[cfg(test)] use super::BroCatli; + #[test] fn test_deserialization() { let broccoli = BroCatli { diff --git a/src/enc/backward_references/test.rs b/src/enc/backward_references/test.rs index db749a94..d92f48a5 100644 --- a/src/enc/backward_references/test.rs +++ b/src/enc/backward_references/test.rs @@ -1,5 +1,5 @@ -#![cfg(feature = "std")] #![cfg(test)] +#![cfg(feature = "std")] use alloc_stdlib::StandardAlloc; diff --git a/src/enc/command.rs b/src/enc/command.rs index 78417b25..6abb6b19 100644 --- a/src/enc/command.rs +++ b/src/enc/command.rs @@ -240,9 +240,83 @@ impl Command { } } +pub fn RecomputeDistancePrefixes( + cmds: &mut [Command], + num_commands: usize, + num_direct_distance_codes: u32, + distance_postfix_bits: u32, + dist: &BrotliDistanceParams, +) { + if num_direct_distance_codes == 0u32 && (distance_postfix_bits == 0u32) { + return; + } + for i in 0usize..num_commands { + let cmd: &mut Command = &mut cmds[i]; + if cmd.copy_len() != 0 && cmd.cmd_prefix_ >= 128 { + PrefixEncodeCopyDistance( + cmd.restore_distance_code(dist) as usize, + num_direct_distance_codes as usize, + distance_postfix_bits as (u64), + &mut cmd.dist_prefix_, + &mut cmd.dist_extra_, + ); + } + } +} + +impl Command { + pub fn init( + &mut self, + dist: &BrotliDistanceParams, + insertlen: usize, + copylen: usize, + copylen_code: usize, + distance_code: usize, + ) { + self.insert_len_ = insertlen as u32; + let copylen_code_delta = (copylen_code as i32 - copylen as i32) as i8; + self.copy_len_ = (copylen as u32 | (u32::from(copylen_code_delta as u8) << 25)); + PrefixEncodeCopyDistance( + distance_code, + dist.num_direct_distance_codes as usize, + u64::from(dist.distance_postfix_bits), + &mut self.dist_prefix_, + &mut self.dist_extra_, + ); + GetLengthCode( + insertlen, + copylen_code, + if (self.dist_prefix_ & 0x3ff) == 0 { + 1 + } else { + 0 + }, + &mut self.cmd_prefix_, + ); + } + + pub fn new( + dist: &BrotliDistanceParams, + insertlen: usize, + copylen: usize, + copylen_code: usize, + distance_code: usize, + ) -> Self { + let mut cmd = Command { + insert_len_: insertlen as u32, + copy_len_: (copylen | ((copylen_code ^ copylen) << 25)) as u32, + dist_extra_: 0, + cmd_prefix_: 0, + dist_prefix_: 0, + }; + cmd.init(dist, insertlen, copylen, copylen_code, distance_code); + cmd + } +} + +#[cfg(test)] mod test { // returns which distance code to use ( 0 means none, 1 means last, 2 means penultimate, 3 means the prior to penultimate - #[cfg(test)] pub fn helperCommandDistanceIndexAndOffset( cmd: &super::Command, dist: &super::BrotliDistanceParams, @@ -363,76 +437,3 @@ mod test { } }*/ } -pub fn RecomputeDistancePrefixes( - cmds: &mut [Command], - num_commands: usize, - num_direct_distance_codes: u32, - distance_postfix_bits: u32, - dist: &BrotliDistanceParams, -) { - if num_direct_distance_codes == 0u32 && (distance_postfix_bits == 0u32) { - return; - } - for i in 0usize..num_commands { - let cmd: &mut Command = &mut cmds[i]; - if cmd.copy_len() != 0 && cmd.cmd_prefix_ >= 128 { - PrefixEncodeCopyDistance( - cmd.restore_distance_code(dist) as usize, - num_direct_distance_codes as usize, - distance_postfix_bits as (u64), - &mut cmd.dist_prefix_, - &mut cmd.dist_extra_, - ); - } - } -} - -impl Command { - pub fn init( - &mut self, - dist: &BrotliDistanceParams, - insertlen: usize, - copylen: usize, - copylen_code: usize, - distance_code: usize, - ) { - self.insert_len_ = insertlen as u32; - let copylen_code_delta = (copylen_code as i32 - copylen as i32) as i8; - self.copy_len_ = (copylen as u32 | (u32::from(copylen_code_delta as u8) << 25)); - PrefixEncodeCopyDistance( - distance_code, - dist.num_direct_distance_codes as usize, - u64::from(dist.distance_postfix_bits), - &mut self.dist_prefix_, - &mut self.dist_extra_, - ); - GetLengthCode( - insertlen, - copylen_code, - if (self.dist_prefix_ & 0x3ff) == 0 { - 1 - } else { - 0 - }, - &mut self.cmd_prefix_, - ); - } - - pub fn new( - dist: &BrotliDistanceParams, - insertlen: usize, - copylen: usize, - copylen_code: usize, - distance_code: usize, - ) -> Self { - let mut cmd = Command { - insert_len_: insertlen as u32, - copy_len_: (copylen | ((copylen_code ^ copylen) << 25)) as u32, - dist_extra_: 0, - cmd_prefix_: 0, - dist_prefix_: 0, - }; - cmd.init(dist, insertlen, copylen, copylen_code, distance_code); - cmd - } -} diff --git a/src/enc/interface.rs b/src/enc/interface.rs index 41ac6ea5..79f2bd16 100644 --- a/src/enc/interface.rs +++ b/src/enc/interface.rs @@ -761,6 +761,7 @@ pub fn u8_to_speed(data: u8) -> u16 { (1u16 << log_val) | (rem >> 3) } } + #[cfg(test)] mod test { use super::{speed_to_u8, u8_to_speed}; diff --git a/src/enc/static_dict.rs b/src/enc/static_dict.rs index 9bfa2893..fa8f7a36 100644 --- a/src/enc/static_dict.rs +++ b/src/enc/static_dict.rs @@ -234,102 +234,6 @@ pub fn ComplexFindMatchLengthWithLimit(mut s1: &[u8], mut s2: &[u8], mut limit: matched + (limit & 7usize) // made it through the loop } -mod test { - #[allow(unused)] - fn construct_situation(seed: &[u8], mut output: &mut [u8], limit: usize, matchfor: usize) { - output[..].clone_from_slice(seed); - if matchfor >= limit { - return; - } - output[matchfor] = output[matchfor].wrapping_add((matchfor as u8 % 253u8).wrapping_add(1)); - } - #[test] - fn test_find_match_length() { - let mut a = [91u8; 600000]; - let mut b = [0u8; 600000]; - for i in 1..a.len() { - a[i] = (a[i - 1] % 19u8).wrapping_add(17); - } - construct_situation(&a[..], &mut b[..], a.len(), 0); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 0); - construct_situation(&a[..], &mut b[..], a.len(), 1); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 1); - construct_situation(&a[..], &mut b[..], a.len(), 10); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 10); - construct_situation(&a[..], &mut b[..], a.len(), 9); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 9); - construct_situation(&a[..], &mut b[..], a.len(), 7); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 7); - construct_situation(&a[..], &mut b[..], a.len(), 8); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 8); - construct_situation(&a[..], &mut b[..], a.len(), 48); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 48); - construct_situation(&a[..], &mut b[..], a.len(), 49); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 49); - construct_situation(&a[..], &mut b[..], a.len(), 63); - assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 63); - construct_situation(&a[..], &mut b[..], a.len(), 222); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 222 - ); - construct_situation(&a[..], &mut b[..], a.len(), 1590); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 1590 - ); - construct_situation(&a[..], &mut b[..], a.len(), 12590); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 12590 - ); - construct_situation(&a[..], &mut b[..], a.len(), 52592); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 52592 - ); - construct_situation(&a[..], &mut b[..], a.len(), 152592); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 152592 - ); - construct_situation(&a[..], &mut b[..], a.len(), 252591); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 252591 - ); - construct_situation(&a[..], &mut b[..], a.len(), 131072); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 131072 - ); - construct_situation(&a[..], &mut b[..], a.len(), 131073); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 131073 - ); - construct_situation(&a[..], &mut b[..], a.len(), 131072 + 64 + 32 + 16 + 8); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 131072 + 64 + 32 + 16 + 8 - ); - construct_situation(&a[..], &mut b[..], a.len(), 272144 + 64 + 32 + 16 + 8 + 1); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 272144 + 64 + 32 + 16 + 8 + 1 - ); - construct_situation(&a[..], &mut b[..], a.len(), 2 * 272144 + 64 + 32 + 16 + 8); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - 2 * 272144 + 64 + 32 + 16 + 8 - ); - construct_situation(&a[..], &mut b[..], a.len(), a.len()); - assert_eq!( - super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), - a.len() - ); - } -} #[allow(unused)] pub fn slowFindMatchLengthWithLimit(s1: &[u8], s2: &[u8], limit: usize) -> usize { for (index, it) in s1[..limit].iter().zip(s2[..limit].iter()).enumerate() { @@ -1398,3 +1302,101 @@ pub fn BrotliFindAllStaticDictionaryMatches( } has_found_match } + +#[cfg(test)] +mod test { + #[allow(unused)] + fn construct_situation(seed: &[u8], mut output: &mut [u8], limit: usize, matchfor: usize) { + output[..].clone_from_slice(seed); + if matchfor >= limit { + return; + } + output[matchfor] = output[matchfor].wrapping_add((matchfor as u8 % 253u8).wrapping_add(1)); + } + #[test] + fn test_find_match_length() { + let mut a = [91u8; 600000]; + let mut b = [0u8; 600000]; + for i in 1..a.len() { + a[i] = (a[i - 1] % 19u8).wrapping_add(17); + } + construct_situation(&a[..], &mut b[..], a.len(), 0); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 0); + construct_situation(&a[..], &mut b[..], a.len(), 1); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 1); + construct_situation(&a[..], &mut b[..], a.len(), 10); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 10); + construct_situation(&a[..], &mut b[..], a.len(), 9); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 9); + construct_situation(&a[..], &mut b[..], a.len(), 7); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 7); + construct_situation(&a[..], &mut b[..], a.len(), 8); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 8); + construct_situation(&a[..], &mut b[..], a.len(), 48); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 48); + construct_situation(&a[..], &mut b[..], a.len(), 49); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 49); + construct_situation(&a[..], &mut b[..], a.len(), 63); + assert_eq!(super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), 63); + construct_situation(&a[..], &mut b[..], a.len(), 222); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 222 + ); + construct_situation(&a[..], &mut b[..], a.len(), 1590); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 1590 + ); + construct_situation(&a[..], &mut b[..], a.len(), 12590); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 12590 + ); + construct_situation(&a[..], &mut b[..], a.len(), 52592); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 52592 + ); + construct_situation(&a[..], &mut b[..], a.len(), 152592); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 152592 + ); + construct_situation(&a[..], &mut b[..], a.len(), 252591); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 252591 + ); + construct_situation(&a[..], &mut b[..], a.len(), 131072); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 131072 + ); + construct_situation(&a[..], &mut b[..], a.len(), 131073); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 131073 + ); + construct_situation(&a[..], &mut b[..], a.len(), 131072 + 64 + 32 + 16 + 8); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 131072 + 64 + 32 + 16 + 8 + ); + construct_situation(&a[..], &mut b[..], a.len(), 272144 + 64 + 32 + 16 + 8 + 1); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 272144 + 64 + 32 + 16 + 8 + 1 + ); + construct_situation(&a[..], &mut b[..], a.len(), 2 * 272144 + 64 + 32 + 16 + 8); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + 2 * 272144 + 64 + 32 + 16 + 8 + ); + construct_situation(&a[..], &mut b[..], a.len(), a.len()); + assert_eq!( + super::FindMatchLengthWithLimit(&a[..], &b[..], a.len()), + a.len() + ); + } +} diff --git a/src/enc/util.rs b/src/enc/util.rs index 1fd90367..a004e5b1 100644 --- a/src/enc/util.rs +++ b/src/enc/util.rs @@ -357,6 +357,7 @@ pub fn Log2FloorNonZero(v: u64) -> u32 { (63u32 ^ v.leading_zeros()) } +#[cfg(test)] mod test { fn baseline_log2_floor_non_zero(mut n: u64) -> u32 { let mut result: u32 = 0u32; diff --git a/src/ffi/multicompress/mod.rs b/src/ffi/multicompress/mod.rs index ef9038d7..0e77d809 100644 --- a/src/ffi/multicompress/mod.rs +++ b/src/ffi/multicompress/mod.rs @@ -1,11 +1,12 @@ #![cfg(not(feature = "safe"))] -#[cfg(feature = "std")] -use std::io::Write; -#[cfg(feature = "std")] -use std::{io, panic, thread}; mod test; + use alloc::SliceWrapper; use core::cmp::min; +#[cfg(feature = "std")] +use std::io::Write; +#[cfg(feature = "std")] +use std::panic; use brotli_decompressor::ffi::alloc_util::SubclassableAllocator; use brotli_decompressor::ffi::interface::{ @@ -429,13 +430,13 @@ pub unsafe extern "C" fn BrotliEncoderCompressWorkPool( #[cfg(all(feature = "std", not(feature = "pass-through-ffi-panics")))] fn catch_panic_wstate *mut BrotliEncoderWorkPool + panic::UnwindSafe>( f: F, -) -> thread::Result<*mut BrotliEncoderWorkPool> { +) -> std::thread::Result<*mut BrotliEncoderWorkPool> { panic::catch_unwind(f) } #[cfg(all(feature = "std", not(feature = "pass-through-ffi-panics")))] fn error_print(err: Err) { - let _ign = writeln!(&mut io::stderr(), "Internal Error {:?}", err); + let _ign = writeln!(&mut std::io::stderr(), "Internal Error {:?}", err); } #[cfg(any(not(feature = "std"), feature = "pass-through-ffi-panics"))]