rust-average/tests/macros.rs
Vinzent Steinberg e4345f5046 impl FromIterator<&f64> and FromIterator<&(f64, f64)>
This allows to write

    let k: Kurtosis = a.iter().collect();

instead of

    let k: Kurtosis = a.iter().map(|x| *x).collect();

but breaks type inference for code like

    let m: Min = (1..6).map(Into::into).collect();

where

    let m: Min = (1..6).map(f64::from).collect();

has to be used instead.

Fixes #8.
2018-02-28 23:44:40 +01:00

57 lines
1.1 KiB
Rust

#![cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
#[macro_use] extern crate average;
extern crate core;
use average::Estimate;
#[test]
fn concatenate_simple() {
use average::{Min, Max};
concatenate!(MinMax, [Min, min], [Max, max]);
{
let mut s = MinMax::new();
for i in 1..6 {
s.add(f64::from(i));
}
assert_eq!(s.min(), 1.0);
assert_eq!(s.max(), 5.0);
}
{
let mut s = MinMax::default();
for i in 1..6 {
s.add(f64::from(i));
}
assert_eq!(s.min(), 1.0);
assert_eq!(s.max(), 5.0);
}
{
let s: MinMax = (1..6).map(f64::from).collect();
assert_eq!(s.min(), 1.0);
assert_eq!(s.max(), 5.0);
}
}
#[test]
fn concatenate_moments() {
use average::{Variance, Quantile};
concatenate!(Estimator,
[Variance, variance, mean, sample_variance],
[Quantile, quantile, quantile]);
let e: Estimator = (1..6).map(f64::from).collect();
assert_eq!(e.mean(), 3.0);
assert_eq!(e.sample_variance(), 2.5);
assert_eq!(e.quantile(), 3.0);
}