Skip to content

Commit

Permalink
Fuzz tests: fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Feb 23, 2020
1 parent 162a2f1 commit 7c39f25
Show file tree
Hide file tree
Showing 27 changed files with 675 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2273,6 +2273,12 @@ public int[] expectedArgSize() {

private static IExpr factorList(IExpr expr, List<IExpr> varList, boolean factorSquareFree)
throws JASConversionException {
if (!expr.isAST()) {
if (expr.isNumber()) {
return F.List(F.List(expr, F.C1));
}
return F.List(F.List(F.C1, F.C1), F.List(expr, F.C1));
}
JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
GenPolynomial<BigRational> polyRat = jas.expr2JAS(expr, false);
Object[] objects = jas.factorTerms(polyRat);
Expand Down Expand Up @@ -3185,7 +3191,15 @@ private static class PolynomialQuotient extends PolynomialQuotientRemainder {
public IExpr evaluate(final IAST ast, EvalEngine engine) {

if (ast.size() == 4 || ast.size() == 5) {
IExpr variable = ast.arg3();
IExpr variable;
if (ast.arg3().isAST()) {
variable = ast.arg3();
} else {
variable = Validate.checkSymbolType(ast, 3, engine);
if (!variable.isPresent()) {
return F.NIL;
}
}
IExpr arg1 = F.evalExpandAll(ast.arg1(), engine);
IExpr arg2 = F.evalExpandAll(ast.arg2(), engine);

Expand Down Expand Up @@ -3290,29 +3304,43 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (temp != null) {
return temp;
}
IExpr variable = ast.arg3();
IExpr variable;
if (ast.arg3().isAST()) {
variable = ast.arg3();
} else {
variable = Validate.checkSymbolType(ast, 3, engine);
if (!variable.isPresent()) {
return F.NIL;
}
}
IExpr arg1 = F.evalExpandAll(ast.arg1(), engine);
IExpr arg2 = F.evalExpandAll(ast.arg2(), engine);

IExpr result = F.NIL;
if (ast.size() == 5) {
final OptionArgs options = new OptionArgs(ast.topHead(), ast, 4, engine);
IExpr option = options.getOption(F.Modulus);
if (option.isInteger() && !option.isZero()) {
IExpr[] quotientRemainderModInteger = quotientRemainderModInteger(arg1, arg2, variable, option);
if (quotientRemainderModInteger != null) {
result = F.List(quotientRemainderModInteger[0], quotientRemainderModInteger[1]);
try {
IExpr result = F.NIL;
if (ast.size() == 5) {
final OptionArgs options = new OptionArgs(ast.topHead(), ast, 4, engine);
IExpr option = options.getOption(F.Modulus);
if (option.isInteger() && !option.isZero()) {
IExpr[] quotientRemainderModInteger = quotientRemainderModInteger(arg1, arg2, variable, option);
if (quotientRemainderModInteger != null) {
result = F.List(quotientRemainderModInteger[0], quotientRemainderModInteger[1]);
}
}
F.REMEMBER_AST_CACHE.put(ast, result);
return result;
}
IExpr[] quotientRemainder = quotientRemainder(arg1, arg2, variable);
if (quotientRemainder != null) {
result = F.List(quotientRemainder[0], quotientRemainder[1]);
}
F.REMEMBER_AST_CACHE.put(ast, result);
return result;
} catch (RuntimeException rex) {
if (Config.SHOW_STACKTRACE) {
rex.printStackTrace();
}
return F.NIL;
}
IExpr[] quotientRemainder = quotientRemainder(arg1, arg2, variable);
if (quotientRemainder != null) {
result = F.List(quotientRemainder[0], quotientRemainder[1]);
}
F.REMEMBER_AST_CACHE.put(ast, result);
return result;
}

public int[] expectedArgSize() {
Expand Down Expand Up @@ -3376,27 +3404,41 @@ private static class PolynomialRemainder extends PolynomialQuotientRemainder {

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
IExpr variable = ast.arg3();
IExpr variable;
if (ast.arg3().isAST()) {
variable = ast.arg3();
} else {
variable = Validate.checkSymbolType(ast, 3, engine);
if (!variable.isPresent()) {
return F.NIL;
}
}
IExpr arg1 = F.evalExpandAll(ast.arg1(), engine);
IExpr arg2 = F.evalExpandAll(ast.arg2(), engine);

if (ast.size() == 5) {
final OptionArgs options = new OptionArgs(ast.topHead(), ast, 4, engine);
IExpr option = options.getOption(F.Modulus);
if (option.isInteger() && !option.isZero()) {
IExpr[] result = quotientRemainderModInteger(arg1, arg2, variable, option);
if (result == null) {
return F.NIL;
try {
if (ast.size() == 5) {
final OptionArgs options = new OptionArgs(ast.topHead(), ast, 4, engine);
IExpr option = options.getOption(F.Modulus);
if (option.isInteger() && !option.isZero()) {
IExpr[] result = quotientRemainderModInteger(arg1, arg2, variable, option);
if (result == null) {
return F.NIL;
}
return result[1];
}
return result[1];
return F.NIL;
}
IExpr[] result = quotientRemainder(arg1, arg2, variable);
if (result == null) {
return F.NIL;
}
return result[1];
} catch (RuntimeException rex) {
if (Config.SHOW_STACKTRACE) {
rex.printStackTrace();
}
return F.NIL;
}
IExpr[] result = quotientRemainder(arg1, arg2, variable);
if (result == null) {
return F.NIL;
}
return result[1];
}

public int[] expectedArgSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,47 +888,52 @@ private static class BooleanMinimize extends AbstractFunctionEvaluator {

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
FormulaFactory factory = new FormulaFactory();
LogicFormula lf = new LogicFormula(factory);
Formula formula = lf.expr2BooleanFunction(ast.arg1());
// only DNF form can be used in QuineMcCluskeyAlgorithm at the moment
formula = formula.transform(new DNFFactorization());
IExpr ex = lf.booleanFunction2Expr(formula);
IASTAppendable vars = F.Or();
if (ex.isOr()) {
IAST orAST = (IAST) ex;
IASTAppendable rest = F.Or();
for (int i = 1; i < orAST.size(); i++) {
IExpr temp = orAST.get(i);
if (temp.isAnd()) {
rest.append(temp);
try {
FormulaFactory factory = new FormulaFactory();
LogicFormula lf = new LogicFormula(factory);
Formula formula = lf.expr2BooleanFunction(ast.arg1());
// only DNF form can be used in QuineMcCluskeyAlgorithm at the moment
formula = formula.transform(new DNFFactorization());
IExpr ex = lf.booleanFunction2Expr(formula);
IASTAppendable vars = F.Or();
if (ex.isOr()) {
IAST orAST = (IAST) ex;
IASTAppendable rest = F.Or();
for (int i = 1; i < orAST.size(); i++) {
IExpr temp = orAST.get(i);
if (temp.isAnd()) {
rest.append(temp);
} else {
vars.append(temp);
}
}
if (rest.size() == 1) {
vars = F.Or();
} else {
vars.append(temp);
formula = lf.expr2BooleanFunction(rest);
}
}
if (rest.size() == 1) {
vars = F.Or();

formula = QuineMcCluskeyAlgorithm.compute(formula);
// System.out.println(formula.toString());
IExpr result = lf.booleanFunction2Expr(formula);
if (result.isOr()) {
vars.appendArgs((IAST) result);
EvalAttributes.sort(vars);
result = vars;
} else {
formula = lf.expr2BooleanFunction(rest);
vars.append(result);
EvalAttributes.sort(vars);
result = vars;
}
}
return result;
// TODO CNF form after minimizing blows up the formula.
// FormulaTransformation transformation = BooleanConvert.transformation(ast, engine);
// return lf.booleanFunction2Expr(formula.transform(transformation));
} catch (RuntimeException rex) {

formula = QuineMcCluskeyAlgorithm.compute(formula);
// System.out.println(formula.toString());
IExpr result = lf.booleanFunction2Expr(formula);
if (result.isOr()) {
vars.appendArgs((IAST) result);
EvalAttributes.sort(vars);
result = vars;
} else {
vars.append(result);
EvalAttributes.sort(vars);
result = vars;
}
return result;
// TODO CNF form after minimizing blows up the formula.
// FormulaTransformation transformation = BooleanConvert.transformation(ast, engine);
// return lf.booleanFunction2Expr(formula.transform(transformation));
return ast.arg1();
}

public int[] expectedArgSize() {
Expand Down Expand Up @@ -2461,7 +2466,7 @@ private IExpr maximum(IAST list, boolean flattenedList) {
if (!evaled) {
evaled = flattenedList;
}

if (list.size() == 1) {
return F.CNInfinity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.matheclipse.core.combinatoric.KSubsets;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.eval.exception.ArgumentTypeException;
import org.matheclipse.core.eval.exception.Validate;
import org.matheclipse.core.eval.interfaces.AbstractEvaluator;
import org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator;
Expand Down Expand Up @@ -1467,40 +1468,44 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
return F.NIL;
}
if (ast.arg1().isAST()) {
final IAST f = (IAST) ast.arg1();
int n = f.argSize();
final LevelSpecification level;
if (ast.isAST2()) {
if (ast.arg2().isInteger()) {
n = ((IInteger) ast.arg2()).toIntDefault(Integer.MIN_VALUE);
if (n > Integer.MIN_VALUE) {
level = new LevelSpecification(0, n);
try {
final IAST f = (IAST) ast.arg1();
int n = f.argSize();
final LevelSpecification level;
if (ast.isAST2()) {
if (ast.arg2().isInteger()) {
n = ((IInteger) ast.arg2()).toIntDefault(Integer.MIN_VALUE);
if (n > Integer.MIN_VALUE) {
level = new LevelSpecification(0, n);
} else {
return F.NIL;
}
} else {
return F.NIL;
level = new LevelSpecification(ast.arg2(), false);
}
} else {
level = new LevelSpecification(ast.arg2(), false);
level = new LevelSpecification(0, n);
}
} else {
level = new LevelSpecification(0, n);
}

int k;
final IASTAppendable result = F.ast(f.head());
level.setFromLevelAsCurrent();
while (level.isInRange()) {
k = level.getCurrentLevel();
final KSubsetsList iter = createKSubsets(f, k, F.ast(F.List), 1);
for (IAST part : iter) {
if (part == null) {
break;
int k;
final IASTAppendable result = F.ast(f.head());
level.setFromLevelAsCurrent();
while (level.isInRange()) {
k = level.getCurrentLevel();
final KSubsetsList iter = createKSubsets(f, k, F.ast(F.List), 1);
for (IAST part : iter) {
if (part == null) {
break;
}
result.append(part);
}
result.append(part);
level.incCurrentLevel();
}
level.incCurrentLevel();
}

return result;
return result;
} catch (ArgumentTypeException ate) {
// see LevelSpecification
}
}
return F.NIL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,11 @@ public IExpr evaluate(IAST ast, EvalEngine engine) {
return F.NIL;
}

@Override
public int[] expectedArgSize() {
return IOFunctions.ARGS_2_2;
}

@Override
public void setUp(final ISymbol newSymbol) {
newSymbol.setAttributes(ISymbol.LISTABLE | ISymbol.NUMERICFUNCTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ public int[] expectedArgSize() {
"intnm", "Non-negative machine-sized integer expected at position `2` in `1`.", //
"intpm", "Positive integer (less equal 2147483647) expected at position `2` in `1`.", //
"iterb", "Iterator does not have appropriate bounds.", //
"itform", "Argument `1` at position `2` does not have the correct form for an iterator.", //
"ivar", "`1` is not a valid variable.", //
"level", "Level specification `1` is not of the form n, {n}, or {m, n}.", //
"list", "List expected at position `1` in `2`.", //
Expand Down
Loading

0 comments on commit 7c39f25

Please sign in to comment.