rust: time: add examples and tests for Delta
Subsystems (2)
cover rust: time: add examples and tests for Delta
From: FUJITA Tomonori <[email protected]>
This adds the examples and tests for Delta that I promised in the as_micros_ceil() fix thread [1].
As agreed in that thread [2], the examples show positive and negative values and how the methods saturate or round, without the boundary values. The boundary values are tested with KUnit instead, using constants in the test module.
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://lore.kernel.org/rust-for-linux/[email protected]/
FUJITA Tomonori (2):
rust: time: add examples to Delta methods
rust: time: add KUnit tests for Delta boundaries
rust/kernel/time.rs | 147 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+)
base-commit: 6b391eec2ddeb3ab2681cc5dee16d29e00202438
Signature · 1 line
2.43.0
PATCH 1/2 rust: time: add examples to Delta methods
From: FUJITA Tomonori <[email protected]>
Add examples to the following Delta methods: from_micros(), from_millis(), from_secs(), as_micros_ceil(), as_millis(), as_millis_ceil() and rem_nanos().
The examples show positive and negative values, and how the methods saturate or round.
--- rust/kernel/time.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+)
rust/kernel/time.rs +84 -0
@@ -547,6 +547,19 @@ pub const fn from_nanos(nanos: i64) -> Self {/// The `micros` can range from -9_223_372_036_854_775 to 9_223_372_036_854_775. /// If `micros` is outside this range, `i64::MIN` is used for negative values, /// and `i64::MAX` is used for positive values due to saturation. +/// +/// # Examples +/// +/// ``` +/// use kernel::time::Delta; +/// +/// assert_eq!(Delta::from_micros(1).as_nanos(), 1_000); +/// assert_eq!(Delta::from_micros(-1).as_nanos(), -1_000); +/// +/// // A value out of range saturates. +/// assert_eq!(Delta::from_micros(i64::MAX).as_nanos(), i64::MAX); +/// assert_eq!(Delta::from_micros(i64::MIN).as_nanos(), i64::MIN); +/// ``` #[inline] pub const fn from_micros (micros:i64 ) ->Self {Self {
@@ -559,6 +572,19 @@ pub const fn from_micros(micros: i64) -> Self {/// The `millis` can range from -9_223_372_036_854 to 9_223_372_036_854. /// If `millis` is outside this range, `i64::MIN` is used for negative values, /// and `i64::MAX` is used for positive values due to saturation. +/// +/// # Examples +/// +/// ``` +/// use kernel::time::Delta; +/// +/// assert_eq!(Delta::from_millis(1).as_nanos(), 1_000_000); +/// assert_eq!(Delta::from_millis(-1).as_nanos(), -1_000_000); +/// +/// // A value out of range saturates. +/// assert_eq!(Delta::from_millis(i64::MAX).as_nanos(), i64::MAX); +/// assert_eq!(Delta::from_millis(i64::MIN).as_nanos(), i64::MIN); +/// ``` #[inline] pub const fn from_millis (millis:i64 ) ->Self {Self {
@@ -571,6 +597,19 @@ pub const fn from_millis(millis: i64) -> Self {/// The `secs` can range from -9_223_372_036 to 9_223_372_036. /// If `secs` is outside this range, `i64::MIN` is used for negative values, /// and `i64::MAX` is used for positive values due to saturation. +/// +/// # Examples +/// +/// ``` +/// use kernel::time::Delta; +/// +/// assert_eq!(Delta::from_secs(1).as_nanos(), 1_000_000_000); +/// assert_eq!(Delta::from_secs(-1).as_nanos(), -1_000_000_000); +/// +/// // A value out of range saturates. +/// assert_eq!(Delta::from_secs(i64::MAX).as_nanos(), i64::MAX); +/// assert_eq!(Delta::from_secs(i64::MIN).as_nanos(), i64::MIN); +/// ``` #[inline] pub const fn from_secs (secs:i64 ) ->Self {Self {
@@ -598,6 +637,18 @@ pub const fn as_nanos(self) -> i64 {/// Return the smallest number of microseconds greater than or equal /// to the value in the [`Delta`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::time::Delta; +/// +/// assert_eq!(Delta::from_nanos(1_000).as_micros_ceil(), 1); +/// +/// // A value between two whole microseconds is rounded towards positive infinity. +/// assert_eq!(Delta::from_nanos(1_001).as_micros_ceil(), 2); +/// assert_eq!(Delta::from_nanos(-1_001).as_micros_ceil(), -1); +/// ``` #[inline] pub fn as_micros_ceil (self) ->i64 {// Only positive values need to be rounded up: truncating division already
@@ -623,6 +674,18 @@ pub fn as_micros_ceil(self) -> i64 { }/// Return the number of milliseconds in the [`Delta`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::time::Delta; +/// +/// assert_eq!(Delta::from_nanos(1_000_000).as_millis(), 1); +/// +/// // A value between two whole milliseconds is rounded towards zero. +/// assert_eq!(Delta::from_nanos(1_999_999).as_millis(), 1); +/// assert_eq!(Delta::from_nanos(-1_999_999).as_millis(), -1); +/// ``` #[inline] pub fn as_millis (self) ->i64 {#[cfg( CONFIG_64BIT )]
@@ -639,6 +702,18 @@ pub fn as_millis(self) -> i64 {/// Return the smallest number of milliseconds greater than or equal /// to the value in the [`Delta`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::time::Delta; +/// +/// assert_eq!(Delta::from_nanos(1_000_000).as_millis_ceil(), 1); +/// +/// // A value between two whole milliseconds is rounded towards positive infinity. +/// assert_eq!(Delta::from_nanos(1_000_001).as_millis_ceil(), 2); +/// assert_eq!(Delta::from_nanos(-1_000_001).as_millis_ceil(), -1); +/// ``` #[inline] pub fn as_millis_ceil (self) ->i64 {// Only positive values need to be rounded up: truncating division already
@@ -725,6 +800,15 @@ pub fn to_jiffies_timeout(self) -> Delta<Jiffy> {/// /// Panics if `divisor` is zero, or if `self` represents [`i64::MIN`] /// nanoseconds and `divisor` is `-1`. +/// +/// # Examples +/// +/// ``` +/// use kernel::time::Delta; +/// +/// assert_eq!(Delta::from_nanos(10).rem_nanos(3).as_nanos(), 1); +/// assert_eq!(Delta::from_nanos(-10).rem_nanos(3).as_nanos(), -1); +/// ``` #[inline] pub fn rem_nanos (self, divisor:i32 ) ->Self {if divisor ==0 {
Signature · 1 line
2.43.0
PATCH 2/2 rust: time: add KUnit tests for Delta boundaries
In reply to FUJITA TomonoriFrom: FUJITA Tomonori <[email protected]>
Add KUnit tests for the boundary values of from_micros(), from_millis(), from_secs(), as_micros_ceil() and as_millis_ceil().
The boundary values are defined as constants in the test module, so they are not part of the API.
--- rust/kernel/time.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
rust/kernel/time.rs +63 -0
@@ -846,6 +846,69 @@ pub fn rem_nanos(self, divisor: i32) -> Self {mod tests {use super ::*; +// The largest values that `Delta::from_micros()`, `Delta::from_millis()` and +// `Delta::from_secs()` convert without saturation. +const MAX_MICROS :i64 =9_223_372_036_854_775 ; +const MAX_MILLIS :i64 =9_223_372_036_854 ; +const MAX_SECS :i64 =9_223_372_036 ; + +#[test] +fn from_micros_saturates () { +assert_eq! ( +Delta ::from_micros (MAX_MICROS ).as_nanos (), +MAX_MICROS *NSEC_PER_USEC + ); +assert_eq! (Delta ::from_micros (MAX_MICROS +1 ).as_nanos (),i64 ::MAX ); + +assert_eq! ( +Delta ::from_micros (-MAX_MICROS ).as_nanos (), + -MAX_MICROS *NSEC_PER_USEC + ); +assert_eq! (Delta ::from_micros (-MAX_MICROS -1 ).as_nanos (),i64 ::MIN ); + } + +#[test] +fn from_millis_saturates () { +assert_eq! ( +Delta ::from_millis (MAX_MILLIS ).as_nanos (), +MAX_MILLIS *NSEC_PER_MSEC + ); +assert_eq! (Delta ::from_millis (MAX_MILLIS +1 ).as_nanos (),i64 ::MAX ); + +assert_eq! ( +Delta ::from_millis (-MAX_MILLIS ).as_nanos (), + -MAX_MILLIS *NSEC_PER_MSEC + ); +assert_eq! (Delta ::from_millis (-MAX_MILLIS -1 ).as_nanos (),i64 ::MIN ); + } + +#[test] +fn from_secs_saturates () { +assert_eq! ( +Delta ::from_secs (MAX_SECS ).as_nanos (), +MAX_SECS *NSEC_PER_SEC + ); +assert_eq! (Delta ::from_secs (MAX_SECS +1 ).as_nanos (),i64 ::MAX ); + +assert_eq! ( +Delta ::from_secs (-MAX_SECS ).as_nanos (), + -MAX_SECS *NSEC_PER_SEC + ); +assert_eq! (Delta ::from_secs (-MAX_SECS -1 ).as_nanos (),i64 ::MIN ); + } + +#[test] +fn as_micros_ceil_handles_extremes () { +assert_eq! (Delta ::from_nanos (i64 ::MAX ).as_micros_ceil (),MAX_MICROS +1 ); +assert_eq! (Delta ::from_nanos (i64 ::MIN ).as_micros_ceil (), -MAX_MICROS ); + } + +#[test] +fn as_millis_ceil_handles_extremes () { +assert_eq! (Delta ::from_nanos (i64 ::MAX ).as_millis_ceil (),MAX_MILLIS +1 ); +assert_eq! (Delta ::from_nanos (i64 ::MIN ).as_millis_ceil (), -MAX_MILLIS ); + } +#[test] fn to_jiffies_timeout_converts () {const HZ :isize = bindings::HZ as isize ;
Signature · 1 line
2.43.0