nalgebra/nalgebra-sparse/tests/common/mod.rs

78 lines
2.4 KiB
Rust
Raw Permalink Normal View History

2020-12-09 21:42:31 +08:00
use nalgebra_sparse::csc::CscMatrix;
2021-01-26 00:26:27 +08:00
use nalgebra_sparse::csr::CsrMatrix;
use nalgebra_sparse::proptest::{csc, csr};
use proptest::strategy::Strategy;
use std::convert::TryFrom;
2021-01-04 20:39:41 +08:00
use std::fmt::Debug;
2021-01-26 00:26:27 +08:00
use std::ops::RangeInclusive;
2020-12-09 21:42:31 +08:00
2020-07-14 00:44:40 +08:00
#[macro_export]
macro_rules! assert_panics {
($e:expr) => {{
2021-01-26 00:26:27 +08:00
use std::panic::catch_unwind;
2020-07-14 00:44:40 +08:00
use std::stringify;
let expr_string = stringify!($e);
// Note: We cannot manipulate the panic hook here, because it is global and the test
// suite is run in parallel, which leads to race conditions in the sense
// that some regular tests that panic might not output anything anymore.
// Unfortunately this means that output is still printed to stdout if
// we run cargo test -- --nocapture. But Cargo does not forward this if the test
// binary is not run with nocapture, so it is somewhat acceptable nonetheless.
let result = catch_unwind(|| $e);
if result.is_ok() {
2021-01-26 00:26:27 +08:00
panic!(
"assert_panics!({}) failed: the expression did not panic.",
expr_string
);
2020-07-14 00:44:40 +08:00
}
}};
2020-12-09 21:42:31 +08:00
}
2020-12-14 23:55:06 +08:00
pub const PROPTEST_MATRIX_DIM: RangeInclusive<usize> = 0..=6;
pub const PROPTEST_MAX_NNZ: usize = 40;
2021-01-26 00:26:27 +08:00
pub const PROPTEST_I32_VALUE_STRATEGY: RangeInclusive<i32> = -5..=5;
2020-12-14 23:55:06 +08:00
2021-01-04 20:39:41 +08:00
pub fn value_strategy<T>() -> RangeInclusive<T>
where
T: TryFrom<i32>,
2021-01-26 00:26:27 +08:00
T::Error: Debug,
2021-01-04 20:39:41 +08:00
{
2021-01-26 00:26:27 +08:00
let (start, end) = (
PROPTEST_I32_VALUE_STRATEGY.start(),
PROPTEST_I32_VALUE_STRATEGY.end(),
);
T::try_from(*start).unwrap()..=T::try_from(*end).unwrap()
2021-01-04 20:39:41 +08:00
}
2021-01-26 00:26:27 +08:00
pub fn non_zero_i32_value_strategy() -> impl Strategy<Value = i32> {
let (start, end) = (
PROPTEST_I32_VALUE_STRATEGY.start(),
PROPTEST_I32_VALUE_STRATEGY.end(),
);
assert!(start < &0);
assert!(end > &0);
// Note: we don't use RangeInclusive for the second range, because then we'd have different
// types, which would require boxing
2021-01-26 00:26:27 +08:00
(*start..0).prop_union(1..*end + 1)
}
2021-01-26 00:26:27 +08:00
pub fn csr_strategy() -> impl Strategy<Value = CsrMatrix<i32>> {
csr(
PROPTEST_I32_VALUE_STRATEGY,
PROPTEST_MATRIX_DIM,
PROPTEST_MATRIX_DIM,
PROPTEST_MAX_NNZ,
)
2020-12-09 21:42:31 +08:00
}
2021-01-26 00:26:27 +08:00
pub fn csc_strategy() -> impl Strategy<Value = CscMatrix<i32>> {
csc(
PROPTEST_I32_VALUE_STRATEGY,
PROPTEST_MATRIX_DIM,
PROPTEST_MATRIX_DIM,
PROPTEST_MAX_NNZ,
)
2020-12-09 21:42:31 +08:00
}