test: get to 100% coverage, clean up cargo features
- eliminated `f64` feature because Cargo doesn’t understand mutually exclusive features. So, we default to `f64`, and leave the `f32` feature there if people want to change to that. - integrated tarpaulin for coverage - added tests to get to 100% coverage - fixed NetworkCoordinate Div impl safety checks (now acts like Add and Sub) - update README to explain cargo feature change - cleanup build script with the new simpler features model Signed-off-by: Stephen Waits <steve@waits.net>
This commit is contained in:
1 parent
e95402ca62
commit
28097611a7
8 files changed
+79
-32
No files matched your search
+41
-8
@@ -58,7 +58,7 @@ use crate::vector::{self, Vector};
|
||||
type FloatType = f32;
|
||||
|
||||
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
||||
#[cfg(feature = "f64")]
|
||||
#[cfg(not(feature = "f32"))]
|
||||
type FloatType = f64;
|
||||
|
||||
//
|
||||
@@ -122,15 +122,16 @@ impl<const N: usize> HeightVector<N> {
|
||||
/// by the inveerse of its length.
|
||||
pub(crate) fn normalized(&self) -> Self {
|
||||
let len = self.len();
|
||||
if len < FloatType::EPSILON {
|
||||
// scale it by the inverse of its magnitude like any normal vector
|
||||
let ret = Self {
|
||||
position: self.position / len,
|
||||
height: self.height / len,
|
||||
};
|
||||
if ret.is_valid() {
|
||||
ret
|
||||
} else {
|
||||
// if we have a bad vector, generate a new random vector
|
||||
Self::new()
|
||||
} else {
|
||||
// otherwise scale it by the inverse of its magnitude like any normal vector
|
||||
Self {
|
||||
position: self.position / len,
|
||||
height: self.height / len,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -377,4 +378,36 @@ mod tests {
|
||||
let t = serde_json::to_string(&a);
|
||||
assert_eq!(t.as_ref().unwrap(), s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sub_invalid() {
|
||||
let valid = HeightVector::<2>::from(([1.0, 2.0], 3.0));
|
||||
let mut invalid = HeightVector::<2>::from(([1.0, 2.0], 3.0));
|
||||
// force invalidity here - because `::from()` actually also catches this
|
||||
invalid.height = 1.0 / 0.0;
|
||||
let result = valid - invalid;
|
||||
assert_approx_eq!(result.len(), 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_invalid() {
|
||||
let valid = HeightVector::<2>::from(([1.0, 2.0], 3.0));
|
||||
let mut invalid = HeightVector::<2>::from(([1.0 / 0.0, 2.0], 3.0));
|
||||
|
||||
// make sure `from()` caught and replaced the invalid vec
|
||||
assert_approx_eq!(invalid.len(), 1.0);
|
||||
|
||||
// force invalidity here - because `::from()` actually also catches this
|
||||
invalid.height = 1.0 / 0.0;
|
||||
let result = valid + invalid;
|
||||
assert_approx_eq!(result.len(), 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_norm() {
|
||||
let a = HeightVector::<2>::from(([0.0, 0.0], 0.0));
|
||||
let b = a.normalized();
|
||||
let len = b.len();
|
||||
assert_approx_eq!(len, 1.0);
|
||||
}
|
||||
}
|
||||
@@ -108,10 +108,6 @@
|
||||
mod height_vector;
|
||||
mod vector;
|
||||
|
||||
// make sure we don't have conflicting features
|
||||
#[cfg(all(feature = "f32", feature = "f64"))]
|
||||
compile_error!("feature \"f32\" and feature \"f64\" cannot be enabled at the same time");
|
||||
|
||||
// publish our interface
|
||||
pub mod network_coordinate;
|
||||
pub use network_coordinate::NetworkCoordinate;
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::height_vector::HeightVector;
|
||||
#[cfg(feature = "f32")]
|
||||
type FloatType = f32;
|
||||
|
||||
#[cfg(feature = "f64")]
|
||||
#[cfg(not(feature = "f32"))]
|
||||
type FloatType = f64;
|
||||
|
||||
//
|
||||
@@ -127,7 +127,7 @@ impl<const N: usize> NetworkCoordinate<N> {
|
||||
#[cfg(feature = "f32")]
|
||||
return Duration::from_secs_f32((self.heightvec - rhs.heightvec).len() / 1000.0);
|
||||
|
||||
#[cfg(feature = "f64")]
|
||||
#[cfg(not(feature = "f32"))]
|
||||
return Duration::from_secs_f64((self.heightvec - rhs.heightvec).len() / 1000.0);
|
||||
}
|
||||
|
||||
@@ -195,14 +195,17 @@ impl<const N: usize> NetworkCoordinate<N> {
|
||||
#[cfg(feature = "f32")]
|
||||
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f32() * 1000.0;
|
||||
|
||||
#[cfg(feature = "f64")]
|
||||
#[cfg(not(feature = "f32"))]
|
||||
let rtt_ms = rtt.as_secs_f64() * 1000.0;
|
||||
#[cfg(feature = "f64")]
|
||||
#[cfg(not(feature = "f32"))]
|
||||
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f64() * 1000.0;
|
||||
|
||||
// rtt needs to be positive
|
||||
if rtt_ms < 0.0 {
|
||||
return self;
|
||||
// Note: `rtt` is guaranteed to be positive because `Duration` enforces it.
|
||||
// If this panics, something changed where `Duration` now allows for negative
|
||||
// values.
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
// Sample weight balances local and remote error. (1)
|
||||
@@ -233,6 +236,10 @@ impl<const N: usize> NetworkCoordinate<N> {
|
||||
// error
|
||||
if self.heightvec.is_invalid() {
|
||||
*self = Self::new();
|
||||
|
||||
// We should never get here because the call to `normalized()` above should catch any
|
||||
// invalid `heightvec`
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
// return reference to updated self
|
||||
@@ -376,4 +383,11 @@ mod tests {
|
||||
let estimate = a.estimated_rtt(&b);
|
||||
assert_approx_eq!(estimate.as_secs_f32(), 0.080_099);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_getter() {
|
||||
let s = "{\"position\":[1.5,0.5,2.0],\"height\":25.0,\"error\":1.0}";
|
||||
let a: NetworkCoordinate<3> = serde_json::from_str(s).unwrap();
|
||||
assert_approx_eq!(a.error(), 1.0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user