1#[cfg(test)]
2macro_rules! test {
3 ($ty:ident) => {
4 mod $ty {
5 use quickcheck::TestResult;
6 use quickcheck::quickcheck;
7 use approx::relative_eq;
8
9 use crate::stats::bivariate::regression::Slope;
10 use crate::stats::bivariate::Data;
11
12 quickcheck! {
13 fn means(size: u8, start: u8,
14 offset: u8, nresamples: u8) -> TestResult {
15 let size = size as usize;
16 let start = start as usize;
17 let offset = offset as usize;
18 let nresamples = nresamples as usize;
19 if let Some(x) = crate::stats::test::vec::<$ty>(size, start) {
20 let y = crate::stats::test::vec::<$ty>(size + offset, start + offset).unwrap();
21 let data = Data::new(&x[start..], &y[start+offset..]);
22
23 let (x_means, y_means) = if nresamples > 0 {
24 data.bootstrap(nresamples, |d| (d.x().mean(), d.y().mean()))
25 } else {
26 return TestResult::discard();
27 };
28
29 let x_min = data.x().min();
30 let x_max = data.x().max();
31 let y_min = data.y().min();
32 let y_max = data.y().max();
33
34 TestResult::from_bool(
35 // Computed the correct number of resamples
36 x_means.len() == nresamples &&
37 y_means.len() == nresamples &&
38 // No uninitialized values
39 x_means.iter().all(|&x| {
40 (x > x_min || relative_eq!(x, x_min)) &&
41 (x < x_max || relative_eq!(x, x_max))
42 }) &&
43 y_means.iter().all(|&y| {
44 (y > y_min || relative_eq!(y, y_min)) &&
45 (y < y_max || relative_eq!(y, y_max))
46 })
47 )
48 } else {
49 TestResult::discard()
50 }
51 }
52 }
53
54 quickcheck! {
55 fn slope(size: u8, start: u8,
56 offset: u8, nresamples: u8) -> TestResult {
57 let size = size as usize;
58 let start = start as usize;
59 let offset = offset as usize;
60 let nresamples = nresamples as usize;
61 if let Some(x) = crate::stats::test::vec::<$ty>(size, start) {
62 let y = crate::stats::test::vec::<$ty>(size + offset, start + offset).unwrap();
63 let data = Data::new(&x[start..], &y[start+offset..]);
64
65 let slopes = if nresamples > 0 {
66 data.bootstrap(nresamples, |d| (Slope::fit(&d),)).0
67 } else {
68 return TestResult::discard();
69 };
70
71 TestResult::from_bool(
72 // Computed the correct number of resamples
73 slopes.len() == nresamples &&
74 // No uninitialized values
75 slopes.iter().all(|s| s.0 > 0.)
76 )
77 } else {
78 TestResult::discard()
79 }
80 }
81 }
82
83 }
84 };
85}
86
87#[cfg(test)]
88mod test {
89 test!(f32);
90 test!(f64);
91}
92