From 408847f9859e668527e70989b1712dd28e8f75a2 Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 27 Apr 2016 20:59:31 +0200 Subject: [PATCH] Percent test --- .../android/calculator/BaseNumberBuilder.java | 4 +- .../calculator/ToJsclTextProcessorTest.java | 105 ++++++++++-------- .../java/jscl/math/operator/PercentTest.java | 105 +++++++++--------- 3 files changed, 109 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java b/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java index 6b4b51d6..de8783a1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseNumberBuilder.java @@ -23,7 +23,7 @@ package org.solovyev.android.calculator; import android.text.SpannableStringBuilder; -import android.text.TextUtils; +import com.google.common.base.Strings; import jscl.NumeralBase; import org.solovyev.android.calculator.math.MathType; @@ -60,7 +60,7 @@ public abstract class BaseNumberBuilder { } 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) { diff --git a/app/src/test/java/org/solovyev/android/calculator/ToJsclTextProcessorTest.java b/app/src/test/java/org/solovyev/android/calculator/ToJsclTextProcessorTest.java index b261c215..38acd2e5 100644 --- a/app/src/test/java/org/solovyev/android/calculator/ToJsclTextProcessorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/ToJsclTextProcessorTest.java @@ -24,10 +24,12 @@ package org.solovyev.android.calculator; import jscl.JsclMathEngine; import jscl.NumeralBase; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + public class ToJsclTextProcessorTest { private ToJsclTextProcessor preprocessor; @@ -40,84 +42,84 @@ public class ToJsclTextProcessorTest { @Test public void testSpecialCases() throws ParseException { - Assert.assertEquals("3^E10", preprocessor.process("3^E10").toString()); + assertEquals("3^E10", preprocessor.process("3^E10").toString()); } @Test public void testProcess() throws Exception { - 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()); + assertEquals("", preprocessor.process("").toString()); + assertEquals("()", preprocessor.process("[]").toString()); + assertEquals("()*()", preprocessor.process("[][]").toString()); + assertEquals("()*(1)", preprocessor.process("[][1]").toString()); + assertEquals("(0)*(1)", preprocessor.process("[0][1]").toString()); + assertEquals("(0)*(1E)", preprocessor.process("[0][1E]").toString()); + assertEquals("(0)*(1E1)", preprocessor.process("[0][1E1]").toString()); + assertEquals("(0)*(1E-1)", preprocessor.process("[0][1E-1]").toString()); + assertEquals("(0)*(1.E-1)", preprocessor.process("[0][1.E-1]").toString()); + assertEquals("(0)*(2*E-1)", preprocessor.process("[0][2*E-1]").toString()); + assertEquals("(0)*ln(1)*(2*E-1)", preprocessor.process("[0]ln(1)[2*E-1]").toString()); + assertEquals("sin(4)*asin(0.5)*√(2)", preprocessor.process("sin(4)asin(0.5)√(2)").toString()); + assertEquals("sin(4)*cos(5)", preprocessor.process("sin(4)cos(5)").toString()); + assertEquals("π*sin(4)*π*cos(√(5))", preprocessor.process("πsin(4)πcos(√(5))").toString()); + assertEquals("π*sin(4)+π*cos(√(5))", preprocessor.process("πsin(4)+πcos(√(5))").toString()); + assertEquals("π*sin(4)+π*cos(√(5+(√(-1))))", preprocessor.process("πsin(4)+πcos(√(5+i))").toString()); + assertEquals("π*sin(4.01)+π*cos(√(5+(√(-1))))", preprocessor.process("π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()); + 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))))E-2", preprocessor.process("e^πsin(4.01)+πcos(√(5+i))E-2").toString()); + assertEquals("E2", preprocessor.process("E2").toString()); + assertEquals("E-2", preprocessor.process("E-2").toString()); + assertEquals("E-1/2", preprocessor.process("E-1/2").toString()); + assertEquals("E-1.2", preprocessor.process("E-1.2").toString()); + assertEquals("E+1.2", preprocessor.process("E+1.2").toString()); + assertEquals("E(-1.2)", preprocessor.process("E(-1.2)").toString()); + assertEquals("EE", preprocessor.process("EE").toString()); try { 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 { preprocessor.engine.getMathEngine().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()); + assertEquals("0x:ABCDEF", preprocessor.process("0x:ABCDEF").toString()); + assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString()); + assertEquals("0x:ABCDEF", preprocessor.process("0x:A BC DEF").toString()); + assertEquals("0x:ABCDEF*0*x", preprocessor.process("0x:A BC DEF*0x").toString()); + assertEquals("0x:ABCDEF001*0*x", preprocessor.process("0x:A BC DEF001*0x").toString()); + assertEquals("0x:ABCDEF001*0*c", preprocessor.process("0x:A BC DEF001*0c").toString()); + assertEquals("0x:ABCDEF001*c", preprocessor.process("0x:A BC DEF001*c").toString()); + assertEquals("0b:1101", preprocessor.process("0b:1101").toString()); + assertEquals("0x:1C", preprocessor.process("0x:1C").toString()); + assertEquals("0x:1C", preprocessor.process(" 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()); + 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(); + fail(); } catch (ParseException ignored) { } try { preprocessor.process("ln()ln()"); - Assert.fail(); + fail(); } catch (ParseException ignored) { } try { preprocessor.process("eln()eln()ln()ln()ln()e"); - Assert.fail(); + fail(); } catch (ParseException ignored) { } try { preprocessor.process("ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln()))))))))))))))"); - Assert.fail(); + fail(); } catch (ParseException ignored) { } 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(); + fail(); } catch (ParseException ignored) { } } @@ -131,12 +133,17 @@ public class ToJsclTextProcessorTest { final NumeralBase defaultNumeralBase = JsclMathEngine.getInstance().getNumeralBase(); try { 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); - Assert.assertEquals("56CE+CAD", preprocessor.process("56CE+CAD").getValue()); + assertEquals("56CE+CAD", preprocessor.process("56CE+CAD").getValue()); } finally { JsclMathEngine.getInstance().setNumeralBase(defaultNumeralBase); } } + + @Test + public void testPercents() throws Exception { + assertEquals("100+100%*100", preprocessor.process("100+100%100").toString()); + } } diff --git a/jscl/src/test/java/jscl/math/operator/PercentTest.java b/jscl/src/test/java/jscl/math/operator/PercentTest.java index 86755ab3..46cc202b 100644 --- a/jscl/src/test/java/jscl/math/operator/PercentTest.java +++ b/jscl/src/test/java/jscl/math/operator/PercentTest.java @@ -2,81 +2,78 @@ package jscl.math.operator; import jscl.JsclMathEngine; import jscl.text.ParseException; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Test; -/** - * User: serso - * Date: 11/14/11 - * Time: 2:10 PM - */ +import static org.junit.Assert.assertEquals; + public class PercentTest { @Test public void testNumeric() throws Exception { - final JsclMathEngine mathEngine = JsclMathEngine.getInstance(); + final JsclMathEngine me = new JsclMathEngine(); - Assert.assertEquals("150", mathEngine.evaluate("100+50%")); - Assert.assertEquals("0", mathEngine.evaluate("100-100%")); - Assert.assertEquals("50", mathEngine.evaluate("100*50%")); - Assert.assertEquals("150", mathEngine.evaluate("100+100*50%")); - Assert.assertEquals("125", mathEngine.evaluate("100+100*50%*50%")); - Assert.assertEquals("125", mathEngine.evaluate("100+100*50%*(25+25)%")); - Assert.assertEquals("250", mathEngine.evaluate("100+100*50%*(25+25)%+100%")); - Assert.assertEquals("150", mathEngine.evaluate("100+(100*50%*(25+25)%+100%)")); - Assert.assertEquals("140", mathEngine.evaluate("100+(20+20)%")); - // todo serso: think about such behaviour - Assert.assertEquals("124", mathEngine.evaluate("100+(20%+20%)")); + assertEquals("150", me.evaluate("100+50%")); + assertEquals("0", me.evaluate("100-100%")); + assertEquals("50", me.evaluate("100*50%")); + assertEquals("150", me.evaluate("100+100*50%")); + assertEquals("125", me.evaluate("100+100*50%*50%")); + assertEquals("125", me.evaluate("100+100*50%*(25+25)%")); + assertEquals("250", me.evaluate("100+100*50%*(25+25)%+100%")); + assertEquals("150", me.evaluate("100+(100*50%*(25+25)%+100%)")); + assertEquals("140", me.evaluate("100+(20+20)%")); + assertEquals("124", me.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%")); - Assert.assertEquals("150", mathEngine.evaluate("((100+100*50%)*50%)+100%")); - 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)%")); + assertEquals("0", me.evaluate("100-(10+2*40+10)%")); + assertEquals("3", me.evaluate("100-(10+2*40+10)%+3")); - Assert.assertEquals("0", mathEngine.evaluate("100-(10+2*40+10)%")); - Assert.assertEquals("3", mathEngine.evaluate("100-(10+2*40+10)%+3")); + assertEquals("0", me.evaluate("100-(200/2)%")); + assertEquals("3", me.evaluate("100-(200/2)%+3")); - Assert.assertEquals("0", mathEngine.evaluate("100-(200/2)%")); - Assert.assertEquals("3", mathEngine.evaluate("100-(200/2)%+3")); + assertEquals("99", me.evaluate("100-2*50%")); + assertEquals("102", me.evaluate("100-2*50%+3")); - Assert.assertEquals("99", mathEngine.evaluate("100-2*50%")); - Assert.assertEquals("102", mathEngine.evaluate("100-2*50%+3")); + assertEquals("84", me.evaluate("20+2^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!")); - 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%")); + assertEquals("120", me.evaluate("100-20+50%")); try { - mathEngine.evaluate("+50%"); + me.evaluate("+50%"); Assert.fail(); } catch (ParseException e) { } - Assert.assertEquals("0.5", mathEngine.evaluate("50%")); - Assert.assertEquals("-0.5", mathEngine.evaluate("-50%")); - Assert.assertEquals("225", mathEngine.evaluate("(100+50%)+50%")); + assertEquals("0.5", me.evaluate("50%")); + assertEquals("-0.5", me.evaluate("-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")); } }