Percent test

This commit is contained in:
serso 2016-04-27 20:59:31 +02:00
parent 5315679add
commit 408847f985
3 changed files with 109 additions and 105 deletions

View File

@ -23,7 +23,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.TextUtils; import com.google.common.base.Strings;
import jscl.NumeralBase; import jscl.NumeralBase;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
@ -60,7 +60,7 @@ public abstract class BaseNumberBuilder {
} }
private boolean spaceBefore(@Nonnull MathType.Result mathTypeResult) { private boolean spaceBefore(@Nonnull MathType.Result mathTypeResult) {
return numberBuilder == null && TextUtils.isEmpty(mathTypeResult.match.trim()); return numberBuilder == null && Strings.isNullOrEmpty(mathTypeResult.match.trim());
} }
private boolean numeralBaseInTheStart(@Nonnull MathType mathType) { private boolean numeralBaseInTheStart(@Nonnull MathType mathType) {

View File

@ -24,10 +24,12 @@ package org.solovyev.android.calculator;
import jscl.JsclMathEngine; import jscl.JsclMathEngine;
import jscl.NumeralBase; import jscl.NumeralBase;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class ToJsclTextProcessorTest { public class ToJsclTextProcessorTest {
private ToJsclTextProcessor preprocessor; private ToJsclTextProcessor preprocessor;
@ -40,84 +42,84 @@ public class ToJsclTextProcessorTest {
@Test @Test
public void testSpecialCases() throws ParseException { public void testSpecialCases() throws ParseException {
Assert.assertEquals("3^E10", preprocessor.process("3^E10").toString()); assertEquals("3^E10", preprocessor.process("3^E10").toString());
} }
@Test @Test
public void testProcess() throws Exception { public void testProcess() throws Exception {
Assert.assertEquals("", preprocessor.process("").toString()); assertEquals("", preprocessor.process("").toString());
Assert.assertEquals("()", preprocessor.process("[]").toString()); assertEquals("()", preprocessor.process("[]").toString());
Assert.assertEquals("()*()", preprocessor.process("[][]").toString()); assertEquals("()*()", preprocessor.process("[][]").toString());
Assert.assertEquals("()*(1)", preprocessor.process("[][1]").toString()); assertEquals("()*(1)", preprocessor.process("[][1]").toString());
Assert.assertEquals("(0)*(1)", preprocessor.process("[0][1]").toString()); assertEquals("(0)*(1)", preprocessor.process("[0][1]").toString());
Assert.assertEquals("(0)*(1E)", preprocessor.process("[0][1E]").toString()); assertEquals("(0)*(1E)", preprocessor.process("[0][1E]").toString());
Assert.assertEquals("(0)*(1E1)", preprocessor.process("[0][1E1]").toString()); assertEquals("(0)*(1E1)", preprocessor.process("[0][1E1]").toString());
Assert.assertEquals("(0)*(1E-1)", preprocessor.process("[0][1E-1]").toString()); assertEquals("(0)*(1E-1)", preprocessor.process("[0][1E-1]").toString());
Assert.assertEquals("(0)*(1.E-1)", preprocessor.process("[0][1.E-1]").toString()); 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()); 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()); 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()); 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()); 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()); 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()); 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()); 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()); 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()); 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()); 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()); 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()); assertEquals("E2", preprocessor.process("E2").toString());
Assert.assertEquals("E-2", preprocessor.process("E-2").toString()); assertEquals("E-2", preprocessor.process("E-2").toString());
Assert.assertEquals("E-1/2", preprocessor.process("E-1/2").toString()); assertEquals("E-1/2", preprocessor.process("E-1/2").toString());
Assert.assertEquals("E-1.2", preprocessor.process("E-1.2").toString()); assertEquals("E-1.2", preprocessor.process("E-1.2").toString());
Assert.assertEquals("E+1.2", preprocessor.process("E+1.2").toString()); assertEquals("E+1.2", preprocessor.process("E+1.2").toString());
Assert.assertEquals("E(-1.2)", preprocessor.process("E(-1.2)").toString()); assertEquals("E(-1.2)", preprocessor.process("E(-1.2)").toString());
Assert.assertEquals("EE", preprocessor.process("EE").toString()); assertEquals("EE", preprocessor.process("EE").toString());
try { try {
preprocessor.engine.getMathEngine().setNumeralBase(NumeralBase.hex); preprocessor.engine.getMathEngine().setNumeralBase(NumeralBase.hex);
Assert.assertEquals("22F*exp(F)", preprocessor.process("22Fexp(F)").toString()); assertEquals("22F*exp(F)", preprocessor.process("22Fexp(F)").toString());
} finally { } finally {
preprocessor.engine.getMathEngine().setNumeralBase(NumeralBase.dec); preprocessor.engine.getMathEngine().setNumeralBase(NumeralBase.dec);
} }
Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:ABCDEF").toString()); assertEquals("0x:ABCDEF", preprocessor.process("0x:ABCDEF").toString());
Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString()); assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString());
Assert.assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString()); assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString());
Assert.assertEquals("0x:ABCDEF*0*x", preprocessor.process("0x:A BC DEF*0x").toString()); 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()); 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()); 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()); assertEquals("0x:ABCDEF001*c", preprocessor.process("0x:A BC DEF001*c").toString());
Assert.assertEquals("0b:1101", preprocessor.process("0b:1101").toString()); assertEquals("0b:1101", preprocessor.process("0b:1101").toString());
Assert.assertEquals("0x:1C", preprocessor.process("0x:1C").toString()); assertEquals("0x:1C", preprocessor.process("0x:1C").toString());
Assert.assertEquals("0x:1C", preprocessor.process(" 0x:1C").toString()); 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()); 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()); 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 { try {
preprocessor.process("ln()"); preprocessor.process("ln()");
Assert.fail(); fail();
} catch (ParseException ignored) { } catch (ParseException ignored) {
} }
try { try {
preprocessor.process("ln()ln()"); preprocessor.process("ln()ln()");
Assert.fail(); fail();
} catch (ParseException ignored) { } catch (ParseException ignored) {
} }
try { try {
preprocessor.process("eln()eln()ln()ln()ln()e"); preprocessor.process("eln()eln()ln()ln()ln()e");
Assert.fail(); fail();
} catch (ParseException ignored) { } catch (ParseException ignored) {
} }
try { try {
preprocessor.process("ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln()))))))))))))))"); preprocessor.process("ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln()))))))))))))))");
Assert.fail(); fail();
} catch (ParseException ignored) { } catch (ParseException ignored) {
} }
try { 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())))))))))))))))))))))))))))))))))))))"); 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(); fail();
} catch (ParseException ignored) { } catch (ParseException ignored) {
} }
} }
@ -131,12 +133,17 @@ public class ToJsclTextProcessorTest {
final NumeralBase defaultNumeralBase = JsclMathEngine.getInstance().getNumeralBase(); final NumeralBase defaultNumeralBase = JsclMathEngine.getInstance().getNumeralBase();
try { try {
JsclMathEngine.getInstance().setNumeralBase(NumeralBase.bin); JsclMathEngine.getInstance().setNumeralBase(NumeralBase.bin);
Assert.assertEquals("101", JsclMathEngine.getInstance().evaluate("10+11")); assertEquals("101", JsclMathEngine.getInstance().evaluate("10+11"));
JsclMathEngine.getInstance().setNumeralBase(NumeralBase.hex); JsclMathEngine.getInstance().setNumeralBase(NumeralBase.hex);
Assert.assertEquals("56CE+CAD", preprocessor.process("56CE+CAD").getValue()); assertEquals("56CE+CAD", preprocessor.process("56CE+CAD").getValue());
} finally { } finally {
JsclMathEngine.getInstance().setNumeralBase(defaultNumeralBase); JsclMathEngine.getInstance().setNumeralBase(defaultNumeralBase);
} }
} }
@Test
public void testPercents() throws Exception {
assertEquals("100+100%*100", preprocessor.process("100+100%100").toString());
}
} }

