Skip to content

Commit

Permalink
assert_almost_eq! (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
mo271 authored Nov 25, 2024
1 parent 25589fa commit 10edd52
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
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;
7 changes: 5 additions & 2 deletions jxl/src/render/stages/upsample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ impl RenderPipelineStage for Upsample2x {
#[cfg(test)]
mod test {
use super::*;
use crate::{error::Result, image::Image, render::test::make_and_run_simple_pipeline};
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 +115,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
1 change: 1 addition & 0 deletions jxl/src/util/concat_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl<'first, 'second> ConcatSlice<'first, 'second> {
}
}

#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.slices.0.len() + self.slices.1.len()
}
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);
}
}

0 comments on commit 10edd52

Please sign in to comment.