Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conditional cfg(test) to modules #202

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/concat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,10 @@ impl BroCatli {
}
}

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

#[test]
fn test_deserialization() {
let broccoli = BroCatli {
Expand Down
2 changes: 1 addition & 1 deletion src/enc/backward_references/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(feature = "std")]
#![cfg(test)]
#![cfg(feature = "std")]

use alloc_stdlib::StandardAlloc;

Expand Down
141 changes: 71 additions & 70 deletions src/enc/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,79 @@ 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_,
);
get_length_code(
insertlen,
copylen_code,
(self.dist_prefix_ & 0x3ff) == 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,
Expand Down Expand Up @@ -380,72 +450,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_,
);
get_length_code(
insertlen,
copylen_code,
(self.dist_prefix_ & 0x3ff) == 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
}
}
1 change: 1 addition & 0 deletions src/enc/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
194 changes: 98 additions & 96 deletions src/enc/static_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
);
}
}
Loading