fix for jscl and empty functions

This commit is contained in:
serso 2011-10-13 13:58:51 +04:00
parent a7a6073109
commit 5531565840
4 changed files with 29 additions and 3 deletions

View File

@ -139,7 +139,7 @@ public enum MathType {
@NotNull @NotNull
private final String match; private final String match;
private Result(@NotNull MathType mathType, @NotNull String match){ public Result(@NotNull MathType mathType, @NotNull String match){
this.mathType = mathType; this.mathType = mathType;
this.match = match; this.match = match;

View File

@ -42,6 +42,14 @@ class ToJsclTextProcessor implements TextProcessor {
} else if (mathType == MathType.function) { } else if (mathType == MathType.function) {
sb.append(toJsclFunction(mathTypeResult.getMatch())); sb.append(toJsclFunction(mathTypeResult.getMatch()));
i += mathTypeResult.getMatch().length() - 1; i += mathTypeResult.getMatch().length() - 1;
// NOTE: fix for jscl for EMPTY functions processing (see tests)
startsWithFinder.setI(i + 1);
if ( i < s.length() && CollectionsUtils.get(MathType.groupSymbols, startsWithFinder) != null) {
i += 2;
sb.append("(foo)");
mathTypeResult = new MathType.Result(MathType.close_group_symbol, ")");
}
} else if (mathType == MathType.constant) { } else if (mathType == MathType.constant) {
sb.append(mathTypeResult.getMatch()); sb.append(mathTypeResult.getMatch());
i += mathTypeResult.getMatch().length() - 1; i += mathTypeResult.getMatch().length() - 1;

View File

@ -48,7 +48,19 @@ public class CalculatorModelTest {
Assert.assertEquals("-2.0+2.1i", cm.evaluate(JsclOperation.numeric, "-2+2.1i")); Assert.assertEquals("-2.0+2.1i", cm.evaluate(JsclOperation.numeric, "-2+2.1i"));
Assert.assertEquals("-3.41007+3.41007i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)")); Assert.assertEquals("-3.41007+3.41007i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)"));
Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)")); Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)"));
//Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(acos(acos(acos(acos(acos(acos(acos(acos(cos(cos(cos(cos(cosh(acos(cos()))))))))))))))))))"));
try {
cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(acos(acos(acos(acos(acos(acos(acos(acos(cos(cos(cos(cos(cosh(acos(cos(cos(cos(cos(cos(acos(acos(acos(acos(acos(acos(acos(acos(cos(cos(cos(cos(cosh(acos(cos())))))))))))))))))))))))))))))))))))))");
Assert.fail();
} catch (EvalError e){
}
Assert.assertEquals("NaN", cm.evaluate(JsclOperation.numeric, "ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(100)))))))))))))))"));
try {
cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos())))))))))))))))))))))))))))))))))))");
Assert.fail();
} catch (EvalError e){
}
Assert.assertEquals("0.73909", cm.evaluate(JsclOperation.numeric, "cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(cos(1))))))))))))))))))))))))))))))))))))"));
CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d)); CalculatorModel.instance.getVarsRegister().addVar(null, new Var.Builder("si", 5d));
Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si")); Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si"));

View File

@ -9,13 +9,14 @@ package org.solovyev.android.calculator.model;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.JsclOperation;
/** /**
* User: serso * User: serso
* Date: 9/26/11 * Date: 9/26/11
* Time: 12:13 PM * Time: 12:13 PM
*/ */
public class ToJsclPreprocessorTest { public class ToJsclTextProcessorTest {
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
@ -34,6 +35,11 @@ public class ToJsclPreprocessorTest {
Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))")); Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))"));
Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))*10^2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E2")); Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))*10^2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E2"));
Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))*10^-2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E-2")); Assert.assertEquals( "2.718281828459045^3.141592653589793*sin(4.01)+3.141592653589793*cos(sqrt(5+sqrt(-1)))*10^-2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E-2"));
Assert.assertEquals( "log(foo)", preprocessor.process("ln()"));
Assert.assertEquals( "log(foo)*log(foo)", preprocessor.process("ln()ln()"));
Assert.assertEquals( "2.718281828459045*log(foo)*2.718281828459045*log(foo)*log(foo)*log(foo)*log(foo)*2.718281828459045", preprocessor.process("eln()eln()ln()ln()ln()e"));
Assert.assertEquals( "log(log(log(log(log(log(log(log(log(log(log(log(log(log(log(foo)))))))))))))))", preprocessor.process("ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln()))))))))))))))"));
} }
@Test @Test