From 067922402110c067b1e82b2e03c4a58b3629b305 Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Thu, 22 Aug 2024 19:40:15 +0200 Subject: [PATCH 1/3] lib: Update offset calculation to convert timestamp subtraction to a signed integer Fix: #26 --- src/lib.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a262906..11ac5eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -619,8 +619,8 @@ fn roundtrip_calculate( } fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 { - let theta = ((t2.wrapping_sub(t1) / 2) as i64) - .saturating_add((t3.wrapping_sub(t4) / 2) as i64); + let theta = (t2.wrapping_sub(t1) as i64 / 2) + .saturating_add(t3.wrapping_sub(t4) as i64 / 2); let theta_sec = (theta.unsigned_abs() & SECONDS_MASK) >> 32; let theta_sec_fraction = theta.unsigned_abs() & SECONDS_FRAC_MASK; @@ -640,15 +640,59 @@ fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 { #[test] fn test_offset_calculate() { - let t1 = 9487534663484046772u64; - let t2 = 16882120099581835046u64; - let t3 = 16882120099583884144u64; - let t4 = 9487534663651464597u64; - - assert_eq!( - offset_calculate(t1, t2, t3, t4, Units::Microseconds), - 1721686086620926 - ); + struct Timestamps(u64, u64, u64, u64); + struct TestCase(Timestamps, i64); + + let tests = vec![ + TestCase( + Timestamps( + 16893142954672769962, + 16893142959053084959, + 16893142959053112968, + 16893142954793063406, + ), + 1005870, + ), + TestCase( + Timestamps( + 16893362966131575843, + 16893362966715800791, + 16893362966715869584, + 16893362967084349913, + ), + 25115, + ), + TestCase( + Timestamps( + 16893399716399327198, + 16893399716453045029, + 16893399716453098083, + 16893399716961924964, + ), + -52981, + ), + TestCase( + Timestamps( + 9487534663484046772u64, + 16882120099581835046u64, + 16882120099583884144u64, + 9487534663651464597u64, + ), + 1721686086620926, + ), + ]; + + for t in tests { + let offset = offset_calculate( + t.0 .0, + t.0 .1, + t.0 .2, + t.0 .3, + Units::Microseconds, + ); + let expected = t.1; + assert_eq!(offset, expected); + } } #[cfg(feature = "log")] From eca226e1ca579125a5ec61fc9855edc100e8cc5d Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Thu, 22 Aug 2024 19:44:38 +0200 Subject: [PATCH 2/3] lib: Fix tests for no_std --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 11ac5eb..fc66246 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -643,7 +643,7 @@ fn test_offset_calculate() { struct Timestamps(u64, u64, u64, u64); struct TestCase(Timestamps, i64); - let tests = vec![ + let tests = [ TestCase( Timestamps( 16893142954672769962, From b2e96d7b788603f95ac9bbccd6d38d366eda9845 Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Thu, 22 Aug 2024 21:22:53 +0200 Subject: [PATCH 3/3] lib: Clean up tests --- src/lib.rs | 138 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 80 insertions(+), 58 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fc66246..3c0505f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -638,63 +638,6 @@ fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 { } } -#[test] -fn test_offset_calculate() { - struct Timestamps(u64, u64, u64, u64); - struct TestCase(Timestamps, i64); - - let tests = [ - TestCase( - Timestamps( - 16893142954672769962, - 16893142959053084959, - 16893142959053112968, - 16893142954793063406, - ), - 1005870, - ), - TestCase( - Timestamps( - 16893362966131575843, - 16893362966715800791, - 16893362966715869584, - 16893362967084349913, - ), - 25115, - ), - TestCase( - Timestamps( - 16893399716399327198, - 16893399716453045029, - 16893399716453098083, - 16893399716961924964, - ), - -52981, - ), - TestCase( - Timestamps( - 9487534663484046772u64, - 16882120099581835046u64, - 16882120099583884144u64, - 9487534663651464597u64, - ), - 1721686086620926, - ), - ]; - - for t in tests { - let offset = offset_calculate( - t.0 .0, - t.0 .1, - t.0 .2, - t.0 .3, - Units::Microseconds, - ); - let expected = t.1; - assert_eq!(offset, expected); - } -} - #[cfg(feature = "log")] fn debug_ntp_packet(packet: &NtpPacket, _recv_timestamp: u64) { let mode = shifter(packet.li_vn_mode, MODE_MASK, MODE_SHIFT); @@ -873,9 +816,42 @@ mod sntpc_ntp_result_tests { #[cfg(all(test, feature = "std"))] mod sntpc_tests { - use crate::{get_time, Error, NtpContext, StdTimestampGen, Units}; + use crate::{ + get_time, offset_calculate, Error, NtpContext, StdTimestampGen, Units, + }; use std::net::UdpSocket; + struct Timestamps(u64, u64, u64, u64); + struct OffsetCalcTestCase { + timestamp: Timestamps, + expected: i64, + } + + impl OffsetCalcTestCase { + fn new(t1: u64, t2: u64, t3: u64, t4: u64, expected: i64) -> Self { + OffsetCalcTestCase { + timestamp: Timestamps(t1, t2, t3, t4), + expected, + } + } + + fn t1(&self) -> u64 { + self.timestamp.0 + } + + fn t2(&self) -> u64 { + self.timestamp.1 + } + + fn t3(&self) -> u64 { + self.timestamp.2 + } + + fn t4(&self) -> u64 { + self.timestamp.3 + } + } + #[test] fn test_ntp_request_sntpv4_supported() { let context = NtpContext::new(StdTimestampGen::default()); @@ -941,4 +917,50 @@ mod sntpc_tests { assert_eq!(format!("{}", Units::Milliseconds), "ms"); assert_eq!(format!("{}", Units::Microseconds), "us"); } + + #[test] + fn test_offset_calculate() { + let tests = [ + OffsetCalcTestCase::new( + 16893142954672769962, + 16893142959053084959, + 16893142959053112968, + 16893142954793063406, + 1005870, + ), + OffsetCalcTestCase::new( + 16893362966131575843, + 16893362966715800791, + 16893362966715869584, + 16893362967084349913, + 25115, + ), + OffsetCalcTestCase::new( + 16893399716399327198, + 16893399716453045029, + 16893399716453098083, + 16893399716961924964, + -52981, + ), + OffsetCalcTestCase::new( + 9487534663484046772u64, + 16882120099581835046u64, + 16882120099583884144u64, + 9487534663651464597u64, + 1721686086620926, + ), + ]; + + for t in tests { + let offset = offset_calculate( + t.t1(), + t.t2(), + t.t3(), + t.t4(), + Units::Microseconds, + ); + let expected = t.expected; + assert_eq!(offset, expected); + } + } }