Skip to content

Commit

Permalink
Add monotonicity tests for timeout value computations
Browse files Browse the repository at this point in the history
  • Loading branch information
sirhcel committed Aug 2, 2024
1 parent 8b5cf39 commit cf7d329
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/posix/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,73 @@ fn wait_fd(fd: RawFd, events: PollFlags, timeout: Duration) -> io::Result<()> {
fn milliseconds_i64(duration: Duration) -> i64 {
duration.as_secs() as i64 * 1000 + i64::from(duration.subsec_nanos()) / 1_000_000
}

#[cfg(test)]
mod test {
use super::*;

use nix::libc::c_int;

// TODO: Harmonize with corresponding tests for Windows.
fn monotonicity_test_durations() -> Vec<Duration> {
vec![
Duration::ZERO,
Duration::from_nanos(1),
Duration::from_millis(1),
Duration::from_secs(1),
Duration::from_secs(i16::MAX as u64 - 1),
Duration::from_secs(i16::MAX as u64),
Duration::from_secs(i16::MAX as u64 + 1),
Duration::from_secs(i32::MAX as u64 - 1),
Duration::from_secs(i32::MAX as u64),
Duration::from_secs(i32::MAX as u64 + 1),
Duration::from_secs(i64::MAX as u64 - 1),
Duration::from_secs(i64::MAX as u64),
Duration::from_secs(i64::MAX as u64 + 1),
Duration::from_secs(u64::MAX - 1),
Duration::from_secs(u64::MAX),
Duration::from_secs(u64::MAX) + Duration::from_millis(1),
Duration::MAX,
]
}

#[test]
fn milliseconds_i64_as_c_int_is_monotonic() {
let mut last = milliseconds_i64(Duration::ZERO) as c_int;

for (i, d) in monotonicity_test_durations().iter().enumerate() {
let next = milliseconds_i64(*d) as c_int;
dbg!((i, d));
assert!(
next >= last,
"{next} >= {last} failed for {d:?} at index {i}"
);
last = next;
}
}

#[test]
fn milliseconds_i64_as_c_int_zero_is_zero() {
assert_eq!(0, milliseconds_i64(Duration::ZERO) as c_int);
}

#[test]
fn milliseconds_i64_is_monotonic() {
let mut last = milliseconds_i64(Duration::ZERO);

for (i, d) in monotonicity_test_durations().iter().enumerate() {
let next = milliseconds_i64(*d);
dbg!((i, d));
assert!(
next >= last,
"{next} >= {last} failed for {d:?} at index {i}"
);
last = next;
}
}

#[test]
fn milliseconds_i64_zero_is_zero() {
assert_eq!(0, milliseconds_i64(Duration::ZERO));
}
}
48 changes: 48 additions & 0 deletions src/windows/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,51 @@ impl SerialPort for COMPort {
}
}
}

#[cfg(test)]
mod test {
use super::*;

// TODO: Harmonize with corresponding tests for POSIX.
fn monotonicity_test_durations() -> Vec<Duration> {
vec![
Duration::ZERO,
Duration::from_nanos(1),
Duration::from_millis(1),
Duration::from_secs(1),
Duration::from_secs(i16::MAX as u64 - 1),
Duration::from_secs(i16::MAX as u64),
Duration::from_secs(i16::MAX as u64 + 1),
Duration::from_secs(i32::MAX as u64 - 1),
Duration::from_secs(i32::MAX as u64),
Duration::from_secs(i32::MAX as u64 + 1),
Duration::from_secs(i64::MAX as u64 - 1),
Duration::from_secs(i64::MAX as u64),
Duration::from_secs(i64::MAX as u64 + 1),
Duration::from_secs(u64::MAX - 1),
Duration::from_secs(u64::MAX),
Duration::from_secs(u64::MAX) + Duration::from_millis(1),
Duration::MAX,
]
}

#[test]
fn timeout_constant_is_monotonic() {
let mut last = COMPort::timeout_constant(Duration::ZERO);

for (i, d) in monotonicity_test_durations().iter().enumerate() {
let next = COMPort::timeout_constant(*d);
dbg!((i, d));
assert!(
next >= last,
"{next} >= {last} failed for {d:?} at index {i}"
);
last = next;
}
}

#[test]
fn timeout_constant_zero_is_zero() {
assert_eq!(0, COMPort::timeout_constant(Duration::ZERO));
}
}

0 comments on commit cf7d329

Please sign in to comment.