use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class ExprEvaluatorTests method nonBuiltinFunctionFuzz.
public static void nonBuiltinFunctionFuzz() {
Config.MAX_AST_SIZE = 10000;
Config.MAX_OUTPUT_SIZE = 10000;
Config.MAX_INPUT_LEAVES = 100L;
Config.MAX_MATRIX_DIMENSION_SIZE = 100;
Config.MAX_PRECISION_APFLOAT = 100;
Config.MAX_BIT_LENGTH = 200000;
Config.MAX_POLYNOMIAL_DEGREE = 100;
Config.FILESYSTEM_ENABLED = false;
EvalEngine engine = new EvalEngine(true);
engine.setRecursionLimit(256);
engine.setIterationLimit(1000);
ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
byte[] bArray = new byte[0];
ByteArrayExpr ba = ByteArrayExpr.newInstance(bArray);
byte[] b0Array = new byte[] { 0 };
ByteArrayExpr b0a = ByteArrayExpr.newInstance(b0Array);
F.x.setAttributes(ISymbol.PROTECTED);
F.y.setAttributes(ISymbol.PROTECTED);
IAST seedList = //
F.List(//
ba, //
b0a, //
F.complex(-0.5, 0.5), //
F.complex(0.0, 0.5), //
F.complex(0.0, -1.0), //
F.complex(0.0, 1.0), //
F.num(0.5), //
F.num(-0.5), //
F.num(Math.PI * (-0.5)), //
F.num(Math.PI * 0.5), //
F.num(-Math.PI), //
F.num(Math.PI), //
F.num(-Math.E), //
F.num(Math.E), //
F.C0, //
F.C1, //
F.CN1, //
F.CN1D2, //
F.C1D2, //
F.CNI, //
F.CI, //
F.CInfinity, //
F.CNInfinity, //
F.Null, //
F.Power(F.x, F.C2), //
F.Indeterminate, //
F.ComplexInfinity, //
F.x_, //
F.y_, //
F.CEmptyList, //
F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1))), //
F.assoc(F.List()), //
F.assoc(F.List(F.Rule(F.stringx("s1"), F.C0), F.RuleDelayed(F.stringx("s2"), F.C1))), //
SparseArrayExpr.newDenseList(F.List(F.C0, F.C0), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.C0, F.C1, F.C0, F.C2), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), F.C0), //
SparseArrayExpr.newDenseList(F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), F.C0), //
F.List(F.List(F.C0)), //
F.List(F.List(F.C1)), //
F.List(F.List(F.CN1)), //
F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), //
F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), //
F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1), F.C0), //
F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0), F.C0), //
F.List(F.CN1, F.CN2, F.C3), //
F.List(F.CN1D2, F.CN2, F.C3), //
F.List(F.x, F.CN2, F.C3), //
F.List(F.x, F.C5, F.CN3), //
F.List(F.x, F.CN3, F.CN1D2), // simulate level spec
F.List(F.CN5), // simulate level spec
F.List(F.C7), //
F.C1DSqrt5, //
F.C2Pi, //
F.CN3D2, //
F.C3D2, //
F.C3D4, //
F.Slot2, //
F.stringx(""), //
F.stringx("\\"), //
F.stringx("\r"), //
F.stringx("\t"), //
F.stringx("\n"), //
F.stringx("\r\n"), //
F.stringx("\n "), //
F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
String[] functionStrs = AST2Expr.FUNCTION_STRINGS;
ThreadLocalRandom random = ThreadLocalRandom.current();
int[] counter = new int[] { 0 };
for (int i = 0; i < functionStrs.length; i++) {
IBuiltInSymbol sym = (IBuiltInSymbol) F.symbol(functionStrs[i]);
IEvaluator evaluator = sym.getEvaluator();
if (evaluator instanceof IFunctionEvaluator) {
continue;
}
generateASTs(sym, 1, 5, seedList, random, counter, null, engine, false, false);
generateASTs(sym, 1, 5, seedList, random, counter, null, engine, false, true);
generateASTs(sym, 1, 5, seedList, random, counter, null, engine, true, false);
generateASTs(sym, 1, 5, seedList, random, counter, null, engine, true, true);
}
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class TeXTests method testSmartFuzz.
public void testSmartFuzz() {
boolean quietMode = true;
EvalEngine engine = EvalEngine.get();
List<ASTNode> node = parseFileToList();
IExpr temp;
TeXFormFactory fTeXFactory = new TeXFormFactory();
OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
fInputFactory.setInputForm(true);
AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
IAST seedList = //
F.List(//
F.complex(-0.5, 0.5), //
F.complex(0.0, 0.5), //
F.complex(0.0, -1.0), //
F.complex(0.0, 1.0), //
F.num(-0.5), //
F.num(0.5), //
F.num(Math.PI * (-0.5)), //
F.num(Math.PI * 0.5), //
F.num(-Math.PI), //
F.num(Math.PI), //
F.num(-Math.E), //
F.num(Math.E), //
S.True, //
S.False, //
F.assoc(F.CEmptyList), //
F.assoc(F.List(F.Rule(F.x, F.y))), //
F.CEmptyList, //
F.List(F.Rule(F.C1, F.C0)), //
F.List(F.Rule(F.x, F.CN1)), //
F.C0, //
F.C1, //
F.CN1, //
F.C2, //
F.CN2, //
F.CN10, //
F.CN1D2, //
F.C1D2, //
F.CNI, //
F.CI, //
F.CInfinity, //
F.CNInfinity, //
F.Null, //
F.Power(F.x, F.C2), //
F.ComplexInfinity, //
F.x_, //
F.y_, //
F.C1DSqrt5, //
F.Slot1, //
F.stringx(""), //
F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
int counter = 0;
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int j = 1; j < 10000; j++) {
int i = 0;
while (i < node.size()) {
temp = ast2Expr.convert(node.get(i++));
if (temp.isAST() && temp.size() > 1) {
final StringBuilder buf = new StringBuilder();
int seedIndex = random.nextInt(1, seedList.size());
IExpr seed = seedList.get(seedIndex);
IASTMutable mutant = ((IAST) temp).copy();
int randomIndex = random.nextInt(1, mutant.size());
mutant.set(randomIndex, seed);
for (int k = 0; k < 1; k++) {
seedIndex = random.nextInt(1, seedList.size());
seed = seedList.get(seedIndex);
randomIndex = random.nextInt(1, mutant.size());
mutant.set(randomIndex, seed);
}
engine.init();
engine.setQuietMode(quietMode);
engine.setRecursionLimit(256);
engine.setIterationLimit(1000);
final String mutantStr = fInputFactory.toString(mutant);
try {
// System.out.print(".");
if (counter++ > 80) {
// System.out.println("");
counter = 0;
System.out.flush();
System.err.flush();
}
// eval.eval(mutantStr);
fTeXFactory.convert(buf, mutant, 0);
System.out.println(buf.toString());
} catch (FlowControlException mex) {
if (!quietMode) {
System.err.println(mutantStr);
mex.printStackTrace();
System.err.println();
}
} catch (SyntaxError se) {
if (!quietMode) {
System.err.println(mutantStr);
se.printStackTrace();
System.err.println();
}
// fail();
} catch (MathException mex) {
System.err.println(mutantStr);
mex.printStackTrace();
System.err.println();
fail();
} catch (RuntimeException rex) {
System.err.println(mutantStr);
rex.printStackTrace();
fail();
} catch (Error rex) {
System.err.println(mutantStr);
if (rex instanceof StackOverflowError) {
System.err.println("java.lang.StackOverflowError");
rex.printStackTrace();
} else {
rex.printStackTrace();
fail();
}
}
}
}
}
// return result;
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class AbstractRubiTestCase method interpreter.
/**
* Evaluates the given string-expression and returns the result in <code>OutputForm</code>
*/
public String interpreter(final String inputExpression, final String expectedResult, String manuallyCheckedResult) {
IExpr result;
final StringWriter buf = new StringWriter();
IExpr integral = fEvaluator.parse(inputExpression).first();
try {
if (fSeconds <= 0) {
result = fEvaluator.eval(inputExpression);
} else {
EvalEngine engine = fEvaluator.getEvalEngine();
engine.setSeconds(fSeconds);
result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
}
if (result != null) {
return printResult(integral, result, expectedResult, manuallyCheckedResult);
}
} catch (final AbortException re) {
return printResult(integral, F.$Aborted, expectedResult, manuallyCheckedResult);
} catch (final FailedException re) {
return printResult(integral, F.$Failed, expectedResult, manuallyCheckedResult);
} catch (final SyntaxError se) {
String msg = se.getMessage();
System.err.println(msg);
System.err.println();
System.err.flush();
return "";
} catch (final RuntimeException re) {
Throwable me = re.getCause();
if (me instanceof MathException) {
Validate.printException(buf, me);
} else {
Validate.printException(buf, re);
}
System.err.println(buf.toString());
System.err.flush();
return "";
} catch (final Exception e) {
Validate.printException(buf, e);
System.err.println(buf.toString());
System.err.flush();
return "";
} catch (final OutOfMemoryError e) {
Validate.printException(buf, e);
System.err.println(buf.toString());
System.err.flush();
return "";
} catch (final StackOverflowError e) {
Validate.printException(buf, e);
System.err.println(buf.toString());
System.err.flush();
return "";
}
return buf.toString();
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class AbstractRubiTestCase method interpreter.
/**
* Evaluates the given string-expression and returns the result in <code>OutputForm</code>
*/
public String interpreter(final String inputExpression, final String expectedResult, String manuallyCheckedResult) {
IExpr result;
final StringWriter buf = new StringWriter();
try {
if (fSeconds <= 0) {
result = fEvaluator.eval(inputExpression);
} else {
EvalEngine engine = fEvaluator.getEvalEngine();
engine.setSeconds(fSeconds);
result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
}
if (result != null) {
return printResult(result, expectedResult, manuallyCheckedResult);
}
} catch (final AbortException re) {
return printResult(F.$Aborted, expectedResult, manuallyCheckedResult);
} catch (final FailedException re) {
return printResult(F.$Failed, expectedResult, manuallyCheckedResult);
} catch (final SyntaxError se) {
String msg = se.getMessage();
System.err.println(msg);
System.err.println();
System.err.flush();
return "";
} catch (final RuntimeException re) {
Throwable me = re.getCause();
if (me instanceof MathException) {
Validate.printException(buf, me);
} else {
Validate.printException(buf, re);
}
System.err.println(buf.toString());
System.err.flush();
return "";
} catch (final Exception e) {
Validate.printException(buf, e);
System.err.println(buf.toString());
System.err.flush();
return "";
} catch (final OutOfMemoryError e) {
Validate.printException(buf, e);
System.err.println(buf.toString());
System.err.flush();
return "";
} catch (final StackOverflowError e) {
Validate.printException(buf, e);
System.err.println(buf.toString());
System.err.flush();
return "";
}
return buf.toString();
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class Blank method isConditionMatched.
@Override
public boolean isConditionMatched(final IExpr expr, PatternMap patternMap) {
if (fCondition == null || expr.head().equals(fCondition)) {
patternMap.setValue(this, expr);
return true;
}
EvalEngine engine = EvalEngine.get();
boolean traceMode = false;
try {
traceMode = engine.isTraceMode();
engine.setTraceMode(false);
final Predicate<IExpr> matcher = Predicates.isTrue(engine, fCondition);
if (matcher.test(expr)) {
patternMap.setValue(this, expr);
return true;
}
} finally {
if (traceMode) {
engine.setTraceMode(true);
}
}
return false;
}
Aggregations