use super::Score;
#[test]
fn rejects_what_is_not_a_score() {
assert!(Score::new(f32::NAN).is_none());
assert!(Score::new(f32::INFINITY).is_none());
assert!(Score::new(-0.1).is_none());
assert!(Score::new(1.0000001).is_none());
}
#[test]
fn accepts_both_endpoints() {
assert_eq!(Score::new(0.0).unwrap().get(), 0.0);
assert_eq!(Score::new(1.0).unwrap().get(), 1.0);
}
#[test]
fn negative_zero_is_normalised() {
let neg = Score::new(-0.0).unwrap();
assert_eq!(neg, Score::ZERO);
assert_eq!(neg.get().to_bits(), 0.0f32.to_bits());
}
#[test]
fn orders_totally() {
let mut v = [
Score::new(0.7).unwrap(),
Score::ZERO,
Score::ONE,
Score::new(0.3).unwrap(),
];
v.sort();
assert_eq!(
v,
[
Score::ZERO,
Score::new(0.3).unwrap(),
Score::new(0.7).unwrap(),
Score::ONE
]
);
}
#[test]
fn equal_scores_hash_equally() {
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
let h = |s: Score| {
let mut d = DefaultHasher::new();
s.hash(&mut d);
d.finish()
};
assert_eq!(h(Score::new(-0.0).unwrap()), h(Score::ZERO));
}
#[test]
fn score_is_the_f32_confidence() {
fn same<T>(_: core::marker::PhantomData<T>, _: core::marker::PhantomData<T>) {}
same(
core::marker::PhantomData::<Score>,
core::marker::PhantomData::<super::Confidence<f32>>,
);
}
#[test]
fn rejects_what_is_not_a_confidence_at_f64() {
type C = super::Confidence<f64>;
assert!(C::new(f64::NAN).is_none());
assert!(C::new(f64::INFINITY).is_none());
assert!(C::new(-0.1).is_none());
assert!(C::new(1.0000001).is_none());
assert_eq!(C::new(0.0).unwrap().get(), 0.0);
assert_eq!(C::new(1.0).unwrap().get(), 1.0);
}
#[test]
fn negative_zero_is_normalised_at_f64() {
type C = super::Confidence<f64>;
let neg = C::new(-0.0).unwrap();
assert_eq!(neg, C::ZERO);
assert_eq!(neg.get().to_bits(), 0.0f64.to_bits());
assert_eq!(hash(neg), hash(C::ZERO));
}
#[test]
fn orders_totally_at_f64() {
type C = super::Confidence<f64>;
let mut v = [C::new(0.7).unwrap(), C::ZERO, C::ONE, C::new(0.3).unwrap()];
v.sort();
assert_eq!(
v,
[C::ZERO, C::new(0.3).unwrap(), C::new(0.7).unwrap(), C::ONE]
);
}
#[cfg(feature = "half")]
#[test]
fn refuses_and_normalises_at_f16() {
use half::f16;
type C = super::Confidence<f16>;
assert!(C::new(f16::NAN).is_none());
assert!(C::new(f16::INFINITY).is_none());
assert!(C::new(f16::from_f32(-0.1)).is_none());
assert!(C::new(f16::from_f32(1.5)).is_none());
assert_eq!(C::new(f16::ZERO).unwrap().get(), f16::ZERO);
assert_eq!(C::new(f16::ONE).unwrap().get(), f16::ONE);
let neg = C::new(f16::NEG_ZERO).unwrap();
assert_eq!(neg, C::ZERO);
assert_eq!(neg.get().to_bits(), f16::ZERO.to_bits());
assert_eq!(hash(neg), hash(C::ZERO));
}
#[cfg(feature = "rust_decimal")]
#[test]
fn equal_decimals_hash_equally() {
use core::str::FromStr;
use rust_decimal::Decimal;
type C = super::Confidence<Decimal>;
let neg_zero = C::new(Decimal::from_str("-0").unwrap()).unwrap();
assert_eq!(neg_zero, C::ZERO);
assert_eq!(hash(neg_zero), hash(C::ZERO));
let scaled = C::new(Decimal::from_str("0.50").unwrap()).unwrap();
let plain = C::new(Decimal::from_str("0.5").unwrap()).unwrap();
assert_eq!(scaled, plain);
assert_eq!(hash(scaled), hash(plain));
}
#[cfg(feature = "rust_decimal")]
#[test]
fn refuses_only_the_interval_at_decimal() {
use core::str::FromStr;
use rust_decimal::Decimal;
type C = super::Confidence<Decimal>;
assert!(C::new(Decimal::from_str("-0.1").unwrap()).is_none());
assert!(C::new(Decimal::from_str("1.0000001").unwrap()).is_none());
assert_eq!(C::new(Decimal::ZERO).unwrap().get(), Decimal::ZERO);
assert_eq!(C::new(Decimal::ONE).unwrap().get(), Decimal::ONE);
let mut v = [
C::ONE,
C::ZERO,
C::new(Decimal::from_str("0.5").unwrap()).unwrap(),
];
v.sort();
assert_eq!(
v,
[
C::ZERO,
C::new(Decimal::from_str("0.5").unwrap()).unwrap(),
C::ONE
]
);
}
fn hash<T: core::hash::Hash>(value: T) -> u64 {
use std::{collections::hash_map::DefaultHasher, hash::Hasher};
let mut state = DefaultHasher::new();
value.hash(&mut state);
state.finish()
}