From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/rustix/tests/time/monotonic.rs | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 vendor/rustix/tests/time/monotonic.rs (limited to 'vendor/rustix/tests/time/monotonic.rs') diff --git a/vendor/rustix/tests/time/monotonic.rs b/vendor/rustix/tests/time/monotonic.rs new file mode 100644 index 000000000..89470f2b4 --- /dev/null +++ b/vendor/rustix/tests/time/monotonic.rs @@ -0,0 +1,45 @@ +#[cfg(feature = "thread")] +use rustix::thread::nanosleep; +use rustix::time::{clock_gettime, ClockId, Timespec}; + +/// Attempt to test that the monotonic clock is monotonic. Time may or may not +/// advance, but it shouldn't regress. +#[test] +fn test_monotonic_clock() { + let a = clock_gettime(ClockId::Monotonic); + let b = clock_gettime(ClockId::Monotonic); + if b.tv_sec == a.tv_sec { + assert!(b.tv_nsec >= a.tv_nsec); + } else { + assert!(b.tv_sec > a.tv_sec); + } +} + +/// With the "thread" feature, we can sleep so that we're guaranteed that time +/// has advanced. +#[cfg(feature = "thread")] +#[test] +fn test_monotonic_clock_with_sleep_1s() { + let a = clock_gettime(ClockId::Monotonic); + let _rem = nanosleep(&Timespec { + tv_sec: 1, + tv_nsec: 0, + }); + let b = clock_gettime(ClockId::Monotonic); + assert!(b.tv_sec > a.tv_sec); +} + +/// With the "thread" feature, we can sleep so that we're guaranteed that time +/// has advanced. +#[cfg(feature = "thread")] +#[test] +fn test_monotonic_clock_with_sleep_1ms() { + let a = clock_gettime(ClockId::Monotonic); + let _rem = nanosleep(&Timespec { + tv_sec: 0, + tv_nsec: 1_000_000, + }); + let b = clock_gettime(ClockId::Monotonic); + assert!(b.tv_sec >= a.tv_sec); + assert!(b.tv_sec != a.tv_sec || b.tv_nsec > a.tv_nsec); +} -- cgit v1.2.3