use diskann_utils::views::rowmajor;
use half::f16;
use rand::{
Rng,
distr::{Distribution, StandardUniform},
};
pub(super) fn panic_message_for<F>(f: F) -> String
where
F: FnOnce() + std::panic::UnwindSafe,
{
match std::panic::catch_unwind(f) {
Ok(()) => panic!("closure did not panic when it was expected"),
Err(e) => match e.downcast::<String>() {
Ok(message) => *message,
Err(e) => std::panic::resume_unwind(e),
},
}
}
#[derive(Debug, Clone, Copy)]
pub(super) struct TestDistr;
impl TestDistr {
pub(super) fn matrix<T>(
nrows: usize,
ncols: usize,
rng: &mut impl rand::Rng,
) -> rowmajor::Owned<T>
where
Self: Distribution<T>,
{
rowmajor::Owned::from_fn(nrows, ncols, |_| (Self).sample(rng))
}
}
impl Distribution<f32> for TestDistr {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f32 {
f32::from(rng.random_range(-16i16..=16)) * 0.25
}
}
impl Distribution<i8> for TestDistr {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> i8 {
StandardUniform {}.sample(rng)
}
}
impl Distribution<f16> for TestDistr {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f16 {
f16::from_f32(<Self as Distribution<f32>>::sample(self, rng))
}
}