diff --git a/src/main/java/net/objecthunter/exp4j/Expression.java b/src/main/java/net/objecthunter/exp4j/Expression.java index 45889319..e1b1aa5d 100644 --- a/src/main/java/net/objecthunter/exp4j/Expression.java +++ b/src/main/java/net/objecthunter/exp4j/Expression.java @@ -97,9 +97,9 @@ public Expression setVariables(Map variables) { return this; } - public Set getVariableTokenNames() { - Set variables = new TreeSet(); - for (Token t: tokens) { + public Set getVariableNames() { + final Set variables = new HashSet(); + for (final Token t: tokens) { if (t.getType() == Token.TOKEN_VARIABLE) variables.add(((VariableToken)t).getName()); } diff --git a/src/test/java/net/objecthunter/exp4j/ExpressionBuilderTest.java b/src/test/java/net/objecthunter/exp4j/ExpressionBuilderTest.java index 6489a37d..f841af9c 100644 --- a/src/test/java/net/objecthunter/exp4j/ExpressionBuilderTest.java +++ b/src/test/java/net/objecthunter/exp4j/ExpressionBuilderTest.java @@ -19,9 +19,9 @@ import static java.lang.Math.*; import static org.junit.Assert.*; -import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -2652,6 +2652,28 @@ public double apply(double... args) { assertEquals(expected, result, 0d); } + @Test + public void testGetVariableNames1() throws Exception{ + Expression e = new ExpressionBuilder("b*a-9.24c") + .variables("b", "a", "c") + .build(); + Set variableNames = e.getVariableNames(); + assertTrue(variableNames.contains("a")); + assertTrue(variableNames.contains("b")); + assertTrue(variableNames.contains("c")); + } + + @Test + public void testGetVariableNames2() throws Exception{ + Expression e = new ExpressionBuilder("log(bar)-FOO.s/9.24c") + .variables("bar", "FOO.s", "c") + .build(); + Set variableNames = e.getVariableNames(); + assertTrue(variableNames.contains("bar")); + assertTrue(variableNames.contains("FOO.s")); + assertTrue(variableNames.contains("c")); + } + @Test(expected = IllegalArgumentException.class) public void testSameVariableAndBuiltinFunctionName() { Expression e = new ExpressionBuilder("log10(log10)") diff --git a/src/test/java/net/objecthunter/exp4j/ExpressionTest.java b/src/test/java/net/objecthunter/exp4j/ExpressionTest.java index 323c43e0..50c5bd44 100644 --- a/src/test/java/net/objecthunter/exp4j/ExpressionTest.java +++ b/src/test/java/net/objecthunter/exp4j/ExpressionTest.java @@ -19,10 +19,7 @@ import net.objecthunter.exp4j.function.Functions; import net.objecthunter.exp4j.operator.Operators; -import net.objecthunter.exp4j.tokenizer.FunctionToken; -import net.objecthunter.exp4j.tokenizer.NumberToken; -import net.objecthunter.exp4j.tokenizer.OperatorToken; -import net.objecthunter.exp4j.tokenizer.Token; +import net.objecthunter.exp4j.tokenizer.*; import org.junit.Ignore; import org.junit.Test; @@ -53,6 +50,18 @@ public void testExpression2() throws Exception{ assertEquals(0d, exp.evaluate(), 0d); } + @Test + public void testGetVariableNames1() throws Exception{ + Token[] tokens = new Token[] { + new VariableToken("a"), + new VariableToken("b"), + new OperatorToken(Operators.getBuiltinOperator('+', 2)) + }; + Expression exp = new Expression(tokens); + + assertEquals(2, exp.getVariableNames().size()); + } + @Test @Ignore // If Expression should be threads safe this test must pass diff --git a/src/test/java/net/objecthunter/exp4j/VariablesTest.java b/src/test/java/net/objecthunter/exp4j/VariablesTest.java deleted file mode 100644 index 736d8a03..00000000 --- a/src/test/java/net/objecthunter/exp4j/VariablesTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* -* Copyright 2014 Frank Asseg -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -package net.objecthunter.exp4j; - -import net.objecthunter.exp4j.operator.Operators; -import net.objecthunter.exp4j.tokenizer.OperatorToken; -import net.objecthunter.exp4j.tokenizer.Token; -import net.objecthunter.exp4j.tokenizer.VariableToken; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - - -public class VariablesTest { - - @Test - public void testExpression1() throws Exception{ - Token[] tokens = new Token[] { - new VariableToken("a"), - new VariableToken("b"), - new OperatorToken(Operators.getBuiltinOperator('+', 2)) - }; - Expression exp = new Expression(tokens); - - assertEquals(2, exp.getVariableTokenNames().size()); - } - -}