Skip to content

Commit

Permalink
Fix fuzz tests and related bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoohey31 committed Nov 29, 2022
1 parent e9d18ce commit 8b2a9f1
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/wrap_first_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ struct Word {
#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> f64 { self.width }
fn whitespace_width(&self) -> f64 { self.whitespace_width }
fn whitespace_width(&self, _: u8) -> f64 { self.whitespace_width }
fn penalty_width(&self) -> f64 { self.penalty_width }
}

fuzz_target!(|input: (f64, Vec<Word>)| {
let width = input.0;
let words = input.1;
let _ = wrap_first_fit(&words, &[width]);
let _ = wrap_first_fit(&words, &[width], 0);
});
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/wrap_optimal_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct Word {
#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> f64 { self.width }
fn whitespace_width(&self) -> f64 { self.whitespace_width }
fn whitespace_width(&self, _: u8) -> f64 { self.whitespace_width }
fn penalty_width(&self) -> f64 { self.penalty_width }
}

Expand All @@ -57,5 +57,5 @@ fuzz_target!(|input: (usize, Vec<Word>, Penalties)| {
}
}

let _ = wrap_optimal_fit(&words, &[width as f64], &penalties);
let _ = wrap_optimal_fit(&words, &[width as f64], 0, &penalties);
});
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/wrap_optimal_fit_usize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct Word {
#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> f64 { self.width as f64 }
fn whitespace_width(&self) -> f64 { self.whitespace_width as f64 }
fn whitespace_width(&self, _: u8) -> f64 { self.whitespace_width as f64 }
fn penalty_width(&self) -> f64 { self.penalty_width as f64 }
}

Expand All @@ -45,5 +45,5 @@ fuzz_target!(|input: (usize, Vec<Word>, Penalties)| {
let width = input.0;
let words = input.1;
let penalties = input.2.into();
let _ = wrap_optimal_fit(&words, &[width as f64], &penalties);
let _ = wrap_optimal_fit(&words, &[width as f64], 0, &penalties);
});
2 changes: 1 addition & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'a> Word<'a> {
/// All trailing whitespace is automatically taken to be the whitespace part
/// of the word.
pub fn from(word: &str) -> Word<'_> {
let trimmed = word.trim_end();
let trimmed = word.trim_end_matches(&[' ', '\t']);
Word {
word: trimmed,
// trimmed shouldn't contain whitespace, so we don't need to pass
Expand Down
4 changes: 3 additions & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub(crate) fn wrap_single_line<'a>(
options.subsequent_indent
};
if line.len() < options.width && options.tab_width <= 1 && indent.is_empty() {
lines.push(Cow::from(line.trim_end_matches(' ')));
lines.push(Cow::from(line.trim_end_matches(&[' ', '\t'])));
} else {
wrap_single_line_slow_path(line, options, lines)
}
Expand All @@ -217,6 +217,8 @@ pub(crate) fn wrap_single_line_slow_path<'a>(
options: &Options<'_>,
lines: &mut Vec<Cow<'a, str>>,
) {
let line = line.trim_end_matches(&[' ', '\t']);

let initial_width = options
.width
.saturating_sub(display_width(options.initial_indent, options.tab_width));
Expand Down

0 comments on commit 8b2a9f1

Please sign in to comment.