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

Use assert_relative_eq! macros in ndarray and nalgebra backend tests #404

Merged
merged 4 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ jobs:
run: cargo clippy -p argmin --all-targets --all-features -- -D warnings
- name: Clippy (argmin-observer-slog)
run: cargo clippy -p argmin-observer-slog --all-targets --features "serde1" -- -D warnings
- name: Clippy (argmin-observer-paramwriter)
run: cargo clippy -p argmin-observer-paramwriter --all-targets -- -D warnings
- name: Clippy (argmin-observer-spectator)
run: cargo clippy -p argmin-observer-spectator --all-targets -- -D warnings
- name: Clippy (argmin-checkpointing-file)
run: cargo clippy -p argmin-checkpointing-file --all-targets -- -D warnings
- name: Clippy (spectator)
run: cargo clippy -p spectator --all-targets -- -D warnings

Expand Down
85 changes: 68 additions & 17 deletions argmin-math/ndarray-linalg-tests/ndarray_latest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,102 @@
mod add {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/add.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/add.rs"
));
}
mod conj {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/conj.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/conj.rs"
));
}
mod div {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/div.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/div.rs"
));
}
mod dot {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/dot.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/dot.rs"
));
}
mod eye {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/eye.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/eye.rs"
));
}
mod inv {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/inv.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/inv.rs"
));
}
mod l1norm {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/l1norm.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/l1norm.rs"
));
}
mod l2norm {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/l2norm.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/l2norm.rs"
));
}
mod minmax {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/minmax.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/minmax.rs"
));
}
mod mul {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/mul.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/mul.rs"
));
}
mod random {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/random.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/random.rs"
));
}
mod scaledadd {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/scaledadd.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/scaledadd.rs"
));
}
mod scaledsub {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/scaledsub.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/scaledsub.rs"
));
}
mod signum {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/signum.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/signum.rs"
));
}
mod sub {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/sub.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/sub.rs"
));
}
mod transpose {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/transpose.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/transpose.rs"
));
}
mod zero {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../ndarray-tests-src/zero.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../ndarray-tests-src/zero.rs"
));
}
18 changes: 8 additions & 10 deletions argmin-math/ndarray-tests-src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.


#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
use approx::assert_relative_eq;
use argmin_math::ArgminAdd;
use ndarray::{Array1, Array2};

use ndarray::array;
use ndarray::{Array1, Array2};
use paste::item;

macro_rules! make_test {
Expand All @@ -26,7 +25,7 @@ mod tests {
let target = array![35 as $t, 38 as $t, 42 as $t];
let res = <Array1<$t> as ArgminAdd<$t, Array1<$t>>>::add(&a, &b);
for i in 0..3 {
assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand All @@ -39,7 +38,7 @@ mod tests {
let target = array![35 as $t, 38 as $t, 42 as $t];
let res = <$t as ArgminAdd<Array1<$t>, Array1<$t>>>::add(&b, &a);
for i in 0..3 {
assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand All @@ -52,7 +51,7 @@ mod tests {
let target = array![42 as $t, 42 as $t, 42 as $t];
let res = <Array1<$t> as ArgminAdd<Array1<$t>, Array1<$t>>>::add(&a, &b);
for i in 0..3 {
assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand Down Expand Up @@ -105,7 +104,7 @@ mod tests {
let res = <Array2<$t> as ArgminAdd<Array2<$t>, Array2<$t>>>::add(&a, &b);
for i in 0..3 {
for j in 0..2 {
assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand All @@ -126,7 +125,7 @@ mod tests {
let res = <Array2<$t> as ArgminAdd<$t, Array2<$t>>>::add(&a, &b);
for i in 0..3 {
for j in 0..2 {
assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand Down Expand Up @@ -172,5 +171,4 @@ mod tests {
make_test!(u64);
make_test!(f32);
make_test!(f64);

}
}
17 changes: 7 additions & 10 deletions argmin-math/ndarray-tests-src/conj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.



#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
use approx::assert_relative_eq;
use argmin_math::ArgminConj;
use ndarray::array;
use ndarray::{Array1, Array2};
use num_complex::Complex;
use ndarray::array;
use paste::item;

macro_rules! make_test {
Expand Down Expand Up @@ -57,8 +56,8 @@ mod tests {
let res = <Array2<Complex<$t>> as ArgminConj>::conj(&a);
for i in 0..2 {
for j in 0..3 {
assert!((b[(j, i)].re as f64 - res[(j, i)].re as f64).abs() < std::f64::EPSILON);
assert!((b[(j, i)].im as f64 - res[(j, i)].im as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(b[(j, i)].re as f64, res[(j, i)].re as f64, epsilon = std::f64::EPSILON);
assert_relative_eq!(b[(j, i)].im as f64, res[(j, i)].im as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand All @@ -70,7 +69,7 @@ mod tests {
let a = Array1::from(vec![1 as $t, 4 as $t, 8 as $t]);
let res = <Array1<$t> as ArgminConj>::conj(&a);
for i in 0..3 {
assert!((a[i] as f64 - res[i] as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(a[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand All @@ -85,20 +84,18 @@ mod tests {
let res = <Array2<$t> as ArgminConj>::conj(&a);
for i in 0..3 {
for j in 0..2 {
assert!((a[(j, i)] as f64 - res[(j, i)] as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(a[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON);
}
}
}
}
};
}

make_test!(isize);
make_test!(i8);
make_test!(i16);
make_test!(i32);
make_test!(i64);
make_test!(f32);
make_test!(f64);

}
}
17 changes: 7 additions & 10 deletions argmin-math/ndarray-tests-src/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.



#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
use approx::assert_relative_eq;
use argmin_math::ArgminDiv;
use ndarray::{Array1, Array2};

use ndarray::array;
use ndarray::{Array1, Array2};
use paste::item;

macro_rules! make_test {
Expand All @@ -27,7 +25,7 @@ mod tests {
let target = array![2 as $t, 8 as $t, 4 as $t];
let res = <Array1<$t> as ArgminDiv<$t, Array1<$t>>>::div(&a, &b);
for i in 0..3 {
assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand All @@ -40,7 +38,7 @@ mod tests {
let target = array![16 as $t, 8 as $t, 4 as $t];
let res = <$t as ArgminDiv<Array1<$t>, Array1<$t>>>::div(&b, &a);
for i in 0..3 {
assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand All @@ -53,7 +51,7 @@ mod tests {
let target = array![2 as $t, 3 as $t, 2 as $t];
let res = <Array1<$t> as ArgminDiv<Array1<$t>, Array1<$t>>>::div(&a, &b);
for i in 0..3 {
assert!(((target[i] - res[i]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[i] as f64, res[i] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand Down Expand Up @@ -106,7 +104,7 @@ mod tests {
let res = <Array2<$t> as ArgminDiv<Array2<$t>, Array2<$t>>>::div(&a, &b);
for i in 0..3 {
for j in 0..2 {
assert!(((target[(j, i)] - res[(j, i)]) as f64).abs() < std::f64::EPSILON);
assert_relative_eq!(target[(j, i)] as f64, res[(j, i)] as f64, epsilon = std::f64::EPSILON);
}
}
}
Expand Down Expand Up @@ -152,5 +150,4 @@ mod tests {
make_test!(u64);
make_test!(f32);
make_test!(f64);

}
}
Loading