degrees + fix for tests

This commit is contained in:
serso 2011-10-23 14:11:33 +04:00
parent b94b62e675
commit 9babf293a6
13 changed files with 88 additions and 35 deletions

View File

@ -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<String> {
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

View File

@ -49,8 +49,7 @@ public class Functions {
allPrefix = functions;
}
public final static String FACT = "!";
public final static String DEGREE = "°";
public static final List<String> allPostfix = Arrays.asList(FACT, DEGREE);
public static final List<String> allPostfix = Arrays.asList(DEGREE);
}

View File

@ -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

View File

@ -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<Integer> implements NumberPicker.OnChangedListener {
private static final NumberIntervalMapper<Integer> INTEGER_NUMBER_INTERVAL_MAPPER = new NumberIntervalMapper<Integer>(Integer.class);
@NotNull
private NumberPicker numberPicker;
@ -35,7 +37,7 @@ public class NumberPickerDialogPreference extends AbstractDialogPreference<Integ
super(context, attrs, null, false);
//noinspection ConstantConditions
boundaries = new GenericIntervalMapper<Integer>(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<Integ
@NotNull
@Override
protected Mapper<Integer> getMapper() {
return new Mapper<Integer>() {
@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

View File

@ -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<T> implements Mapper<Interval<T>> {
@Override
public Interval<T> parseValue(@Nullable String s) throws IllegalArgumentException {
Log.d(AbstractIntervalMapper.class.getName(), "Parsing: " + s);
//Log.d(AbstractIntervalMapper.class.getName(), "Parsing: " + s);
final List<T> list = CollectionsUtils.split(s, ";", getParser());
assert list.size() == 2;
return new IntervalImpl<T>(list.get(0), list.get(1));
return newInstance(list.get(0), list.get(1));
}
@NotNull
protected abstract Interval<T> newInstance(@Nullable T left, @Nullable T right);
@NotNull
protected abstract Parser<T> getParser();
}

View File

@ -16,7 +16,7 @@ import org.solovyev.common.utils.Parser;
* Date: 9/26/11
* Time: 10:45 PM
*/
public class GenericIntervalMapper<T> extends AbstractIntervalMapper<T> {
public abstract class GenericIntervalMapper<T> extends AbstractIntervalMapper<T> {
@NotNull
private final Mapper<T> mapper;
@ -36,4 +36,9 @@ public class GenericIntervalMapper<T> extends AbstractIntervalMapper<T> {
protected Parser<T> getParser() {
return mapper;
}
@NotNull
public Mapper<T> getMapper() {
return mapper;
}
}

View File

@ -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<T extends Number> extends GenericIntervalMappe
public NumberIntervalMapper(@NotNull Class<T> clazz) {
super(new NumberMapper<T>(clazz));
}
@NotNull
@Override
protected Interval<T> newInstance(@Nullable T left, @Nullable T right) {
return new NumberInterval<T>(left, right);
}
}

View File

@ -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"));

View File

@ -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, ""));
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°))"));
}
}

View File

@ -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"));

View File

@ -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("").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());
}
}

View File

@ -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<Interval<Float>> mapper = new NumberIntervalMapper<Float>(Float.class);
Assert.assertEquals(new IntervalImpl<Float>(1.2f, 12.2f), mapper.parseValue("1.2;12.2"));
Assert.assertEquals(new IntervalImpl<Float>(0f, 0f), mapper.parseValue("0;0"));
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"));
}
}

View File

@ -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));