📄 jeptest.java
字号:
package com.singularsys.jeptests;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Stack;
import com.singularsys.jep.*;
import com.singularsys.jep.configurableparser.ConfigurableParser;
import com.singularsys.jep.functions.LazyLogical;
import com.singularsys.jep.functions.PostfixMathCommand;
import com.singularsys.jep.functions.StrictNaturalLogarithm;
import com.singularsys.jep.parser.ASTConstant;
import com.singularsys.jep.parser.ASTFunNode;
import com.singularsys.jep.parser.ASTOpNode;
import com.singularsys.jep.parser.ASTVarNode;
import com.singularsys.jep.parser.Node;
import com.singularsys.jep.standard.Complex;
import junit.framework.Assert;
import junit.framework.TestCase;
public class JepTest extends TestCase {
public static final boolean PRINT_RESULTS = true;
/** The parser */
protected Jep jep;
public JepTest(String name) {
super(name);
}
/**
* Sets up the parser.
*/
public void setUp() {
// Set up the parser
jep = new Jep();
jep.setImplicitMul(true);
//jep.addStandardFunctions();
jep.addStandardConstants();
//jep.addComplex();
//jep.setTraverse(false);
}
/*------------------- utility functions -------------------------------*/
/**
* Prints a header with the name of the test as specified in str.
*/
private void printTestHeader(String str) {
System.out.println("\n\n------------------------------------------------------------------------");
System.out.println("Running \""+str+"\"\n");
}
/**
* Test result j.evaluate(j.parse(expr))
* @param expr the expression to parse and evaluate
* @param expected result expected
* @throws Exception
*/
protected void valueTest(String expr,Object expected) throws Exception
{
Node node = jep.parse(expr);
Object res = jep.evaluate(node);
myAssertEquals(expr,expected,res);
}
/**
* Calculate the value of an expression.
* @param node
* @throws Exception
*/
protected Object calcValue(Node node) throws Exception {
Object res = jep.evaluate(node);
return res;
}
/**
* Calculate the value of an expression.
* @param expr
* @throws Exception
*/
protected Object calcValue(String expr) throws Exception {
Node node = jep.parse(expr);
Object res = calcValue(node);
return res;
}
protected void myAssertEquals(String msg, Object expected, Object actual) {
if(PRINT_RESULTS && !actual.equals(expected))
System.out.println("Error: '"+msg+"' is '"+actual+"' should be '"+expected+"'");
assertEquals("<"+msg+">",expected,actual);
if(PRINT_RESULTS)
System.out.println("Success: Value of \""+msg+"\" is "+actual+"");
}
public void myAssertNaN(String msg,Object actual)
{
if(actual instanceof Double) {
if(Double.isNaN( ((Double) actual).doubleValue()) ) {
System.out.println("Success: Value of \""+msg+"\" is "+actual+"");
}
else {
System.out.println("Error: \""+msg+"\" is '"+actual+"' should be NaN");
fail("<"+msg+"> is "+actual+" should be NaN");
}
}
else {
System.out.println("Error: '"+msg+"' is '"+actual+"' should be 'NaN'");
fail("<"+msg+">");
}
}
/**
* Test whether an expression evaluates to NaN
* @param expr
* @throws Exception
*/
public void valueTestNaN(String expr) throws Exception
{
Object res = calcValue(expr);
myAssertNaN(expr,res);
}
public void valueTestString(String expr,String expected) throws Exception
{
Object res = calcValue(expr);
myAssertEquals(expr,expected,res.toString());
}
/** Test parse-evaluate with complex number and given tolerence.
*
* @param expr
* @param expected
* @param tol
* @throws Exception
*/
public void complexValueTest(String expr,Complex expected,double tol) throws Exception
{
Object res = calcValue(expr);
if(expected.equals((Complex) res,tol))
System.out.println("Success value of \""+expr+"\" is "+res);
else {
System.out.println("Error value of \""+expr+"\" is "+res+" should be "+expected);
fail("<"+expr+"> expected: <"+expected+"> but was <"+res+">");
}
}
/**
* Test values to within a given precision
*
* @param expr
* expression
* @param a
* the expected value
* @param tol
* tolerance
* @throws Exception
*/
public void valueTest(String expr, double a, double tol) throws Exception {
Object res = calcValue(expr);
if (res instanceof Double) {
double val = ((Double) res).doubleValue();
if (Math.abs(val - a) < tol) {
System.out.println("Success value of \"" + expr + "\" is "
+ res);
} else {
System.out.println("Error value of \"" + expr + "\" is " + res
+ " should be " + a);
assertEquals(expr, a, val, tol);
}
} else {
System.out.println("Error value of \"" + expr + "\" is " + res
+ " should be " + a);
fail("<" + expr + "> expected: <" + a + "> but was <" + res + ">");
}
}
static public void nodeTest(Node n, Operator op) {
assertTrue("Node " + n.toString() + "should have been an ASTOpNode",
n instanceof ASTOpNode);
assertEquals(op, ((ASTOpNode) n).getOperator());
}
static public void nodeTest(Node n, String name) {
assertTrue("Node " + n.toString() + "should have been an ASTFunNode",
n instanceof ASTFunNode);
assertEquals(name, ((ASTFunNode) n).getName());
}
static public void nodeTest(Node n, Variable v) {
assertTrue("Node " + n.toString() + "should have been an ASTVarNode",
n instanceof ASTVarNode);
assertEquals(v, ((ASTVarNode) n).getVar());
}
static public void nodeTest(Node n, Object v) {
assertTrue("Node " + n.toString() + "should have been an ASTConstant node",
n instanceof ASTConstant);
assertEquals(v, ((ASTConstant) n).getValue());
}
/*------------------------- tests ---------------------------------------*/
/**
* Tests the evaluate() method.
*/
public void testEvaluate() throws Exception {
// test a very basic expression
Object result = jep.evaluate(jep.parse("2.1345"));
Assert.assertTrue(result instanceof Double);
Assert.assertTrue(result.equals(2.1345));
// Test Complex numbers
result = jep.evaluate(jep.parse("i"));
Assert.assertTrue(result instanceof Complex);
Assert.assertTrue(result.equals(new Complex(0,1)));
// Test whether a String is passed through
result = jep.evaluate(jep.parse("\"asdf\""));
Assert.assertTrue(result instanceof String);
Assert.assertEquals("asdf",result);
}
/**
* Test changing variables value after parsing
*/
public void testChangeVariable() {
Object result;
Complex c;
// add the complex variable x = 0,0
jep.addVariable("x", new Complex(0, 0));
// parse a simple expression with the variable x
try {
jep.parse("x");
} catch (ParseException e) {
Assert.fail("Exception occured");
}
// jep.getVarValue("x");
try {
result = jep.evaluate();
} catch (EvaluationException e) {
Assert.fail("Exception occured"+e.getMessage());
return;
}
Assert.assertTrue(result instanceof Complex);
c = (Complex)result;
Assert.assertTrue(c.re() == 0);
Assert.assertTrue(c.im() == 0);
// change the value of x
jep.addVariable("x", new Complex(1, 1));
// jep.getVarValue("x");
try {
result = jep.evaluate();
} catch (EvaluationException e) {
Assert.fail("Exception occured");
return;
}
Assert.assertTrue(result instanceof Complex);
c = (Complex)result;
Assert.assertTrue(c.re() == 1);
Assert.assertTrue(c.im() == 1);
}
/**
* Tests whether allowUndeclared is working properly.
*
*/
public void testSetAllowUndeclared() {
// test whether setAllowUndeclared(true) works
jep.getVariableTable().clear(); // clear the Variable Table
jep.setAllowUndeclared(true);
try {
jep.parse("x");
} catch (ParseException e) {
Assert.fail("Exception occured "+e.getMessage());
}
VariableTable st = jep.getVariableTable();
// should only contain a single variable x
Assert.assertTrue(st.size()==1);
Assert.assertTrue(st.getVariable("x") != null);
// test whether setAllowUndeclared(false) works
jep.getVariableTable().clear();
jep.addVariable("x", new Double(1));
jep.setAllowUndeclared(false);
try {
jep.parse("p");
// since p is not declared, an error should occur
Assert.fail("A ParseException should have been thrown creating variable 'p'.");
} catch (ParseException e) {
// exception was thrown, so all is well
}
}
public void testSimpleSum() throws Exception
{
valueTest("1+2",3.0);
valueTest("2*6+3",15.0);
valueTest("2*(6+3)",18.0);
}
public void testNumbers() throws Exception
{
valueTest("0",0.0);
valueTest("0.",0.0);
valueTest(".0",0.0);
valueTest("0.0",0.0);
valueTest("-0",-0.0);
valueTest("-0.0",-0.0);
valueTest("1",1.0);
valueTest("1.",1.0);
valueTest("1.0",1.0);
valueTest("-1",-1.0);
valueTest("-1.",-1.0);
valueTest("-1.0",-1.0);
valueTest("1.5",1.5);
valueTest(".5",0.5);
valueTest("0.5",0.5);
valueTest("-1.5",-1.5);
valueTest("-.5",-0.5);
valueTest("-0.5",-0.5);
valueTest("5e2",500.0);
valueTest("5E2",500.0);
valueTest("5e-2",0.05);
valueTest("5e-2",0.05);
valueTest("-5e2",-500.0);
valueTest("-5E2",-500.0);
valueTest("-5e-2",-0.05);
valueTest("-5e-2",-0.05);
valueTest(".5e3",500.0);
valueTest(".5E3",500.0);
valueTest(".5e-1",0.05);
valueTest(".5e-1",0.05);
valueTest("-.5e3",-500.0);
valueTest("-.5E3",-500.0);
valueTest("-.5e-1",-0.05);
valueTest("-.5e-1",-0.05);
valueTest("0.5e3",500.0);
valueTest("0.5E3",500.0);
valueTest("0.5e-1",0.05);
valueTest(".5e-1",0.05);
valueTest("-.5e3",-500.0);
valueTest("-.5E3",-500.0);
valueTest("-.5e-1",-0.05);
valueTest("-.5e-1",-0.05);
valueTest("1.5e3",1500.0);
valueTest("1.5E3",1500.0);
valueTest("1.5e-1",0.15);
valueTest("1.5e-1",0.15);
valueTest("-1.5e3",-1500.0);
valueTest("-1.5E3",-1500.0);
valueTest("-1.5e-1",-0.15);
valueTest("-1.5e-1",-0.15);
}
public void testStrings() throws Exception
{
valueTest("\"\"","");
valueTest("\"a\"","a");
valueTest("\"abcdefghijklmnopqrstuvwxyz\"","abcdefghijklmnopqrstuvwxyz");
valueTest("sum(\"a\",\"b\")","ab");
valueTest("\"a\\\"b\"","a\"b");
}
public void testLogical() throws Exception
{
System.out.println("\nTesting logical operations");
valueTest("T=1",1.0);
valueTest("F=0",0.0);
valueTest("!T",myFalse);
valueTest("!F",myTrue);
valueTest("!5",myFalse);
valueTest("-0==0",myTrue);
valueTest("!-5",myFalse);
//valueTest("-!5==0",myTrue);
//valueTest("-!0",-1.0);
valueTest("T&&T",myTrue);
valueTest("T&&F",myFalse);
valueTest("F&&T",myFalse);
valueTest("F&&F",myFalse);
valueTest("T||T",myTrue);
valueTest("T||F",myTrue);
valueTest("F||T",myTrue);
valueTest("F||F",myFalse);
calcValue("a=F"); calcValue("b=F"); calcValue("c=F");
valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",myTrue);
valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",myTrue);
calcValue("a=F"); calcValue("b=F"); calcValue("c=T");
valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",myTrue);
valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",myTrue);
calcValue("a=F"); calcValue("b=T"); calcValue("c=F");
valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",myTrue);
valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",myTrue);
calcValue("a=F"); calcValue("b=T"); calcValue("c=T");
valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",myTrue);
valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",myTrue);
calcValue("a=T"); calcValue("b=F"); calcValue("c=F");
valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",myTrue);
valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",myTrue);
calcValue("a=T"); calcValue("b=F"); calcValue("c=T");
valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",myTrue);
valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",myTrue);
calcValue("a=T"); calcValue("b=T"); calcValue("c=F");
valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",myTrue);
valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",myTrue);
calcValue("a=T"); calcValue("b=T"); calcValue("c=T");
valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",myTrue);
valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",myTrue);
jep.addVariable("true",myTrue);
jep.addVariable("false",myFalse);
valueTest("true",myTrue);
valueTest("false",myFalse);
valueTest("!true",myFalse);
valueTest("!false",myTrue);
valueTest("true==true",myTrue);
valueTest("false==false",myTrue);
valueTest("true==false",myFalse);
valueTest("true==true&&false==false",myTrue);
valueTest("if(true==true&&false==false,6,7)",6.0);
valueTest("if(false&&true,6,7)",7.0);
valueTest("if(true&&false==false,6,7)",6.0);
valueTest("if((true&&true)==true,6,7)",6.0);
valueTest("if((!false)==true,6,7)",6.0);
}
protected Object myTrue = Boolean.TRUE;
protected Object myFalse = Boolean.FALSE;
public void testNaN() throws Exception
{
printTestHeader("Testing for NaN");
jep.addVariable("x",new Double(Double.NaN));
System.out.println("x=NaN");
valueTestNaN("ln(x)");
valueTestNaN("log(x)");
valueTestNaN("sin(x)");
valueTestNaN("x+x");
valueTest("x!=x",myTrue);
valueTest("x==x",myFalse);
jep.addVariable("y",new Double(Double.NaN));
Node n = jep.parse("x+5");
System.out.println(calcValue(n));
Node n2 = jep.parse("y");
System.out.println(calcValue(n2));
valueTest("x == x+5",myFalse);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -