From 55315658404721dde25296e0d817cf5f6f65a19c Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 13 Oct 2011 13:58:51 +0400 Subject: [PATCH] fix for jscl and empty functions --- .../solovyev/android/calculator/math/MathType.java | 2 +- .../calculator/model/ToJsclTextProcessor.java | 8 ++++++++ .../calculator/model/CalculatorModelTest.java | 14 +++++++++++++- ...essorTest.java => ToJsclTextProcessorTest.java} | 8 +++++++- 4 files changed, 29 insertions(+), 3 deletions(-) rename src/test/java/org/solovyev/android/calculator/model/{ToJsclPreprocessorTest.java => ToJsclTextProcessorTest.java} (83%) diff --git a/src/main/java/org/solovyev/android/calculator/math/MathType.java b/src/main/java/org/solovyev/android/calculator/math/MathType.java index 32e1eaf7..a1671d31 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -139,7 +139,7 @@ public enum MathType { @NotNull private final String match; - private Result(@NotNull MathType mathType, @NotNull String match){ + public Result(@NotNull MathType mathType, @NotNull String match){ this.mathType = mathType; this.match = match; diff --git a/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java b/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java index 12b37c9f..ba18ddb2 100644 --- a/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java +++ b/src/main/java/org/solovyev/android/calculator/model/ToJsclTextProcessor.java @@ -42,6 +42,14 @@ class ToJsclTextProcessor implements TextProcessor { } else if (mathType == MathType.function) { sb.append(toJsclFunction(mathTypeResult.getMatch())); 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) { sb.append(mathTypeResult.getMatch()); i += mathTypeResult.getMatch().length() - 1; diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorModelTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorModelTest.java index 21dc24c9..2fb5266e 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorModelTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorModelTest.java @@ -48,7 +48,19 @@ public class CalculatorModelTest { 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("-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)); Assert.assertEquals("5.0", cm.evaluate(JsclOperation.numeric, "si")); diff --git a/src/test/java/org/solovyev/android/calculator/model/ToJsclPreprocessorTest.java b/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java similarity index 83% rename from src/test/java/org/solovyev/android/calculator/model/ToJsclPreprocessorTest.java rename to src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index 9be3b77f..b2bfb204 100644 --- a/src/test/java/org/solovyev/android/calculator/model/ToJsclPreprocessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -9,13 +9,14 @@ package org.solovyev.android.calculator.model; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.solovyev.android.calculator.JsclOperation; /** * User: serso * Date: 9/26/11 * Time: 12:13 PM */ -public class ToJsclPreprocessorTest { +public class ToJsclTextProcessorTest { @BeforeClass 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)))*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( "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