coreutils/tests/by-util/test_shuf.rs

459 lines
12 KiB
Rust
Raw Normal View History

2023-08-21 08:49:27 +00:00
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
2023-03-20 13:51:19 +00:00
use crate::common::util::TestScenario;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn test_output_is_random_permutation() {
let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let input = input_seq
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
2021-04-22 20:37:44 +00:00
let result = new_ucmd!().pipe_in(input.as_bytes()).succeeds();
result.no_stderr();
let mut result_seq: Vec<i32> = result
2021-04-22 20:37:44 +00:00
.stdout_str()
2021-05-29 12:32:35 +00:00
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
2021-05-29 12:32:35 +00:00
result_seq.sort_unstable();
assert_ne!(result.stdout_str(), input, "Output is not randomized");
assert_eq!(result_seq, input_seq, "Output is not a permutation");
}
#[test]
fn test_explicit_stdin_file() {
let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let input = input_seq
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
let result = new_ucmd!().arg("-").pipe_in(input.as_bytes()).succeeds();
result.no_stderr();
let mut result_seq: Vec<i32> = 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_zero_termination() {
let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2021-04-22 20:37:44 +00:00
let result = new_ucmd!().arg("-z").arg("-i1-10").succeeds();
result.no_stderr();
let mut result_seq: Vec<i32> = result
2021-04-22 20:37:44 +00:00
.stdout_str()
2021-05-29 12:32:35 +00:00
.split('\0')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
2021-05-29 12:32:35 +00:00
result_seq.sort_unstable();
assert_eq!(result_seq, input_seq, "Output is not a permutation");
}
#[test]
fn test_zero_termination_multi() {
let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let result = new_ucmd!().arg("-z").arg("-z").arg("-i1-10").succeeds();
result.no_stderr();
let mut result_seq: Vec<i32> = result
.stdout_str()
.split('\0')
.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_empty_input() {
let result = new_ucmd!().pipe_in(vec![]).succeeds();
result.no_stderr();
result.no_stdout();
}
#[test]
fn test_echo() {
let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let result = new_ucmd!()
.arg("-e")
.args(
&input_seq
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>(),
)
2021-04-22 20:37:44 +00:00
.succeeds();
result.no_stderr();
let mut result_seq: Vec<i32> = result
2021-04-22 20:37:44 +00:00
.stdout_str()
2021-05-29 12:32:35 +00:00
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
2021-05-29 12:32:35 +00:00
result_seq.sort_unstable();
assert_eq!(result_seq, input_seq, "Output is not a permutation");
}
#[test]
fn test_echo_multi() {
let result = new_ucmd!()
.arg("-e")
.arg("a")
.arg("b")
.arg("-e")
.arg("c")
.succeeds();
result.no_stderr();
let mut result_seq: Vec<String> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.into())
.collect();
result_seq.sort_unstable();
assert_eq!(result_seq, ["a", "b", "c"], "Output is not a permutation");
}
#[test]
fn test_echo_postfix() {
let result = new_ucmd!().arg("a").arg("b").arg("c").arg("-e").succeeds();
result.no_stderr();
let mut result_seq: Vec<String> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.into())
.collect();
result_seq.sort_unstable();
assert_eq!(result_seq, ["a", "b", "c"], "Output is not a permutation");
}
#[test]
fn test_echo_short_collapsed_zero() {
let result = new_ucmd!().arg("-ez").arg("a").arg("b").arg("c").succeeds();
result.no_stderr();
let mut result_seq: Vec<String> = result
.stdout_str()
.split('\0')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
result_seq.sort_unstable();
assert_eq!(result_seq, ["a", "b", "c"], "Output is not a permutation");
}
#[test]
fn test_head_count() {
let repeat_limit = 5;
2023-08-21 06:11:38 +00:00
let input_seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let input = input_seq
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
let result = new_ucmd!()
.args(&["-n", &repeat_limit.to_string()])
.pipe_in(input.as_bytes())
2021-04-22 20:37:44 +00:00
.succeeds();
result.no_stderr();
let mut result_seq: Vec<i32> = result
2021-04-22 20:37:44 +00:00
.stdout_str()
2021-05-29 12:32:35 +00:00
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
2021-05-29 12:32:35 +00:00
result_seq.sort_unstable();
assert_eq!(result_seq.len(), repeat_limit, "Output is not limited");
assert!(
result_seq.iter().all(|x| input_seq.contains(x)),
"Output includes element not from input: {}",
result.stdout_str()
);
}
#[test]
fn test_head_count_multi_big_then_small() {
let repeat_limit = 5;
let input_seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let input = input_seq
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
let result = new_ucmd!()
.arg("-n")
.arg(&(repeat_limit + 1).to_string())
.arg("-n")
.arg(&repeat_limit.to_string())
.pipe_in(input.as_bytes())
.succeeds();
result.no_stderr();
let result_seq: Vec<i32> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), repeat_limit, "Output is not limited");
assert!(
result_seq.iter().all(|x| input_seq.contains(x)),
"Output includes element not from input: {}",
result.stdout_str()
);
}
#[test]
fn test_head_count_multi_small_then_big() {
let repeat_limit = 5;
let input_seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let input = input_seq
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
let result = new_ucmd!()
.arg("-n")
.arg(&repeat_limit.to_string())
.arg("-n")
.arg(&(repeat_limit + 1).to_string())
.pipe_in(input.as_bytes())
.succeeds();
result.no_stderr();
let result_seq: Vec<i32> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(result_seq.len(), repeat_limit, "Output is not limited");
assert!(
result_seq.iter().all(|x| input_seq.contains(x)),
2021-03-31 11:30:06 +00:00
"Output includes element not from input: {}",
2021-04-22 20:37:44 +00:00
result.stdout_str()
2022-01-30 12:55:03 +00:00
);
}
#[test]
fn test_repeat() {
let repeat_limit = 15000;
2023-08-21 06:11:38 +00:00
let input_seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let input = input_seq
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
let result = new_ucmd!()
.arg("-r")
.args(&["-n", &repeat_limit.to_string()])
.pipe_in(input.as_bytes())
2021-04-22 20:37:44 +00:00
.succeeds();
result.no_stderr();
let result_seq: Vec<i32> = result
2021-04-22 20:37:44 +00:00
.stdout_str()
2021-05-29 12:32:35 +00:00
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(
result_seq.len(),
repeat_limit,
"Output is not repeating forever"
);
assert!(
result_seq.iter().all(|x| input_seq.contains(x)),
2021-03-31 11:30:06 +00:00
"Output includes element not from input: {:?}",
result_seq
.iter()
.filter(|x| !input_seq.contains(x))
.collect::<Vec<&i32>>()
2022-01-30 12:55:03 +00:00
);
}
#[test]
fn test_repeat_multi() {
let repeat_limit = 15000;
let input_seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let input = input_seq
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n");
let result = new_ucmd!()
.arg("-r")
.arg("-r") // The only difference to test_repeat()
.args(&["-n", &repeat_limit.to_string()])
.pipe_in(input.as_bytes())
.succeeds();
result.no_stderr();
let result_seq: Vec<i32> = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
assert_eq!(
result_seq.len(),
repeat_limit,
"Output is not repeating forever"
);
assert!(
result_seq.iter().all(|x| input_seq.contains(x)),
"Output includes element not from input: {:?}",
result_seq
.iter()
.filter(|x| !input_seq.contains(x))
.collect::<Vec<&i32>>()
);
}
#[test]
fn test_file_input() {
let expected_seq = vec![11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
2021-04-22 20:37:44 +00:00
let result = new_ucmd!().arg("file_input.txt").succeeds();
result.no_stderr();
let mut result_seq: Vec<i32> = result
2021-04-22 20:37:44 +00:00
.stdout_str()
2021-05-29 12:32:35 +00:00
.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse().unwrap())
.collect();
2021-05-29 12:32:35 +00:00
result_seq.sort_unstable();
assert_eq!(result_seq, expected_seq, "Output is not a permutation");
}
#[test]
fn test_shuf_echo_and_input_range_not_allowed() {
2021-04-22 20:37:44 +00:00
new_ucmd!()
.args(&["-e", "0", "-i", "0-2"])
.fails()
2022-01-11 13:45:10 +00:00
.stderr_contains("cannot be used with");
}
#[test]
fn test_shuf_input_range_and_file_not_allowed() {
2021-04-22 20:37:44 +00:00
new_ucmd!()
.args(&["-i", "0-9", "file"])
.fails()
2022-01-11 13:45:10 +00:00
.stderr_contains("cannot be used with");
}
#[test]
fn test_shuf_invalid_input_range_one() {
2021-04-22 20:37:44 +00:00
new_ucmd!()
.args(&["-i", "0"])
.fails()
.stderr_contains("invalid input range");
}
#[test]
fn test_shuf_invalid_input_range_two() {
2021-04-22 20:37:44 +00:00
new_ucmd!()
.args(&["-i", "a-9"])
.fails()
.stderr_contains("invalid input range: 'a'");
}
#[test]
fn test_shuf_invalid_input_range_three() {
2021-04-22 20:37:44 +00:00
new_ucmd!()
.args(&["-i", "0-b"])
.fails()
.stderr_contains("invalid input range: 'b'");
}
#[test]
fn test_shuf_multiple_input_ranges() {
new_ucmd!()
.args(&["-i", "2-9", "-i", "2-9"])
.fails()
.stderr_contains("--input-range")
.stderr_contains("cannot be used multiple times");
}
#[test]
fn test_shuf_multiple_outputs() {
new_ucmd!()
.args(&["-o", "file_a", "-o", "file_b"])
.fails()
.stderr_contains("--output")
.stderr_contains("cannot be used multiple times");
}
#[test]
fn test_shuf_two_input_files() {
new_ucmd!()
.args(&["file_a", "file_b"])
.fails()
.stderr_contains("unexpected argument 'file_b' found");
}
#[test]
fn test_shuf_three_input_files() {
new_ucmd!()
.args(&["file_a", "file_b", "file_c"])
.fails()
.stderr_contains("unexpected argument 'file_b' found");
}
#[test]
fn test_shuf_invalid_input_line_count() {
2021-04-22 20:37:44 +00:00
new_ucmd!()
.args(&["-n", "a"])
.fails()
.stderr_contains("invalid line count: 'a'");
}
#[test]
fn test_shuf_multiple_input_line_count() {
let result = new_ucmd!()
.args(&["-i10-200", "-n", "10", "-n", "5"])
.succeeds();
2022-03-28 15:33:38 +00:00
2022-03-28 15:09:26 +00:00
result.no_stderr();
2022-03-28 15:36:50 +00:00
let result_count = result
.stdout_str()
.split('\n')
.filter(|x| !x.is_empty())
.count();
2022-03-28 15:33:38 +00:00
assert_eq!(result_count, 5, "Output should have 5 items");
}