From 213d6fd270fb682f9dc1b2df5cbcc13a465b3d42 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Sun, 7 Oct 2012 21:15:42 +0400 Subject: [PATCH] registries --- calculatorpp-core/pom.xml | 7 + .../calculator/AbstractCalculatorTest.java | 18 +- .../calculator/CalculatorTestUtils.java | 33 ++ .../FromJsclSimplifyTextProcessorTest.java | 12 +- .../FromJsclNumericTextProcessorTest.java | 11 +- .../android/calculator/math/MathTypeTest.java | 3 +- .../model/AndroidCalculatorEngineTest.java | 448 ++++++++++++++++++ .../calculator/model/NumeralBaseTest.java | 12 +- .../model/ToJsclTextProcessorTest.java | 11 +- .../android/calculator/model/nb_table.csv | 0 calculatorpp/pom.xml | 7 + .../calculator/CalculatorTestUtils.java | 33 ++ .../{plot => }/CalculatorppTestRunner.java | 2 +- .../calculator/TextHighlighterTest.java | 8 +- .../model/AndroidCalculatorEngineTest.java | 428 ----------------- pom.xml | 20 +- 16 files changed, 571 insertions(+), 482 deletions(-) create mode 100644 calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java rename {calculatorpp/src/test/java/org/solovyev/android/calculator/model => calculatorpp-core/src/test/java/org/solovyev/android/calculator}/FromJsclSimplifyTextProcessorTest.java (88%) rename {calculatorpp => calculatorpp-core}/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java (83%) create mode 100644 calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java rename {calculatorpp => calculatorpp-core}/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java (91%) rename {calculatorpp => calculatorpp-core}/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java (95%) rename {calculatorpp => calculatorpp-core}/src/test/resources/org/solovyev/android/calculator/model/nb_table.csv (100%) create mode 100644 calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java rename calculatorpp/src/test/java/org/solovyev/android/calculator/{plot => }/CalculatorppTestRunner.java (90%) delete mode 100644 calculatorpp/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java diff --git a/calculatorpp-core/pom.xml b/calculatorpp-core/pom.xml index ad384cd1..97e64d4f 100644 --- a/calculatorpp-core/pom.xml +++ b/calculatorpp-core/pom.xml @@ -30,6 +30,13 @@ test + + net.sf.opencsv + opencsv + 2.0 + test + + org.mockito mockito-core diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java index c4b67ff3..6fe7698e 100644 --- a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/AbstractCalculatorTest.java @@ -1,7 +1,5 @@ package org.solovyev.android.calculator; -import jscl.JsclMathEngine; -import org.jetbrains.annotations.NotNull; import org.mockito.Mockito; import org.solovyev.android.calculator.history.CalculatorHistory; @@ -12,21 +10,9 @@ import org.solovyev.android.calculator.history.CalculatorHistory; */ public class AbstractCalculatorTest { - protected static void staticSetUp() throws Exception { - CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class)); - } - protected void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class)); + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), CalculatorTestUtils.newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class)); + CalculatorLocatorImpl.getInstance().getEngine().init(); } - @NotNull - private static CalculatorEngineImpl newCalculatorEngine() { - final CalculatorMathRegistry varsRegistry = Mockito.mock(CalculatorMathRegistry.class); - //Mockito.when(varsRegistry.get()) - - final CalculatorEngineImpl result = new CalculatorEngineImpl(JsclMathEngine.getInstance(), varsRegistry, Mockito.mock(CalculatorMathRegistry.class), Mockito.mock(CalculatorMathRegistry.class), Mockito.mock(CalculatorMathRegistry.class), null); - result.init(); - return result; - } } diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java new file mode 100644 index 00000000..3f538770 --- /dev/null +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -0,0 +1,33 @@ +package org.solovyev.android.calculator; + +import jscl.JsclMathEngine; +import org.jetbrains.annotations.NotNull; +import org.mockito.Mockito; +import org.solovyev.android.calculator.history.CalculatorHistory; + +/** + * User: serso + * Date: 10/7/12 + * Time: 8:40 PM + */ +public class CalculatorTestUtils { + + public static void staticSetUp() throws Exception { + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class)); + CalculatorLocatorImpl.getInstance().getEngine().init(); + } + + @NotNull + static CalculatorEngineImpl newCalculatorEngine() { + final MathEntityDao mathEntityDao = Mockito.mock(MathEntityDao.class); + + final JsclMathEngine jsclEngine = JsclMathEngine.getInstance(); + + final CalculatorVarsRegistry varsRegistry = new CalculatorVarsRegistry(jsclEngine.getConstantsRegistry(), mathEntityDao); + final CalculatorFunctionsMathRegistry functionsRegistry = new CalculatorFunctionsMathRegistry(jsclEngine.getFunctionsRegistry(), mathEntityDao); + final CalculatorOperatorsMathRegistry operatorsRegistry = new CalculatorOperatorsMathRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao); + final CalculatorPostfixFunctionsRegistry postfixFunctionsRegistry = new CalculatorPostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao); + + return new CalculatorEngineImpl(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry, null); + } +} diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java similarity index 88% rename from calculatorpp/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java rename to calculatorpp-core/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java index a9b21894..9d0e3d27 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/FromJsclSimplifyTextProcessorTest.java @@ -1,9 +1,9 @@ -package org.solovyev.android.calculator.model; +package org.solovyev.android.calculator; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.model.Var; import org.solovyev.android.calculator.text.FromJsclSimplifyTextProcessor; import java.text.DecimalFormatSymbols; @@ -13,11 +13,11 @@ import java.text.DecimalFormatSymbols; * Date: 10/20/11 * Time: 3:43 PM */ -public class FromJsclSimplifyTextProcessorTest { +public class FromJsclSimplifyTextProcessorTest extends AbstractCalculatorTest { @BeforeClass - public static void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().getEngine().init(); + public static void staticSetUp() throws Exception { + CalculatorTestUtils.staticSetUp(); } @Test @@ -28,7 +28,7 @@ public class FromJsclSimplifyTextProcessorTest { //Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))")); DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(); decimalGroupSymbols.setGroupingSeparator(' '); - ((AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine()).setDecimalGroupSymbols(decimalGroupSymbols); + CalculatorLocatorImpl.getInstance().getEngine().setDecimalGroupSymbols(decimalGroupSymbols); //Assert.assertEquals("123 456 789e", tp.process("123456789*2.718281828459045")); //Assert.assertEquals("123 456 789e", tp.process("123 456 789 * 2.718281828459045")); //Assert.assertEquals("t11e", tp.process("t11*2.718281828459045")); diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java similarity index 83% rename from calculatorpp/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java rename to calculatorpp-core/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java index 33fde252..3ba2a771 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java @@ -13,18 +13,19 @@ import jscl.math.Generic; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.AbstractCalculatorTest; +import org.solovyev.android.calculator.CalculatorTestUtils; /** * User: serso * Date: 10/18/11 * Time: 10:42 PM */ -public class FromJsclNumericTextProcessorTest { +public class FromJsclNumericTextProcessorTest extends AbstractCalculatorTest { - @BeforeClass - public static void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().getEngine().init(); + @BeforeClass + public static void staticSetUp() throws Exception { + CalculatorTestUtils.staticSetUp(); } @Test diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java index 2d678522..af6e8e86 100644 --- a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/math/MathTypeTest.java @@ -10,6 +10,7 @@ import junit.framework.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.solovyev.android.calculator.AbstractCalculatorTest; +import org.solovyev.android.calculator.CalculatorTestUtils; /** * User: serso @@ -20,7 +21,7 @@ public class MathTypeTest extends AbstractCalculatorTest { @BeforeClass public static void staticSetUp() throws Exception { - AbstractCalculatorTest.staticSetUp(); + CalculatorTestUtils.staticSetUp(); } @Test diff --git a/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java new file mode 100644 index 00000000..edffffd9 --- /dev/null +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java @@ -0,0 +1,448 @@ +/* + * Copyright (c) 2009-2011. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + */ + +package org.solovyev.android.calculator.model; + +import jscl.AngleUnit; +import jscl.JsclMathEngine; +import jscl.MathEngine; +import jscl.NumeralBase; +import jscl.math.Expression; +import jscl.math.Generic; +import jscl.math.function.Constant; +import jscl.math.function.CustomFunction; +import jscl.text.ParseException; +import junit.framework.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.solovyev.android.calculator.CalculatorEvalException; +import org.solovyev.android.calculator.CalculatorLocatorImpl; +import org.solovyev.android.calculator.CalculatorTestUtils; + +import java.text.DecimalFormatSymbols; +import java.util.Locale; + +import static junit.framework.Assert.fail; + +/** + * User: serso + * Date: 9/17/11 + * Time: 9:47 PM + */ + +@SuppressWarnings("deprecation") +public class AndroidCalculatorEngineTest { + + @BeforeClass + public static void setUp() throws Exception { + CalculatorTestUtils.staticSetUp(); + CalculatorLocatorImpl.getInstance().getEngine().setPrecision(3); + } + + + @Test + public void testDegrees() throws Exception { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + final AngleUnit defaultAngleUnit = cm.getAngleUnits(); + try { + cm.setAngleUnits(AngleUnit.rad); + cm.setPrecision(3); + try { + Assert.assertEquals("0.017", cm.evaluate("°")); + fail(); + } catch (ParseException e) { + + } + + Assert.assertEquals("0.017", cm.evaluate( "1°")); + Assert.assertEquals("0.349", cm.evaluate( "20.0°")); + Assert.assertEquals("0.5", cm.evaluate( "sin(30°)")); + Assert.assertEquals("0.524", cm.evaluate( "asin(sin(30°))")); + Assert.assertEquals("∂(cos(t), t, t, 1°)", cm.evaluate( "∂(cos(t),t,t,1°)")); + + Assert.assertEquals("∂(cos(t), t, t, 1°)", cm.simplify("∂(cos(t),t,t,1°)")); + } finally { + cm.setAngleUnits(defaultAngleUnit); + } + } + +/* @Test + public void testLongExecution() throws Exception { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + try { + cm.evaluate( "3^10^10^10"); + fail(); + } catch (ParseException e) { + if (e.getMessageCode().equals(Messages.msg_3)) { + + } else { + System.out.print(e.getCause().getMessage()); + fail(); + } + } + + try { + cm.evaluate("9999999!"); + fail(); + } catch (ParseException e) { + if (e.getMessageCode().equals(Messages.msg_3)) { + + } else { + System.out.print(e.getCause().getMessage()); + fail(); + } + } + + final long start = System.currentTimeMillis(); + try { + cm.evaluate( "3^10^10^10"); + fail(); + } catch (ParseException e) { + if (e.getMessage().startsWith("Too long calculation")) { + final long end = System.currentTimeMillis(); + Assert.assertTrue(end - start < 1000); + } else { + fail(); + } + } + + }*/ + + @Test + public void testEvaluate() throws Exception { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + Assert.assertEquals("cos(t)+10%", cm.simplify( "cos(t)+10%")); + + final Generic expression = cm.simplifyGeneric("cos(t)+10%"); + expression.substitute(new Constant("t"), Expression.valueOf(100d)); + + Assert.assertEquals("it", cm.simplify( "it")); + Assert.assertEquals("10%", cm.simplify( "10%")); + Assert.assertEquals("0", cm.evaluate( "eq(0, 1)")); + Assert.assertEquals("1", cm.evaluate( "eq(1, 1)")); + Assert.assertEquals("1", cm.evaluate( "eq( 1, 1)")); + Assert.assertEquals("1", cm.simplify( "eq( 1, 1)")); + Assert.assertEquals("1", cm.evaluate( "lg(10)")); + Assert.assertEquals("4", cm.evaluate( "2+2")); + final AngleUnit defaultAngleUnit = cm.getAngleUnits(); + try { + cm.setAngleUnits(AngleUnit.rad); + Assert.assertEquals("-0.757", cm.evaluate( "sin(4)")); + Assert.assertEquals("0.524", cm.evaluate( "asin(0.5)")); + Assert.assertEquals("-0.396", cm.evaluate( "sin(4)asin(0.5)")); + Assert.assertEquals("-0.56", cm.evaluate( "sin(4)asin(0.5)√(2)")); + Assert.assertEquals("-0.56", cm.evaluate( "sin(4)asin(0.5)√(2)")); + } finally { + cm.setAngleUnits(defaultAngleUnit); + } + Assert.assertEquals("7.389", cm.evaluate( "e^2")); + Assert.assertEquals("7.389", cm.evaluate( "exp(1)^2")); + Assert.assertEquals("7.389", cm.evaluate( "exp(2)")); + Assert.assertEquals("2+i", cm.evaluate( "2*1+√(-1)")); + try { + cm.setAngleUnits(AngleUnit.rad); + Assert.assertEquals("0.921+Πi", cm.evaluate( "ln(5cosh(38π√(2cos(2))))")); + Assert.assertEquals("-3.41+3.41i", cm.evaluate( "(5tan(2i)+2i)/(1-i)")); + } finally { + cm.setAngleUnits(defaultAngleUnit); + } + Assert.assertEquals("7.389i", cm.evaluate( "iexp(2)")); + Assert.assertEquals("2+7.389i", cm.evaluate( "2+iexp(2)")); + Assert.assertEquals("2+7.389i", cm.evaluate( "2+√(-1)exp(2)")); + Assert.assertEquals("2-2.5i", cm.evaluate( "2-2.5i")); + Assert.assertEquals("-2-2.5i", cm.evaluate( "-2-2.5i")); + Assert.assertEquals("-2+2.5i", cm.evaluate( "-2+2.5i")); + Assert.assertEquals("-2+2.1i", cm.evaluate( "-2+2.1i")); + Assert.assertEquals("-0.1-0.2i", cm.evaluate( "(1-i)/(2+6i)")); + + junit.framework.Assert.assertEquals("24", cm.evaluate( "4!")); + junit.framework.Assert.assertEquals("24", cm.evaluate( "(2+2)!")); + junit.framework.Assert.assertEquals("120", cm.evaluate( "(2+2+1)!")); + junit.framework.Assert.assertEquals("24", cm.evaluate( "(2.0+2.0)!")); + junit.framework.Assert.assertEquals("24", cm.evaluate( "4.0!")); + junit.framework.Assert.assertEquals("720", cm.evaluate( "(3!)!")); + junit.framework.Assert.assertEquals("36", Expression.valueOf("3!^2").numeric().toString()); + junit.framework.Assert.assertEquals("3", Expression.valueOf("cubic(27)").numeric().toString()); + try { + junit.framework.Assert.assertEquals("√(-1)!", cm.evaluate( "i!")); + fail(); + } catch (ParseException e) { + } + + junit.framework.Assert.assertEquals("1", cm.evaluate( "(π/π)!")); + + try { + junit.framework.Assert.assertEquals("i", cm.evaluate( "(-1)i!")); + fail(); + } catch (ParseException e) { + + } + junit.framework.Assert.assertEquals("24i", cm.evaluate( "4!i")); + + CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("si", 5d)); + + try { + cm.setAngleUnits(AngleUnit.rad); + Assert.assertEquals("0.451", cm.evaluate( "acos(0.8999999999999811)")); + Assert.assertEquals("-0.959", cm.evaluate( "sin(5)")); + Assert.assertEquals("-4.795", cm.evaluate( "sin(5)si")); + Assert.assertEquals("-23.973", cm.evaluate( "sisin(5)si")); + Assert.assertEquals("-23.973", cm.evaluate( "si*sin(5)si")); + Assert.assertEquals("-3.309", cm.evaluate( "sisin(5si)si")); + } finally { + cm.setAngleUnits(defaultAngleUnit); + } + + CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("s", 1d)); + Assert.assertEquals("5", cm.evaluate( "si")); + + CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("k", 3.5d)); + CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("k1", 4d)); + Assert.assertEquals("4", cm.evaluate( "k11")); + + CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", (String) null)); + Assert.assertEquals("11t", cm.evaluate( "t11")); + Assert.assertEquals("11et", cm.evaluate( "t11e")); + Assert.assertEquals("∞", cm.evaluate( "∞")); + Assert.assertEquals("∞", cm.evaluate( "Infinity")); + Assert.assertEquals("11∞t", cm.evaluate( "t11∞")); + Assert.assertEquals("-t+t^3", cm.evaluate( "t(t-1)(t+1)")); + + Assert.assertEquals("100", cm.evaluate( "0.1E3")); + Assert.assertEquals("3.957", cm.evaluate( "ln(8)lg(8)+ln(8)")); + + Assert.assertEquals("0.933", cm.evaluate( "0x:E/0x:F")); + + try { + cm.setNumeralBase(NumeralBase.hex); + Assert.assertEquals("E/F", cm.evaluate( "0x:E/0x:F")); + Assert.assertEquals("E/F", cm.simplify( "0x:E/0x:F")); + Assert.assertEquals("E/F", cm.evaluate( "E/F")); + Assert.assertEquals("E/F", cm.simplify( "E/F")); + } finally { + cm.setNumeralBase(NumeralBase.dec); + } + + Assert.assertEquals("0", cm.evaluate( "((((((0))))))")); + Assert.assertEquals("0", cm.evaluate( "((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((0))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))")); + + + /* Assert.assertEquals("0.524", cm.evaluate( "30°").getResult()); + Assert.assertEquals("0.524", cm.evaluate( "(10+20)°").getResult()); + Assert.assertEquals("1.047", cm.evaluate( "(10+20)°*2").getResult()); + try { + Assert.assertEquals("0.278", cm.evaluate( "30°^2").getResult()); + junit.framework.Assert.fail(); + } catch (ParseException e) { + if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) { + junit.framework.Assert.fail(); + } + }*//* + +*//* try { + cm.setTimeout(5000); + Assert.assertEquals("2", cm.evaluate( "2!").getResult()); + } finally { + cm.setTimeout(3000); + }*/ + + CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", (String) null)); + Assert.assertEquals("2t", cm.simplify( "∂(t^2,t)")); + Assert.assertEquals("2t", cm.evaluate( "∂(t^2,t)")); + CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", "2")); + Assert.assertEquals("2t", cm.simplify( "∂(t^2,t)")); + Assert.assertEquals("4", cm.evaluate( "∂(t^2,t)")); + + Assert.assertEquals("-x+x*ln(x)", cm.simplify("∫(ln(x), x)")); + Assert.assertEquals("-(x-x*ln(x))/(ln(2)+ln(5))", cm.simplify("∫(log(10, x), x)")); + + Assert.assertEquals("∫((ln(2)+ln(5))/ln(x), x)", cm.simplify("∫(ln(10)/ln(x), x)")); + Assert.assertEquals("∫(ln(10)/ln(x), x)", Expression.valueOf("∫(log(x, 10), x)").expand().toString()); + Assert.assertEquals("∫((ln(2)+ln(5))/ln(x), x)", cm.simplify("∫(log(x, 10), x)")); + } + + @Test + public void testFormatting() throws Exception { + final MathEngine ce = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + Assert.assertEquals("12 345", ce.simplify( "12345")); + + } + + @Test + public void testI() throws ParseException, CalculatorEvalException { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + Assert.assertEquals("-i", cm.evaluate( "i^3")); + for (int i = 0; i < 1000; i++) { + double real = (Math.random()-0.5) * 1000; + double imag = (Math.random()-0.5) * 1000; + int exp = (int)(Math.random() * 10); + + final StringBuilder sb = new StringBuilder(); + sb.append(real); + if ( imag > 0 ) { + sb.append("+"); + } + sb.append(imag); + sb.append("^").append(exp); + try { + cm.evaluate( sb.toString()); + } catch (Throwable e) { + fail(sb.toString()); + } + } + } + + @Test + public void testEmptyFunction() throws Exception { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + try { + cm.evaluate( "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 (ParseException e) { + } + Assert.assertEquals("0.34+1.382i", cm.evaluate( "ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(100)))))))))))))))")); + try { + cm.evaluate( "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 (ParseException e) { + } + + final AngleUnit defaultAngleUnit = cm.getAngleUnits(); + try { + cm.setAngleUnits(AngleUnit.rad); + Assert.assertEquals("0.739", cm.evaluate( "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))))))))))))))))))))))))))))))))))))")); + } finally { + cm.setAngleUnits(defaultAngleUnit); + } + + CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("si", 5d)); + Assert.assertEquals("5", cm.evaluate( "si")); + + try { + cm.evaluate( "sin"); + Assert.fail(); + } catch (ParseException e) { + } + } + + @Test + public void testRounding() throws Exception { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + try { + DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); + decimalGroupSymbols.setDecimalSeparator('.'); + decimalGroupSymbols.setGroupingSeparator('\''); + cm.setDecimalGroupSymbols(decimalGroupSymbols); + cm.setPrecision(2); + Assert.assertEquals("12'345'678.9", cm.evaluate( "1.23456789E7")); + cm.setPrecision(10); + Assert.assertEquals("12'345'678.9", cm.evaluate( "1.23456789E7")); + Assert.assertEquals("123'456'789", cm.evaluate( "1.234567890E8")); + Assert.assertEquals("1'234'567'890.1", cm.evaluate( "1.2345678901E9")); + } finally { + cm.setPrecision(3); + DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); + decimalGroupSymbols.setDecimalSeparator('.'); + decimalGroupSymbols.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT.charAt(0)); + cm.setDecimalGroupSymbols(decimalGroupSymbols); + } + } + + @Test + public void testComparisonFunction() throws Exception { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + Assert.assertEquals("0", cm.evaluate( "eq(0, 1)")); + Assert.assertEquals("1", cm.evaluate( "eq(1, 1)")); + Assert.assertEquals("1", cm.evaluate( "eq(1, 1.0)")); + Assert.assertEquals("0", cm.evaluate( "eq(1, 1.000000000000001)")); + Assert.assertEquals("0", cm.evaluate( "eq(1, 0)")); + + Assert.assertEquals("1", cm.evaluate( "lt(0, 1)")); + Assert.assertEquals("0", cm.evaluate( "lt(1, 1)")); + Assert.assertEquals("0", cm.evaluate( "lt(1, 0)")); + + Assert.assertEquals("0", cm.evaluate( "gt(0, 1)")); + Assert.assertEquals("0", cm.evaluate( "gt(1, 1)")); + Assert.assertEquals("1", cm.evaluate( "gt(1, 0)")); + + Assert.assertEquals("1", cm.evaluate( "ne(0, 1)")); + Assert.assertEquals("0", cm.evaluate( "ne(1, 1)")); + Assert.assertEquals("1", cm.evaluate( "ne(1, 0)")); + + Assert.assertEquals("1", cm.evaluate( "le(0, 1)")); + Assert.assertEquals("1", cm.evaluate( "le(1, 1)")); + Assert.assertEquals("0", cm.evaluate( "le(1, 0)")); + + Assert.assertEquals("0", cm.evaluate( "ge(0, 1)")); + Assert.assertEquals("1", cm.evaluate( "ge(1, 1)")); + Assert.assertEquals("1", cm.evaluate( "ge(1, 0)")); + + Assert.assertEquals("0", cm.evaluate( "ap(0, 1)")); + Assert.assertEquals("1", cm.evaluate( "ap(1, 1)")); + //Assert.assertEquals("1", cm.evaluate( "ap(1, 1.000000000000001)").getResult()); + Assert.assertEquals("0", cm.evaluate( "ap(1, 0)")); + + } + + + @Test + public void testNumeralSystems() throws Exception { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + Assert.assertEquals("11 259 375", cm.evaluate( "0x:ABCDEF")); + Assert.assertEquals("30 606 154.462", cm.evaluate( "0x:ABCDEF*e")); + Assert.assertEquals("30 606 154.462", cm.evaluate( "e*0x:ABCDEF")); + Assert.assertEquals("e", cm.evaluate( "e*0x:ABCDEF/0x:ABCDEF")); + Assert.assertEquals("30 606 154.462", cm.evaluate( "0x:ABCDEF*e*0x:ABCDEF/0x:ABCDEF")); + Assert.assertEquals("30 606 154.462", cm.evaluate( "c+0x:ABCDEF*e*0x:ABCDEF/0x:ABCDEF-c+0x:C-0x:C")); + Assert.assertEquals("1 446 257 064 651.832", cm.evaluate( "28*28 * sin(28) - 0b:1101 + √(28) + exp ( 28) ")); + Assert.assertEquals("13", cm.evaluate( "0b:1101")); + + try { + cm.evaluate( "0b:π"); + Assert.fail(); + } catch (ParseException e) { + // ok + } + + final NumeralBase defaultNumeralBase = cm.getNumeralBase(); + try{ + cm.setNumeralBase(NumeralBase.bin); + Assert.assertEquals("101", cm.evaluate( "10+11")); + Assert.assertEquals("10/11", cm.evaluate( "10/11")); + + cm.setNumeralBase(NumeralBase.hex); + Assert.assertEquals("63 7B", cm.evaluate( "56CE+CAD")); + Assert.assertEquals("E", cm.evaluate( "E")); + } finally { + cm.setNumeralBase(defaultNumeralBase); + } + } + + @Test + public void testLog() throws Exception { + final MathEngine cm = CalculatorLocatorImpl.getInstance().getEngine().getMathEngine0(); + + Assert.assertEquals("∞", Expression.valueOf("1/0").numeric().toString()); + Assert.assertEquals("∞", Expression.valueOf("ln(10)/ln(1)").numeric().toString()); + + // logarithm + Assert.assertEquals("ln(x)/ln(base)", ((CustomFunction) cm.getFunctionsRegistry().get("log")).getContent()); + Assert.assertEquals("∞", cm.evaluate( "log(1, 10)")); + Assert.assertEquals("3.322", cm.evaluate( "log(2, 10)")); + Assert.assertEquals("1.431", cm.evaluate( "log(5, 10)")); + Assert.assertEquals("0.96", cm.evaluate( "log(11, 10)")); + Assert.assertEquals("1/(bln(a))", cm.simplify( "∂(log(a, b), b)")); + Assert.assertEquals("-ln(b)/(aln(a)^2)", cm.simplify( "∂(log(a, b), a)")); + + } +} diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java similarity index 91% rename from calculatorpp/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java rename to calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java index 57f2600a..3ee2b8b7 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/NumeralBaseTest.java @@ -10,9 +10,7 @@ import org.jetbrains.annotations.NotNull; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.solovyev.android.calculator.CalculatorEvalException; -import org.solovyev.android.calculator.CalculatorLocatorImpl; -import org.solovyev.android.calculator.CalculatorParseException; +import org.solovyev.android.calculator.*; import org.solovyev.common.Converter; import java.io.InputStreamReader; @@ -24,12 +22,12 @@ import java.util.List; * Date: 12/14/11 * Time: 4:16 PM */ -public class NumeralBaseTest { +public class NumeralBaseTest extends AbstractCalculatorTest { @BeforeClass - public static void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().getEngine().init(); - ((AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine()).setPrecision(3); + public static void staticSetUp() throws Exception { + CalculatorTestUtils.staticSetUp(); + CalculatorLocatorImpl.getInstance().getEngine().setPrecision(3); } @Test diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java similarity index 95% rename from calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java rename to calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index 0d8a8b17..ecf0db42 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java +++ b/calculatorpp-core/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -11,10 +11,7 @@ import jscl.NumeralBase; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.solovyev.android.calculator.CalculatorLocatorImpl; -import org.solovyev.android.calculator.CalculatorParseException; -import org.solovyev.android.calculator.PreparedExpression; -import org.solovyev.android.calculator.ToJsclTextProcessor; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.text.TextProcessor; /** @@ -22,11 +19,11 @@ import org.solovyev.android.calculator.text.TextProcessor; * Date: 9/26/11 * Time: 12:13 PM */ -public class ToJsclTextProcessorTest { +public class ToJsclTextProcessorTest extends AbstractCalculatorTest { @BeforeClass - public static void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().getEngine().init(); + public static void staticSetUp() throws Exception { + CalculatorTestUtils.staticSetUp(); } @Test diff --git a/calculatorpp/src/test/resources/org/solovyev/android/calculator/model/nb_table.csv b/calculatorpp-core/src/test/resources/org/solovyev/android/calculator/model/nb_table.csv similarity index 100% rename from calculatorpp/src/test/resources/org/solovyev/android/calculator/model/nb_table.csv rename to calculatorpp-core/src/test/resources/org/solovyev/android/calculator/model/nb_table.csv diff --git a/calculatorpp/pom.xml b/calculatorpp/pom.xml index 69e4a62e..408c8e4a 100644 --- a/calculatorpp/pom.xml +++ b/calculatorpp/pom.xml @@ -115,6 +115,13 @@ test + + org.mockito + mockito-core + 1.9.0 + test + + org.simpleframework simple-xml diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java new file mode 100644 index 00000000..31fd2bd0 --- /dev/null +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorTestUtils.java @@ -0,0 +1,33 @@ +package org.solovyev.android.calculator; + +import jscl.JsclMathEngine; +import org.jetbrains.annotations.NotNull; +import org.mockito.Mockito; +import org.solovyev.android.calculator.history.CalculatorHistory; + +/** + * User: serso + * Date: 10/7/12 + * Time: 8:56 PM + */ +public class CalculatorTestUtils { + + public static void staticSetUp() throws Exception { + CalculatorLocatorImpl.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class)); + CalculatorLocatorImpl.getInstance().getEngine().init(); + } + + @NotNull + static CalculatorEngineImpl newCalculatorEngine() { + final MathEntityDao mathEntityDao = Mockito.mock(MathEntityDao.class); + + final JsclMathEngine jsclEngine = JsclMathEngine.getInstance(); + + final CalculatorVarsRegistry varsRegistry = new CalculatorVarsRegistry(jsclEngine.getConstantsRegistry(), mathEntityDao); + final CalculatorFunctionsMathRegistry functionsRegistry = new CalculatorFunctionsMathRegistry(jsclEngine.getFunctionsRegistry(), mathEntityDao); + final CalculatorOperatorsMathRegistry operatorsRegistry = new CalculatorOperatorsMathRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao); + final CalculatorPostfixFunctionsRegistry postfixFunctionsRegistry = new CalculatorPostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao); + + return new CalculatorEngineImpl(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry, null); + } +} diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/CalculatorppTestRunner.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorppTestRunner.java similarity index 90% rename from calculatorpp/src/test/java/org/solovyev/android/calculator/plot/CalculatorppTestRunner.java rename to calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorppTestRunner.java index c5155e17..d6258b1e 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/plot/CalculatorppTestRunner.java +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/CalculatorppTestRunner.java @@ -1,4 +1,4 @@ -package org.solovyev.android.calculator.plot; +package org.solovyev.android.calculator; import com.xtremelabs.robolectric.RobolectricTestRunner; import org.jetbrains.annotations.NotNull; diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java index b2e10d35..a381faca 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java +++ b/calculatorpp/src/test/java/org/solovyev/android/calculator/TextHighlighterTest.java @@ -9,6 +9,7 @@ package org.solovyev.android.calculator; import jscl.MathEngine; import jscl.NumeralBase; import junit.framework.Assert; +import org.junit.Before; import org.junit.Test; import org.solovyev.android.calculator.text.TextProcessor; import org.solovyev.android.calculator.view.TextHighlighter; @@ -23,7 +24,12 @@ import java.util.Random; */ public class TextHighlighterTest { - @Test + @Before + public void setUp() throws Exception { + CalculatorTestUtils.staticSetUp(); + } + + @Test public void testProcess() throws Exception { TextProcessor textHighlighter = new TextHighlighter(0, false); diff --git a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java b/calculatorpp/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java deleted file mode 100644 index cf98f495..00000000 --- a/calculatorpp/src/test/java/org/solovyev/android/calculator/model/AndroidCalculatorEngineTest.java +++ /dev/null @@ -1,428 +0,0 @@ -/* - * Copyright (c) 2009-2011. Created by serso aka se.solovyev. - * For more information, please, contact se.solovyev@gmail.com - */ - -package org.solovyev.android.calculator.model; - -import org.junit.BeforeClass; -import org.solovyev.android.calculator.CalculatorLocatorImpl; - -/** - * User: serso - * Date: 9/17/11 - * Time: 9:47 PM - */ - -public class AndroidCalculatorEngineTest { - - @BeforeClass - public static void setUp() throws Exception { - CalculatorLocatorImpl.getInstance().getEngine().init(); - ((AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine()).setPrecision(3); - } -/* - @Test - public void testDegrees() throws Exception { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - final AngleUnit defaultAngleUnit = cm.getEngine().getAngleUnits(); - try { - cm.getEngine().setAngleUnits(AngleUnit.rad); - cm.setPrecision(3); - try { - Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "°")); - fail(); - } catch (CalculatorParseException e) { - - } - - Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "1°").getStringResult()); - Assert.assertEquals("0.349", cm.evaluate(JsclOperation.numeric, "20.0°").getStringResult()); - Assert.assertEquals("0.5", cm.evaluate(JsclOperation.numeric, "sin(30°)").getStringResult()); - Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(sin(30°))").getStringResult()); - Assert.assertEquals("∂(cos(t), t, t, 1°)", cm.evaluate(JsclOperation.numeric, "∂(cos(t),t,t,1°)").getStringResult()); - - Assert.assertEquals("∂(cos(t), t, t, 1°)", cm.evaluate(JsclOperation.simplify, "∂(cos(t),t,t,1°)").getStringResult()); - } finally { - cm.getEngine().setAngleUnits(defaultAngleUnit); - } - } - - @Test - public void testLongExecution() throws Exception { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - try { - cm.evaluate(JsclOperation.numeric, "3^10^10^10"); - Assert.fail(); - } catch (CalculatorParseException e) { - if (e.getMessageCode().equals(Messages.msg_3)) { - - } else { - System.out.print(e.getCause().getMessage()); - Assert.fail(); - } - } - - try { - cm.evaluate(JsclOperation.numeric, "9999999!"); - Assert.fail(); - } catch (CalculatorParseException e) { - if (e.getMessageCode().equals(Messages.msg_3)) { - - } else { - System.out.print(e.getCause().getMessage()); - Assert.fail(); - } - } - - *//*final long start = System.currentTimeMillis(); - try { - cm.evaluate(JsclOperation.numeric, "3^10^10^10"); - Assert.fail(); - } catch (ParseException e) { - if (e.getMessage().startsWith("Too long calculation")) { - final long end = System.currentTimeMillis(); - Assert.assertTrue(end - start < 1000); - } else { - Assert.fail(); - } - }*//* - - } - - @Test - public void testEvaluate() throws Exception { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - Assert.assertEquals("cos(t)+10%", cm.evaluate(JsclOperation.simplify, "cos(t)+10%").getStringResult()); - - final Generic expression = cm.getEngine().simplifyGeneric("cos(t)+10%"); - expression.substitute(new Constant("t"), Expression.valueOf(100d)); - - Assert.assertEquals("it", cm.evaluate(JsclOperation.simplify, "it").getStringResult()); - Assert.assertEquals("10%", cm.evaluate(JsclOperation.simplify, "10%").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(0, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq(1, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq( 1, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.simplify, "eq( 1, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "lg(10)").getStringResult()); - Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "2+2").getStringResult()); - final AngleUnit defaultAngleUnit = cm.getEngine().getAngleUnits(); - try { - cm.getEngine().setAngleUnits(AngleUnit.rad); - Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)").getStringResult()); - Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(0.5)").getStringResult()); - Assert.assertEquals("-0.396", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)").getStringResult()); - Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getStringResult()); - Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getStringResult()); - } finally { - cm.getEngine().setAngleUnits(defaultAngleUnit); - } - Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "e^2").getStringResult()); - Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(1)^2").getStringResult()); - Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(2)").getStringResult()); - Assert.assertEquals("2+i", cm.evaluate(JsclOperation.numeric, "2*1+√(-1)").getStringResult()); - try { - cm.getEngine().setAngleUnits(AngleUnit.rad); - Assert.assertEquals("0.921+Πi", cm.evaluate(JsclOperation.numeric, "ln(5cosh(38π√(2cos(2))))").getStringResult()); - Assert.assertEquals("-3.41+3.41i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)").getStringResult()); - } finally { - cm.getEngine().setAngleUnits(defaultAngleUnit); - } - Assert.assertEquals("7.389i", cm.evaluate(JsclOperation.numeric, "iexp(2)").getStringResult()); - Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+iexp(2)").getStringResult()); - Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+√(-1)exp(2)").getStringResult()); - Assert.assertEquals("2-2.5i", cm.evaluate(JsclOperation.numeric, "2-2.5i").getStringResult()); - Assert.assertEquals("-2-2.5i", cm.evaluate(JsclOperation.numeric, "-2-2.5i").getStringResult()); - Assert.assertEquals("-2+2.5i", cm.evaluate(JsclOperation.numeric, "-2+2.5i").getStringResult()); - Assert.assertEquals("-2+2.1i", cm.evaluate(JsclOperation.numeric, "-2+2.1i").getStringResult()); - Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)").getStringResult()); - - junit.framework.Assert.assertEquals("24", cm.evaluate(JsclOperation.numeric, "4!").getStringResult()); - junit.framework.Assert.assertEquals("24", cm.evaluate(JsclOperation.numeric, "(2+2)!").getStringResult()); - junit.framework.Assert.assertEquals("120", cm.evaluate(JsclOperation.numeric, "(2+2+1)!").getStringResult()); - junit.framework.Assert.assertEquals("24", cm.evaluate(JsclOperation.numeric, "(2.0+2.0)!").getStringResult()); - junit.framework.Assert.assertEquals("24", cm.evaluate(JsclOperation.numeric, "4.0!").getStringResult()); - junit.framework.Assert.assertEquals("720", cm.evaluate(JsclOperation.numeric, "(3!)!").getStringResult()); - junit.framework.Assert.assertEquals("36", Expression.valueOf("3!^2").numeric().toString()); - junit.framework.Assert.assertEquals("3", Expression.valueOf("cubic(27)").numeric().toString()); - try { - junit.framework.Assert.assertEquals("√(-1)!", cm.evaluate(JsclOperation.numeric, "i!").getStringResult()); - fail(); - } catch (CalculatorParseException e) { - } - - junit.framework.Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "(π/π)!").getStringResult()); - - try { - junit.framework.Assert.assertEquals("i", cm.evaluate(JsclOperation.numeric, "(-1)i!").getStringResult()); - fail(); - } catch (CalculatorParseException e) { - - } - junit.framework.Assert.assertEquals("24i", cm.evaluate(JsclOperation.numeric, "4!i").getStringResult()); - - CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("si", 5d)); - - try { - cm.getEngine().setAngleUnits(AngleUnit.rad); - Assert.assertEquals("0.451", cm.evaluate(JsclOperation.numeric, "acos(0.8999999999999811)").getStringResult()); - Assert.assertEquals("-0.959", cm.evaluate(JsclOperation.numeric, "sin(5)").getStringResult()); - Assert.assertEquals("-4.795", cm.evaluate(JsclOperation.numeric, "sin(5)si").getStringResult()); - Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "sisin(5)si").getStringResult()); - Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "si*sin(5)si").getStringResult()); - Assert.assertEquals("-3.309", cm.evaluate(JsclOperation.numeric, "sisin(5si)si").getStringResult()); - } finally { - cm.getEngine().setAngleUnits(defaultAngleUnit); - } - - CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("s", 1d)); - Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getStringResult()); - - CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("k", 3.5d)); - CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("k1", 4d)); - Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11").getStringResult()); - - CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", (String) null)); - Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11").getStringResult()); - Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e").getStringResult()); - Assert.assertEquals("∞", cm.evaluate(JsclOperation.numeric, "∞").getStringResult()); - Assert.assertEquals("∞", cm.evaluate(JsclOperation.numeric, "Infinity").getStringResult()); - Assert.assertEquals("11∞t", cm.evaluate(JsclOperation.numeric, "t11∞").getStringResult()); - Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)").getStringResult()); - - Assert.assertEquals("100", cm.evaluate(JsclOperation.numeric, "0.1E3").getStringResult()); - Assert.assertEquals("3.957", cm.evaluate(JsclOperation.numeric, "ln(8)lg(8)+ln(8)").getStringResult()); - - Assert.assertEquals("0.933", cm.evaluate(JsclOperation.numeric, "0x:E/0x:F").getStringResult()); - - try { - cm.getEngine().setNumeralBase(NumeralBase.hex); - Assert.assertEquals("E/F", cm.evaluate(JsclOperation.numeric, "0x:E/0x:F").getStringResult()); - Assert.assertEquals("E/F", cm.evaluate(JsclOperation.simplify, "0x:E/0x:F").getStringResult()); - Assert.assertEquals("E/F", cm.evaluate(JsclOperation.numeric, "E/F").getStringResult()); - Assert.assertEquals("E/F", cm.evaluate(JsclOperation.simplify, "E/F").getStringResult()); - } finally { - cm.getEngine().setNumeralBase(NumeralBase.dec); - } - - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "((((((0))))))").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((0))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))").getStringResult()); - - - *//* Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "30°").getResult()); - Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "(10+20)°").getResult()); - Assert.assertEquals("1.047", cm.evaluate(JsclOperation.numeric, "(10+20)°*2").getResult()); - try { - Assert.assertEquals("0.278", cm.evaluate(JsclOperation.numeric, "30°^2").getResult()); - junit.framework.Assert.fail(); - } catch (ParseException e) { - if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) { - junit.framework.Assert.fail(); - } - }*//* - -*//* try { - cm.setTimeout(5000); - Assert.assertEquals("2", cm.evaluate(JsclOperation.numeric, "2!").getResult()); - } finally { - cm.setTimeout(3000); - }*//* - - CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", (String) null)); - Assert.assertEquals("2t", cm.evaluate(JsclOperation.simplify, "∂(t^2,t)").getStringResult()); - Assert.assertEquals("2t", cm.evaluate(JsclOperation.numeric, "∂(t^2,t)").getStringResult()); - CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("t", "2")); - Assert.assertEquals("2t", cm.evaluate(JsclOperation.simplify, "∂(t^2,t)").getStringResult()); - Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "∂(t^2,t)").getStringResult()); - - Assert.assertEquals("-x+x*ln(x)", cm.getEngine().simplify("∫(ln(x), x)")); - Assert.assertEquals("-(x-x*ln(x))/(ln(2)+ln(5))", cm.getEngine().simplify("∫(log(10, x), x)")); - - Assert.assertEquals("∫((ln(2)+ln(5))/ln(x), x)", cm.getEngine().simplify("∫(ln(10)/ln(x), x)")); - Assert.assertEquals("∫(ln(10)/ln(x), x)", Expression.valueOf("∫(log(x, 10), x)").expand().toString()); - Assert.assertEquals("∫((ln(2)+ln(5))/ln(x), x)", cm.getEngine().simplify("∫(log(x, 10), x)")); - } - - @Test - public void testFormatting() throws Exception { - final AndroidCalculatorEngine ce = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - Assert.assertEquals("12 345", ce.evaluate(JsclOperation.simplify, "12345").getStringResult()); - - } - - @Test - public void testI() throws CalculatorParseException, CalculatorEvalException { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - Assert.assertEquals("-i", cm.evaluate(JsclOperation.numeric, "i^3").getStringResult()); - for (int i = 0; i < 1000; i++) { - double real = (Math.random()-0.5) * 1000; - double imag = (Math.random()-0.5) * 1000; - int exp = (int)(Math.random() * 10); - - final StringBuilder sb = new StringBuilder(); - sb.append(real); - if ( imag > 0 ) { - sb.append("+"); - } - sb.append(imag); - sb.append("^").append(exp); - try { - cm.evaluate(JsclOperation.numeric, sb.toString()).getStringResult(); - } catch (Throwable e) { - fail(sb.toString()); - } - } - } - - @Test - public void testEmptyFunction() throws Exception { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - 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 (CalculatorParseException e) { - } - Assert.assertEquals("0.34+1.382i", cm.evaluate(JsclOperation.numeric, "ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(100)))))))))))))))").getStringResult()); - 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 (CalculatorParseException e) { - } - - final AngleUnit defaultAngleUnit = cm.getEngine().getAngleUnits(); - try { - cm.getEngine().setAngleUnits(AngleUnit.rad); - Assert.assertEquals("0.739", 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))))))))))))))))))))))))))))))))))))").getStringResult()); - } finally { - cm.getEngine().setAngleUnits(defaultAngleUnit); - } - - CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().add(new Var.Builder("si", 5d)); - Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getStringResult()); - - try { - cm.evaluate(JsclOperation.numeric, "sin"); - Assert.fail(); - } catch (CalculatorParseException e) { - } - } - - @Test - public void testRounding() throws Exception { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - try { - DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); - decimalGroupSymbols.setDecimalSeparator('.'); - decimalGroupSymbols.setGroupingSeparator('\''); - cm.setDecimalGroupSymbols(decimalGroupSymbols); - cm.setPrecision(2); - Assert.assertEquals("12'345'678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7").getStringResult()); - cm.setPrecision(10); - Assert.assertEquals("12'345'678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7").getStringResult()); - Assert.assertEquals("123'456'789", cm.evaluate(JsclOperation.numeric, "1.234567890E8").getStringResult()); - Assert.assertEquals("1'234'567'890.1", cm.evaluate(JsclOperation.numeric, "1.2345678901E9").getStringResult()); - } finally { - cm.setPrecision(3); - DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols(Locale.getDefault()); - decimalGroupSymbols.setDecimalSeparator('.'); - decimalGroupSymbols.setGroupingSeparator(JsclMathEngine.GROUPING_SEPARATOR_DEFAULT.charAt(0)); - cm.setDecimalGroupSymbols(decimalGroupSymbols); - } - } - - @Test - public void testComparisonFunction() throws Exception { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(0, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq(1, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq(1, 1.0)").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(1, 1.000000000000001)").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(1, 0)").getStringResult()); - - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "lt(0, 1)").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "lt(1, 1)").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "lt(1, 0)").getStringResult()); - - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "gt(0, 1)").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "gt(1, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "gt(1, 0)").getStringResult()); - - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ne(0, 1)").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "ne(1, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ne(1, 0)").getStringResult()); - - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "le(0, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "le(1, 1)").getStringResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "le(1, 0)").getStringResult()); - - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "ge(0, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ge(1, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ge(1, 0)").getStringResult()); - - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "ap(0, 1)").getStringResult()); - Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ap(1, 1)").getStringResult()); - //Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ap(1, 1.000000000000001)").getResult()); - Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "ap(1, 0)").getStringResult()); - - } - - - @Test - public void testNumeralSystems() throws Exception { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - Assert.assertEquals("11 259 375", cm.evaluate(JsclOperation.numeric, "0x:ABCDEF").getStringResult()); - Assert.assertEquals("30 606 154.462", cm.evaluate(JsclOperation.numeric, "0x:ABCDEF*e").getStringResult()); - Assert.assertEquals("30 606 154.462", cm.evaluate(JsclOperation.numeric, "e*0x:ABCDEF").getStringResult()); - Assert.assertEquals("e", cm.evaluate(JsclOperation.numeric, "e*0x:ABCDEF/0x:ABCDEF").getStringResult()); - Assert.assertEquals("30 606 154.462", cm.evaluate(JsclOperation.numeric, "0x:ABCDEF*e*0x:ABCDEF/0x:ABCDEF").getStringResult()); - Assert.assertEquals("30 606 154.462", cm.evaluate(JsclOperation.numeric, "c+0x:ABCDEF*e*0x:ABCDEF/0x:ABCDEF-c+0x:C-0x:C").getStringResult()); - Assert.assertEquals("1 446 257 064 651.832", cm.evaluate(JsclOperation.numeric, "28*28 * sin(28) - 0b:1101 + √(28) + exp ( 28) ").getStringResult()); - Assert.assertEquals("13", cm.evaluate(JsclOperation.numeric, "0b:1101").getStringResult()); - - try { - cm.evaluate(JsclOperation.numeric, "0b:π").getStringResult(); - Assert.fail(); - } catch (CalculatorParseException e) { - // ok - } - - final NumeralBase defaultNumeralBase = cm.getEngine().getNumeralBase(); - try{ - cm.getEngine().setNumeralBase(NumeralBase.bin); - Assert.assertEquals("101", cm.evaluate(JsclOperation.numeric, "10+11").getStringResult()); - Assert.assertEquals("10/11", cm.evaluate(JsclOperation.numeric, "10/11").getStringResult()); - - cm.getEngine().setNumeralBase(NumeralBase.hex); - Assert.assertEquals("63 7B", cm.evaluate(JsclOperation.numeric, "56CE+CAD").getStringResult()); - Assert.assertEquals("E", cm.evaluate(JsclOperation.numeric, "E").getStringResult()); - } finally { - cm.setNumeralBase(defaultNumeralBase); - } - } - - @Test - public void testLog() throws Exception { - final AndroidCalculatorEngine cm = (AndroidCalculatorEngine) CalculatorLocatorImpl.getInstance().getEngine(); - - Assert.assertEquals("∞", Expression.valueOf("1/0").numeric().toString()); - Assert.assertEquals("∞", Expression.valueOf("ln(10)/ln(1)").numeric().toString()); - - // logarithm - Assert.assertEquals("ln(x)/ln(base)", ((CustomFunction) cm.getFunctionsRegistry().get("log")).getContent()); - Assert.assertEquals("∞", cm.evaluate(JsclOperation.numeric, "log(1, 10)").getStringResult()); - Assert.assertEquals("3.322", cm.evaluate(JsclOperation.numeric, "log(2, 10)").getStringResult()); - Assert.assertEquals("1.431", cm.evaluate(JsclOperation.numeric, "log(5, 10)").getStringResult()); - Assert.assertEquals("0.96", cm.evaluate(JsclOperation.numeric, "log(11, 10)").getStringResult()); - Assert.assertEquals("1/(bln(a))", cm.evaluate(JsclOperation.simplify, "∂(log(a, b), b)").getStringResult()); - Assert.assertEquals("-ln(b)/(aln(a)^2)", cm.evaluate(JsclOperation.simplify, "∂(log(a, b), a)").getStringResult()); - - }*/ -} diff --git a/pom.xml b/pom.xml index 471d7388..529242c7 100644 --- a/pom.xml +++ b/pom.xml @@ -181,6 +181,16 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12.3 + + perTest + + + @@ -234,16 +244,6 @@ 1.5 - - -