-
Notifications
You must be signed in to change notification settings - Fork 43
/
wrap_optimal_fit.rs
61 lines (54 loc) · 1.85 KB
/
wrap_optimal_fit.rs
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
#![no_main]
use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
use textwrap::wrap_algorithms::wrap_optimal_fit;
use textwrap::{core, wrap_algorithms};
#[derive(Arbitrary, Debug)]
struct Penalties {
nline_penalty: usize,
overflow_penalty: usize,
short_last_line_fraction: usize,
short_last_line_penalty: usize,
hyphen_penalty: usize,
}
impl Into<wrap_algorithms::Penalties> for Penalties {
fn into(self) -> wrap_algorithms::Penalties {
wrap_algorithms::Penalties {
nline_penalty: self.nline_penalty,
overflow_penalty: self.overflow_penalty,
short_last_line_fraction: std::cmp::max(1, self.short_last_line_fraction),
short_last_line_penalty: self.short_last_line_penalty,
hyphen_penalty: self.hyphen_penalty,
}
}
}
#[derive(Arbitrary, Debug, PartialEq)]
struct Word {
width: f64,
whitespace_width: f64,
penalty_width: f64,
}
#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> f64 { self.width }
fn whitespace_width(&self) -> f64 { self.whitespace_width }
fn penalty_width(&self) -> f64 { self.penalty_width }
}
// Check wrapping fragments with mostly arbitrary widths. Infinite
// widths are not supported since they instantly trigger an overflow
// in the cost computation. Similarly for very large values: the 1e100
// bound used here is somewhat conservative, the real bound seems to
// be around 1e170.
fuzz_target!(|input: (usize, Vec<Word>, Penalties)| {
let width = input.0;
let words = input.1;
let penalties = input.2.into();
for word in &words {
for width in [word.width, word.whitespace_width, word.penalty_width] {
if !width.is_finite() || width.abs() > 1e100 {
return;
}
}
}
let _ = wrap_optimal_fit(&words, &[width as f64], &penalties);
});