Skip to content

Commit 91d3b2d

Browse files
committed
Cover subtraction-formula evaluation through the engine
The new component-graph meter-minus-sibling formulas (e.g. COALESCE(frequenz-floss#8, frequenz-floss#5 - frequenz-floss#6, 0.0)) were only checked by display-string assertions; add a numeric round-trip through FormulaEngine. Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent 70e24b8 commit 91d3b2d

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

src/logical_meter/logical_meter_actor.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,42 @@ mod tests {
656656
quantity::{Frequency, Power},
657657
};
658658

659+
/// The component-graph meter-minus-sibling subtraction formulas the actor
660+
/// now receives (e.g. `COALESCE(#8, #5 - #6, 0.0)`) parse and evaluate
661+
/// through the formula engine: the component's own reading wins, else the
662+
/// parent meter minus its sibling, else zero.
663+
#[test]
664+
fn subtraction_formula_evaluates_through_engine() {
665+
let engine = FormulaEngine::<f32>::try_new("COALESCE(#8, #5 - #6, 0.0)")
666+
.expect("formula should parse");
667+
668+
// Own reading present: used directly.
669+
assert_eq!(
670+
engine
671+
.calculate(&HashMap::from([
672+
(8, Some(7.0)),
673+
(5, Some(10.0)),
674+
(6, Some(3.0))
675+
]))
676+
.unwrap(),
677+
Some(7.0),
678+
);
679+
// Own reading missing: parent meter minus its sibling (10 - 3).
680+
assert_eq!(
681+
engine
682+
.calculate(&HashMap::from([(8, None), (5, Some(10.0)), (6, Some(3.0))]))
683+
.unwrap(),
684+
Some(7.0),
685+
);
686+
// Own reading and meter missing: the difference is null, so zero wins.
687+
assert_eq!(
688+
engine
689+
.calculate(&HashMap::from([(8, None), (5, None), (6, Some(3.0))]))
690+
.unwrap(),
691+
Some(0.0),
692+
);
693+
}
694+
659695
async fn new_handle(
660696
meter: MockComponent,
661697
config: LogicalMeterConfig,

0 commit comments

Comments
 (0)