Directory renamed
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||
import org.solovyev.android.calculator.model.TextProcessor;
|
||||
import org.solovyev.android.calculator.view.TextHighlighter;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/12/11
|
||||
* Time: 10:07 PM
|
||||
*/
|
||||
public class TextHighlighterTest {
|
||||
|
||||
@Test
|
||||
public void testProcess() throws Exception {
|
||||
TextProcessor<?, String> textHighlighter = new TextHighlighter(0, false, JsclMathEngine.instance);
|
||||
|
||||
final Random random = new Random(new Date().getTime());
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
for (int j = 0; j < 1000; j++) {
|
||||
sb.append(random.nextBoolean() ? "(" : ")");
|
||||
}
|
||||
try {
|
||||
textHighlighter.process(sb.toString());
|
||||
} catch (Exception e) {
|
||||
System.out.println(sb.toString());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertEquals("<font color=\"#000000\"></font>)(((())())", textHighlighter.process(")(((())())").toString());
|
||||
Assert.assertEquals(")", textHighlighter.process(")").toString());
|
||||
Assert.assertEquals(")()(", textHighlighter.process(")()(").toString());
|
||||
|
||||
textHighlighter = new TextHighlighter(0, true, JsclMathEngine.instance);
|
||||
Assert.assertEquals("1 000 000", textHighlighter.process("1000000").toString());
|
||||
Assert.assertEquals("1 000 000", textHighlighter.process("1000000").toString());
|
||||
Assert.assertEquals("0.1E3", textHighlighter.process("0.1E3").toString());
|
||||
Assert.assertEquals("1E3", textHighlighter.process("1E3").toString());
|
||||
Assert.assertEquals("2<b>0x:</b>", textHighlighter.process("20x:").toString());
|
||||
Assert.assertEquals("20x", textHighlighter.process("20x").toString());
|
||||
Assert.assertEquals("22x", textHighlighter.process("22x").toString());
|
||||
Assert.assertEquals("20t", textHighlighter.process("20t").toString());
|
||||
Assert.assertEquals("20k", textHighlighter.process("20k").toString());
|
||||
Assert.assertEquals("3!!", textHighlighter.process("3!!").toString());
|
||||
Assert.assertEquals("2", textHighlighter.process("2").toString());
|
||||
Assert.assertEquals("21", textHighlighter.process("21").toString());
|
||||
Assert.assertEquals("214", textHighlighter.process("214").toString());
|
||||
Assert.assertEquals("2 145", textHighlighter.process("2 145").toString());
|
||||
Assert.assertEquals("1 000 000E3", textHighlighter.process("1000000E3").toString());
|
||||
Assert.assertEquals("-1 000 000E3", textHighlighter.process("-1000000E3").toString());
|
||||
Assert.assertEquals("-1 000 000E-3", textHighlighter.process("-1000000E-3").toString());
|
||||
Assert.assertEquals("-1 000 000E-30000", textHighlighter.process("-1000000E-30000").toString());
|
||||
textHighlighter = new TextHighlighter(0, false, JsclMathEngine.instance);
|
||||
|
||||
textHighlighter.process("cannot calculate 3^10^10 !!!\n" +
|
||||
" unable to enter 0. FIXED\n" +
|
||||
" empty display in Xperia Rayo\n" +
|
||||
" check привиденная FIXED\n" +
|
||||
" set display result only if text in editor was not changed FIXED\n" +
|
||||
" shift M text to the left\n" +
|
||||
" do not show SYNTAX ERROR always (may be show send clock?q) FIXED\n" +
|
||||
" ln(8)*log(8) => ln(8)*og(8) FIXED\n" +
|
||||
" copy/paste ln(8)*log(8)\n" +
|
||||
" 6!^2 ERROR");
|
||||
|
||||
Assert.assertEquals("<font color=\"#000000\"><i>sin</i>(</font><font color=\"#ffff9a\">2</font><font color=\"#000000\">)</font>", textHighlighter.process("sin(2)").toString());
|
||||
Assert.assertEquals("<font color=\"#000000\"><i>atanh</i>(</font><font color=\"#ffff9a\">2</font><font color=\"#000000\">)</font>", textHighlighter.process("atanh(2)").toString());
|
||||
|
||||
|
||||
Assert.assertEquals("<b>0x:</b>E", textHighlighter.process("0x:E").toString());
|
||||
Assert.assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:6F").toString());
|
||||
Assert.assertEquals("<b>0x:</b>6F.", textHighlighter.process("0x:6F.").toString());
|
||||
Assert.assertEquals("<b>0x:</b>6F.2", textHighlighter.process("0x:6F.2").toString());
|
||||
Assert.assertEquals("<b>0x:</b>6F.B", textHighlighter.process("0x:6F.B").toString());
|
||||
Assert.assertEquals("<b>0x:</b>006F.B", textHighlighter.process("0x:006F.B").toString());
|
||||
Assert.assertEquals("<b>0x:</b>0", textHighlighter.process("0x:0").toString());
|
||||
Assert.assertEquals("<b>0x:</b>FF33233FFE", textHighlighter.process("0x:FF33233FFE").toString());
|
||||
Assert.assertEquals("<b>0x:</b>FF33 233 FFE", textHighlighter.process("0x:FF33 233 FFE").toString());
|
||||
|
||||
final MathEngine me = CalculatorEngine.instance.getEngine();
|
||||
try {
|
||||
me.setNumeralBase(NumeralBase.hex);
|
||||
Assert.assertEquals("E", textHighlighter.process("E").toString());
|
||||
Assert.assertEquals(".E", textHighlighter.process(".E").toString());
|
||||
Assert.assertEquals("E.", textHighlighter.process("E.").toString());
|
||||
Assert.assertEquals(".E.", textHighlighter.process(".E.").toString());
|
||||
Assert.assertEquals("6F", textHighlighter.process("6F").toString());
|
||||
Assert.assertEquals("6F", textHighlighter.process("6F").toString());
|
||||
Assert.assertEquals("6F.", textHighlighter.process("6F.").toString());
|
||||
Assert.assertEquals("6F.2", textHighlighter.process("6F.2").toString());
|
||||
Assert.assertEquals("6F.B", textHighlighter.process("6F.B").toString());
|
||||
Assert.assertEquals("006F.B", textHighlighter.process("006F.B").toString());
|
||||
} finally {
|
||||
me.setNumeralBase(NumeralBase.dec);
|
||||
}
|
||||
|
||||
Assert.assertEquals("<b>0b:</b>110101", textHighlighter.process("0b:110101").toString());
|
||||
Assert.assertEquals("<b>0b:</b>110101.", textHighlighter.process("0b:110101.").toString());
|
||||
Assert.assertEquals("<b>0b:</b>110101.101", textHighlighter.process("0b:110101.101").toString());
|
||||
Assert.assertEquals("<b>0b:</b>11010100.1", textHighlighter.process("0b:11010100.1").toString());
|
||||
Assert.assertEquals("<b>0b:</b>110101.0", textHighlighter.process("0b:110101.0").toString());
|
||||
Assert.assertEquals("<b>0b:</b>0", textHighlighter.process("0b:0").toString());
|
||||
Assert.assertEquals("<b>0b:</b>1010100101111010101001", textHighlighter.process("0b:1010100101111010101001").toString());
|
||||
Assert.assertEquals("<b>0b:</b>101 010 01 0 111 1 0 10101001", textHighlighter.process("0b:101 010 01 0 111 1 0 10101001").toString());
|
||||
|
||||
try {
|
||||
me.setNumeralBase(NumeralBase.bin);
|
||||
Assert.assertEquals("110101", textHighlighter.process("110101").toString());
|
||||
Assert.assertEquals("110101.", textHighlighter.process("110101.").toString());
|
||||
Assert.assertEquals("110101.101", textHighlighter.process("110101.101").toString());
|
||||
Assert.assertEquals("11010100.1", textHighlighter.process("11010100.1").toString());
|
||||
Assert.assertEquals("110101.0", textHighlighter.process("110101.0").toString());
|
||||
Assert.assertEquals("0", textHighlighter.process("0").toString());
|
||||
Assert.assertEquals("1010100101111010101001", textHighlighter.process("1010100101111010101001").toString());
|
||||
Assert.assertEquals("101 010 01 0 111 1 0 10101001", textHighlighter.process("101 010 01 0 111 1 0 10101001").toString());
|
||||
} finally {
|
||||
me.setNumeralBase(NumeralBase.dec);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,320 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.ICalculatorDisplay;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.utils.CollectionEqualizer;
|
||||
import org.solovyev.common.utils.EqualsTool;
|
||||
import org.solovyev.common.utils.history.HistoryHelper;
|
||||
import org.solovyev.common.utils.history.SimpleHistoryHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/17/11
|
||||
* Time: 10:01 PM
|
||||
*/
|
||||
public class HistoryUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testFromXml() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
private static final String emptyHistory = "<history>\n" +
|
||||
" <historyItems class=\"java.util.ArrayList\"/>\n" +
|
||||
"</history>";
|
||||
|
||||
private static final String toXml1 = "<history>\n" +
|
||||
" <historyItems class=\"java.util.ArrayList\">\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>3</cursorPosition>\n" +
|
||||
" <text>1+1</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>1</cursorPosition>\n" +
|
||||
" <text>Error</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>simplify</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" </historyItems>\n" +
|
||||
"</history>";
|
||||
|
||||
private static final String toXml2 = "<history>\n" +
|
||||
" <historyItems class=\"java.util.ArrayList\">\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>3</cursorPosition>\n" +
|
||||
" <text>1+1</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>1</cursorPosition>\n" +
|
||||
" <text>Error</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>simplify</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>2</cursorPosition>\n" +
|
||||
" <text>5/6</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>3</cursorPosition>\n" +
|
||||
" <text>5/6</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>numeric</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>1</cursorPosition>\n" +
|
||||
" <text>null</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>1</cursorPosition>\n" +
|
||||
" <text>Error</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>elementary</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>0</cursorPosition>\n" +
|
||||
" <text>4+5/35sin(41)+dfdsfsdfs</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>1</cursorPosition>\n" +
|
||||
" <text>4+5/35sin(41)+dfdsfsdfs</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>numeric</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" </historyItems>\n" +
|
||||
"</history>";
|
||||
|
||||
@Test
|
||||
public void testToXml() throws Exception {
|
||||
final Date date = new Date(100000000);
|
||||
|
||||
HistoryHelper<CalculatorHistoryState> history = new SimpleHistoryHelper<CalculatorHistoryState>();
|
||||
|
||||
ICalculatorDisplay calculatorDisplay = new TestCalculatorDisplay();
|
||||
calculatorDisplay.setErrorMessage("error_msg1");
|
||||
calculatorDisplay.setText("Error");
|
||||
calculatorDisplay.setSelection(1);
|
||||
calculatorDisplay.setJsclOperation(JsclOperation.simplify);
|
||||
|
||||
Editor calculatorEditor = new TestEditor();
|
||||
calculatorEditor.setSelection(3);
|
||||
calculatorEditor.setText("1+1");
|
||||
|
||||
CalculatorHistoryState state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
Assert.assertEquals(emptyHistory, HistoryUtils.toXml(history.getStates()));
|
||||
|
||||
|
||||
state.setSaved(true);
|
||||
|
||||
Assert.assertEquals(toXml1, HistoryUtils.toXml(history.getStates()));
|
||||
|
||||
calculatorDisplay = new TestCalculatorDisplay();
|
||||
calculatorDisplay.setErrorMessage(null);
|
||||
calculatorDisplay.setText("5/6");
|
||||
calculatorDisplay.setSelection(3);
|
||||
calculatorDisplay.setJsclOperation(JsclOperation.numeric);
|
||||
|
||||
calculatorEditor = new TestEditor();
|
||||
calculatorEditor.setSelection(2);
|
||||
calculatorEditor.setText("5/6");
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
calculatorDisplay = new TestCalculatorDisplay();
|
||||
calculatorDisplay.setErrorMessage("error_msg2");
|
||||
calculatorDisplay.setText("Error");
|
||||
calculatorDisplay.setSelection(1);
|
||||
calculatorDisplay.setJsclOperation(JsclOperation.elementary);
|
||||
|
||||
calculatorEditor = new TestEditor();
|
||||
calculatorEditor.setSelection(1);
|
||||
calculatorEditor.setText(null);
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
calculatorDisplay = new TestCalculatorDisplay();
|
||||
calculatorDisplay.setErrorMessage(null);
|
||||
calculatorDisplay.setText("4+5/35sin(41)+dfdsfsdfs");
|
||||
calculatorDisplay.setSelection(1);
|
||||
calculatorDisplay.setJsclOperation(JsclOperation.numeric);
|
||||
|
||||
calculatorEditor = new TestEditor();
|
||||
calculatorEditor.setSelection(0);
|
||||
calculatorEditor.setText("4+5/35sin(41)+dfdsfsdfs");
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
String xml = HistoryUtils.toXml(history.getStates());
|
||||
Assert.assertEquals(toXml2, xml);
|
||||
|
||||
final List<CalculatorHistoryState> fromXml = new ArrayList<CalculatorHistoryState>();
|
||||
final HistoryHelper<CalculatorHistoryState> historyFromXml = new SimpleHistoryHelper<CalculatorHistoryState>();
|
||||
HistoryUtils.fromXml(xml, fromXml);
|
||||
for (CalculatorHistoryState historyState : fromXml) {
|
||||
historyFromXml.addState(historyState);
|
||||
}
|
||||
|
||||
Assert.assertEquals(history.getStates().size(), historyFromXml.getStates().size());
|
||||
|
||||
for (CalculatorHistoryState historyState : history.getStates()) {
|
||||
historyState.setId(0);
|
||||
historyState.setSaved(true);
|
||||
}
|
||||
for (CalculatorHistoryState historyState : historyFromXml.getStates()) {
|
||||
historyState.setId(0);
|
||||
historyState.setSaved(true);
|
||||
}
|
||||
Assert.assertTrue(EqualsTool.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer<CalculatorHistoryState>(null)));
|
||||
}
|
||||
|
||||
|
||||
private static class TestCalculatorDisplay implements ICalculatorDisplay {
|
||||
|
||||
@NotNull
|
||||
private final TestEditor testEditor = new TestEditor();
|
||||
|
||||
private boolean valid;
|
||||
|
||||
private String errorMessage;
|
||||
|
||||
private JsclOperation operation;
|
||||
|
||||
private Generic genericResult;
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValid(boolean valid) {
|
||||
this.valid = valid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrorMessage() {
|
||||
return this.errorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorMessage(@Nullable String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setJsclOperation(@NotNull JsclOperation jsclOperation) {
|
||||
this.operation = jsclOperation;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public JsclOperation getJsclOperation() {
|
||||
return this.operation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGenericResult(@Nullable Generic genericResult) {
|
||||
this.genericResult = genericResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Generic getGenericResult() {
|
||||
return this.genericResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getText() {
|
||||
return this.testEditor.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(@Nullable CharSequence text) {
|
||||
this.testEditor.setText(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return this.testEditor.getSelection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelection(int selection) {
|
||||
this.testEditor.setSelection(selection);
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestEditor implements Editor {
|
||||
|
||||
@Nullable
|
||||
private CharSequence text;
|
||||
|
||||
private int selection;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public CharSequence getText() {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(@Nullable CharSequence text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSelection() {
|
||||
return this.selection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelection(int selection) {
|
||||
this.selection = selection;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.jscl;
|
||||
|
||||
import jscl.math.Expression;
|
||||
import jscl.math.Generic;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/18/11
|
||||
* Time: 10:42 PM
|
||||
*/
|
||||
public class FromJsclNumericTextProcessorTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
CalculatorEngine.instance.init(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateResultForComplexNumber() throws Exception {
|
||||
final FromJsclNumericTextProcessor cm = new FromJsclNumericTextProcessor();
|
||||
|
||||
Assert.assertEquals("1.22133+23 123i", cm.process(Expression.valueOf("1.22133232+23123*i").numeric()));
|
||||
Assert.assertEquals("1.22133+1.2i", cm.process(Expression.valueOf("1.22133232+1.2*i").numeric()));
|
||||
Assert.assertEquals("1.22i", cm.process(Expression.valueOf("1.22*i").numeric()));
|
||||
Assert.assertEquals("i", cm.process(Expression.valueOf("i").numeric()));
|
||||
Generic numeric = Expression.valueOf("e^(π*i)+1").numeric();
|
||||
junit.framework.Assert.assertEquals("0i", cm.process(numeric));
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.math;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/5/11
|
||||
* Time: 1:25 AM
|
||||
*/
|
||||
public class MathTypeTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
CalculatorEngine.instance.init(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetType() throws Exception {
|
||||
Assert.assertEquals(MathType.function, MathType.getType("sin", 0, false).getMathType());
|
||||
Assert.assertEquals(MathType.text, MathType.getType("sn", 0, false).getMathType());
|
||||
Assert.assertEquals(MathType.text, MathType.getType("s", 0, false).getMathType());
|
||||
Assert.assertEquals(MathType.text, MathType.getType("", 0, false).getMathType());
|
||||
|
||||
try {
|
||||
Assert.assertEquals(MathType.text, MathType.getType("22", -1, false).getMathType());
|
||||
Assert.fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
Assert.assertEquals(MathType.text, MathType.getType("22", 2, false).getMathType());
|
||||
Assert.fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
|
||||
Assert.assertEquals("atanh", MathType.getType("atanh", 0, false).getMatch());
|
||||
}
|
||||
|
||||
/* @Test
|
||||
public void testPostfixFunctionsProcessing() throws Exception {
|
||||
|
||||
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart("5!", 1));
|
||||
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("!", 1));
|
||||
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart("5.4434234!", 9));
|
||||
org.junit.Assert.assertEquals(1, MathType.getPostfixFunctionStart("2+5!", 3));
|
||||
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+5.4434234!", 14));
|
||||
org.junit.Assert.assertEquals(14, MathType.getPostfixFunctionStart("2.23+5.4434234*5!", 16));
|
||||
org.junit.Assert.assertEquals(14, MathType.getPostfixFunctionStart("2.23+5.4434234*5.1!", 18));
|
||||
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*5.1)!", 20));
|
||||
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*(5.1+1))!", 24));
|
||||
org.junit.Assert.assertEquals(4, MathType.getPostfixFunctionStart("2.23+(5.4434234*sin(5.1+1))!", 27));
|
||||
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("sin(5)!", 6));
|
||||
org.junit.Assert.assertEquals(-1, MathType.getPostfixFunctionStart(")!", ")!".indexOf("!")));
|
||||
org.junit.Assert.assertEquals(0, MathType.getPostfixFunctionStart("sin(5sin(5sin(5)))!", 18));
|
||||
org.junit.Assert.assertEquals(2, MathType.getPostfixFunctionStart("2+sin(5sin(5sin(5)))!", 20));
|
||||
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1+1))!", 30));
|
||||
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1E2+e))!", "2.23+sin(5.4434234*sin(5.1E2+e))!".indexOf("!")));
|
||||
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1E2+5 555 555))!", "2.23+sin(5.4434234*sin(5.1E2+5 555 555))!".indexOf("!")));
|
||||
org.junit.Assert.assertEquals(5, MathType.getPostfixFunctionStart("2.23+sin(5.4434234^sin(5.1E2!+5'555'555))!", "2.23+sin(5.4434234^sin(5.1E2!+5'555'555))!".lastIndexOf("!")));
|
||||
}*/
|
||||
}
|
@@ -0,0 +1,443 @@
|
||||
/*
|
||||
* 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.NumeralBase;
|
||||
import jscl.math.Expression;
|
||||
import jscl.math.Generic;
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.CustomFunction;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.Locale;
|
||||
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/17/11
|
||||
* Time: 9:47 PM
|
||||
*/
|
||||
|
||||
public class CalculatorEngineTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
CalculatorEngine.instance.init(null, null);
|
||||
CalculatorEngine.instance.setPrecision(3);
|
||||
CalculatorEngine.instance.setThreadKiller(new CalculatorEngine.ThreadKillerImpl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDegrees() throws Exception {
|
||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||
|
||||
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°").getResult());
|
||||
Assert.assertEquals("0.349", cm.evaluate(JsclOperation.numeric, "20.0°").getResult());
|
||||
Assert.assertEquals("0.5", cm.evaluate(JsclOperation.numeric, "sin(30°)").getResult());
|
||||
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(sin(30°))").getResult());
|
||||
Assert.assertEquals("∂(cos(t), t, t, 1°)", cm.evaluate(JsclOperation.numeric, "∂(cos(t),t,t,1°)").getResult());
|
||||
|
||||
Assert.assertEquals("∂(cos(t), t, t, 1°)", cm.evaluate(JsclOperation.simplify, "∂(cos(t),t,t,1°)").getResult());
|
||||
} finally {
|
||||
cm.getEngine().setAngleUnits(defaultAngleUnit);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongExecution() throws Exception {
|
||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||
|
||||
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 CalculatorEngine cm = CalculatorEngine.instance;
|
||||
|
||||
Assert.assertEquals("cos(t)+10%", cm.evaluate(JsclOperation.simplify, "cos(t)+10%").getResult());
|
||||
|
||||
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").getResult());
|
||||
Assert.assertEquals("10%", cm.evaluate(JsclOperation.simplify, "10%").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(0, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq(1, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq( 1, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.simplify, "eq( 1, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "lg(10)").getResult());
|
||||
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "2+2").getResult());
|
||||
final AngleUnit defaultAngleUnit = cm.getEngine().getAngleUnits();
|
||||
try {
|
||||
cm.getEngine().setAngleUnits(AngleUnit.rad);
|
||||
Assert.assertEquals("-0.757", cm.evaluate(JsclOperation.numeric, "sin(4)").getResult());
|
||||
Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(0.5)").getResult());
|
||||
Assert.assertEquals("-0.396", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)").getResult());
|
||||
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getResult());
|
||||
Assert.assertEquals("-0.56", cm.evaluate(JsclOperation.numeric, "sin(4)asin(0.5)√(2)").getResult());
|
||||
} finally {
|
||||
cm.getEngine().setAngleUnits(defaultAngleUnit);
|
||||
}
|
||||
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "e^2").getResult());
|
||||
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(1)^2").getResult());
|
||||
Assert.assertEquals("7.389", cm.evaluate(JsclOperation.numeric, "exp(2)").getResult());
|
||||
Assert.assertEquals("2+i", cm.evaluate(JsclOperation.numeric, "2*1+√(-1)").getResult());
|
||||
try {
|
||||
cm.getEngine().setAngleUnits(AngleUnit.rad);
|
||||
Assert.assertEquals("0.921+πi", cm.evaluate(JsclOperation.numeric, "ln(5cosh(38π√(2cos(2))))").getResult());
|
||||
Assert.assertEquals("-3.41+3.41i", cm.evaluate(JsclOperation.numeric, "(5tan(2i)+2i)/(1-i)").getResult());
|
||||
} finally {
|
||||
cm.getEngine().setAngleUnits(defaultAngleUnit);
|
||||
}
|
||||
Assert.assertEquals("7.389i", cm.evaluate(JsclOperation.numeric, "iexp(2)").getResult());
|
||||
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+iexp(2)").getResult());
|
||||
Assert.assertEquals("2+7.389i", cm.evaluate(JsclOperation.numeric, "2+√(-1)exp(2)").getResult());
|
||||
Assert.assertEquals("2-2.5i", cm.evaluate(JsclOperation.numeric, "2-2.5i").getResult());
|
||||
Assert.assertEquals("-2-2.5i", cm.evaluate(JsclOperation.numeric, "-2-2.5i").getResult());
|
||||
Assert.assertEquals("-2+2.5i", cm.evaluate(JsclOperation.numeric, "-2+2.5i").getResult());
|
||||
Assert.assertEquals("-2+2.1i", cm.evaluate(JsclOperation.numeric, "-2+2.1i").getResult());
|
||||
Assert.assertEquals("-0.1-0.2i", cm.evaluate(JsclOperation.numeric, "(1-i)/(2+6i)").getResult());
|
||||
|
||||
junit.framework.Assert.assertEquals("24", cm.evaluate(JsclOperation.numeric, "4!").getResult());
|
||||
junit.framework.Assert.assertEquals("24", cm.evaluate(JsclOperation.numeric, "(2+2)!").getResult());
|
||||
junit.framework.Assert.assertEquals("120", cm.evaluate(JsclOperation.numeric, "(2+2+1)!").getResult());
|
||||
junit.framework.Assert.assertEquals("24", cm.evaluate(JsclOperation.numeric, "(2.0+2.0)!").getResult());
|
||||
junit.framework.Assert.assertEquals("24", cm.evaluate(JsclOperation.numeric, "4.0!").getResult());
|
||||
junit.framework.Assert.assertEquals("720", cm.evaluate(JsclOperation.numeric, "(3!)!").getResult());
|
||||
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!").getResult());
|
||||
fail();
|
||||
} catch (CalculatorParseException e) {
|
||||
}
|
||||
|
||||
junit.framework.Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "(π/π)!").getResult());
|
||||
|
||||
try {
|
||||
junit.framework.Assert.assertEquals("i", cm.evaluate(JsclOperation.numeric, "(-1)i!").getResult());
|
||||
fail();
|
||||
} catch (CalculatorParseException e) {
|
||||
|
||||
}
|
||||
junit.framework.Assert.assertEquals("24i", cm.evaluate(JsclOperation.numeric, "4!i").getResult());
|
||||
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("si", 5d));
|
||||
|
||||
try {
|
||||
cm.getEngine().setAngleUnits(AngleUnit.rad);
|
||||
Assert.assertEquals("0.451", cm.evaluate(JsclOperation.numeric, "acos(0.8999999999999811)").getResult());
|
||||
Assert.assertEquals("-0.959", cm.evaluate(JsclOperation.numeric, "sin(5)").getResult());
|
||||
Assert.assertEquals("-4.795", cm.evaluate(JsclOperation.numeric, "sin(5)si").getResult());
|
||||
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "sisin(5)si").getResult());
|
||||
Assert.assertEquals("-23.973", cm.evaluate(JsclOperation.numeric, "si*sin(5)si").getResult());
|
||||
Assert.assertEquals("-3.309", cm.evaluate(JsclOperation.numeric, "sisin(5si)si").getResult());
|
||||
} finally {
|
||||
cm.getEngine().setAngleUnits(defaultAngleUnit);
|
||||
}
|
||||
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("s", 1d));
|
||||
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult());
|
||||
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("k", 3.5d));
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("k1", 4d));
|
||||
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11").getResult());
|
||||
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", (String) null));
|
||||
Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11").getResult());
|
||||
Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e").getResult());
|
||||
Assert.assertEquals("∞", cm.evaluate(JsclOperation.numeric, "∞").getResult());
|
||||
Assert.assertEquals("∞", cm.evaluate(JsclOperation.numeric, "Infinity").getResult());
|
||||
Assert.assertEquals("11∞t", cm.evaluate(JsclOperation.numeric, "t11∞").getResult());
|
||||
Assert.assertEquals("-t+t^3", cm.evaluate(JsclOperation.numeric, "t(t-1)(t+1)").getResult());
|
||||
|
||||
Assert.assertEquals("100", cm.evaluate(JsclOperation.numeric, "0.1E3").getResult());
|
||||
Assert.assertEquals("3.957", cm.evaluate(JsclOperation.numeric, "ln(8)lg(8)+ln(8)").getResult());
|
||||
|
||||
Assert.assertEquals("0.933", cm.evaluate(JsclOperation.numeric, "0x:E/0x:F").getResult());
|
||||
|
||||
try {
|
||||
cm.getEngine().setNumeralBase(NumeralBase.hex);
|
||||
Assert.assertEquals("E/F", cm.evaluate(JsclOperation.numeric, "0x:E/0x:F").getResult());
|
||||
Assert.assertEquals("E/F", cm.evaluate(JsclOperation.simplify, "0x:E/0x:F").getResult());
|
||||
Assert.assertEquals("E/F", cm.evaluate(JsclOperation.numeric, "E/F").getResult());
|
||||
Assert.assertEquals("E/F", cm.evaluate(JsclOperation.simplify, "E/F").getResult());
|
||||
} finally {
|
||||
cm.getEngine().setNumeralBase(NumeralBase.dec);
|
||||
}
|
||||
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "((((((0))))))").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((0))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))").getResult());
|
||||
|
||||
|
||||
/* 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);
|
||||
}*/
|
||||
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", (String) null));
|
||||
Assert.assertEquals("2t", cm.evaluate(JsclOperation.simplify, "∂(t^2,t)").getResult());
|
||||
Assert.assertEquals("2t", cm.evaluate(JsclOperation.numeric, "∂(t^2,t)").getResult());
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", "2"));
|
||||
Assert.assertEquals("2t", cm.evaluate(JsclOperation.simplify, "∂(t^2,t)").getResult());
|
||||
Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "∂(t^2,t)").getResult());
|
||||
|
||||
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 CalculatorEngine ce = CalculatorEngine.instance;
|
||||
|
||||
Assert.assertEquals("12 345", ce.evaluate(JsclOperation.simplify, "12345").getResult());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testI() throws CalculatorParseException, CalculatorEvalException {
|
||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||
|
||||
Assert.assertEquals("-i", cm.evaluate(JsclOperation.numeric, "i^3").getResult());
|
||||
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()).getResult();
|
||||
} catch (Throwable e) {
|
||||
fail(sb.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyFunction() throws Exception {
|
||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||
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)))))))))))))))").getResult());
|
||||
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))))))))))))))))))))))))))))))))))))").getResult());
|
||||
} finally {
|
||||
cm.getEngine().setAngleUnits(defaultAngleUnit);
|
||||
}
|
||||
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("si", 5d));
|
||||
Assert.assertEquals("5", cm.evaluate(JsclOperation.numeric, "si").getResult());
|
||||
|
||||
try {
|
||||
cm.evaluate(JsclOperation.numeric, "sin");
|
||||
Assert.fail();
|
||||
} catch (CalculatorParseException e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRounding() throws Exception {
|
||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||
|
||||
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").getResult());
|
||||
cm.setPrecision(10);
|
||||
Assert.assertEquals("12'345'678.9", cm.evaluate(JsclOperation.numeric, "1.23456789E7").getResult());
|
||||
Assert.assertEquals("123'456'789", cm.evaluate(JsclOperation.numeric, "1.234567890E8").getResult());
|
||||
Assert.assertEquals("1'234'567'890.1", cm.evaluate(JsclOperation.numeric, "1.2345678901E9").getResult());
|
||||
} 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 CalculatorEngine cm = CalculatorEngine.instance;
|
||||
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(0, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq(1, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq(1, 1.0)").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(1, 1.000000000000001)").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(1, 0)").getResult());
|
||||
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "lt(0, 1)").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "lt(1, 1)").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "lt(1, 0)").getResult());
|
||||
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "gt(0, 1)").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "gt(1, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "gt(1, 0)").getResult());
|
||||
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ne(0, 1)").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "ne(1, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ne(1, 0)").getResult());
|
||||
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "le(0, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "le(1, 1)").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "le(1, 0)").getResult());
|
||||
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "ge(0, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ge(1, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ge(1, 0)").getResult());
|
||||
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "ap(0, 1)").getResult());
|
||||
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ap(1, 1)").getResult());
|
||||
//Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "ap(1, 1.000000000000001)").getResult());
|
||||
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "ap(1, 0)").getResult());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNumeralSystems() throws Exception {
|
||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||
|
||||
Assert.assertEquals("11 259 375", cm.evaluate(JsclOperation.numeric, "0x:ABCDEF").getResult());
|
||||
Assert.assertEquals("30 606 154.462", cm.evaluate(JsclOperation.numeric, "0x:ABCDEF*e").getResult());
|
||||
Assert.assertEquals("30 606 154.462", cm.evaluate(JsclOperation.numeric, "e*0x:ABCDEF").getResult());
|
||||
Assert.assertEquals("e", cm.evaluate(JsclOperation.numeric, "e*0x:ABCDEF/0x:ABCDEF").getResult());
|
||||
Assert.assertEquals("30 606 154.462", cm.evaluate(JsclOperation.numeric, "0x:ABCDEF*e*0x:ABCDEF/0x:ABCDEF").getResult());
|
||||
Assert.assertEquals("30 606 154.462", cm.evaluate(JsclOperation.numeric, "c+0x:ABCDEF*e*0x:ABCDEF/0x:ABCDEF-c+0x:C-0x:C").getResult());
|
||||
Assert.assertEquals("1 446 257 064 651.832", cm.evaluate(JsclOperation.numeric, "28*28 * sin(28) - 0b:1101 + √(28) + exp ( 28) ").getResult());
|
||||
Assert.assertEquals("13", cm.evaluate(JsclOperation.numeric, "0b:1101").getResult());
|
||||
|
||||
try {
|
||||
cm.evaluate(JsclOperation.numeric, "0b:π").getResult();
|
||||
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").getResult());
|
||||
Assert.assertEquals("10/11", cm.evaluate(JsclOperation.numeric, "10/11").getResult());
|
||||
|
||||
cm.getEngine().setNumeralBase(NumeralBase.hex);
|
||||
Assert.assertEquals("63 7B", cm.evaluate(JsclOperation.numeric, "56CE+CAD").getResult());
|
||||
Assert.assertEquals("E", cm.evaluate(JsclOperation.numeric, "E").getResult());
|
||||
} finally {
|
||||
cm.setNumeralBase(defaultNumeralBase);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLog() throws Exception {
|
||||
final CalculatorEngine cm = CalculatorEngine.instance;
|
||||
|
||||
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)").getResult());
|
||||
Assert.assertEquals("3.322", cm.evaluate(JsclOperation.numeric, "log(2, 10)").getResult());
|
||||
Assert.assertEquals("1.431", cm.evaluate(JsclOperation.numeric, "log(5, 10)").getResult());
|
||||
Assert.assertEquals("0.96", cm.evaluate(JsclOperation.numeric, "log(11, 10)").getResult());
|
||||
Assert.assertEquals("1/(bln(a))", cm.evaluate(JsclOperation.simplify, "∂(log(a, b), b)").getResult());
|
||||
Assert.assertEquals("-ln(b)/(aln(a)^2)", cm.evaluate(JsclOperation.simplify, "∂(log(a, b), a)").getResult());
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DecimalFormatSymbols;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/20/11
|
||||
* Time: 3:43 PM
|
||||
*/
|
||||
public class FromJsclSimplifyTextProcessorTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
CalculatorEngine.instance.init(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcess() throws Exception {
|
||||
FromJsclSimplifyTextProcessor tp = new FromJsclSimplifyTextProcessor();
|
||||
//Assert.assertEquals("(e)", tp.process("(2.718281828459045)"));
|
||||
//Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045"));
|
||||
//Assert.assertEquals("((e)(e))", tp.process("((2.718281828459045)*(2.718281828459045))"));
|
||||
DecimalFormatSymbols decimalGroupSymbols = new DecimalFormatSymbols();
|
||||
decimalGroupSymbols.setGroupingSeparator(' ');
|
||||
CalculatorEngine.instance.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"));
|
||||
//Assert.assertEquals("e", tp.process("2.718281828459045"));
|
||||
//Assert.assertEquals("tee", tp.process("t2.718281828459045*2.718281828459045"));
|
||||
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t2.718281828459045", "2"));
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", (String)null));
|
||||
//Assert.assertEquals("t2.718281828459045e", tp.process("t2.718281828459045*2.718281828459045"));
|
||||
//Assert.assertEquals("ee", tp.process("2.718281828459045*2.718281828459045"));
|
||||
Assert.assertEquals("t×", tp.process("t*"));
|
||||
Assert.assertEquals("×t", tp.process("*t"));
|
||||
Assert.assertEquals("t2", tp.process("t*2"));
|
||||
Assert.assertEquals("2t", tp.process("2*t"));
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("t", (String) null));
|
||||
Assert.assertEquals("t×", tp.process("t*"));
|
||||
Assert.assertEquals("×t", tp.process("*t"));
|
||||
|
||||
Assert.assertEquals("t2", tp.process("t*2"));
|
||||
Assert.assertEquals("2t", tp.process("2*t"));
|
||||
|
||||
Assert.assertEquals("t^2×2", tp.process("t^2*2"));
|
||||
Assert.assertEquals("2t^2", tp.process("2*t^2"));
|
||||
|
||||
Assert.assertEquals("t^[2×2t]", tp.process("t^[2*2*t]"));
|
||||
Assert.assertEquals("2t^2[2t]", tp.process("2*t^2[2*t]"));
|
||||
|
||||
CalculatorEngine.instance.getVarsRegistry().add(new Var.Builder("k", (String) null));
|
||||
Assert.assertEquals("(t+2k)[k+2t]", tp.process("(t+2*k)*[k+2*t]"));
|
||||
Assert.assertEquals("(te+2k)e[k+2te]", tp.process("(t*e+2*k)*e*[k+2*t*e]"));
|
||||
|
||||
|
||||
Assert.assertEquals("tlog(3)", tp.process("t*log(3)"));
|
||||
Assert.assertEquals("t√(3)", tp.process("t*√(3)"));
|
||||
Assert.assertEquals("20x", tp.process("20*x"));
|
||||
Assert.assertEquals("20x", tp.process("20x"));
|
||||
Assert.assertEquals("2×0x3", tp.process("2*0x3"));
|
||||
Assert.assertEquals("2×0x:3", tp.process("2*0x:3"));
|
||||
}
|
||||
}
|
@@ -0,0 +1,146 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import au.com.bytecode.opencsv.CSVReader;
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.MathEngine;
|
||||
import jscl.math.Expression;
|
||||
import jscl.text.ParseException;
|
||||
import jscl.util.ExpressionGeneratorWithInput;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.utils.Converter;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/14/11
|
||||
* Time: 4:16 PM
|
||||
*/
|
||||
public class NumeralBaseTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
CalculatorEngine.instance.init(null, null);
|
||||
CalculatorEngine.instance.setPrecision(3);
|
||||
CalculatorEngine.instance.setThreadKiller(new CalculatorEngine.ThreadKillerImpl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConversion() throws Exception {
|
||||
CSVReader reader = null;
|
||||
try {
|
||||
final MathEngine me = JsclMathEngine.instance;
|
||||
|
||||
reader = new CSVReader(new InputStreamReader(NumeralBaseTest.class.getResourceAsStream("/org/solovyev/android/calculator/model/nb_table.csv")), '\t');
|
||||
|
||||
// skip first line
|
||||
reader.readNext();
|
||||
|
||||
String[] line = reader.readNext();
|
||||
for (; line != null; line = reader.readNext()) {
|
||||
testExpression(line, new DummyExpression());
|
||||
testExpression(line, new Expression1());
|
||||
testExpression(line, new Expression2());
|
||||
testExpression(line, new Expression3());
|
||||
|
||||
final String dec = line[0].toUpperCase();
|
||||
final String hex = "0x:" + line[1].toUpperCase();
|
||||
final String bin = "0b:" + line[2].toUpperCase();
|
||||
|
||||
final List<String> input = new ArrayList<String>();
|
||||
input.add(dec);
|
||||
input.add(hex);
|
||||
input.add(bin);
|
||||
|
||||
//System.out.println("Dec: " + dec);
|
||||
//System.out.println("Hex: " + hex);
|
||||
//System.out.println("Bin: " + bin);
|
||||
|
||||
final ExpressionGeneratorWithInput eg = new ExpressionGeneratorWithInput(input, 20);
|
||||
final List<String> expressions = eg.generate();
|
||||
|
||||
final String decExpression = expressions.get(0);
|
||||
final String hexExpression = expressions.get(1);
|
||||
final String binExpression = expressions.get(2);
|
||||
|
||||
//System.out.println("Dec expression: " + decExpression);
|
||||
//System.out.println("Hex expression: " + hexExpression);
|
||||
//System.out.println("Bin expression: " + binExpression);
|
||||
|
||||
final String decResult = Expression.valueOf(decExpression).numeric().toString();
|
||||
//System.out.println("Dec result: " + decResult);
|
||||
|
||||
final String hexResult = Expression.valueOf(hexExpression).numeric().toString();
|
||||
//System.out.println("Hex result: " + hexResult);
|
||||
|
||||
final String binResult = Expression.valueOf(binExpression).numeric().toString();
|
||||
//System.out.println("Bin result: " + binResult);
|
||||
|
||||
Assert.assertEquals("dec-hex: " + decExpression + " : " + hexExpression, decResult, hexResult);
|
||||
Assert.assertEquals("dec-bin: " + decExpression + " : " + binExpression, decResult, binResult);
|
||||
}
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void testExpression(@NotNull String[] line, @NotNull Converter<String, String> converter) throws ParseException, CalculatorEvalException, CalculatorParseException {
|
||||
final String dec = line[0].toUpperCase();
|
||||
final String hex = "0x:" + line[1].toUpperCase();
|
||||
final String bin = "0b:" + line[2].toUpperCase();
|
||||
|
||||
final String decExpression = converter.convert(dec);
|
||||
final String decResult = CalculatorEngine.instance.evaluate(JsclOperation.numeric, decExpression).getResult();
|
||||
final String hexExpression = converter.convert(hex);
|
||||
final String hexResult = CalculatorEngine.instance.evaluate(JsclOperation.numeric, hexExpression).getResult();
|
||||
final String binExpression = converter.convert(bin);
|
||||
final String binResult = CalculatorEngine.instance.evaluate(JsclOperation.numeric, binExpression).getResult();
|
||||
|
||||
Assert.assertEquals("dec-hex: " + decExpression + " : " + hexExpression, decResult, hexResult);
|
||||
Assert.assertEquals("dec-bin: " + decExpression + " : " + binExpression, decResult, binResult);
|
||||
}
|
||||
|
||||
private static class DummyExpression implements Converter<String, String> {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String convert(@NotNull String s) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Expression1 implements Converter<String, String> {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String convert(@NotNull String s) {
|
||||
return s + "*" + s;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Expression2 implements Converter<String, String> {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String convert(@NotNull String s) {
|
||||
return s + "*" + s + " * sin(" + s + ") - 0b:1101";
|
||||
}
|
||||
}
|
||||
|
||||
private static class Expression3 implements Converter<String, String> {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String convert(@NotNull String s) {
|
||||
return s + "*" + s + " * sin(" + s + ") - 0b:1101 + √(" + s + ") + exp ( " + s + ")";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.JsclMathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/26/11
|
||||
* Time: 12:13 PM
|
||||
*/
|
||||
public class ToJsclTextProcessorTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
CalculatorEngine.instance.init(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecialCases() throws CalculatorParseException {
|
||||
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
|
||||
Assert.assertEquals( "3^E10", preprocessor.process("3^E10").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcess() throws Exception {
|
||||
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
|
||||
|
||||
Assert.assertEquals( "", preprocessor.process("").toString());
|
||||
Assert.assertEquals( "()", preprocessor.process("[]").toString());
|
||||
Assert.assertEquals( "()*()", preprocessor.process("[][]").toString());
|
||||
Assert.assertEquals( "()*(1)", preprocessor.process("[][1]").toString());
|
||||
Assert.assertEquals( "(0)*(1)", preprocessor.process("[0][1]").toString());
|
||||
Assert.assertEquals( "(0)*(1E)", preprocessor.process("[0][1E]").toString());
|
||||
Assert.assertEquals( "(0)*(1E1)", preprocessor.process("[0][1E1]").toString());
|
||||
Assert.assertEquals( "(0)*(1E-1)", preprocessor.process("[0][1E-1]").toString());
|
||||
Assert.assertEquals( "(0)*(1.E-1)", preprocessor.process("[0][1.E-1]").toString());
|
||||
Assert.assertEquals( "(0)*(2*E-1)", preprocessor.process("[0][2*E-1]").toString());
|
||||
Assert.assertEquals( "(0)*ln(1)*(2*E-1)", preprocessor.process("[0]ln(1)[2*E-1]").toString());
|
||||
Assert.assertEquals( "sin(4)*asin(0.5)*√(2)", preprocessor.process("sin(4)asin(0.5)√(2)").toString());
|
||||
Assert.assertEquals( "sin(4)*cos(5)", preprocessor.process("sin(4)cos(5)").toString());
|
||||
Assert.assertEquals( "π*sin(4)*π*cos(√(5))", preprocessor.process("πsin(4)πcos(√(5))").toString());
|
||||
Assert.assertEquals( "π*sin(4)+π*cos(√(5))", preprocessor.process("πsin(4)+πcos(√(5))").toString());
|
||||
Assert.assertEquals( "π*sin(4)+π*cos(√(5+(√(-1))))", preprocessor.process("πsin(4)+πcos(√(5+i))").toString());
|
||||
Assert.assertEquals( "π*sin(4.01)+π*cos(√(5+(√(-1))))", preprocessor.process("πsin(4.01)+πcos(√(5+i))").toString());
|
||||
Assert.assertEquals( "e^π*sin(4.01)+π*cos(√(5+(√(-1))))", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))").toString());
|
||||
Assert.assertEquals( "e^π*sin(4.01)+π*cos(√(5+(√(-1))))E2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E2").toString());
|
||||
Assert.assertEquals( "e^π*sin(4.01)+π*cos(√(5+(√(-1))))E-2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E-2").toString());
|
||||
Assert.assertEquals( "E2", preprocessor.process("E2").toString());
|
||||
Assert.assertEquals( "E-2", preprocessor.process("E-2").toString());
|
||||
Assert.assertEquals( "E-1/2", preprocessor.process("E-1/2").toString());
|
||||
Assert.assertEquals( "E-1.2", preprocessor.process("E-1.2").toString());
|
||||
Assert.assertEquals( "E+1.2", preprocessor.process("E+1.2").toString());
|
||||
Assert.assertEquals( "E(-1.2)", preprocessor.process("E(-1.2)").toString());
|
||||
Assert.assertEquals( "EE", preprocessor.process("EE").toString());
|
||||
|
||||
try {
|
||||
CalculatorEngine.instance.getEngine().setNumeralBase(NumeralBase.hex);
|
||||
Assert.assertEquals( "22F*exp(F)", preprocessor.process("22Fexp(F)").toString());
|
||||
} finally {
|
||||
CalculatorEngine.instance.getEngine().setNumeralBase(NumeralBase.dec);
|
||||
}
|
||||
Assert.assertEquals( "0x:ABCDEF", preprocessor.process("0x:ABCDEF").toString());
|
||||
Assert.assertEquals( "0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString());
|
||||
Assert.assertEquals( "0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString());
|
||||
Assert.assertEquals( "0x:ABCDEF*0*x", preprocessor.process("0x:A BC DEF*0x").toString());
|
||||
Assert.assertEquals( "0x:ABCDEF001*0*x", preprocessor.process("0x:A BC DEF001*0x").toString());
|
||||
Assert.assertEquals( "0x:ABCDEF001*0*c", preprocessor.process("0x:A BC DEF001*0c").toString());
|
||||
Assert.assertEquals( "0x:ABCDEF001*c", preprocessor.process("0x:A BC DEF001*c").toString());
|
||||
Assert.assertEquals( "0b:1101", preprocessor.process("0b:1101").toString());
|
||||
Assert.assertEquals( "0x:1C", preprocessor.process("0x:1C").toString());
|
||||
Assert.assertEquals( "0x:1C", preprocessor.process(" 0x:1C").toString());
|
||||
Assert.assertEquals( "0x:1C*0x:1C*sin(0x:1C)-0b:1101+√(0x:1C)+exp(0x:1C)", preprocessor.process("0x:1C*0x:1C * sin(0x:1C) - 0b:1101 + √(0x:1C) + exp ( 0x:1C)").toString());
|
||||
Assert.assertEquals( "0x:1C*0x:1C*sin(0x:1C)-0b:1101+√(0x:1C)+exp(0x:1C)", preprocessor.process("0x:1C*0x:1C * sin(0x:1C) - 0b:1101 + √(0x:1C) + exp ( 0x:1C)").toString());
|
||||
|
||||
try {
|
||||
preprocessor.process("ln()");
|
||||
Assert.fail();
|
||||
} catch (CalculatorParseException e) {
|
||||
}
|
||||
try {
|
||||
preprocessor.process("ln()ln()");
|
||||
Assert.fail();
|
||||
} catch (CalculatorParseException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
preprocessor.process("eln()eln()ln()ln()ln()e");
|
||||
Assert.fail();
|
||||
} catch (CalculatorParseException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
preprocessor.process("ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln()))))))))))))))");
|
||||
Assert.fail();
|
||||
} catch (CalculatorParseException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
preprocessor.process("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) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDegrees() throws Exception {
|
||||
final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor();
|
||||
|
||||
Assert.assertEquals( "", preprocessor.process("").toString());
|
||||
/* try {
|
||||
Assert.assertEquals( "π/180", preprocessor.process("°").toString());
|
||||
} catch (ParseException e) {
|
||||
if ( !e.getMessage().startsWith("Could not find start of prefix") ){
|
||||
junit.framework.Assert.fail();
|
||||
}
|
||||
}
|
||||
Assert.assertEquals( "1*π/180", preprocessor.process("1°").toString());
|
||||
Assert.assertEquals( "20.0*π/180", preprocessor.process("20.0°").toString());
|
||||
Assert.assertEquals( "sin(30*π/180)", preprocessor.process("sin(30°)").toString());
|
||||
Assert.assertEquals( "asin(sin(π/6))*π/180", preprocessor.process("asin(sin(π/6))°").toString());
|
||||
Assert.assertEquals( "1*π/180*sin(1)", preprocessor.process("1°sin(1)").toString());
|
||||
try {
|
||||
Assert.assertEquals( "1*π/180^sin(1)", preprocessor.process("1°^sin(1)").toString());
|
||||
junit.framework.Assert.fail();
|
||||
} catch (ParseException e) {
|
||||
if ( !e.getMessage().equals("Power operation after postfix function is currently unsupported!") ) {
|
||||
junit.framework.Assert.fail();
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPostfixFunction() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumeralBases() throws Exception {
|
||||
final ToJsclTextProcessor processor = new ToJsclTextProcessor();
|
||||
|
||||
final NumeralBase defaultNumeralBase = JsclMathEngine.instance.getNumeralBase();
|
||||
try{
|
||||
JsclMathEngine.instance.setNumeralBase(NumeralBase.bin);
|
||||
Assert.assertEquals("101", JsclMathEngine.instance.evaluate("10+11"));
|
||||
|
||||
JsclMathEngine.instance.setNumeralBase(NumeralBase.hex);
|
||||
Assert.assertEquals("56CE+CAD", processor.process("56CE+CAD").getExpression());
|
||||
} finally {
|
||||
JsclMathEngine.instance.setNumeralBase(defaultNumeralBase);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.math.function.IConstant;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/7/11
|
||||
* Time: 7:52 PM
|
||||
*/
|
||||
public class VarTest {
|
||||
|
||||
private static final String xml = "<vars>\n" +
|
||||
" <vars class=\"java.util.ArrayList\">\n" +
|
||||
" <var>\n" +
|
||||
" <name>e</name>\n" +
|
||||
" <value>2.718281828459045</value>\n" +
|
||||
" <system>true</system>\n" +
|
||||
" <description>description</description>\n" +
|
||||
" </var>\n" +
|
||||
" <var>\n" +
|
||||
" <name>;</name>\n" +
|
||||
" <value>3.0</value>\n" +
|
||||
" <system>true</system>\n" +
|
||||
" </var>\n" +
|
||||
" </vars>\n" +
|
||||
"</vars>";
|
||||
|
||||
@Test
|
||||
public void testXml() throws Exception {
|
||||
final Vars vars = new Vars();
|
||||
Var first = new Var.Builder("e", Math.E).setDescription("description").setSystem(true).create();
|
||||
vars.getEntities().add(first);
|
||||
Var second = new Var.Builder(";", 3d).setSystem(true).create();
|
||||
vars.getEntities().add(second);
|
||||
|
||||
final StringWriter sw = new StringWriter();
|
||||
final Serializer serializer = new Persister();
|
||||
serializer.write(vars, sw);
|
||||
|
||||
Assert.assertEquals(xml, sw.toString());
|
||||
|
||||
final Vars result = serializer.read(Vars.class, xml);
|
||||
final IConstant actualFirst = result.getEntities().get(0);
|
||||
final IConstant actualSecond = result.getEntities().get(1);
|
||||
|
||||
areEqual(first, actualFirst);
|
||||
areEqual(second, actualSecond);
|
||||
|
||||
}
|
||||
|
||||
private void areEqual(IConstant expected, IConstant actual) {
|
||||
Assert.assertEquals(expected.getName(), actual.getName());
|
||||
Assert.assertEquals(expected.getDescription(), actual.getDescription());
|
||||
Assert.assertEquals(expected.getValue(), actual.getValue());
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.plot;
|
||||
|
||||
import jscl.math.Expression;
|
||||
import jscl.math.function.Constant;
|
||||
import junit.framework.Assert;
|
||||
import org.achartengine.model.XYSeries;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/5/11
|
||||
* Time: 9:07 PM
|
||||
*/
|
||||
|
||||
public class PlotUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testAddXY() throws Exception {
|
||||
MyXYSeries series = new MyXYSeries("test_01", 100);
|
||||
PlotUtils.addXY(-10, 10, Expression.valueOf("asin(t)"), new Constant("t"), series, new MyXYSeries("test_01_imag"), false, 200);
|
||||
testAscSeries(series);
|
||||
PlotUtils.addXY(-1, 1, Expression.valueOf("asin(t)"), new Constant("t"), series, new MyXYSeries("test_01_imag"), true, 200);
|
||||
testAscSeries(series);
|
||||
|
||||
series = new MyXYSeries("test_02", 1000);
|
||||
PlotUtils.addXY(-10, 10, Expression.valueOf("1/t"), new Constant("t"), series, new MyXYSeries("test_01_imag"), false, 1000);
|
||||
testAscSeries(series);
|
||||
PlotUtils.addXY(-1, 1, Expression.valueOf("1/t"), new Constant("t"), series, new MyXYSeries("test_01_imag"), true, 1000);
|
||||
testAscSeries(series);
|
||||
|
||||
}
|
||||
|
||||
public void testAscSeries(@NotNull XYSeries series) {
|
||||
for ( int i = 0; i < series.getItemCount(); i++ ) {
|
||||
if (i > 1) {
|
||||
Assert.assertTrue(series.getX(i - 1) + " > " +series.getX(i) + " at " + i + " of " + series.getItemCount(), series.getX(i - 1) <= series.getX(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
package org.solovyev.common;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.common.text.Mapper;
|
||||
import org.solovyev.common.text.NumberIntervalMapper;
|
||||
import org.solovyev.common.utils.Interval;
|
||||
import org.solovyev.common.utils.NumberInterval;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/23/11
|
||||
* Time: 2:54 PM
|
||||
*/
|
||||
public class FloatIntervalMapperTest {
|
||||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
final Mapper<Interval<Float>> mapper = new NumberIntervalMapper<Float>(Float.class);
|
||||
|
||||
Assert.assertEquals(new NumberInterval<Float>(1.2f, 12.2f), mapper.parseValue("1.2;12.2"));
|
||||
Assert.assertEquals(new NumberInterval<Float>(0f, 0f), mapper.parseValue("0;0"));
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package org.solovyev.common.math;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.common.utils.MathUtils;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 9/20/11
|
||||
* Time: 5:51 PM
|
||||
*/
|
||||
public class DiscreteNormalizerTest {
|
||||
|
||||
@Test
|
||||
public void testNormalize() throws Exception {
|
||||
DiscreteNormalizer dn = new DiscreteNormalizer(0, 10, 1d);
|
||||
|
||||
Assert.assertTrue(MathUtils.equals(0, dn.normalize(0.5), 2));
|
||||
Assert.assertTrue(MathUtils.equals(0, dn.normalize(0.99), 2));
|
||||
Assert.assertTrue(MathUtils.equals(0.1, dn.normalize(1), 2));
|
||||
Assert.assertTrue(MathUtils.equals(0.1, dn.normalize(1.01), 2));
|
||||
Assert.assertTrue(MathUtils.equals(1, dn.normalize(10), 2));
|
||||
Assert.assertTrue(MathUtils.equals(0.9, dn.normalize(9.99), 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDenormalize() throws Exception {
|
||||
DiscreteNormalizer dn = new DiscreteNormalizer(0, 10, 1d);
|
||||
|
||||
Assert.assertTrue(MathUtils.equals(0, dn.normalize(dn.denormalize(0)), 2));
|
||||
Assert.assertTrue(MathUtils.equals(0.1, dn.normalize(dn.denormalize(0.1)), 2));
|
||||
Assert.assertTrue(MathUtils.equals(1, dn.normalize(dn.denormalize(1)), 2));
|
||||
Assert.assertTrue(MathUtils.equals(0.9, dn.normalize(dn.denormalize(0.9)), 2));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user