Basic time bin test case
This commit is contained in:
@@ -15,6 +15,7 @@ use std::any::Any;
|
||||
use std::collections::VecDeque;
|
||||
use std::{fmt, mem};
|
||||
|
||||
// TODO make members private
|
||||
#[derive(Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct BinsDim0<NTY> {
|
||||
pub ts1s: VecDeque<u64>,
|
||||
@@ -57,6 +58,15 @@ impl<NTY: ScalarOps> BinsDim0<NTY> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, ts1: u64, ts2: u64, count: u64, min: NTY, max: NTY, avg: f32) {
|
||||
self.ts1s.push_back(ts1);
|
||||
self.ts2s.push_back(ts2);
|
||||
self.counts.push_back(count);
|
||||
self.mins.push_back(min);
|
||||
self.maxs.push_back(max);
|
||||
self.avgs.push_back(avg);
|
||||
}
|
||||
|
||||
pub fn append_zero(&mut self, beg: u64, end: u64) {
|
||||
self.ts1s.push_back(beg);
|
||||
self.ts2s.push_back(end);
|
||||
@@ -74,6 +84,36 @@ impl<NTY: ScalarOps> BinsDim0<NTY> {
|
||||
self.maxs.extend(src.maxs.drain(..));
|
||||
self.avgs.extend(src.avgs.drain(..));
|
||||
}
|
||||
|
||||
pub fn equal_slack(&self, other: &Self) -> bool {
|
||||
for (&a, &b) in self.ts1s.iter().zip(other.ts1s.iter()) {
|
||||
if a != b {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (&a, &b) in self.ts2s.iter().zip(other.ts2s.iter()) {
|
||||
if a != b {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (a, b) in self.mins.iter().zip(other.mins.iter()) {
|
||||
if !a.equal_slack(b) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
for (a, b) in self.maxs.iter().zip(other.maxs.iter()) {
|
||||
if !a.equal_slack(b) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (a, b) in self.avgs.iter().zip(other.avgs.iter()) {
|
||||
if !a.equal_slack(b) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY> WithLen for BinsDim0<NTY> {
|
||||
|
||||
@@ -111,28 +111,45 @@ pub trait ScalarOps:
|
||||
fmt::Debug + Clone + PartialOrd + SubFrId + AsPrimF32 + Serialize + Unpin + Send + 'static
|
||||
{
|
||||
fn zero() -> Self;
|
||||
fn equal_slack(&self, rhs: &Self) -> bool;
|
||||
}
|
||||
|
||||
macro_rules! impl_num_ops {
|
||||
($ty:ident, $zero:expr) => {
|
||||
($ty:ident, $zero:expr, $equal_slack:ident) => {
|
||||
impl ScalarOps for $ty {
|
||||
fn zero() -> Self {
|
||||
$zero
|
||||
}
|
||||
|
||||
fn equal_slack(&self, rhs: &Self) -> bool {
|
||||
$equal_slack(*self, *rhs)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_num_ops!(u8, 0);
|
||||
impl_num_ops!(u16, 0);
|
||||
impl_num_ops!(u32, 0);
|
||||
impl_num_ops!(u64, 0);
|
||||
impl_num_ops!(i8, 0);
|
||||
impl_num_ops!(i16, 0);
|
||||
impl_num_ops!(i32, 0);
|
||||
impl_num_ops!(i64, 0);
|
||||
impl_num_ops!(f32, 0.);
|
||||
impl_num_ops!(f64, 0.);
|
||||
fn equal_int<T: PartialEq>(a: T, b: T) -> bool {
|
||||
a == b
|
||||
}
|
||||
|
||||
fn equal_f32(a: f32, b: f32) -> bool {
|
||||
(a - b).abs() < 1e-4 || (a / b > 0.999 && a / b < 1.001)
|
||||
}
|
||||
|
||||
fn equal_f64(a: f64, b: f64) -> bool {
|
||||
(a - b).abs() < 1e-6 || (a / b > 0.99999 && a / b < 1.00001)
|
||||
}
|
||||
|
||||
impl_num_ops!(u8, 0, equal_int);
|
||||
impl_num_ops!(u16, 0, equal_int);
|
||||
impl_num_ops!(u32, 0, equal_int);
|
||||
impl_num_ops!(u64, 0, equal_int);
|
||||
impl_num_ops!(i8, 0, equal_int);
|
||||
impl_num_ops!(i16, 0, equal_int);
|
||||
impl_num_ops!(i32, 0, equal_int);
|
||||
impl_num_ops!(i64, 0, equal_int);
|
||||
impl_num_ops!(f32, 0., equal_f32);
|
||||
impl_num_ops!(f64, 0., equal_f64);
|
||||
|
||||
#[allow(unused)]
|
||||
struct Ts(u64);
|
||||
|
||||
Reference in New Issue
Block a user