Skip to content
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
32 changes: 32 additions & 0 deletions src/uucore/src/lib/features/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ impl FromStr for Range {
fn parse(s: &str) -> Result<usize, &'static str> {
match s.parse::<usize>() {
Ok(0) => Err("fields and positions are numbered from 1"),
// GNU fails when we are at the limit. Match their behavior
Ok(n) if n == usize::MAX => Err("byte/character offset is too large"),
Ok(n) => Ok(n),
Err(_) => Err("failed to parse range"),
}
Expand Down Expand Up @@ -159,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);
Expand Down Expand Up @@ -229,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")
);
}
}
8 changes: 8 additions & 0 deletions tests/by-util/test_cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ fn test_whitespace_with_char() {
.code_is(1);
}

#[test]
fn test_too_large() {
new_ucmd!()
.args(&["-b1-18446744073709551615", "/dev/null"])
.fails()
.code_is(1);
}

#[test]
fn test_specify_delimiter() {
for param in ["-d", "--delimiter", "--del"] {
Expand Down