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
2 changes: 1 addition & 1 deletion jxl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ pub mod headers;
pub mod icc;
pub mod image;
pub mod render;
mod util;
pub mod util;
8 changes: 6 additions & 2 deletions jxl/src/render/stages/upsample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ impl RenderPipelineStage for Upsample2x {
#[cfg(test)]
mod test {
use super::*;
use crate::{error::Result, image::Image, render::test::make_and_run_simple_pipeline};
//use util::test::assert_almost_eq;
mo271 marked this conversation as resolved.
Show resolved Hide resolved
use crate::{
error::Result, image::Image, render::test::make_and_run_simple_pipeline,
util::test::assert_almost_eq,
};
use test_log::test;

#[test]
Expand All @@ -112,7 +116,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
3 changes: 3 additions & 0 deletions jxl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#[allow(unused)]
pub mod test;

mod bits;
#[allow(unused)]
mod concat_slice;
Expand Down
84 changes: 84 additions & 0 deletions jxl/src/util/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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_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
),
_ => {}
}
}
}
};
}
pub(crate) use assert_almost_eq;

#[cfg(test)]
mod tests {
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]
#[should_panic]
fn test_panic_float() {
assert_almost_eq!(1.0, 1.2, 0.1);
}
#[test]
#[should_panic]
fn test_panic_integer() {
assert_almost_eq!(100, 105, 2);
}

#[test]
#[should_panic]
fn test_nan_comparison() {
assert_almost_eq!(f64::NAN, f64::NAN, 0.1);
}

#[test]
#[should_panic]
fn test_nan_tolerance() {
assert_almost_eq!(1.0, 1.0, f64::NAN);
}

#[test]
fn test_infinity_tolerance() {
assert_almost_eq!(1.0, 1.0, f64::INFINITY);
}

#[test]
#[should_panic]
fn test_nan_comparison_with_infinity_tolerance() {
assert_almost_eq!(f32::NAN, f32::NAN, f32::INFINITY);
}

#[test]
#[should_panic]
fn test_infinity_comparison_with_infinity_tolerance() {
assert_almost_eq!(f32::INFINITY, f32::INFINITY, f32::INFINITY);
}
}
Loading