From 3f82a320f9d930939bf6057fd73fee992792d8ac Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Thu, 25 Apr 2019 14:49:23 +0200 Subject: [PATCH] Use proptest to test reasonable bounds of mean --- Cargo.toml | 1 + tests/proptest.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/proptest.rs diff --git a/Cargo.toml b/Cargo.toml index fb460f0..a4cf210 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,3 +43,4 @@ rand = "0.6" serde_json = "1" streaming-stats = "0.2" quantiles = "0.7" +proptest = "0.9" diff --git a/tests/proptest.rs b/tests/proptest.rs new file mode 100644 index 0000000..4a28e2c --- /dev/null +++ b/tests/proptest.rs @@ -0,0 +1,28 @@ +#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))] + +#[macro_use] extern crate average; +#[macro_use] extern crate proptest; + +extern crate core; + +use core::iter::Iterator; + +use average::Mean; +use proptest::prelude::*; +use prop::num::f64; + +proptest! { + #![proptest_config(ProptestConfig::with_cases(10000))] + #[test] + fn reasonable_bounds(s in prop::collection::vec( + f64::POSITIVE | f64::NEGATIVE | f64::SUBNORMAL | f64::ZERO, 1..100usize)) { + // See https://hypothesis.works/articles/calculating-the-mean/. + let max = s.iter().cloned().fold(0./0., f64::max); + let min = s.iter().cloned().fold(0./0., f64::min); + let a: Mean = s.iter().collect(); + let mean = a.mean(); + println!("min: {} mean: {} max: {}", min, mean, max); + assert!(min <= mean); + assert!(mean <= max); + } +}