sic_testing/
lib.rs

1#![deny(clippy::all)]
2
3use std::path::{Path, PathBuf};
4
5// re-export parameterized macro's
6pub use parameterized::ide;
7pub use parameterized::parameterized as pm;
8use sic_core::image::GenericImageView;
9use sic_core::SicImage;
10
11// just enough, absolute tolerance, floating point comparison.
12#[macro_export]
13macro_rules! approx_eq_f32 {
14    ($input:expr, $expected:expr) => {
15        assert!(($input - $expected).abs() <= std::f32::EPSILON);
16    };
17}
18
19#[macro_export]
20macro_rules! out_ {
21    ($path:expr) => {
22        &[env!("CARGO_MANIFEST_DIR"), "/../../target/", $path].concat()
23    };
24}
25
26#[macro_export]
27macro_rules! in_ {
28    ($path:expr) => {
29        &[env!("CARGO_MANIFEST_DIR"), "/../../resources/", $path].concat()
30    };
31}
32
33/// TODO{issue#128}: rework to provide flexibility and consistency, so all modules can use this;
34pub fn setup_test_image(test_image_path: &str) -> PathBuf {
35    Path::new("").join(in_!(test_image_path))
36}
37
38pub fn setup_output_path(test_output_path: &str) -> PathBuf {
39    Path::new("").join(out_!(test_output_path))
40}
41
42pub fn clean_up_output_path(test_output_path: &str) {
43    std::fs::remove_file(setup_output_path(test_output_path))
44        .expect("Unable to remove file after test.");
45}
46
47pub fn open_test_image<P: AsRef<Path>>(path: P) -> sic_core::SicImage {
48    sic_core::image::open(path.as_ref()).unwrap().into()
49}
50
51pub fn image_eq<T: Into<sic_core::SicImage>>(left: T, right: T) -> bool {
52    let left = left.into();
53    let right = right.into();
54
55    left.dimensions() == right.dimensions()
56        && left
57            .pixels()
58            .zip(right.pixels())
59            .all(|(l, r)| l.0 == r.0 && l.1 == r.1 && l.2 == r.2)
60}
61
62// Adds direct access for static images.
63pub trait SicImageDirectAccess {
64    fn get_pixel<I: GenericImageView>(&self, x: u32, y: u32) -> I::Pixel
65    where
66        Self: AsRef<I>,
67    {
68        self.as_ref().get_pixel(x, y)
69    }
70
71    fn width<I: GenericImageView>(&self) -> u32
72    where
73        Self: AsRef<I>,
74    {
75        self.as_ref().width()
76    }
77
78    fn height<I: GenericImageView>(&self) -> u32
79    where
80        Self: AsRef<I>,
81    {
82        self.as_ref().height()
83    }
84
85    fn dimensions<I: GenericImageView>(&self) -> (u32, u32)
86    where
87        Self: AsRef<I>,
88    {
89        self.as_ref().dimensions()
90    }
91
92    fn pixels<I: GenericImageView>(&self) -> sic_core::image::Pixels<I>
93    where
94        Self: AsRef<I>,
95    {
96        self.as_ref().pixels()
97    }
98}
99
100impl SicImageDirectAccess for SicImage {}