pub fn fit_index(thresholds: &[f32], width: f32) -> usize {
let mut selected = 0;
for i in 0..thresholds.len() {
if thresholds[i] > width {
break; }
if i == 0 || thresholds[i - 1] != thresholds[i] {
selected = i;
}
}
selected
}
#[cfg(test)]
mod tests {
use super::fit_index;
#[test]
fn picks_the_largest_threshold_that_fits() {
assert_eq!(fit_index(&[0.0, 480.0, 720.0], 0.0), 0);
assert_eq!(fit_index(&[0.0, 480.0, 720.0], 100.0), 0);
assert_eq!(fit_index(&[0.0, 480.0, 720.0], 479.0), 0);
assert_eq!(fit_index(&[0.0, 480.0, 720.0], 480.0), 1);
assert_eq!(fit_index(&[0.0, 480.0, 720.0], 500.0), 1);
assert_eq!(fit_index(&[0.0, 480.0, 720.0], 719.0), 1);
assert_eq!(fit_index(&[0.0, 480.0, 720.0], 720.0), 2);
assert_eq!(fit_index(&[0.0, 480.0, 720.0], 1000.0), 2);
}
#[test]
fn equal_thresholds_prefer_the_lower_index() {
assert_eq!(fit_index(&[0.0, 480.0, 480.0], 600.0), 1);
assert_eq!(fit_index(&[0.0, 0.0], 10.0), 0);
}
#[test]
fn empty_table_is_the_fallback() {
assert_eq!(fit_index(&[], 500.0), 0);
}
}