2017-05-30 10:54:14 +02:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))]
|
|
|
|
|
2017-05-24 19:16:52 +02:00
|
|
|
extern crate average;
|
|
|
|
|
|
|
|
extern crate core;
|
2019-01-22 14:29:29 +01:00
|
|
|
#[cfg(feature = "serde1")]
|
2017-12-20 22:46:50 +01:00
|
|
|
extern crate serde_json;
|
2017-05-24 19:16:52 +02:00
|
|
|
|
|
|
|
use core::iter::Iterator;
|
|
|
|
|
2017-06-25 14:39:23 +02:00
|
|
|
use average::{Max, Estimate, Merge};
|
2017-05-24 19:16:52 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trivial() {
|
|
|
|
let mut m = Max::new();
|
|
|
|
m.add(2.);
|
|
|
|
m.add(1.);
|
|
|
|
assert_eq!(m.max(), 2.);
|
|
|
|
m.add(3.);
|
|
|
|
m.add(1.);
|
|
|
|
assert_eq!(m.max(), 3.)
|
|
|
|
}
|
|
|
|
|
2019-01-22 14:29:29 +01:00
|
|
|
#[cfg(feature = "serde1")]
|
2017-12-20 22:46:50 +01:00
|
|
|
#[test]
|
|
|
|
fn trivial_serde() {
|
|
|
|
let mut m = Max::new();
|
|
|
|
m.add(2.);
|
|
|
|
m.add(1.);
|
|
|
|
m.add(3.);
|
|
|
|
m.add(1.);
|
|
|
|
let b = serde_json::to_string(&m).unwrap();
|
|
|
|
assert_eq!(&b, "{\"x\":3.0}");
|
|
|
|
let c: Max = serde_json::from_str(&b).unwrap();
|
|
|
|
assert_eq!(c.max(), 3.)
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:16:52 +02:00
|
|
|
#[test]
|
|
|
|
fn merge() {
|
|
|
|
let sequence: &[f64] = &[1., 2., 3., 4., 5., 6., 7., 8., 9.];
|
|
|
|
for mid in 1..sequence.len() {
|
|
|
|
let (left, right) = sequence.split_at(mid);
|
2018-02-27 01:38:45 +01:00
|
|
|
let max_total: Max = sequence.iter().collect();
|
2017-05-24 19:16:52 +02:00
|
|
|
assert_eq!(max_total.max(), 9.);
|
2018-02-27 01:38:45 +01:00
|
|
|
let mut max_left: Max = left.iter().collect();
|
2017-05-24 19:16:52 +02:00
|
|
|
assert_eq!(max_left.max(), sequence[mid - 1]);
|
2018-02-27 01:38:45 +01:00
|
|
|
let max_right: Max = right.iter().collect();
|
2017-05-24 19:16:52 +02:00
|
|
|
assert_eq!(max_right.max(), 9.);
|
|
|
|
max_left.merge(&max_right);
|
|
|
|
assert_eq!(max_total.max(), max_left.max());
|
|
|
|
}
|
|
|
|
}
|