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

fuzz: Simplify the compare_result function

This commit is contained in:
Sylvestre Ledru 2023-12-17 17:57:21 +01:00
parent be9b675ec1
commit b2e26f2aec
6 changed files with 29 additions and 45 deletions

View file

@ -222,15 +222,19 @@ pub fn run_gnu_cmd(
} }
} }
/// Compare results from two different implementations of a command.
///
/// # Arguments
/// * `test_type` - The command.
/// * `input` - The input provided to the command.
/// * `rust_result` - The result of running the command with the Rust implementation.
/// * `gnu_result` - The result of running the command with the GNU implementation.
/// * `fail_on_stderr_diff` - Whether to fail the test if there is a difference in stderr output.
pub fn compare_result( pub fn compare_result(
test_type: &str, test_type: &str,
input: &str, input: &str,
rust_stdout: &str, rust_result: &CommandResult,
gnu_stdout: &str, gnu_result: &CommandResult,
rust_stderr: &str,
gnu_stderr: &str,
rust_exit_code: i32,
gnu_exit_code: i32,
fail_on_stderr_diff: bool, fail_on_stderr_diff: bool,
) { ) {
println!("Test Type: {}", test_type); println!("Test Type: {}", test_type);
@ -239,24 +243,24 @@ pub fn compare_result(
let mut discrepancies = Vec::new(); let mut discrepancies = Vec::new();
let mut should_panic = false; let mut should_panic = false;
if rust_stdout.trim() != gnu_stdout.trim() { if rust_result.stdout.trim() != gnu_result.stdout.trim() {
discrepancies.push("stdout differs"); discrepancies.push("stdout differs");
println!("Rust stdout: {}", rust_stdout); println!("Rust stdout: {}", rust_result.stdout);
println!("GNU stdout: {}", gnu_stdout); println!("GNU stdout: {}", gnu_result.stdout);
should_panic = true; should_panic = true;
} }
if rust_stderr.trim() != gnu_stderr.trim() { if rust_result.stderr.trim() != gnu_result.stderr.trim() {
discrepancies.push("stderr differs"); discrepancies.push("stderr differs");
println!("Rust stderr: {}", rust_stderr); println!("Rust stderr: {}", rust_result.stderr);
println!("GNU stderr: {}", gnu_stderr); println!("GNU stderr: {}", gnu_result.stderr);
if fail_on_stderr_diff { if fail_on_stderr_diff {
should_panic = true; should_panic = true;
} }
} }
if rust_exit_code != gnu_exit_code { if rust_result.exit_code != gnu_result.exit_code {
discrepancies.push("exit code differs"); discrepancies.push("exit code differs");
println!("Rust exit code: {}", rust_exit_code); println!("Rust exit code: {}", rust_result.exit_code);
println!("GNU exit code: {}", gnu_exit_code); println!("GNU exit code: {}", gnu_result.exit_code);
should_panic = true; should_panic = true;
} }

View file

@ -78,12 +78,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result( compare_result(
"echo", "echo",
&format!("{:?}", &args[1..]), &format!("{:?}", &args[1..]),
&rust_result.stdout, &rust_result,
&gnu_result.stdout, &gnu_result,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
true, true,
); );
}); });

View file

@ -86,12 +86,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result( compare_result(
"expr", "expr",
&format!("{:?}", &args[1..]), &format!("{:?}", &args[1..]),
&rust_result.stdout, &rust_result,
&gnu_result.stdout, &gnu_result,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
false, // Set to true if you want to fail on stderr diff false, // Set to true if you want to fail on stderr diff
); );
}); });

View file

@ -99,12 +99,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result( compare_result(
"printf", "printf",
&format!("{:?}", &args[1..]), &format!("{:?}", &args[1..]),
&rust_result.stdout, &rust_result,
&gnu_result.stdout, &gnu_result,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
false, // Set to true if you want to fail on stderr diff false, // Set to true if you want to fail on stderr diff
); );
}); });

View file

@ -67,12 +67,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result( compare_result(
"seq", "seq",
&format!("{:?}", &args[1..]), &format!("{:?}", &args[1..]),
&rust_result.stdout, &rust_result,
&gnu_result.stdout, &gnu_result,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
false, // Set to true if you want to fail on stderr diff false, // Set to true if you want to fail on stderr diff
); );
}); });

View file

@ -203,12 +203,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result( compare_result(
"test", "test",
&format!("{:?}", &args[1..]), &format!("{:?}", &args[1..]),
&rust_result.stdout, &rust_result,
&gnu_result.stdout, &gnu_result,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
false, // Set to true if you want to fail on stderr diff false, // Set to true if you want to fail on stderr diff
); );
}); });