chore: use assert_eq! and assert_ne! instead of assert!

Makes intent clearer, and if something fails, prints the values that caused the failure as part of the panic message.
This commit is contained in:
Yuri Astrakhan 2025-04-08 13:25:12 -04:00
parent a77e218a79
commit a27880e8a3
16 changed files with 71 additions and 60 deletions

View file

@ -322,7 +322,7 @@ pub mod fast_encode {
leftover_buffer.extend(stolen_bytes);
// After appending the stolen bytes to `leftover_buffer`, it should be the right size
assert!(leftover_buffer.len() == encode_in_chunks_of_size);
assert_eq!(leftover_buffer.len(), encode_in_chunks_of_size);
// Encode the old unencoded data and the stolen bytes, and add the result to
// `encoded_buffer`
@ -343,7 +343,7 @@ pub mod fast_encode {
let remainder = chunks_exact.remainder();
for sl in chunks_exact {
assert!(sl.len() == encode_in_chunks_of_size);
assert_eq!(sl.len(), encode_in_chunks_of_size);
supports_fast_decode_and_encode.encode_to_vec_deque(sl, encoded_buffer)?;
}
@ -622,7 +622,7 @@ pub mod fast_decode {
leftover_buffer.extend(stolen_bytes);
// After appending the stolen bytes to `leftover_buffer`, it should be the right size
assert!(leftover_buffer.len() == decode_in_chunks_of_size);
assert_eq!(leftover_buffer.len(), decode_in_chunks_of_size);
// Decode the old un-decoded data and the stolen bytes, and add the result to
// `decoded_buffer`
@ -642,7 +642,7 @@ pub mod fast_decode {
let remainder = chunks_exact.remainder();
for sl in chunks_exact {
assert!(sl.len() == decode_in_chunks_of_size);
assert_eq!(sl.len(), decode_in_chunks_of_size);
supports_fast_decode_and_encode.decode_into_vec(sl, decoded_buffer)?;
}

View file

@ -1440,8 +1440,8 @@ mod tests {
fn bsize_test_primes() {
let (n, m) = (7901, 7919);
let res = calc_bsize(n, m);
assert!(res % n == 0);
assert!(res % m == 0);
assert_eq!(res % n, 0);
assert_eq!(res % m, 0);
assert_eq!(res, n * m);
}
@ -1450,8 +1450,8 @@ mod tests {
fn bsize_test_rel_prime_obs_greater() {
let (n, m) = (7 * 5119, 13 * 5119);
let res = calc_bsize(n, m);
assert!(res % n == 0);
assert!(res % m == 0);
assert_eq!(res % n, 0);
assert_eq!(res % m, 0);
assert_eq!(res, 7 * 13 * 5119);
}
@ -1460,8 +1460,8 @@ mod tests {
fn bsize_test_rel_prime_ibs_greater() {
let (n, m) = (13 * 5119, 7 * 5119);
let res = calc_bsize(n, m);
assert!(res % n == 0);
assert!(res % m == 0);
assert_eq!(res % n, 0);
assert_eq!(res % m, 0);
assert_eq!(res, 7 * 13 * 5119);
}
@ -1470,8 +1470,8 @@ mod tests {
fn bsize_test_3fac_rel_prime() {
let (n, m) = (11 * 13 * 5119, 7 * 11 * 5119);
let res = calc_bsize(n, m);
assert!(res % n == 0);
assert!(res % m == 0);
assert_eq!(res % n, 0);
assert_eq!(res % m, 0);
assert_eq!(res, 7 * 11 * 13 * 5119);
}
@ -1480,8 +1480,8 @@ mod tests {
fn bsize_test_ibs_greater() {
let (n, m) = (512 * 1024, 256 * 1024);
let res = calc_bsize(n, m);
assert!(res % n == 0);
assert!(res % m == 0);
assert_eq!(res % n, 0);
assert_eq!(res % m, 0);
assert_eq!(res, n);
}
@ -1490,8 +1490,8 @@ mod tests {
fn bsize_test_obs_greater() {
let (n, m) = (256 * 1024, 512 * 1024);
let res = calc_bsize(n, m);
assert!(res % n == 0);
assert!(res % m == 0);
assert_eq!(res % n, 0);
assert_eq!(res % m, 0);
assert_eq!(res, m);
}
@ -1500,8 +1500,8 @@ mod tests {
fn bsize_test_bs_eq() {
let (n, m) = (1024, 1024);
let res = calc_bsize(n, m);
assert!(res % n == 0);
assert!(res % m == 0);
assert_eq!(res % n, 0);
assert_eq!(res % m, 0);
assert_eq!(res, m);
}

View file

@ -599,7 +599,7 @@ mod tests {
#[test]
fn test_gnu_compatibility() {
let args = options("-n 1 -c 1 -n 5 -c kiB -vqvqv").unwrap(); // spell-checker:disable-line
assert!(args.mode == Mode::FirstBytes(1024));
assert_eq!(args.mode, Mode::FirstBytes(1024));
assert!(args.verbose);
assert_eq!(options("-5").unwrap().mode, Mode::FirstLines(5));
assert_eq!(options("-2b").unwrap().mode, Mode::FirstBytes(1024));

View file

@ -481,8 +481,9 @@ mod tests {
let mut options = get_valid_options();
options.invalid = InvalidModes::Fail;
handle_buffer(BufReader::new(&input_value[..]), &options).unwrap();
assert!(
get_exit_code() == 2,
assert_eq!(
get_exit_code(),
2,
"should set exit code 2 for formatting errors"
);
}
@ -502,8 +503,9 @@ mod tests {
let mut options = get_valid_options();
options.invalid = InvalidModes::Fail;
handle_args(input_value, &options).unwrap();
assert!(
get_exit_code() == 2,
assert_eq!(
get_exit_code(),
2,
"should set exit code 2 for formatting errors"
);
}

View file

@ -254,7 +254,7 @@ fn parse_delimiters(delimiters: &str) -> UResult<Box<[Box<[u8]>]>> {
fn remove_trailing_line_ending_byte(line_ending_byte: u8, output: &mut Vec<u8>) {
if let Some(&byte) = output.last() {
if byte == line_ending_byte {
assert!(output.pop() == Some(line_ending_byte));
assert_eq!(output.pop(), Some(line_ending_byte));
}
}
}
@ -326,7 +326,7 @@ impl<'a> DelimiterState<'a> {
} else {
// This branch is NOT unreachable, must be skipped
// `output` should be empty in this case
assert!(output_len == 0);
assert_eq!(output_len, 0);
}
}
}

View file

@ -125,7 +125,7 @@ mod test {
let mut rng = ReadRng::new(&v[..]);
rng.fill_bytes(&mut w);
assert!(v == w);
assert_eq!(v, w);
}
#[test]

View file

@ -79,7 +79,7 @@ mod tests {
});
let result = copy_stream(&mut pipe_read, &mut dest_file).unwrap();
thread.join().unwrap();
assert!(result == data.len() as u64);
assert_eq!(result, data.len() as u64);
// We would have been at the end already, so seek again to the start.
dest_file.seek(SeekFrom::Start(0)).unwrap();

View file

@ -492,13 +492,13 @@ mod tests {
#[test]
fn test_stat_split() {
let case = "32 (idle_inject/3) S 2 0 0 0 -1 69238848 0 0 0 0 0 0 0 0 -51 0 1 0 34 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 3 50 1 0 0 0 0 0 0 0 0 0 0 0";
assert!(stat_split(case)[1] == "idle_inject/3");
assert_eq!(stat_split(case)[1], "idle_inject/3");
let case = "3508 (sh) S 3478 3478 3478 0 -1 4194304 67 0 0 0 0 0 0 0 20 0 1 0 11911 2961408 238 18446744073709551615 94340156948480 94340157028757 140736274114368 0 0 0 0 4096 65538 1 0 0 17 8 0 0 0 0 0 94340157054704 94340157059616 94340163108864 140736274122780 140736274122976 140736274122976 140736274124784 0";
assert!(stat_split(case)[1] == "sh");
assert_eq!(stat_split(case)[1], "sh");
let case = "47246 (kworker /10:1-events) I 2 0 0 0 -1 69238880 0 0 0 0 17 29 0 0 20 0 1 0 1396260 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 10 0 0 0 0 0 0 0 0 0 0 0 0 0";
assert!(stat_split(case)[1] == "kworker /10:1-events");
assert_eq!(stat_split(case)[1], "kworker /10:1-events");
}
#[test]

View file

@ -389,8 +389,14 @@ fn test_traverse_symlinks() {
.arg("dir3/file")
.succeeds();
assert!(at.plus("dir2/file").metadata().unwrap().gid() == first_group.as_raw());
assert!(at.plus("dir3/file").metadata().unwrap().gid() == first_group.as_raw());
assert_eq!(
at.plus("dir2/file").metadata().unwrap().gid(),
first_group.as_raw()
);
assert_eq!(
at.plus("dir3/file").metadata().unwrap().gid(),
first_group.as_raw()
);
ucmd.arg("-R")
.args(args)

View file

@ -38,11 +38,10 @@ fn run_single_test(test: &TestCase, at: &AtPath, mut ucmd: UCommand) {
make_file(&at.plus_as_string(TEST_FILE), test.before);
let perms = at.metadata(TEST_FILE).permissions().mode();
assert!(
perms == test.before,
assert_eq!(
perms, test.before,
"{}: expected: {:o} got: {perms:o}",
"setting permissions on test files before actual test run failed",
test.after,
"setting permissions on test files before actual test run failed", test.after
);
for arg in &test.args {
@ -58,10 +57,10 @@ fn run_single_test(test: &TestCase, at: &AtPath, mut ucmd: UCommand) {
}
let perms = at.metadata(TEST_FILE).permissions().mode();
assert!(
perms == test.after,
assert_eq!(
perms, test.after,
"{ucmd}: expected: {:o} got: {perms:o}",
test.after,
test.after
);
}

View file

@ -1045,8 +1045,9 @@ mod tests_split_iterator {
);
}
Ok(actual) => {
assert!(
expected == actual.as_slice(),
assert_eq!(
expected,
actual.as_slice(),
"[{i}] After split({input:?}).unwrap()\nexpected: {expected:?}\n actual: {actual:?}\n"
);
}

View file

@ -592,7 +592,7 @@ fn test_install_copy_then_compare_file_with_extra_mode() {
file2_meta = at.metadata(file2);
let after_install_sticky = FileTime::from_last_modification_time(&file2_meta);
assert!(before != after_install_sticky);
assert_ne!(before, after_install_sticky);
sleep(std::time::Duration::from_millis(100));
@ -608,7 +608,7 @@ fn test_install_copy_then_compare_file_with_extra_mode() {
file2_meta = at.metadata(file2);
let after_install_sticky_again = FileTime::from_last_modification_time(&file2_meta);
assert!(after_install_sticky != after_install_sticky_again);
assert_ne!(after_install_sticky, after_install_sticky_again);
}
const STRIP_TARGET_FILE: &str = "helloworld_installed";

View file

@ -39,7 +39,7 @@ fn test_shred() {
// File exists
assert!(at.file_exists(file));
// File is obfuscated
assert!(at.read_bytes(file) != file_original_content.as_bytes());
assert_ne!(at.read_bytes(file), file_original_content.as_bytes());
}
#[test]

View file

@ -336,7 +336,10 @@ fn test_filter_with_env_var_set() {
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$");
assert_eq!(glob.collate(), at.read_bytes(name));
assert!(env::var("FILE").unwrap_or_else(|_| "var was unset".to_owned()) == env_var_value);
assert_eq!(
env::var("FILE").unwrap_or_else(|_| "var was unset".to_owned()),
env_var_value
);
}
#[test]

View file

@ -279,7 +279,7 @@ fn test_touch_set_only_atime() {
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
let (atime, mtime) = get_file_times(&at, file);
assert!(atime != mtime);
assert_ne!(atime, mtime);
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45240);
}
}
@ -380,7 +380,7 @@ fn test_touch_set_only_mtime() {
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
let (atime, mtime) = get_file_times(&at, file);
assert!(atime != mtime);
assert_ne!(atime, mtime);
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45240);
}
}
@ -805,7 +805,7 @@ fn test_touch_changes_time_of_file_in_stdout() {
.no_stderr();
let (_, mtime_after) = get_file_times(&at, file);
assert!(mtime_after != mtime);
assert_ne!(mtime_after, mtime);
}
#[test]

View file

@ -23,7 +23,7 @@ fn test_increase_file_size() {
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -35,7 +35,7 @@ fn test_increase_file_size_kb() {
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -57,7 +57,7 @@ fn test_reference() {
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -69,7 +69,7 @@ fn test_decrease_file_size() {
ucmd.args(&["--size=-4", FILE2]).succeeds();
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -81,7 +81,7 @@ fn test_space_in_size() {
ucmd.args(&["--size", " 4", FILE2]).succeeds();
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -110,7 +110,7 @@ fn test_at_most_shrinks() {
ucmd.args(&["--size", "<4", FILE2]).succeeds();
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -122,7 +122,7 @@ fn test_at_most_no_change() {
ucmd.args(&["--size", "<40", FILE2]).succeeds();
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -134,7 +134,7 @@ fn test_at_least_grows() {
ucmd.args(&["--size", ">15", FILE2]).succeeds();
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -146,7 +146,7 @@ fn test_at_least_no_change() {
ucmd.args(&["--size", ">4", FILE2]).succeeds();
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -158,7 +158,7 @@ fn test_round_down() {
ucmd.args(&["--size", "/4", FILE2]).succeeds();
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -170,7 +170,7 @@ fn test_round_up() {
ucmd.args(&["--size", "%4", FILE2]).succeeds();
file.seek(SeekFrom::End(0)).unwrap();
let actual = file.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]
@ -184,7 +184,7 @@ fn test_size_and_reference() {
.succeeds();
file2.seek(SeekFrom::End(0)).unwrap();
let actual = file2.stream_position().unwrap();
assert!(expected == actual, "expected '{expected}' got '{actual}'");
assert_eq!(expected, actual, "expected '{expected}' got '{actual}'");
}
#[test]