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

shuf: handle --input-range with huge number ranges

This requires special handling, because we cannot always generate all
possible strings beforehand, e.g. in the case of "-n 2 -i 0-2147483647".
This commit is contained in:
Ben Wiederhake 2024-02-16 20:48:12 +01:00
parent 989178f438
commit 352a8a5bd7
2 changed files with 293 additions and 5 deletions

View file

@ -88,6 +88,99 @@ fn test_zero_termination_multi() {
assert_eq!(result_seq, input_seq, "Output is not a permutation");
}
#[test]
fn test_very_large_range() {
let num_samples = 10;
let result = new_ucmd!()
.arg("-n")
.arg(&num_samples.to_string())
.arg("-i0-1234567890")
.succeeds();
result.no_stderr();
let result_seq: Vec<isize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), num_samples, "Miscounted output length!");
assert!(
result_seq.iter().all(|x| (0..=1234567890).contains(x)),
"Output includes element not from range: {}",
result.stdout_str()
);
}
#[test]
fn test_very_large_range_offset() {
let num_samples = 10;
let result = new_ucmd!()
.arg("-n")
.arg(&num_samples.to_string())
.arg("-i1234567890-2147483647")
.succeeds();
result.no_stderr();
let result_seq: Vec<isize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), num_samples, "Miscounted output length!");
assert!(
result_seq
.iter()
.all(|x| (1234567890..=2147483647).contains(x)),
"Output includes element not from range: {}",
result.stdout_str()
);
}
#[test]
fn test_very_high_range_full() {
let input_seq = vec![
2147483641, 2147483642, 2147483643, 2147483644, 2147483645, 2147483646, 2147483647,
];
let result = new_ucmd!().arg("-i2147483641-2147483647").succeeds();
result.no_stderr();
let mut result_seq: Vec<isize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
result_seq.sort_unstable();
assert_eq!(result_seq, input_seq, "Output is not a permutation");
}
#[test]
fn test_range_repeat() {
let num_samples = 500;
let result = new_ucmd!()
.arg("-r")
.arg("-n")
.arg(&num_samples.to_string())
.arg("-i12-34")
.succeeds();
result.no_stderr();
let result_seq: Vec<isize> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), num_samples, "Miscounted output length!");
assert!(
result_seq.iter().all(|x| (12..=34).contains(x)),
"Output includes element not from range: {}",
result.stdout_str()
);
}
#[test]
fn test_empty_input() {
let result = new_ucmd!().pipe_in(vec![]).succeeds();