Skip to content

Commit

Permalink
binding: update floating-point comparisons in tests
Browse files Browse the repository at this point in the history
version 1.60 of the rust stdlib included a change to improve the accuracy
of calculations in `Duration::try_from_secs_f32/64` (see [1]), which had a
minor breaking change of slightly altering the calculated results. version
1.63 further tweaked this (see [2]).

this commit updates tests to match the new calculated values from 1.63+,
and thus fixes failure running tests in newer stable rust releases and
helps us to fix CI failure. this is done at the expense of breaking the
ability to run the tests with older rust versions, which seems reasonably
acceptable at this time. for the record, the new values used here are
those calculated using version 1.66.

it has been suggested that rather than compare float values directly, it
may be better for this crate to compare via the `float_eq` crate instead.
this would essentially mean switching to a less accurate comparison. since
some time has passed now since i originally encountered this problem, i
think that just updating the values compared against is the best move at
this time, though i am not necessarily against moving to use of `float_eq`
at some point in future if this causes significant problems for users of
older rust versions, or if we encounter further such issues from rust
stdlib changes.

note that in the original issue (see [3]) there were four functions whose
doc-tests were failing, yet only three have needed to be modified here.
the lack of needing to fix the fourth function's tests is presumably as a
result of the changes made in rust v1.63. (the original issue was noticed
with a v1.60 nightly).

[1]: rust-lang/rust#90247
[2]: rust-lang/rust#96051
[3]: #48
  • Loading branch information
jnqnfe committed Jan 9, 2023
1 parent 2f5ae7c commit c6d5a93
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pulse-binding/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# [unreleased]

* Tweaked some doc-tests to work with rust v1.63+ (at the expense of not working with older
versions). This relates to float-point calculation changes in `Duration::try_from_secs_f32/64`
made in rust stdlib version 1.60 and further tweaked in 1.63. See [this issue][issue48].

# 2.26.0 (January 13th, 2022)

* Changed the type of the `timestamp` member of `def::TimingInfo` from `Timeval` to `UnixTs` since
Expand Down Expand Up @@ -660,3 +666,4 @@ Note, version number 1.0.4 skipped (it was used for non-crate project changes).
[`bitflags`]: https://docs.rs/bitflags
[issue11]: https://github.com/jnqnfe/pulse-binding-rust/issues/11
[issue26]: https://github.com/jnqnfe/pulse-binding-rust/issues/26
[issue48]: https://github.com/jnqnfe/pulse-binding-rust/issues/48
8 changes: 4 additions & 4 deletions pulse-binding/src/time/microseconds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl MicroSeconds {
/// ```rust
/// # use libpulse_binding::time::MicroSeconds;
/// assert_eq!(MicroSeconds::from_secs_f32(0.5), MicroSeconds(500_000));
/// assert_eq!(MicroSeconds::from_secs_f32(2.3), MicroSeconds(2_300_000));
/// assert_eq!(MicroSeconds::from_secs_f32(2.3), MicroSeconds(2_299_999));
/// ```
///
/// These should panic.
Expand Down Expand Up @@ -510,8 +510,8 @@ impl MicroSeconds {
/// let micros = MicroSeconds(2_700_000_000);
///
/// // Note the rounding errors that are clear here.
/// assert_eq!(micros.mul_f32(3.14), MicroSeconds(8_478_000_152));
/// assert_eq!(micros.mul_f32(3.14e5), MicroSeconds(847_800_018_512_379));
/// assert_eq!(micros.mul_f32(3.14), MicroSeconds(8_478_000_000));
/// assert_eq!(micros.mul_f32(3.14e5), MicroSeconds(847_800_000_000_000));
/// ```
///
/// These should panic.
Expand Down Expand Up @@ -596,7 +596,7 @@ impl MicroSeconds {
/// # use libpulse_binding::time::{MicroSeconds, MICROS_PER_SEC};
/// let micros = MicroSeconds(2_700_000_000);
///
/// assert_eq!(micros.div_f32(3.14), MicroSeconds(859_872_559));
/// assert_eq!(micros.div_f32(3.14), MicroSeconds(859_872_558));
/// assert_eq!(micros.div_f32(3.14e5), MicroSeconds(8_598));
/// ```
///
Expand Down

0 comments on commit c6d5a93

Please sign in to comment.