1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

shuf: fix and test off-by-one errors around ranges

This commit is contained in:
Ben Wiederhake 2024-02-24 22:10:22 +01:00 committed by Sylvestre Ledru
parent 6c29ed037b
commit 4ee3f68e6a
2 changed files with 166 additions and 24 deletions

View file

@ -138,6 +138,99 @@ fn test_very_large_range_offset() {
);
}
#[test]
fn test_range_repeat_no_overflow_1_max() {
let upper_bound = std::usize::MAX;
let result = new_ucmd!()
.arg("-rn1")
.arg(&format!("-i1-{upper_bound}"))
.succeeds();
result.no_stderr();
let result_seq: Vec<usize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), 1, "Miscounted output length!");
}
#[test]
fn test_range_repeat_no_overflow_0_max_minus_1() {
let upper_bound = std::usize::MAX - 1;
let result = new_ucmd!()
.arg("-rn1")
.arg(&format!("-i0-{upper_bound}"))
.succeeds();
result.no_stderr();
let result_seq: Vec<usize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), 1, "Miscounted output length!");
}
#[test]
fn test_range_permute_no_overflow_1_max() {
let upper_bound = std::usize::MAX;
let result = new_ucmd!()
.arg("-n1")
.arg(&format!("-i1-{upper_bound}"))
.succeeds();
result.no_stderr();
let result_seq: Vec<usize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), 1, "Miscounted output length!");
}
#[test]
fn test_range_permute_no_overflow_0_max_minus_1() {
let upper_bound = std::usize::MAX - 1;
let result = new_ucmd!()
.arg("-n1")
.arg(&format!("-i0-{upper_bound}"))
.succeeds();
result.no_stderr();
let result_seq: Vec<usize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), 1, "Miscounted output length!");
}
#[test]
fn test_range_permute_no_overflow_0_max() {
// NOTE: This is different from GNU shuf!
// GNU shuf accepts -i0-MAX-1 and -i1-MAX, but not -i0-MAX.
// This feels like a bug in GNU shuf.
let upper_bound = std::usize::MAX;
let result = new_ucmd!()
.arg("-n1")
.arg(&format!("-i0-{upper_bound}"))
.succeeds();
result.no_stderr();
let result_seq: Vec<usize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), 1, "Miscounted output length!");
}
#[test]
fn test_very_high_range_full() {
let input_seq = vec![
@ -626,7 +719,6 @@ fn test_shuf_multiple_input_line_count() {
}
#[test]
#[ignore = "known issue"]
fn test_shuf_repeat_empty_range() {
new_ucmd!()
.arg("-ri4-3")
@ -653,3 +745,52 @@ fn test_shuf_repeat_empty_input() {
.no_stdout()
.stderr_only("shuf: no lines to repeat\n");
}
#[test]
fn test_range_one_elem() {
new_ucmd!()
.arg("-i5-5")
.succeeds()
.no_stderr()
.stdout_only("5\n");
}
#[test]
fn test_range_empty() {
new_ucmd!().arg("-i5-4").succeeds().no_output();
}
#[test]
fn test_range_empty_minus_one() {
new_ucmd!().arg("-i5-3").succeeds().no_output();
}
#[test]
fn test_range_repeat_one_elem() {
new_ucmd!()
.arg("-n1")
.arg("-ri5-5")
.succeeds()
.no_stderr()
.stdout_only("5\n");
}
#[test]
fn test_range_repeat_empty() {
new_ucmd!()
.arg("-n1")
.arg("-ri5-4")
.fails()
.no_stdout()
.stderr_only("shuf: no lines to repeat\n");
}
#[test]
fn test_range_repeat_empty_minus_one() {
new_ucmd!()
.arg("-n1")
.arg("-ri5-3")
.fails()
.no_stdout()
.stderr_only("shuf: no lines to repeat\n");
}