nalgebra/tests/core/matrixcompare.rs

90 lines
2.6 KiB
Rust
Raw Permalink Normal View History

2019-08-12 00:26:12 +08:00
//! Tests for `matrixcompare` integration.
//!
//! The `matrixcompare` crate itself is responsible for testing the actual comparison.
//! The tests here only check that the necessary trait implementations are correctly implemented,
//! in addition to some sanity checks with example input.
2020-12-19 01:19:38 +08:00
use matrixcompare::assert_matrix_eq;
2021-04-11 17:00:38 +08:00
use nalgebra::{OMatrix, U4, U5};
2020-12-19 01:19:38 +08:00
2021-03-01 00:52:14 +08:00
#[cfg(feature = "proptest-support")]
use {
crate::proptest::*,
matrixcompare::DenseAccess,
nalgebra::DMatrix,
proptest::{prop_assert_eq, proptest},
};
#[cfg(feature = "proptest-support")]
proptest! {
#[test]
fn fetch_single_is_equivalent_to_index_f64(matrix in dmatrix()) {
2019-08-12 00:26:12 +08:00
for i in 0 .. matrix.nrows() {
for j in 0 .. matrix.ncols() {
2021-03-01 00:52:14 +08:00
prop_assert_eq!(matrix.fetch_single(i, j), *matrix.index((i, j)));
2019-08-12 00:26:12 +08:00
}
}
}
2021-03-01 00:52:14 +08:00
#[test]
fn matrixcompare_shape_agrees_with_matrix(matrix in dmatrix()) {
prop_assert_eq!(matrix.nrows(), <DMatrix<f64> as matrixcompare::Matrix<f64>>::rows(&matrix));
prop_assert_eq!(matrix.ncols(), <DMatrix<f64> as matrixcompare::Matrix<f64>>::cols(&matrix));
2019-08-12 00:26:12 +08:00
}
}
#[test]
fn assert_matrix_eq_dense_positive_comparison() {
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let a = OMatrix::<_, U4, U5>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750,
3410, 3520, 3630, 3740, 3850,
4510, 4620, 4730, 4840, 4950,
]);
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let b = OMatrix::<_, U4, U5>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750,
3410, 3520, 3630, 3740, 3850,
4510, 4620, 4730, 4840, 4950,
]);
// Test matrices of static size
assert_matrix_eq!(a, b);
assert_matrix_eq!(&a, b);
assert_matrix_eq!(a, &b);
assert_matrix_eq!(&a, &b);
// Test matrices of dynamic size
let a_dyn = a.index((0..4, 0..5));
let b_dyn = b.index((0..4, 0..5));
assert_matrix_eq!(a_dyn, b_dyn);
assert_matrix_eq!(a_dyn, &b_dyn);
assert_matrix_eq!(&a_dyn, b_dyn);
assert_matrix_eq!(&a_dyn, &b_dyn);
}
#[test]
#[should_panic]
fn assert_matrix_eq_dense_negative_comparison() {
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let a = OMatrix::<_, U4, U5>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750,
3410, 3520, 3630, 3740, 3850,
4510, 4620, -4730, 4840, 4950,
]);
2020-06-30 01:03:20 +08:00
#[rustfmt::skip]
2021-04-11 17:00:38 +08:00
let b = OMatrix::<_, U4, U5>::from_row_slice(&[
2019-08-12 00:26:12 +08:00
1210, 1320, 1430, 1540, 1650,
2310, 2420, 2530, 2640, 2750,
3410, 3520, 3630, 3740, 3850,
4510, 4620, 4730, 4840, 4950,
]);
assert_matrix_eq!(a, b);
2020-06-30 01:03:20 +08:00
}