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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
debug = true

[workspace]
members = ["jxl", "jxl_cli", "jxl_macros"]
members = ["jxl", "jxl_cli", "jxl_macros", "jxl_test_utils"]
resolver = "2"

[workspace.lints.clippy]
Expand Down
1 change: 1 addition & 0 deletions jxl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ array-init = "2.0.0"
half = "1.7.1"
tracing = { version = "0.1.40", optional = true }
jxl_macros = { path = "../jxl_macros" }
jxl_test_utils = {path = "../jxl_test_utils" }

[dev-dependencies]
arbtest = "0.3.1"
Expand Down
4 changes: 3 additions & 1 deletion jxl/src/render/stages/upsample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ impl RenderPipelineStage for Upsample2x {

#[cfg(test)]
mod test {
//use jxl_test_utils::assert_almost_equal;
use super::*;
use crate::{error::Result, image::Image, render::test::make_and_run_simple_pipeline};
use jxl_test_utils::assert_almost_eq;
use test_log::test;

#[test]
Expand All @@ -112,7 +114,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
9 changes: 9 additions & 0 deletions jxl_test_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "jxl_test_utils"
version = "0.1.0"
edition = "2021"

[dependencies]

[lints]
workspace = true
64 changes: 64 additions & 0 deletions jxl_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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]
mo271 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 super::*;

#[test]
fn test_with_floats() {
mo271 marked this conversation as resolved.
Show resolved Hide resolved
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() {
use std::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"
);
}
}