Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert_almost_eq! #55

Merged
merged 15 commits into from
Nov 25, 2024
3 changes: 2 additions & 1 deletion jxl/src/render/stages/upsample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl RenderPipelineStage for Upsample2x {
#[cfg(test)]
mod test {
use super::*;
use crate::assert_almost_eq;
use crate::{error::Result, image::Image, render::test::make_and_run_simple_pipeline};
use test_log::test;

Expand All @@ -112,7 +113,7 @@ mod test {
make_and_run_simple_pipeline(stage, &[input], image_size, 123)?.1;
for x in 0..image_size.0 {
for y in 0..image_size.1 {
assert!((output[0].as_rect().row(y)[x] - val).abs() <= 0.0000001);
assert_almost_eq!(output[0].as_rect().row(y)[x], val, 0.0000001);
}
}
Ok(())
Expand Down
2 changes: 2 additions & 0 deletions jxl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ pub use bits::*;
pub use concat_slice::*;
pub use log2::*;
pub use shift_right_ceil::*;

mod test;
mo271 marked this conversation as resolved.
Show resolved Hide resolved
101 changes: 101 additions & 0 deletions jxl/src/util/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#[macro_export]
veluca93 marked this conversation as resolved.
Show resolved Hide resolved
macro_rules! assert_almost_eq {
($left:expr, $right:expr, $max_error:expr $(,)?) => {
match (&$left, &$right, &$max_error) {
(left_val, right_val, max_error) => {
let diff = if *left_val > *right_val {
*left_val - *right_val
} else {
*right_val - *left_val
};
match diff.partial_cmp(max_error) {
Some(std::cmp::Ordering::Greater) | None => panic!(
"assertion failed: `(left ≈ right)`\n left: `{:?}`,\n right: `{:?}`,\n max_error: `{:?}`",
left_val, right_val, max_error
),
_ => {}
}
}
}
};
}

#[cfg(test)]
mod tests {
use std::f64::INFINITY;
use std::f64::NAN;
use std::panic;

#[test]
fn test_with_floats() {
assert_almost_eq!(1.0000001f64, 1.0000002, 0.000001);
assert_almost_eq!(1.0, 1.1, 0.2);
}

#[test]
fn test_with_integers() {
assert_almost_eq!(100, 101, 2);
assert_almost_eq!(777u32, 770, 7);
assert_almost_eq!(500i64, 498, 3);
}

#[test]
fn test_panic() {
let result = panic::catch_unwind(|| {
assert_almost_eq!(1.0, 1.2, 0.1);
});
assert!(
result.is_err(),
"Expected assert_almost_eq! to panic, but it didn't"
);

let result = panic::catch_unwind(|| {
assert_almost_eq!(100, 105, 2);
});
assert!(
result.is_err(),
"Expected assert_almost_eq! to panic, but it didn't"
);
}
#[test]
fn test_nan() {
let result = panic::catch_unwind(|| {
assert_almost_eq!(NAN, NAN, 0.1);
});
assert!(
result.is_err(),
"Expected assert_almost_eq! to panic, but it didn't"
);

let result = panic::catch_unwind(|| {
assert_almost_eq!(1.0, 1.0, NAN);
});
assert!(
result.is_err(),
"Expected assert_almost_eq! to panic, but it didn't"
);

assert_almost_eq!(1.0, 1.0, INFINITY);

let result = panic::catch_unwind(|| {
assert_almost_eq!(NAN, NAN, INFINITY);
});
assert!(
result.is_err(),
"Expected assert_almost_eq! to panic, but it didn't"
mo271 marked this conversation as resolved.
Show resolved Hide resolved
);

let result = panic::catch_unwind(|| {
assert_almost_eq!(INFINITY, INFINITY, INFINITY);
mo271 marked this conversation as resolved.
Show resolved Hide resolved
});
assert!(
result.is_err(),
"Expected assert_almost_eq! to panic, but it didn't"
);
}
}
Loading