Janitor: Mark up float comparisons where we want bitwise identity

Mark up test cases where we want bitwise identity of float values and
use `assert_eq!` since that produces better output in the error case.
This commit is contained in:
Tobias Hunger 2021-07-08 21:48:22 +02:00 committed by Simon Hausmann
parent 8f7d7afa94
commit ad98137c17
3 changed files with 11 additions and 6 deletions

View file

@ -54,6 +54,7 @@ mod tests {
} }
#[test] #[test]
#[allow(clippy::float_cmp)] // We want bit-wise equality here
fn test_simple() { fn test_simple() {
// Get a pointer to `b` within `Foo` // Get a pointer to `b` within `Foo`
let foo_b = Foo::FIELD_OFFSETS.b; let foo_b = Foo::FIELD_OFFSETS.b;
@ -64,7 +65,7 @@ mod tests {
// Apply the pointer to get at `b` and read it // Apply the pointer to get at `b` and read it
{ {
let y = foo_b.apply(&x); let y = foo_b.apply(&x);
assert!(*y == 2.0); assert_eq!(*y, 2.0);
} }
// Apply the pointer to get at `b` and mutate it // Apply the pointer to get at `b` and mutate it
@ -72,10 +73,11 @@ mod tests {
let y = foo_b.apply_mut(&mut x); let y = foo_b.apply_mut(&mut x);
*y = 42.0; *y = 42.0;
} }
assert!(x.b == 42.0); assert_eq!(x.b, 42.0);
} }
#[test] #[test]
#[allow(clippy::float_cmp)] // We want bit-wise equality here
fn test_nested() { fn test_nested() {
// Construct an example `Foo` // Construct an example `Foo`
let mut x = Bar { x: 0, y: Foo { a: 1, b: 2.0, c: false } }; let mut x = Bar { x: 0, y: Foo { a: 1, b: 2.0, c: false } };
@ -88,10 +90,11 @@ mod tests {
let y = bar_y_b.apply_mut(&mut x); let y = bar_y_b.apply_mut(&mut x);
*y = 42.0; *y = 42.0;
} }
assert!(x.y.b == 42.0); assert_eq!(x.y.b, 42.0);
} }
#[test] #[test]
#[allow(clippy::float_cmp)] // We want bit-wise equality here
fn test_pin() { fn test_pin() {
use ::alloc::boxed::Box; use ::alloc::boxed::Box;
// Get a pointer to `b` within `Foo` // Get a pointer to `b` within `Foo`
@ -99,15 +102,15 @@ mod tests {
let foo_b_pin = unsafe { foo_b.as_pinned_projection() }; let foo_b_pin = unsafe { foo_b.as_pinned_projection() };
let foo = Box::pin(Foo { a: 21, b: 22.0, c: true }); let foo = Box::pin(Foo { a: 21, b: 22.0, c: true });
let pb: Pin<&f64> = foo_b_pin.apply_pin(foo.as_ref()); let pb: Pin<&f64> = foo_b_pin.apply_pin(foo.as_ref());
assert!(*pb == 22.0); assert_eq!(*pb, 22.0);
let mut x = Box::pin(Bar { x: 0, y: Foo { a: 1, b: 52.0, c: false } }); let mut x = Box::pin(Bar { x: 0, y: Foo { a: 1, b: 52.0, c: false } });
let bar_y_b = Bar::FIELD_OFFSETS.y + foo_b_pin; let bar_y_b = Bar::FIELD_OFFSETS.y + foo_b_pin;
assert!(*bar_y_b.apply(&*x) == 52.0); assert_eq!(*bar_y_b.apply(&*x), 52.0);
let bar_y_pin = unsafe { Bar::FIELD_OFFSETS.y.as_pinned_projection() }; let bar_y_pin = unsafe { Bar::FIELD_OFFSETS.y.as_pinned_projection() };
*(bar_y_pin + foo_b_pin).apply_pin_mut(x.as_mut()) = 12.; *(bar_y_pin + foo_b_pin).apply_pin_mut(x.as_mut()) = 12.;
assert!(x.y.b == 12.0); assert_eq!(x.y.b, 12.0);
} }
} }

View file

@ -161,6 +161,7 @@ impl InterpolatedPropertyValue for Brush {
} }
#[test] #[test]
#[allow(clippy::float_cmp)] // We want bit-wise equality here
fn test_linear_gradient_encoding() { fn test_linear_gradient_encoding() {
let stops: SharedVector<GradientStop> = [ let stops: SharedVector<GradientStop> = [
GradientStop { position: 0.0, color: Color::from_argb_u8(255, 255, 0, 0) }, GradientStop { position: 0.0, color: Color::from_argb_u8(255, 255, 0, 0) },

View file

@ -206,6 +206,7 @@ mod grid_internal {
} }
#[test] #[test]
#[allow(clippy::float_cmp)] // We want bit-wise equality here
fn test_layout_items() { fn test_layout_items() {
let my_items = &mut [ let my_items = &mut [
LayoutData { min: 100., max: 200., pref: 100., stretch: 1., ..Default::default() }, LayoutData { min: 100., max: 200., pref: 100., stretch: 1., ..Default::default() },