From 640bd7ef9f1a1e72422e1351bd88e77424c0b7bb Mon Sep 17 00:00:00 2001 From: mrferris Date: Tue, 20 Jun 2023 15:58:14 -0400 Subject: [PATCH] Add time funciton tests --- src/time.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/time.rs b/src/time.rs index cbe0e4c..ff8845a 100644 --- a/src/time.rs +++ b/src/time.rs @@ -17,3 +17,29 @@ pub fn duration_between(earlier_micros: u32, later_micros: u32) -> Duration { Duration::from_micros((later_micros - earlier_micros).into()) } } + +#[test] +fn test_now_duration() { + let micros_a = now_micros(); + let micros_b = now_micros(); + + assert!(micros_a < micros_b); + + let duration = duration_between(micros_a, micros_b); + + let duration_micros = duration.as_micros(); + assert!(duration_micros > 0); + assert!(duration_micros < 2000); +} + +#[test] +fn test_wrapped_duration() { + // Max u32 value minus 10. + let earlier_micros: u32 = 4294967285; + // Wrapped around by 10. + let later_micros: u32 = 10; + // earlier_micros > later_micros + let duration = duration_between(earlier_micros, later_micros); + // With 10 microseconds on each side of the max, total should be 20 microseconds. + assert_eq!(duration.as_micros(), 20); +}