@Test public void setSeveralVariablesOk() throws Exception { String expression = "-10 + myVar * (myVar + myVar2) / -myVar3"; ExpressionSolver target = new ExpressionSolver(expression); target.setVariable("myVar", 1); target.setVariable("myVar2", 2); target.setVariable("myVar3", 3); assertEquals(-11d, target.solve(), 0d); }
@Test public void getAllRequiredVariables() throws Exception { String expression = "1 + myVar + undefinedVar"; ExpressionSolver target = new ExpressionSolver(expression); target.setVariable("myVar", 1); Set<String> actual = target.getAllRequiredVariables(); Set<String> expected = new HashSet<>(Arrays.asList("myVar", "undefinedVar")); assertThat(actual, is(equalTo(expected))); }
@Test public void setVariableOk() throws Exception { String expression = "myVar"; ExpressionSolver target = new ExpressionSolver(expression); String name = "myVar"; double value = 1.0; target.setVariable(name, value); assertEquals(1d, target.solve(), 0d); }
@Test public void areAllVariablesBound() throws Exception { String expression = "1 + myVar"; ExpressionSolver target = new ExpressionSolver(expression); target.setVariable("myVar", 1); boolean actual = target.areAllVariablesBound(); boolean expected = true; assertThat(actual, is(equalTo(expected))); }
@Test public void isVariableNotDefined() throws Exception { String expression = "1 + myVar + undefinedVar"; ExpressionSolver target = new ExpressionSolver(expression); target.setVariable("myVar", 1); boolean actual = target.isVariableDefined("undefinedVar"); boolean expected = false; assertThat(actual, is(equalTo(expected))); }
@Test public void functionInFunction() throws Exception { String expression = "1 + myFunc(myFunc((x + 9*0)))"; ExpressionSolver target = new ExpressionSolver(expression); target.setVariable("x", 2); target.addFunction("myFunc", Arrays.asList("x"), "x*x"); double actual = target.solve(); double expected = 17; assertEquals(expected, actual, 0d); }
@Test public void isSolverReady() throws Exception { String expression = "1 + myVar + myFunc()"; ExpressionSolver target = new ExpressionSolver(expression); target.setVariable("myVar", 1); target.addFunction("myFunc", Collections.<String>emptyList(), "2"); boolean actual = target.isReady(); boolean expected = true; assertThat(actual, is(equalTo(expected))); }