View File

@ -2,81 +2,78 @@ package jscl.math.operator;
import jscl.JsclMathEngine; import jscl.JsclMathEngine;
import jscl.text.ParseException; import jscl.text.ParseException;
import junit.framework.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/** import static org.junit.Assert.assertEquals;
* User: serso
* Date: 11/14/11
* Time: 2:10 PM
*/
public class PercentTest { public class PercentTest {
@Test @Test
public void testNumeric() throws Exception { public void testNumeric() throws Exception {
final JsclMathEngine mathEngine = JsclMathEngine.getInstance(); final JsclMathEngine me = new JsclMathEngine();
Assert.assertEquals("150", mathEngine.evaluate("100+50%")); assertEquals("150", me.evaluate("100+50%"));
Assert.assertEquals("0", mathEngine.evaluate("100-100%")); assertEquals("0", me.evaluate("100-100%"));
Assert.assertEquals("50", mathEngine.evaluate("100*50%")); assertEquals("50", me.evaluate("100*50%"));
Assert.assertEquals("150", mathEngine.evaluate("100+100*50%")); assertEquals("150", me.evaluate("100+100*50%"));
Assert.assertEquals("125", mathEngine.evaluate("100+100*50%*50%")); assertEquals("125", me.evaluate("100+100*50%*50%"));
Assert.assertEquals("125", mathEngine.evaluate("100+100*50%*(25+25)%")); assertEquals("125", me.evaluate("100+100*50%*(25+25)%"));
Assert.assertEquals("250", mathEngine.evaluate("100+100*50%*(25+25)%+100%")); assertEquals("250", me.evaluate("100+100*50%*(25+25)%+100%"));
Assert.assertEquals("150", mathEngine.evaluate("100+(100*50%*(25+25)%+100%)")); assertEquals("150", me.evaluate("100+(100*50%*(25+25)%+100%)"));
Assert.assertEquals("140", mathEngine.evaluate("100+(20+20)%")); assertEquals("140", me.evaluate("100+(20+20)%"));
// todo serso: think about such behaviour assertEquals("124", me.evaluate("100+(20%+20%)"));
Assert.assertEquals("124", mathEngine.evaluate("100+(20%+20%)"));
Assert.assertEquals("100+50%-50%", mathEngine.simplify("100+50%-50%")); assertEquals("100+50%-50%", me.simplify("100+50%-50%"));
Assert.assertEquals("100+(100*50%*(50)%+100%)", mathEngine.simplify("100+(100*50%*(25+25)%+100%)")); assertEquals("100+(100*50%*(50)%+100%)", me.simplify("100+(100*50%*(25+25)%+100%)"));
assertEquals("450", me.evaluate("((100+100*50%)+50%)*200%"));
assertEquals("150", me.evaluate("((100+100*50%)*50%)+100%"));
assertEquals("150", me.evaluate("100*50%+100"));
assertEquals("75", me.evaluate("100+50%-50%"));
assertEquals("75", me.evaluate("100+50%+(-50%)"));
assertEquals("0", me.evaluate("0+(-50%)"));
assertEquals("0", me.evaluate("0+(50%)"));
assertEquals("0", me.evaluate("0+50%"));
assertEquals("-150", me.evaluate("-100+50%"));
assertEquals("-148.5", me.evaluate("1-100+50%"));
assertEquals("-49.5", me.evaluate("1-100-50%"));
assertEquals("-49.5", me.evaluate("(1-100)-50%"));
assertEquals("-49", me.evaluate("1-(100-50%)"));
assertEquals("50", me.evaluate("100-50%"));
assertEquals("2600", me.evaluate("100+50%^2"));
assertEquals("101.0813826568003", me.evaluate("100+50^2%"));
assertEquals("22500", me.evaluate("(100+50%)^2"));
assertEquals("225", me.evaluate("(100+50%)+50%"));
assertEquals("225", me.evaluate("(100+50%)+(abs(-50)+10-10)%"));
Assert.assertEquals("450", mathEngine.evaluate("((100+100*50%)+50%)*200%")); assertEquals("0", me.evaluate("100-(10+2*40+10)%"));
Assert.assertEquals("150", mathEngine.evaluate("((100+100*50%)*50%)+100%")); assertEquals("3", me.evaluate("100-(10+2*40+10)%+3"));
Assert.assertEquals("150", mathEngine.evaluate("100*50%+100"));
Assert.assertEquals("75", mathEngine.evaluate("100+50%-50%"));
Assert.assertEquals("75", mathEngine.evaluate("100+50%+(-50%)"));
Assert.assertEquals("0", mathEngine.evaluate("0+(-50%)"));
Assert.assertEquals("0", mathEngine.evaluate("0+(50%)"));
Assert.assertEquals("0", mathEngine.evaluate("0+50%"));
Assert.assertEquals("-150", mathEngine.evaluate("-100+50%"));
Assert.assertEquals("-148.5", mathEngine.evaluate("1-100+50%"));
Assert.assertEquals("-49.5", mathEngine.evaluate("1-100-50%"));
Assert.assertEquals("-49.5", mathEngine.evaluate("(1-100)-50%"));
Assert.assertEquals("-49", mathEngine.evaluate("1-(100-50%)"));
Assert.assertEquals("50", mathEngine.evaluate("100-50%"));
Assert.assertEquals("2600", mathEngine.evaluate("100+50%^2"));
Assert.assertEquals("101.0813826568003", mathEngine.evaluate("100+50^2%"));
Assert.assertEquals("22500", mathEngine.evaluate("(100+50%)^2"));
Assert.assertEquals("225", mathEngine.evaluate("(100+50%)+50%"));
Assert.assertEquals("225", mathEngine.evaluate("(100+50%)+(abs(-50)+10-10)%"));
Assert.assertEquals("0", mathEngine.evaluate("100-(10+2*40+10)%")); assertEquals("0", me.evaluate("100-(200/2)%"));
Assert.assertEquals("3", mathEngine.evaluate("100-(10+2*40+10)%+3")); assertEquals("3", me.evaluate("100-(200/2)%+3"));
Assert.assertEquals("0", mathEngine.evaluate("100-(200/2)%")); assertEquals("99", me.evaluate("100-2*50%"));
Assert.assertEquals("3", mathEngine.evaluate("100-(200/2)%+3")); assertEquals("102", me.evaluate("100-2*50%+3"));
Assert.assertEquals("99", mathEngine.evaluate("100-2*50%")); assertEquals("84", me.evaluate("20+2^3!"));
Assert.assertEquals("102", mathEngine.evaluate("100-2*50%+3")); assertEquals("21.0471285480509", me.evaluate("20+10^2%"));
assertEquals("20.48", me.evaluate("20+4!*2%"));
Assert.assertEquals("84", mathEngine.evaluate("20+2^3!")); assertEquals("120", me.evaluate("100-20+50%"));
Assert.assertEquals("21.0471285480509", mathEngine.evaluate("20+10^2%"));
Assert.assertEquals("20.48", mathEngine.evaluate("20+4!*2%"));
Assert.assertEquals("120", mathEngine.evaluate("100-20+50%"));
try { try {
mathEngine.evaluate("+50%"); me.evaluate("+50%");
Assert.fail(); Assert.fail();
} catch (ParseException e) { } catch (ParseException e) {
} }
Assert.assertEquals("0.5", mathEngine.evaluate("50%")); assertEquals("0.5", me.evaluate("50%"));
Assert.assertEquals("-0.5", mathEngine.evaluate("-50%")); assertEquals("-0.5", me.evaluate("-50%"));
Assert.assertEquals("225", mathEngine.evaluate("(100+50%)+50%")); assertEquals("225", me.evaluate("(100+50%)+50%"));
// undefined behavior, percent function always uses preceding number ignoring multiplier after
assertEquals("10100", me.evaluate("100+100%*100"));
} }
} }