use super::validation::validate_components;
use super::{Error, Normalization, Tolerance};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(usize)]
pub enum Component {
A = 0,
B = 1,
C = 2,
}
impl Component {
pub const ALL: [Self; 3] = [Self::A, Self::B, Self::C];
pub const fn index(self) -> usize {
self as usize
}
pub const fn others(self) -> [Self; 2] {
match self {
Self::A => [Self::B, Self::C],
Self::B => [Self::A, Self::C],
Self::C => [Self::A, Self::B],
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TernaryPoint {
a: f64,
b: f64,
c: f64,
}
impl TernaryPoint {
pub const fn new(a: f64, b: f64, c: f64) -> Self {
Self { a, b, c }
}
pub const fn component(self, component: Component) -> f64 {
match component {
Component::A => self.a,
Component::B => self.b,
Component::C => self.c,
}
}
pub const fn sum(self) -> f64 {
self.a + self.b + self.c
}
pub const fn as_array(self) -> [f64; 3] {
[self.a, self.b, self.c]
}
pub fn validate(
self,
normalization: Normalization,
tolerance: Tolerance,
) -> Result<Self, Error> {
let [a, b, c] = validate_components(self.as_array(), normalization, tolerance)?;
Ok(Self::new(a, b, c))
}
}
impl From<[f64; 3]> for TernaryPoint {
fn from([a, b, c]: [f64; 3]) -> Self {
Self::new(a, b, c)
}
}
impl From<TernaryPoint> for [f64; 3] {
fn from(point: TernaryPoint) -> Self {
point.as_array()
}
}
impl From<(f64, f64, f64)> for TernaryPoint {
fn from((a, b, c): (f64, f64, f64)) -> Self {
Self::new(a, b, c)
}
}
impl From<TernaryPoint> for (f64, f64, f64) {
fn from(point: TernaryPoint) -> Self {
(point.a, point.b, point.c)
}
}
#[cfg(test)]
mod tests {
use super::*;
const TOLERANCE: Tolerance = Tolerance {
absolute: 1.0e-9,
relative: 1.0e-9,
};
#[test]
fn construction_component_access_and_conversions_preserve_abc_order() {
let point = TernaryPoint::new(0.2, 0.3, 0.5);
assert_eq!(point.component(Component::A), 0.2);
assert_eq!(point.component(Component::B), 0.3);
assert_eq!(point.component(Component::C), 0.5);
assert_eq!(point.sum(), 1.0);
assert_eq!(point.as_array(), [0.2, 0.3, 0.5]);
assert_eq!(TernaryPoint::from([0.2, 0.3, 0.5]), point);
assert_eq!(<[f64; 3]>::from(point), [0.2, 0.3, 0.5]);
assert_eq!(TernaryPoint::from((0.2, 0.3, 0.5)), point);
assert_eq!(<(f64, f64, f64)>::from(point), (0.2, 0.3, 0.5));
assert_eq!(Component::B.index(), 1);
assert_eq!(Component::C.others(), [Component::A, Component::B]);
}
#[test]
fn validation_supports_all_sum_policies() {
let unit = TernaryPoint::new(0.2, 0.3, 0.5)
.validate(Normalization::RequireUnitSum, TOLERANCE)
.unwrap();
assert_eq!(unit, TernaryPoint::new(0.2, 0.3, 0.5));
assert!(matches!(
TernaryPoint::new(2.0, 3.0, 5.0).validate(Normalization::RequireUnitSum, TOLERANCE),
Err(Error::RequiredSumMismatch { .. })
));
let normalised = TernaryPoint::new(2.0, 3.0, 5.0)
.validate(Normalization::Normalize, TOLERANCE)
.unwrap();
assert!((normalised.sum() - 1.0).abs() < 1.0e-12);
assert_eq!(normalised, TernaryPoint::new(0.2, 0.3, 0.5));
let custom = TernaryPoint::new(2.0, 3.0, 5.0)
.validate(Normalization::RequireSum(10.0), TOLERANCE)
.unwrap();
assert_eq!(custom, TernaryPoint::new(2.0, 3.0, 5.0));
assert!(matches!(
TernaryPoint::new(2.0, 3.0, 5.0).validate(Normalization::RequireSum(9.0), TOLERANCE),
Err(Error::RequiredSumMismatch { .. })
));
assert!(matches!(
TernaryPoint::new(0.0, 0.0, 0.0).validate(Normalization::RequireSum(0.0), TOLERANCE),
Err(Error::InvalidRequiredSum { .. })
));
}
#[test]
fn invalid_sums_and_negative_values_are_rejected_or_cleaned_deterministically() {
for point in [
TernaryPoint::new(0.0, 0.0, 0.0),
TernaryPoint::new(1.0e-12, 0.0, 0.0),
] {
assert!(matches!(
point.validate(Normalization::Normalize, TOLERANCE),
Err(Error::InvalidSum { .. })
));
}
assert!(matches!(
TernaryPoint::new(-2.0e-9, 0.5, 0.5).validate(Normalization::RequireUnitSum, TOLERANCE),
Err(Error::NegativeComponent {
component: Component::A,
..
})
));
let cleaned = TernaryPoint::new(-5.0e-10, 0.5, 0.5)
.validate(Normalization::RequireUnitSum, TOLERANCE)
.unwrap();
assert_eq!(cleaned, TernaryPoint::new(0.0, 0.5, 0.5));
}
#[test]
fn non_finite_components_and_invalid_tolerance_are_rejected() {
for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
assert!(matches!(
TernaryPoint::new(value, 0.5, 0.5)
.validate(Normalization::RequireUnitSum, TOLERANCE),
Err(Error::NonFiniteComponent {
component: Component::A,
..
})
));
}
assert!(matches!(
TernaryPoint::new(0.2, 0.3, 0.5).validate(
Normalization::RequireUnitSum,
Tolerance {
absolute: 0.0,
relative: 1.0e-9,
},
),
Err(Error::InvalidTolerance { .. })
));
assert!(matches!(
Tolerance::new(1.0e-9, f64::NAN),
Err(Error::InvalidTolerance { .. })
));
}
}