use proptest::prelude::*;
use simd_vector_engine::kernels::scalar;
use simd_vector_engine::{engine, parallel};
fn ints() -> impl Strategy<Value = Vec<i32>> {
prop::collection::vec(any::<i32>(), 0..4096)
}
fn floats() -> impl Strategy<Value = Vec<f32>> {
prop::collection::vec(
prop_oneof![
any::<f32>(),
Just(f32::NAN),
Just(f32::INFINITY),
Just(f32::NEG_INFINITY),
Just(-0.0f32),
],
0..4096,
)
}
proptest! {
#[test]
fn count_i32_matches_scalar(v in ints(), t in any::<i32>()) {
prop_assert_eq!(engine::count_gt_i32(&v, t), scalar::count_gt_i32(&v, t));
}
#[test]
fn sum_i32_matches_scalar(v in ints()) {
prop_assert_eq!(engine::sum_i32(&v), scalar::sum_i32(&v));
}
#[test]
fn find_returns_first_index(v in ints(), needle in any::<i32>()) {
prop_assert_eq!(engine::find_i32(&v, needle), scalar::find_i32(&v, needle));
}
#[test]
fn count_f32_matches_scalar(v in floats(), t in any::<f32>()) {
prop_assert_eq!(engine::count_gt_f32(&v, t), scalar::count_gt_f32(&v, t));
}
#[test]
fn filter_matches_scalar_exactly(v in ints(), t in any::<i32>()) {
let mut got = vec![0i32; v.len()];
let mut want = vec![0i32; v.len()];
let n = engine::filter_into_i32(&v, t, &mut got);
let m = scalar::filter_into_i32(&v, t, &mut want);
prop_assert_eq!(n, m);
prop_assert_eq!(&got[..n], &want[..m]);
}
#[test]
fn parallel_agrees_with_scalar(v in ints(), t in any::<i32>()) {
prop_assert_eq!(parallel::count_gt_i32(&v, t), scalar::count_gt_i32(&v, t));
prop_assert_eq!(parallel::sum_i32(&v), scalar::sum_i32(&v));
prop_assert_eq!(parallel::find_i32(&v, t), scalar::find_i32(&v, t));
let mut got = vec![0i32; v.len()];
let mut want = vec![0i32; v.len()];
let n = parallel::filter_into_i32(&v, t, &mut got);
let m = scalar::filter_into_i32(&v, t, &mut want);
prop_assert_eq!(n, m);
prop_assert_eq!(&got[..n], &want[..m]);
}
#[test]
fn filter_never_writes_past_out(v in ints(), t in any::<i32>()) {
let mut out = vec![0i32; v.len()];
let n = engine::filter_into_i32(&v, t, &mut out);
prop_assert!(n <= v.len());
}
}