### Question:
MappingExpr { @SuppressWarnings("PMD") static Double eval(String expr, Map<String, ? extends Number> vars) { Deque<Double> stack = new ArrayDeque<>(); String[] parts = expr.split("[,\\s]+"); for (String part : parts) { switch (part) { case ":add": binaryOp(stack, (a, b) -> a + b); break; case ":sub": binaryOp(stack, (a, b) -> a - b); break; case ":mul": binaryOp(stack, (a, b) -> a * b); break; case ":div": binaryOp(stack, (a, b) -> a / b); break; case ":if-changed": ifChanged(stack); break; default: if (part.startsWith("{") && part.endsWith("}")) { Number v = vars.get(part.substring(1, part.length() - 1)); if (v == null) v = Double.NaN; stack.addFirst(v.doubleValue()); } else { stack.addFirst(Double.parseDouble(part)); } break; } } return stack.removeFirst(); } private  MappingExpr();   }### Answer:
@Test public void evalMissing() { Map<String, Number> vars = new HashMap<>(); Double v = MappingExpr.eval("{foo}", vars); Assertions.assertTrue(v.isNaN()); }
@Test public void evalSimple() { Map<String, Number> vars = new HashMap<>(); vars.put("foo", 0.0); Double v = MappingExpr.eval("{foo}", vars); Assertions.assertEquals(0.0, v, 1e-12); }
@Test public void evalConstant() { Map<String, Number> vars = new HashMap<>(); Double v = MappingExpr.eval("42.0", vars); Assertions.assertEquals(42.0, v, 1e-12); }
@Test public void evalAdd() { Map<String, Number> vars = new HashMap<>(); vars.put("foo", 1.0); Double v = MappingExpr.eval("42.0,{foo},:add", vars); Assertions.assertEquals(43.0, v, 1e-12); }
@Test public void evalSub() { Map<String, Number> vars = new HashMap<>(); vars.put("foo", 1.0); Double v = MappingExpr.eval("42.0,{foo},:sub", vars); Assertions.assertEquals(41.0, v, 1e-12); }
@Test public void evalMul() { Map<String, Number> vars = new HashMap<>(); vars.put("foo", 2.0); Double v = MappingExpr.eval("42.0,{foo},:mul", vars); Assertions.assertEquals(84.0, v, 1e-12); }
@Test public void evalDiv() { Map<String, Number> vars = new HashMap<>(); vars.put("foo", 2.0); Double v = MappingExpr.eval("42.0,{foo},:div", vars); Assertions.assertEquals(21.0, v, 1e-12); }
@Test public void evalIfChangedYes() { Map<String, Number> vars = new HashMap<>(); vars.put("foo", 2.0); vars.put("previous:foo", 3.0); Double v = MappingExpr.eval("42.0,{foo},{previous:foo},:if-changed", vars); Assertions.assertEquals(42.0, v, 1e-12); }
@Test public void evalIfChangedNo() { Map<String, Number> vars = new HashMap<>(); vars.put("foo", 2.0); vars.put("previous:foo", 2.0); Double v = MappingExpr.eval("42.0,{foo},{previous:foo},:if-changed", vars); Assertions.assertEquals(0.0, v, 1e-12); }