Skip to main content

sva_samples/physics/
bound.rs

1// Concern: the range one physics parameter must land in, and the test that it did | Non-concern: which parameter takes which range (each model's own valid()) | IO: (value, Bound) -> bool
2
3/// Every shape the six physics models' parameters are checked against. Each implies finiteness,
4/// so a table entry is the whole test for its field.
5#[derive(Clone, Copy)]
6pub enum Bound {
7    Finite,
8    Positive,
9    NonNegative,
10    OpenUnit,
11    HalfOpenUnit,
12    AtLeast(f64),
13    Within(f64, f64),
14}
15
16pub fn holds(v: f64, bound: Bound) -> bool {
17    v.is_finite()
18        && match bound {
19            Bound::Finite => true,
20            Bound::Positive => v > 0.0,
21            Bound::NonNegative => v >= 0.0,
22            Bound::OpenUnit => v > 0.0 && v < 1.0,
23            Bound::HalfOpenUnit => (0.0..1.0).contains(&v),
24            Bound::AtLeast(lo) => v >= lo,
25            Bound::Within(lo, hi) => (lo..=hi).contains(&v),
26        }
27}
28
29pub fn all(fields: &[(f64, Bound)]) -> bool {
30    fields.iter().all(|&(v, bound)| holds(v, bound))
31}