use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{Add, Mul};
use super::Cost;
#[derive(Clone, Copy, Default)]
pub struct NonNegF64(f64);
impl NonNegF64 {
pub const INFINITY: Self = Self(f64::INFINITY);
#[must_use]
pub fn new(value: f64) -> Option<Self> {
if value.is_nan() || value < 0.0 {
return None;
}
Some(Self(value + 0.0))
}
#[must_use]
pub const fn get(self) -> f64 {
self.0
}
}
impl PartialEq for NonNegF64 {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for NonNegF64 {}
impl PartialOrd for NonNegF64 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for NonNegF64 {
fn cmp(&self, other: &Self) -> Ordering {
self.0.total_cmp(&other.0)
}
}
impl Hash for NonNegF64 {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.to_bits().hash(state);
}
}
impl Add for NonNegF64 {
type Output = Self;
fn add(self, other: Self) -> Self {
Self(self.0 + other.0)
}
}
impl Mul for NonNegF64 {
type Output = Self;
fn mul(self, other: Self) -> Self {
crate::WideMul::wide_mul(self, other)
}
}
impl Cost for NonNegF64 {
const ZERO: Self = Self(0.0);
fn saturating_add(self, other: Self) -> Self {
self + other
}
fn saturating_times(self, factor: u32) -> Self {
if factor == 0 {
return Self::ZERO;
}
Self(self.0 * f64::from(factor))
}
}
impl fmt::Display for NonNegF64 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl fmt::Debug for NonNegF64 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl From<NonNegF64> for f64 {
fn from(cost: NonNegF64) -> Self {
cost.0
}
}
#[cfg(test)]
mod tests {
use super::{Cost, NonNegF64};
use proptest::prelude::*;
#[test]
fn new_checks_invariant() {
assert_eq!(NonNegF64::new(f64::NAN), None);
assert_eq!(NonNegF64::new(-1e-300), None);
assert_eq!(NonNegF64::new(f64::NEG_INFINITY), None);
assert_eq!(NonNegF64::new(f64::INFINITY), Some(NonNegF64::INFINITY));
assert_eq!(NonNegF64::new(0.0), Some(NonNegF64::ZERO));
}
#[test]
fn negative_zero_is_normalized() {
let negative = NonNegF64::new(-0.0).unwrap();
assert!(negative.get().is_sign_positive());
assert_eq!(negative, NonNegF64::ZERO);
assert_eq!(negative.get().to_bits(), 0.0_f64.to_bits());
}
#[test]
fn infinity_times_zero_is_zero() {
assert_eq!(NonNegF64::INFINITY.saturating_times(0), NonNegF64::ZERO);
assert_eq!(NonNegF64::INFINITY.saturating_times(3), NonNegF64::INFINITY);
}
#[test]
fn overflow_reaches_infinity() {
let max = NonNegF64::new(f64::MAX).unwrap();
assert_eq!(max + max, NonNegF64::INFINITY);
assert_eq!(
NonNegF64::INFINITY + NonNegF64::INFINITY,
NonNegF64::INFINITY
);
}
fn cost() -> impl Strategy<Value = NonNegF64> {
prop_oneof![
Just(-0.0),
Just(f64::INFINITY),
Just(f64::MAX),
0.0..f64::MAX,
]
.prop_map(|value| NonNegF64::new(value).expect("generated values are valid"))
}
proptest! {
#[test]
fn operations_preserve_invariant(a in cost(), b in cost(), factor: u32) {
for value in [a + b, a.saturating_add(b), a.saturating_times(factor)] {
prop_assert!(!value.get().is_nan());
prop_assert!(value.get().is_sign_positive());
prop_assert!(value >= NonNegF64::ZERO);
}
}
#[test]
fn order_matches_numeric(a in cost(), b in cost()) {
prop_assert_eq!(a.cmp(&b), a.get().partial_cmp(&b.get()).unwrap());
prop_assert_eq!(a == b, a.get().to_bits() == b.get().to_bits());
}
}
}