WIP fix tests

This commit is contained in:
Dominik Werder
2023-05-03 17:34:50 +02:00
parent 479cec75e7
commit 03854395ff
28 changed files with 402 additions and 717 deletions

View File

@@ -6,6 +6,7 @@ pub mod overlap;
pub mod scalar_ops;
pub mod streamitem;
pub mod subfr;
pub mod test;
pub mod timebin;
pub mod transform;

87
items_0/src/test.rs Normal file
View File

@@ -0,0 +1,87 @@
pub fn f32_cmp_near(x: f32, y: f32, abs: f32, rel: f32) -> bool {
/*let x = {
let mut a = x.to_le_bytes();
a[0] &= 0xf0;
f32::from_ne_bytes(a)
};
let y = {
let mut a = y.to_le_bytes();
a[0] &= 0xf0;
f32::from_ne_bytes(a)
};
x == y*/
let ad = (x - y).abs();
ad <= abs || (ad / y).abs() <= rel
}
pub fn f64_cmp_near(x: f64, y: f64, abs: f64, rel: f64) -> bool {
/*let x = {
let mut a = x.to_le_bytes();
a[0] &= 0x00;
a[1] &= 0x00;
f64::from_ne_bytes(a)
};
let y = {
let mut a = y.to_le_bytes();
a[0] &= 0x00;
a[1] &= 0x00;
f64::from_ne_bytes(a)
};
x == y*/
let ad = (x - y).abs();
ad <= abs || (ad / y).abs() <= rel
}
pub fn f32_iter_cmp_near<A, B>(a: A, b: B, abs: f32, rel: f32) -> bool
where
A: IntoIterator<Item = f32>,
B: IntoIterator<Item = f32>,
{
let mut a = a.into_iter();
let mut b = b.into_iter();
loop {
let x = a.next();
let y = b.next();
if let (Some(x), Some(y)) = (x, y) {
if !f32_cmp_near(x, y, abs, rel) {
return false;
}
} else if x.is_some() || y.is_some() {
return false;
} else {
return true;
}
}
}
pub fn f64_iter_cmp_near<A, B>(a: A, b: B, abs: f64, rel: f64) -> bool
where
A: IntoIterator<Item = f64>,
B: IntoIterator<Item = f64>,
{
let mut a = a.into_iter();
let mut b = b.into_iter();
loop {
let x = a.next();
let y = b.next();
if let (Some(x), Some(y)) = (x, y) {
if !f64_cmp_near(x, y, abs, rel) {
return false;
}
} else if x.is_some() || y.is_some() {
return false;
} else {
return true;
}
}
}
#[test]
fn test_f32_iter_cmp_near() {
let a = [-127.553e17];
let b = [-127.554e17];
assert_eq!(f32_iter_cmp_near(a, b, 0.000001, 0.000001), false);
let a = [-127.55300e17];
let b = [-127.55301e17];
assert_eq!(f32_iter_cmp_near(a, b, 0.000001, 0.000001), true);
}