rust-average/tests/min.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2017-05-30 10:54:14 +02:00
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))]
2017-05-24 17:29:20 +02:00
extern crate average;
extern crate core;
#[cfg(feature = "serde1")]
2017-12-20 22:46:50 +01:00
extern crate serde_json;
2017-05-24 17:29:20 +02:00
use core::iter::Iterator;
use average::{Min, Estimate, Merge};
2017-05-24 17:29:20 +02:00
#[test]
fn trivial() {
let mut m = Min::new();
m.add(1.);
m.add(2.);
assert_eq!(m.min(), 1.);
m.add(-1.);
m.add(1.);
assert_eq!(m.min(), -1.)
}
#[cfg(feature = "serde1")]
2017-12-20 22:46:50 +01:00
#[test]
fn trivial_serde() {
let mut m = Min::new();
m.add(1.);
m.add(2.);
m.add(-1.);
m.add(1.);
let b = serde_json::to_string(&m).unwrap();
assert_eq!(&b, "{\"x\":-1.0}");
let c: Min = serde_json::from_str(&b).unwrap();
assert_eq!(c.min(), -1.)
}
2017-05-24 17:29:20 +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);
let min_total: Min = sequence.iter().collect();
2017-05-24 17:29:20 +02:00
assert_eq!(min_total.min(), 1.);
let mut min_left: Min = left.iter().collect();
2017-05-24 17:29:20 +02:00
assert_eq!(min_left.min(), 1.);
let min_right: Min = right.iter().collect();
2017-05-24 17:29:20 +02:00
assert_eq!(min_right.min(), sequence[mid]);
min_left.merge(&min_right);
assert_eq!(min_total.min(), min_left.min());
}
}