mod common;
#[test]
fn numeric_mode_emits_integer_percentages_to_stderr() {
let payload = vec![0u8; 100 * 1024];
let assert = common::rusty_pv_cmd()
.arg("-n")
.arg("-f")
.arg("-i")
.arg("0.05")
.arg("-s")
.arg("100K")
.arg("-L")
.arg("400K")
.write_stdin(payload.clone())
.assert()
.success();
let stderr = String::from_utf8_lossy(&assert.get_output().stderr).into_owned();
assert!(!stderr.is_empty(), "numeric mode should emit something");
let lines: Vec<&str> = stderr.lines().filter(|l| !l.is_empty()).collect();
for line in &lines {
let n: u64 = line
.parse()
.unwrap_or_else(|_| panic!("non-integer line: {line:?}"));
assert!(n <= 100, "percentage > 100: {n}");
}
let nums: Vec<u64> = lines.iter().map(|l| l.parse().unwrap()).collect();
for w in nums.windows(2) {
assert!(
w[1] >= w[0],
"numeric sequence must be monotonic non-decreasing; got {nums:?}"
);
}
assert_eq!(*nums.last().unwrap(), 100, "final tick must be 100");
let hundreds = nums.iter().filter(|&&n| n == 100).count();
assert_eq!(
hundreds, 1,
"numeric mode must emit `100` exactly once; got {hundreds} from {nums:?}"
);
}
#[test]
fn numeric_mode_with_quiet_emits_nothing() {
let assert = common::rusty_pv_cmd()
.arg("-n")
.arg("-q")
.arg("-f")
.arg("-s")
.arg("100")
.write_stdin("x".repeat(100))
.assert()
.success();
assert!(
assert.get_output().stderr.is_empty(),
"numeric + quiet should emit nothing"
);
}
#[test]
fn numeric_mode_unknown_size_emits_zeros() {
let assert = common::rusty_pv_cmd()
.arg("-n")
.arg("-f")
.arg("-i")
.arg("0.1")
.arg("-L")
.arg("10K")
.write_stdin(vec![0u8; 5 * 1024])
.assert()
.success();
let stderr = String::from_utf8_lossy(&assert.get_output().stderr).into_owned();
let lines: Vec<&str> = stderr.lines().filter(|l| !l.is_empty()).collect();
for line in &lines {
assert_eq!(
line.trim(),
"0",
"unknown-size numeric should emit `0`; got: {line:?}"
);
}
}