This commit is contained in:
Sergey Solovyev 2011-12-15 22:50:05 +04:00
parent 041eafeb37
commit 68a7c2c4c3
10 changed files with 260 additions and 223 deletions

View File

@ -10,10 +10,7 @@ import jscl.MathContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.CalculatorParseException;
import org.solovyev.android.calculator.model.NumberBuilder;
import org.solovyev.android.calculator.model.TextProcessor;
import org.solovyev.android.calculator.model.*;
import org.solovyev.common.utils.MutableObject;
import java.util.HashMap;
@ -27,6 +24,7 @@ import java.util.Map;
public class TextHighlighter implements TextProcessor<TextHighlighter.Result, String> {
private static final Map<String, String> nbFontAttributes = new HashMap<String, String>();
static {
nbFontAttributes.put("color", "#008000");
}
@ -75,11 +73,11 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
private final int colorRed;
private final int colorGreen;
private final int colorBlue;
private final boolean allowScientificFormat;
private final boolean formatNumber;
public TextHighlighter(int baseColor, boolean allowScientificFormat, @NotNull MathContext mathContext) {
public TextHighlighter(int baseColor, boolean formatNumber, @NotNull MathContext mathContext) {
this.color = baseColor;
this.allowScientificFormat = allowScientificFormat;
this.formatNumber = formatNumber;
this.mathContext = mathContext;
//this.colorRed = Color.red(baseColor);
this.colorRed = (baseColor >> 16) & 0xFF;
@ -99,15 +97,24 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
final StringBuilder text1 = new StringBuilder();
int numberOffset = 0;
int resultOffset = 0;
final NumberBuilder numberBuilder = new NumberBuilder(allowScientificFormat, CalculatorEngine.instance.getEngine());
final AbstractNumberBuilder numberBuilder;
if (!formatNumber) {
numberBuilder = new LiteNumberBuilder(CalculatorEngine.instance.getEngine());
} else {
numberBuilder = new NumberBuilder(CalculatorEngine.instance.getEngine());
}
for (int i = 0; i < text.length(); i++) {
MathType.Result mathType = MathType.getType(text, i, numberBuilder.isHexMode());
final MutableObject<Integer> localNumberOffset = new MutableObject<Integer>(0);
numberBuilder.process(text1, mathType, localNumberOffset);
numberOffset += localNumberOffset.getObject();
if (numberBuilder instanceof NumberBuilder) {
final MutableObject<Integer> numberOffset = new MutableObject<Integer>(0);
((NumberBuilder) numberBuilder).process(text1, mathType, numberOffset);
resultOffset += numberOffset.getObject();
} else {
((LiteNumberBuilder) numberBuilder).process(mathType);
}
final String match = mathType.getMatch();
switch (mathType.getMathType()) {
@ -144,9 +151,11 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
}
}
final MutableObject<Integer> localNumberOffset = new MutableObject<Integer>(0);
numberBuilder.processNumber(text1, localNumberOffset);
numberOffset += localNumberOffset.getObject();
if (numberBuilder instanceof NumberBuilder) {
final MutableObject<Integer> numberOffset = new MutableObject<Integer>(0);
((NumberBuilder) numberBuilder).processNumber(text1, numberOffset);
resultOffset += numberOffset.getObject();
}
if (maxNumberOfOpenGroupSymbols > 0) {
@ -165,7 +174,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
result = text1.toString();
}
return new Result(result, numberOffset);
return new Result(result, resultOffset);
}
private int processHighlightedText(@NotNull StringBuilder result, int i, @NotNull String match, @NotNull String tag, @Nullable Map<String, String> tagAttributes) {

View File

@ -16,7 +16,7 @@ import org.solovyev.android.calculator.model.TextProcessor;
public enum JsclOperation {
simplify(new FromJsclSimplifyTextProcessor(CalculatorEngine.instance.getEngine())) {
simplify(new FromJsclSimplifyTextProcessor()) {
@NotNull
@Override
public String evaluate(@NotNull String expression) throws ParseException {

View File

@ -0,0 +1,74 @@
/*
* 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.MathEngine;
import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType;
/**
* User: serso
* Date: 12/15/11
* Time: 9:01 PM
*/
public abstract class AbstractNumberBuilder {
@NotNull
protected final MathEngine engine;
@Nullable
protected StringBuilder numberBuilder = null;
@Nullable
protected NumeralBase nb;
protected AbstractNumberBuilder(@NotNull MathEngine engine) {
this.engine = engine;
this.nb = engine.getNumeralBase();
}
/**
* Method determines if we can continue to process current number
*
* @param mathTypeResult current math type result
* @return true if we can continue of processing of current number, if false - new number should be constructed
*/
protected boolean canContinue(@NotNull MathType.Result mathTypeResult) {
return ((mathTypeResult.getMathType().getGroupType() == MathType.MathGroupType.number && numeralBaseCheck(mathTypeResult) && numeralBaseInTheStart(mathTypeResult.getMathType()) || isSignAfterE(mathTypeResult)));
}
private boolean numeralBaseInTheStart(@NotNull MathType mathType) {
return mathType != MathType.numeral_base || numberBuilder == null;
}
private boolean numeralBaseCheck(@NotNull MathType.Result mathType) {
return mathType.getMathType() != MathType.digit || getNumeralBase().getAcceptableCharacters().contains(mathType.getMatch().charAt(0));
}
private boolean isSignAfterE(@NotNull MathType.Result mathTypeResult) {
if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) {
final StringBuilder localNb = numberBuilder;
if (localNb != null && localNb.length() > 0) {
if (localNb.charAt(localNb.length() - 1) == MathType.POWER_10) {
return true;
}
}
}
return false;
}
public boolean isHexMode() {
return nb == NumeralBase.hex || (nb == null && engine.getNumeralBase() == NumeralBase.hex);
}
@NotNull
protected NumeralBase getNumeralBase() {
return nb == null ? engine.getNumeralBase() : nb;
}
}

View File

@ -1,6 +1,5 @@
package org.solovyev.android.calculator.model;
import jscl.MathContext;
import jscl.math.Generic;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -16,35 +15,17 @@ import java.util.List;
*/
public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Generic> {
@NotNull
private final MathContext mathContext;
public FromJsclSimplifyTextProcessor(@NotNull MathContext mathContext) {
this.mathContext = mathContext;
public FromJsclSimplifyTextProcessor() {
}
@NotNull
@Override
public String process(@NotNull Generic from) throws CalculatorParseException {
final String s = from.toString();
return process(s);
return removeMultiplicationSigns(from.toString());
}
public String process(@NotNull String s) {
final StringBuilder sb = new StringBuilder();
final NumberBuilder nb = new NumberBuilder(false, CalculatorEngine.instance.getEngine());
for (int i = 0; i < s.length(); i++) {
final MathType.Result mathTypeResult = MathType.getType(s, i, nb.isHexMode());
nb.process(sb, mathTypeResult, null);
i = mathTypeResult.processFromJscl(sb, i);
}
nb.processNumber(sb, null);
return removeMultiplicationSigns(sb.toString());
return removeMultiplicationSigns(s);
}
@NotNull

View File

@ -0,0 +1,55 @@
/*
* 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.MathEngine;
import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.math.MathType;
/**
* User: serso
* Date: 12/15/11
* Time: 8:33 PM
*/
public class LiteNumberBuilder extends AbstractNumberBuilder {
public LiteNumberBuilder(@NotNull MathEngine engine) {
super(engine);
this.nb = engine.getNumeralBase();
}
public void process(@NotNull MathType.Result mathTypeResult) {
if (canContinue(mathTypeResult)) {
// let's continue building number
if (numberBuilder == null) {
// if new number => create new builder
numberBuilder = new StringBuilder();
}
if (mathTypeResult.getMathType() != MathType.numeral_base) {
// just add matching string
numberBuilder.append(mathTypeResult.getMatch());
} else {
// set explicitly numeral base (do not include it into number)
nb = NumeralBase.getByPrefix(mathTypeResult.getMatch());
}
} else {
// process current number (and go to the next one)
if (numberBuilder != null) {
numberBuilder = null;
// must set default numeral base (exit numeral base mode)
nb = engine.getNumeralBase();
}
}
}
}

View File

@ -27,23 +27,10 @@ import java.util.List;
* Date: 10/23/11
* Time: 2:57 PM
*/
public class NumberBuilder {
public class NumberBuilder extends AbstractNumberBuilder {
@NotNull
private final MathEngine engine;
@Nullable
private StringBuilder numberBuilder = null;
private final boolean allowScientificFormat;
@Nullable
private NumeralBase nb;
public NumberBuilder(boolean allowScientificFormat, @NotNull MathEngine engine) {
this.allowScientificFormat = allowScientificFormat;
this.nb = engine.getNumeralBase();
this.engine = engine;
public NumberBuilder(@NotNull MathEngine engine) {
super(engine);
}
/**
@ -83,48 +70,6 @@ public class NumberBuilder {
return possibleResult == null ? mathTypeResult : possibleResult;
}
/**
* Method determines if we can continue to process current number
* @param mathTypeResult current math type result
*
* @return true if we can continue of processing of current number, if false - new number should be constructed
*/
private boolean canContinue(@NotNull MathType.Result mathTypeResult) {
return ((mathTypeResult.getMathType().getGroupType() == MathType.MathGroupType.number && numeralBaseCheck(mathTypeResult) && numeralBaseInTheStart(mathTypeResult.getMathType()) || isSignAfterE(mathTypeResult)));
}
private boolean numeralBaseInTheStart(@NotNull MathType mathType) {
return mathType != MathType.numeral_base || numberBuilder == null;
}
private boolean numeralBaseCheck(@NotNull MathType.Result mathType) {
if (mathType.getMathType() == MathType.digit) {
final Character ch = mathType.getMatch().charAt(0);
if (NumeralBase.hex.getAcceptableCharacters().contains(ch) && !NumeralBase.dec.getAcceptableCharacters().contains(ch)) {
if (nb == NumeralBase.hex) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return true;
}
}
private boolean isSignAfterE(@NotNull MathType.Result mathTypeResult) {
if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) {
if (numberBuilder != null && numberBuilder.length() > 0) {
if (numberBuilder.charAt(numberBuilder.length() - 1) == MathType.POWER_10) {
return true;
}
}
}
return false;
}
/**
* Method replaces number in text according to some rules (e.g. formatting)
*
@ -172,7 +117,7 @@ public class NumberBuilder {
nb = engine.getNumeralBase();
}
return replaceNumberInText(text, number, trimmedChars, offset, localNb, allowScientificFormat, engine);
return replaceNumberInText(text, number, trimmedChars, offset, localNb, engine);
}
@Nullable
@ -181,7 +126,6 @@ public class NumberBuilder {
int trimmedChars,
@Nullable MutableObject<Integer> offset,
@NotNull NumeralBase nb,
boolean allowScientificFormat,
@NotNull final MathEngine engine) {
MathType.Result result = null;
@ -205,7 +149,7 @@ public class NumberBuilder {
text.append(constant.getName());
result = new MathType.Result(MathType.constant, constant.getName());
} else {
final String newNumber = formatNumber(number, nb, allowScientificFormat, engine);
final String newNumber = formatNumber(number, nb, engine);
if (offset != null) {
// register offset between old number and new number
offset.setObject(newNumber.length() - oldNumberLength);
@ -218,10 +162,9 @@ public class NumberBuilder {
}
@NotNull
private static String formatNumber(@NotNull String number, @NotNull NumeralBase nb, boolean allowScientificFormat, @NotNull MathEngine engine) {
private static String formatNumber(@NotNull String number, @NotNull NumeralBase nb, @NotNull MathEngine engine) {
String result;
if (allowScientificFormat) {
int indexOfDot = number.indexOf('.');
if (indexOfDot < 0) {
@ -232,46 +175,29 @@ public class NumberBuilder {
indexOfE = number.indexOf(MathType.POWER_10);
}
if (indexOfE < 0) {
result = toString(number, nb, engine);
result = engine.addGroupingSeparators(nb, number);
} else {
final String part;
if (indexOfDot != 0) {
part = toString(number.substring(0, indexOfE), nb, engine);
final String partBeforeE;
if (indexOfE != 0) {
partBeforeE = engine.addGroupingSeparators(nb, number.substring(0, indexOfE));
} else {
part = "";
partBeforeE = "";
}
result = part + number.substring(indexOfE);
result = partBeforeE + number.substring(indexOfE);
}
} else {
final String integerPart;
if (indexOfDot != 0) {
integerPart = toString(number.substring(0, indexOfDot), nb, engine);
integerPart = engine.addGroupingSeparators(nb, number.substring(0, indexOfDot));
} else {
integerPart = "";
}
result = integerPart + number.substring(indexOfDot);
}
} else {
result = toString(number, nb, engine);
}
return result;
}
@NotNull
private static String toString(@NotNull String value, @NotNull NumeralBase nb, @NotNull MathContext mathContext) {
return mathContext.format(toDouble(value, nb, mathContext), nb);
}
public boolean isHexMode() {
return nb == NumeralBase.hex || (nb == null && engine.getNumeralBase() == NumeralBase.hex);
}
@NotNull
private NumeralBase getNumeralBase() {
return nb == null ? engine.getNumeralBase() : nb;
}
@NotNull
private static Double toDouble(@NotNull String s, @NotNull NumeralBase nb, @NotNull final MathContext mc) throws NumberFormatException {
final NumeralBase defaultNb = mc.getNumeralBase();

View File

@ -8,7 +8,6 @@ package org.solovyev.android.calculator.model;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.StartsWithFinder;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.common.utils.CollectionsUtils;
@ -36,11 +35,9 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
final StringBuilder result = new StringBuilder();
MathType.Result mathTypeResult = null;
MathType.Result mathTypeBefore = null;
MathType.Result mathTypeBefore;
final StringBuilder sb = new StringBuilder(s);
final NumberBuilder nb = new NumberBuilder(false, CalculatorEngine.instance.getEngine());
final LiteNumberBuilder nb = new LiteNumberBuilder(CalculatorEngine.instance.getEngine());
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') continue;
startsWithFinder.setI(i);
@ -49,7 +46,7 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
mathTypeResult = MathType.getType(s, i, nb.isHexMode());
nb.process(sb, mathTypeResult, null);
nb.process(mathTypeResult);
if (mathTypeBefore != null) {
@ -132,8 +129,4 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
return new PreparedExpression(result.toString(), undefinedVars);
}
public static String wrap(@NotNull JsclOperation operation, @NotNull String s) {
return operation.name() + "(\"" + s + "\");";
}
}

View File

@ -45,10 +45,10 @@ public class TextHighlighterTest {
Assert.assertEquals("<font color=\"#000000\"></font>)(((())())", textHighlighter.process(")(((())())").toString());
Assert.assertEquals(")", textHighlighter.process(")").toString());
Assert.assertEquals(")()(", textHighlighter.process(")()(").toString());
Assert.assertEquals("1 000 000", textHighlighter.process("1000000").toString());
Assert.assertEquals("1 000 000", textHighlighter.process("1000000").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());
@ -56,6 +56,10 @@ public class TextHighlighterTest {
Assert.assertEquals("22x", textHighlighter.process("22x").toString());
Assert.assertEquals("20t", textHighlighter.process("20t").toString());
Assert.assertEquals("20k", textHighlighter.process("20k").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());
@ -79,10 +83,10 @@ public class TextHighlighterTest {
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", textHighlighter.process("0x:6F.2").toString());
Assert.assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:6F.B").toString());
Assert.assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:006F.B").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());
@ -92,23 +96,23 @@ public class TextHighlighterTest {
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(".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", textHighlighter.process("6F.2").toString());
Assert.assertEquals("6F", textHighlighter.process("6F.B").toString());
Assert.assertEquals("6F", textHighlighter.process("006F.B").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>11 0101", textHighlighter.process("0b:110101.").toString());
Assert.assertEquals("<b>0b:</b>11 0101", textHighlighter.process("0b:110101.101").toString());
Assert.assertEquals("<b>0b:</b>1101 0100", textHighlighter.process("0b:11010100.1").toString());
Assert.assertEquals("<b>0b:</b>11 0101", textHighlighter.process("0b:110101.0").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());
@ -116,10 +120,10 @@ public class TextHighlighterTest {
try {
me.setNumeralBase(NumeralBase.bin);
Assert.assertEquals("110101", textHighlighter.process("110101").toString());
Assert.assertEquals("11 0101", textHighlighter.process("110101.").toString());
Assert.assertEquals("11 0101", textHighlighter.process("110101.101").toString());
Assert.assertEquals("1101 0100", textHighlighter.process("11010100.1").toString());
Assert.assertEquals("11 0101", textHighlighter.process("110101.0").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());

View File

@ -300,6 +300,7 @@ public class CalculatorEngineTest {
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));
@ -344,36 +345,6 @@ public class CalculatorEngineTest {
}
@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, "").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());
try {
Assert.assertEquals("∂(cos(t), t, t,1°)", cm.evaluate(JsclOperation.numeric, "∂(cos(t),t,t,1°)").getResult());
fail();
} catch (CalculatorParseException e) {
}
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 testNumeralSystems() throws Exception {
@ -409,4 +380,31 @@ public class CalculatorEngineTest {
}
}
@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, "").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);
}
}
}

View File

@ -1,6 +1,5 @@
package org.solovyev.android.calculator.model;
import jscl.JsclMathEngine;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@ -21,23 +20,23 @@ public class FromJsclSimplifyTextProcessorTest {
@Test
public void testProcess() throws Exception {
FromJsclSimplifyTextProcessor tp = new FromJsclSimplifyTextProcessor(JsclMathEngine.instance);
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))"));
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"));
//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.getVarsRegister().add(new Var.Builder("t2.718281828459045", "2"));
CalculatorEngine.instance.getVarsRegister().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("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"));
@ -66,7 +65,5 @@ public class FromJsclSimplifyTextProcessorTest {
Assert.assertEquals("20x", tp.process("20x"));
Assert.assertEquals("2×0x3", tp.process("2*0x3"));
Assert.assertEquals("2×0x:3", tp.process("2*0x:3"));
Assert.assertEquals("0x:3 00 00 00", tp.process("0x:3 000 000.00000000000001"));
}
}