1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

ranges: add unit tests to verify the parsing

To test them:
$ cargo test -p uucore --features ranges
This commit is contained in:
Sylvestre Ledru 2023-09-24 23:23:27 +02:00
parent 99120d32c1
commit 265c258713

View file

@ -161,6 +161,7 @@ pub fn contain(ranges: &[Range], n: usize) -> bool {
#[cfg(test)]
mod test {
use super::{complement, Range};
use std::str::FromStr;
fn m(a: Vec<Range>, b: &[Range]) {
assert_eq!(Range::merge(a), b);
@ -231,4 +232,33 @@ mod test {
// With start and end
assert_eq!(complement(&[r(1, 4), r(6, usize::MAX - 1)]), vec![r(5, 5)]);
}
#[test]
fn test_from_str() {
assert_eq!(Range::from_str("5"), Ok(Range { low: 5, high: 5 }));
assert_eq!(Range::from_str("3-5"), Ok(Range { low: 3, high: 5 }));
assert_eq!(
Range::from_str("5-3"),
Err("high end of range less than low end")
);
assert_eq!(Range::from_str("-"), Err("invalid range with no endpoint"));
assert_eq!(
Range::from_str("3-"),
Ok(Range {
low: 3,
high: usize::MAX - 1
})
);
assert_eq!(Range::from_str("-5"), Ok(Range { low: 1, high: 5 }));
assert_eq!(
Range::from_str("0"),
Err("fields and positions are numbered from 1")
);
let max_value = format!("{}", usize::MAX);
assert_eq!(
Range::from_str(&max_value),
Err("byte/character offset is too large")
);
}
}