From 9babf293a685b5257e0108ef1a5e731451bc7486 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 23 Oct 2011 14:11:33 +0400 Subject: [PATCH] degrees + fix for tests --- .../android/calculator/TextHighlighter.java | 10 ++++---- .../android/calculator/math/Functions.java | 3 +-- .../android/calculator/math/MathType.java | 15 +++++++++++- .../prefs/NumberPickerDialogPreference.java | 18 ++++----------- .../common/AbstractIntervalMapper.java | 8 ++++--- .../common/GenericIntervalMapper.java | 7 +++++- .../solovyev/common/NumberIntervalMapper.java | 9 ++++++++ .../FromJsclNumericTextProcessorTest.java | 2 +- .../model/CalculatorEngineTest.java | 23 +++++++++++++++---- .../FromJsclSimplifyTextProcessorTest.java | 5 ++++ .../model/ToJsclTextProcessorTest.java | 13 +++++++++++ .../common/FloatIntervalMapperTest.java | 6 ++--- .../common/math/DiscreteNormalizerTest.java | 4 ++-- 13 files changed, 88 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/solovyev/android/calculator/TextHighlighter.java b/src/main/java/org/solovyev/android/calculator/TextHighlighter.java index 17044ba0..79e22d05 100644 --- a/src/main/java/org/solovyev/android/calculator/TextHighlighter.java +++ b/src/main/java/org/solovyev/android/calculator/TextHighlighter.java @@ -6,7 +6,6 @@ package org.solovyev.android.calculator; -import android.graphics.Color; import org.jetbrains.annotations.NotNull; import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.model.ParseException; @@ -26,9 +25,12 @@ public class TextHighlighter implements TextProcessor { public TextHighlighter(int baseColor) { this.color = baseColor; - this.colorRed = Color.red(baseColor); - this.colorGreen = Color.green(baseColor); - this.colorBlue = Color.blue(baseColor); + //this.colorRed = Color.red(baseColor); + this.colorRed = (baseColor >> 16) & 0xFF; + //this.colorGreen = Color.green(baseColor); + this.colorGreen = (color >> 8) & 0xFF; + //this.colorBlue = Color.blue(baseColor); + this.colorBlue = color & 0xFF; } @NotNull diff --git a/src/main/java/org/solovyev/android/calculator/math/Functions.java b/src/main/java/org/solovyev/android/calculator/math/Functions.java index d2e616e4..db1890d0 100644 --- a/src/main/java/org/solovyev/android/calculator/math/Functions.java +++ b/src/main/java/org/solovyev/android/calculator/math/Functions.java @@ -49,8 +49,7 @@ public class Functions { allPrefix = functions; } - public final static String FACT = "!"; public final static String DEGREE = "°"; - public static final List allPostfix = Arrays.asList(FACT, DEGREE); + public static final List allPostfix = Arrays.asList(DEGREE); } diff --git a/src/main/java/org/solovyev/android/calculator/math/MathType.java b/src/main/java/org/solovyev/android/calculator/math/MathType.java index 170343e8..d3832d6c 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -43,7 +43,20 @@ public enum MathType { } }, - postfix_function(400, true, false, Functions.allPostfix), + postfix_function(400, true, false, Functions.allPostfix) { + @Override + protected String getSubstitute(@NotNull String match) { + final String result; + + if ( match.equals(Functions.DEGREE)) { + result = PI + "/180"; + } else { + result = null; + } + + return result; + } + }, unary_operation(500, false, false, "-", "=", "!"), binary_operation(600, false, false, "-", "+", "*", "×", "∙", "/", "^") { @Override diff --git a/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java b/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java index cf848f03..f45706c8 100644 --- a/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java +++ b/src/main/java/org/solovyev/android/view/prefs/NumberPickerDialogPreference.java @@ -14,7 +14,7 @@ import android.widget.LinearLayout; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.view.widgets.NumberPicker; -import org.solovyev.common.GenericIntervalMapper; +import org.solovyev.common.NumberIntervalMapper; import org.solovyev.common.utils.Interval; import org.solovyev.common.utils.Mapper; @@ -25,6 +25,8 @@ import org.solovyev.common.utils.Mapper; */ public class NumberPickerDialogPreference extends AbstractDialogPreference implements NumberPicker.OnChangedListener { + private static final NumberIntervalMapper INTEGER_NUMBER_INTERVAL_MAPPER = new NumberIntervalMapper(Integer.class); + @NotNull private NumberPicker numberPicker; @@ -35,7 +37,7 @@ public class NumberPickerDialogPreference extends AbstractDialogPreference(getMapper()).parseValue(attrs.getAttributeValue(localNameSpace, "boundaries")); + boundaries = INTEGER_NUMBER_INTERVAL_MAPPER.parseValue(attrs.getAttributeValue(localNameSpace, "boundaries")); createPreferenceView(); } @@ -75,17 +77,7 @@ public class NumberPickerDialogPreference extends AbstractDialogPreference getMapper() { - return new Mapper() { - @Override - public String formatValue(@Nullable Integer value) throws IllegalArgumentException { - return String.valueOf(value); - } - - @Override - public Integer parseValue(@Nullable String value) throws IllegalArgumentException { - return Integer.valueOf(value); - } - }; + return INTEGER_NUMBER_INTERVAL_MAPPER.getMapper(); } @Override diff --git a/src/main/java/org/solovyev/common/AbstractIntervalMapper.java b/src/main/java/org/solovyev/common/AbstractIntervalMapper.java index de6b880b..2b90eb50 100644 --- a/src/main/java/org/solovyev/common/AbstractIntervalMapper.java +++ b/src/main/java/org/solovyev/common/AbstractIntervalMapper.java @@ -6,7 +6,6 @@ package org.solovyev.common; -import android.util.Log; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.common.utils.*; @@ -35,13 +34,16 @@ public abstract class AbstractIntervalMapper implements Mapper> { @Override public Interval parseValue(@Nullable String s) throws IllegalArgumentException { - Log.d(AbstractIntervalMapper.class.getName(), "Parsing: " + s); + //Log.d(AbstractIntervalMapper.class.getName(), "Parsing: " + s); final List list = CollectionsUtils.split(s, ";", getParser()); assert list.size() == 2; - return new IntervalImpl(list.get(0), list.get(1)); + return newInstance(list.get(0), list.get(1)); } + @NotNull + protected abstract Interval newInstance(@Nullable T left, @Nullable T right); + @NotNull protected abstract Parser getParser(); } diff --git a/src/main/java/org/solovyev/common/GenericIntervalMapper.java b/src/main/java/org/solovyev/common/GenericIntervalMapper.java index f2a6414b..e6468f47 100644 --- a/src/main/java/org/solovyev/common/GenericIntervalMapper.java +++ b/src/main/java/org/solovyev/common/GenericIntervalMapper.java @@ -16,7 +16,7 @@ import org.solovyev.common.utils.Parser; * Date: 9/26/11 * Time: 10:45 PM */ -public class GenericIntervalMapper extends AbstractIntervalMapper { +public abstract class GenericIntervalMapper extends AbstractIntervalMapper { @NotNull private final Mapper mapper; @@ -36,4 +36,9 @@ public class GenericIntervalMapper extends AbstractIntervalMapper { protected Parser getParser() { return mapper; } + + @NotNull + public Mapper getMapper() { + return mapper; + } } diff --git a/src/main/java/org/solovyev/common/NumberIntervalMapper.java b/src/main/java/org/solovyev/common/NumberIntervalMapper.java index 2f55d1e0..3e09cf95 100644 --- a/src/main/java/org/solovyev/common/NumberIntervalMapper.java +++ b/src/main/java/org/solovyev/common/NumberIntervalMapper.java @@ -7,6 +7,9 @@ package org.solovyev.common; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.common.utils.Interval; +import org.solovyev.common.utils.NumberInterval; /** * User: serso @@ -18,4 +21,10 @@ public class NumberIntervalMapper extends GenericIntervalMappe public NumberIntervalMapper(@NotNull Class clazz) { super(new NumberMapper(clazz)); } + + @NotNull + @Override + protected Interval newInstance(@Nullable T left, @Nullable T right) { + return new NumberInterval(left, right); + } } diff --git a/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java b/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java index 1ad6633a..61840a50 100644 --- a/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/jscl/FromJsclNumericTextProcessorTest.java @@ -20,7 +20,7 @@ public class FromJsclNumericTextProcessorTest { public void testCreateResultForComplexNumber() throws Exception { final FromJsclNumericTextProcessor cm = new FromJsclNumericTextProcessor(); - Assert.assertEquals("1.22133+23123.0i", cm.createResultForComplexNumber("1.22133232+23123*i")); + Assert.assertEquals("1.22133+23 123i", cm.createResultForComplexNumber("1.22133232+23123*i")); Assert.assertEquals("1.22133+1.2i", cm.createResultForComplexNumber("1.22133232+1.2*i")); Assert.assertEquals("1.22i", cm.createResultForComplexNumber("1.22*i")); Assert.assertEquals("i", cm.createResultForComplexNumber("i")); diff --git a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java index 0f533914..621f81fa 100644 --- a/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/CalculatorEngineTest.java @@ -38,7 +38,7 @@ public class CalculatorEngineTest { } catch (EvalError evalError) { Assert.fail(); } catch (ParseException e) { - if ( e.getMessage().startsWith("Too long calculation") ) { + if (e.getMessage().startsWith("Too long calculation")) { } else { Assert.fail(); @@ -52,7 +52,7 @@ public class CalculatorEngineTest { } catch (EvalError evalError) { Assert.fail(); } catch (ParseException e) { - if ( e.getMessage().startsWith("Too long calculation") ) { + if (e.getMessage().startsWith("Too long calculation")) { final long end = System.currentTimeMillis(); Assert.assertTrue(end - start < 1000); } else { @@ -102,7 +102,7 @@ public class CalculatorEngineTest { CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("k1", 4d)); Assert.assertEquals("4", cm.evaluate(JsclOperation.numeric, "k11")); - CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t", (String)null)); + CalculatorEngine.instance.getVarsRegister().addVar(null, new Var.Builder("t", (String) null)); Assert.assertEquals("11t", cm.evaluate(JsclOperation.numeric, "t11")); Assert.assertEquals("11et", cm.evaluate(JsclOperation.numeric, "t11e")); Assert.assertEquals("11×Infinityt", cm.evaluate(JsclOperation.numeric, "t11∞")); @@ -115,13 +115,13 @@ public class CalculatorEngineTest { 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 (ParseException e){ + } catch (ParseException e) { } Assert.assertEquals("NaN", cm.evaluate(JsclOperation.numeric, "ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(ln(100)))))))))))))))")); 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 (ParseException e){ + } catch (ParseException e) { } 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))))))))))))))))))))))))))))))))))))")); @@ -151,4 +151,17 @@ public class CalculatorEngineTest { } + + @Test + public void testDegrees() throws Exception { + final CalculatorEngine cm = CalculatorEngine.instance; + + cm.setPrecision(3); + Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "°")); + Assert.assertEquals("0.017", cm.evaluate(JsclOperation.numeric, "1°")); + Assert.assertEquals("0.349", cm.evaluate(JsclOperation.numeric, "20.0°")); + Assert.assertEquals("0.5", cm.evaluate(JsclOperation.numeric, "sin(30°)")); + Assert.assertEquals("0.524", cm.evaluate(JsclOperation.numeric, "asin(sin(30°))")); + + } } diff --git a/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java b/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java index bab1b6c7..e534d41d 100644 --- a/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/FromJsclSimplifyTextProcessorTest.java @@ -4,6 +4,8 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import java.text.DecimalFormatSymbols; + /** * User: serso * Date: 10/20/11 @@ -20,6 +22,9 @@ public class FromJsclSimplifyTextProcessorTest { public void testProcess() throws Exception { FromJsclSimplifyTextProcessor tp = new FromJsclSimplifyTextProcessor(); Assert.assertEquals("(e)", tp.process("(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")); diff --git a/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java b/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java index d564b110..74690c8d 100644 --- a/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java +++ b/src/test/java/org/solovyev/android/calculator/model/ToJsclTextProcessorTest.java @@ -107,4 +107,17 @@ public class ToJsclTextProcessorTest { Assert.assertEquals(2, preprocessor.getPostfixFunctionStart("2+sin(5sin(5sin(5)))!", 19)); Assert.assertEquals(5, preprocessor.getPostfixFunctionStart("2.23+sin(5.4434234*sin(5.1+1))!", 29)); } + + @Test + public void testDegrees() throws Exception { + final ToJsclTextProcessor preprocessor = new ToJsclTextProcessor(); + + Assert.assertEquals( "", preprocessor.process("").toString()); + Assert.assertEquals( "3.141592653589793/180", preprocessor.process("°").toString()); + Assert.assertEquals( "1*3.141592653589793/180", preprocessor.process("1°").toString()); + Assert.assertEquals( "20.0*3.141592653589793/180", preprocessor.process("20.0°").toString()); + Assert.assertEquals( "sin(30*3.141592653589793/180)", preprocessor.process("sin(30°)").toString()); + Assert.assertEquals( "asin(sin(3.141592653589793/6))*3.141592653589793/180", preprocessor.process("asin(sin(π/6))°").toString()); + + } } diff --git a/src/test/java/org/solovyev/common/FloatIntervalMapperTest.java b/src/test/java/org/solovyev/common/FloatIntervalMapperTest.java index 5c93cc98..2fc0f46c 100644 --- a/src/test/java/org/solovyev/common/FloatIntervalMapperTest.java +++ b/src/test/java/org/solovyev/common/FloatIntervalMapperTest.java @@ -3,8 +3,8 @@ package org.solovyev.common; import junit.framework.Assert; import org.junit.Test; import org.solovyev.common.utils.Interval; -import org.solovyev.common.utils.IntervalImpl; import org.solovyev.common.utils.Mapper; +import org.solovyev.common.utils.NumberInterval; /** * User: serso @@ -17,7 +17,7 @@ public class FloatIntervalMapperTest { public void testParse() throws Exception { final Mapper> mapper = new NumberIntervalMapper(Float.class); - Assert.assertEquals(new IntervalImpl(1.2f, 12.2f), mapper.parseValue("1.2;12.2")); - Assert.assertEquals(new IntervalImpl(0f, 0f), mapper.parseValue("0;0")); + Assert.assertEquals(new NumberInterval(1.2f, 12.2f), mapper.parseValue("1.2;12.2")); + Assert.assertEquals(new NumberInterval(0f, 0f), mapper.parseValue("0;0")); } } diff --git a/src/test/java/org/solovyev/common/math/DiscreteNormalizerTest.java b/src/test/java/org/solovyev/common/math/DiscreteNormalizerTest.java index ce4c5906..ef2fc2d0 100644 --- a/src/test/java/org/solovyev/common/math/DiscreteNormalizerTest.java +++ b/src/test/java/org/solovyev/common/math/DiscreteNormalizerTest.java @@ -13,7 +13,7 @@ public class DiscreteNormalizerTest { @Test public void testNormalize() throws Exception { - DiscreteNormalizer dn = new DiscreteNormalizer(0, 10, 1); + 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)); @@ -25,7 +25,7 @@ public class DiscreteNormalizerTest { @Test public void testDenormalize() throws Exception { - DiscreteNormalizer dn = new DiscreteNormalizer(0, 10, 1); + 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));