コード例 #1
0
 @Test
 public void setSeveralExpressionsOk() 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);
   expression = "myVar + myVar2";
   target.setNewExpression(expression);
   assertEquals(3d, target.solve(), 0d);
 }
コード例 #2
0
 @Test
 public void solveOk() throws Exception {
   String expression = "1+ 2 + 3 * (5 - 4 / (1 + 1 * 3)   )";
   ExpressionSolver target = new ExpressionSolver(expression);
   double actual = target.solve();
   double expected = 15;
   assertEquals(expected, actual, 0d);
 }
コード例 #3
0
 @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);
 }
コード例 #4
0
 @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);
 }
コード例 #5
0
 @Test
 public void addFunction() throws Exception {
   String expression = "myFunc(1, 2, 3)";
   ExpressionSolver target = new ExpressionSolver(expression);
   String name = "myFunc";
   List<String> variables = Arrays.asList("x", "y", "z");
   String functionExpression = "1 + x + y + z";
   target.addFunction(name, variables, functionExpression);
   assertEquals(7d, target.solve(), 0d);
 }
コード例 #6
0
 @Test
 public void functionInConstructor() throws Exception {
   Function f =
       new Function("myFunc", 1) {
         @Override
         public double apply(double... operands) {
           return operands[0];
         }
       };
   String expression = "1 + myFunc(1)";
   ExpressionSolver target = new ExpressionSolver(expression, Arrays.asList(f));
   double actual = target.solve();
   double expected = 2;
   assertEquals(expected, actual, 0d);
 }
コード例 #7
0
 @Test
 public void functionAndVarInConstructor() throws Exception {
   Function f =
       new Function("myFunc", 1) {
         @Override
         public double apply(double... operands) {
           return operands[0];
         }
       };
   String expression = "1 + myFunc(myVar)";
   Map<String, Double> vars = new HashMap<String, Double>();
   vars.put("myVar", 1d);
   ExpressionSolver target = new ExpressionSolver(expression, vars, Arrays.asList(f));
   double actual = target.solve();
   double expected = 2;
   assertEquals(expected, actual, 0d);
 }