From b1a0e588c1285e1bb958d5efe01041bfc2e08fd7 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 5 Mar 2016 11:43:54 +0100 Subject: [PATCH] Module operator fix --- .../android/calculator/DisplayFragment.java | 11 ++-- .../calculator/AndroidEditorViewTest.java | 6 +- .../android/calculator/EditorTest.java | 3 +- jscl/src/main/java/jscl/JsclMathEngine.java | 2 +- jscl/src/main/java/jscl/math/Expression.java | 23 +++++++- jscl/src/main/java/jscl/math/Generic.java | 6 +- jscl/src/main/java/jscl/math/JsclInteger.java | 11 ++-- jscl/src/main/java/jscl/math/JsclVector.java | 7 ++- jscl/src/main/java/jscl/math/Matrix.java | 7 ++- .../main/java/jscl/math/ModularInteger.java | 5 ++ .../java/jscl/math/NotDoubleException.java | 18 ++++++ .../java/jscl/math/NotIntegerException.java | 15 +++-- .../main/java/jscl/math/NumericWrapper.java | 47 ++++++++------- jscl/src/main/java/jscl/math/Rational.java | 12 ++-- .../java/jscl/math/function/Constant.java | 22 +++++-- .../main/java/jscl/math/numeric/Complex.java | 6 ++ .../main/java/jscl/math/numeric/Matrix.java | 6 ++ .../main/java/jscl/math/numeric/Numeric.java | 13 ++--- .../src/main/java/jscl/math/numeric/Real.java | 9 +-- .../main/java/jscl/math/numeric/Vector.java | 6 ++ .../jscl/math/operator/AbstractFunction.java | 1 - .../jscl/math/operator/DoubleFactorial.java | 4 +- .../java/jscl/math/operator/Factorial.java | 4 +- .../main/java/jscl/math/operator/Modulo.java | 37 ++++++++---- .../math/polynomial/NestedPolynomial.java | 7 ++- .../test/java/jscl/math/ExpressionTest.java | 22 +++---- .../jscl/math/NumeralBaseConversionTest.java | 25 ++++---- .../test/java/jscl/math/function/DegTest.java | 5 +- .../java/jscl/math/operator/ModuloTest.java | 57 +++++++++++++++++++ 29 files changed, 281 insertions(+), 116 deletions(-) create mode 100644 jscl/src/main/java/jscl/math/NotDoubleException.java create mode 100644 jscl/src/test/java/jscl/math/operator/ModuloTest.java diff --git a/app/src/main/java/org/solovyev/android/calculator/DisplayFragment.java b/app/src/main/java/org/solovyev/android/calculator/DisplayFragment.java index a7324a68..888aa684 100644 --- a/app/src/main/java/org/solovyev/android/calculator/DisplayFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/DisplayFragment.java @@ -35,6 +35,7 @@ import butterknife.ButterKnife; import com.squareup.otto.Bus; import jscl.NumeralBase; import jscl.math.Generic; +import jscl.math.NotDoubleException; import org.solovyev.android.calculator.converter.ConverterFragment; import org.solovyev.android.calculator.jscl.JsclOperation; @@ -134,8 +135,10 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene addMenu(menu, item.title, this); } } - if (result.toDouble() != null) { + try { + result.doubleValue(); addMenu(menu, R.string.c_convert, this); + } catch (NotDoubleException ignored) { } } if (launcher.canPlot(result)) { @@ -207,10 +210,10 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene if (result == null) { return 1d; } - final Double value = result.toDouble(); - if (value == null) { + try { + return result.doubleValue(); + } catch (NotDoubleException ignored) { return 1d; } - return value; } } diff --git a/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java b/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java index 7cb553b8..7ff80ed5 100644 --- a/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/AndroidEditorViewTest.java @@ -22,6 +22,7 @@ package org.solovyev.android.calculator; +import android.content.SharedPreferences; import android.os.Build; import org.junit.Assert; import org.junit.Test; @@ -37,6 +38,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import static org.mockito.Mockito.mock; + @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) @RunWith(value = RobolectricGradleTestRunner.class) public class AndroidEditorViewTest { @@ -52,6 +55,7 @@ public class AndroidEditorViewTest { final int count = 10; final int maxTextLength = 100; + final Editor editor = new Editor(RuntimeEnvironment.application, mock(SharedPreferences.class), Tests.makeEngine()); final Random random = new Random(new Date().getTime()); final CountDownLatch startLatchLatch = new CountDownLatch(threadNum); final CountDownLatch finishLatch = new CountDownLatch(threadNum * count); @@ -75,7 +79,7 @@ public class AndroidEditorViewTest { for (int j = 0; j < count; j++) { try { int textLength = random.nextInt(maxTextLength); - App.getEditor().insert(Strings.generateRandomString(textLength), textLength); + editor.insert(Strings.generateRandomString(textLength), textLength); } catch (Throwable e) { System.out.println(e); error.set(true); diff --git a/app/src/test/java/org/solovyev/android/calculator/EditorTest.java b/app/src/test/java/org/solovyev/android/calculator/EditorTest.java index 9a16d43f..9b68e358 100644 --- a/app/src/test/java/org/solovyev/android/calculator/EditorTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/EditorTest.java @@ -30,6 +30,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import static org.mockito.Mockito.mock; @@ -42,7 +43,7 @@ public class EditorTest { @Before public void setUp() throws Exception { - editor = new Editor(mock(SharedPreferences.class), Tests.makeEngine()); + editor = new Editor(RuntimeEnvironment.application, mock(SharedPreferences.class), Tests.makeEngine()); editor.bus = mock(Bus.class); } diff --git a/jscl/src/main/java/jscl/JsclMathEngine.java b/jscl/src/main/java/jscl/JsclMathEngine.java index 689b7c62..6aba3f7f 100644 --- a/jscl/src/main/java/jscl/JsclMathEngine.java +++ b/jscl/src/main/java/jscl/JsclMathEngine.java @@ -62,7 +62,7 @@ public class JsclMathEngine implements MathEngine { if (Math.floor(value) == value) { return (int) value; } else { - throw new NotIntegerException(); + throw NotIntegerException.get(); } } diff --git a/jscl/src/main/java/jscl/math/Expression.java b/jscl/src/main/java/jscl/math/Expression.java index 997d011c..63d94eef 100644 --- a/jscl/src/main/java/jscl/math/Expression.java +++ b/jscl/src/main/java/jscl/math/Expression.java @@ -626,17 +626,34 @@ public class Expression extends Generic { if (size == 0) { return JsclInteger.valueOf(0); } else if (size == 1) { - final Literal l = literals[0]; final JsclInteger c = coefficients[0]; if (l.degree() == 0) { return c; } else { - throw new NotIntegerException(); + throw NotIntegerException.get(); } } else { - throw new NotIntegerException(); + throw NotIntegerException.get(); + } + } + + @Override + public double doubleValue() throws NotDoubleException { + if (size == 0) { + return 0f; + } else if (size == 1) { + final Literal l = literals[0]; + final JsclInteger c = coefficients[0]; + + if (l.degree() == 0) { + return c.doubleValue(); + } else { + throw NotDoubleException.get(); + } + } else { + throw NotDoubleException.get(); } } diff --git a/jscl/src/main/java/jscl/math/Generic.java b/jscl/src/main/java/jscl/math/Generic.java index fa5acaec..aa562bb2 100644 --- a/jscl/src/main/java/jscl/math/Generic.java +++ b/jscl/src/main/java/jscl/math/Generic.java @@ -39,10 +39,6 @@ public abstract class Generic implements Arithmetic, Comparable { return null; } - public Double toDouble() { - return null; - } - @Nonnull public Generic subtract(@Nonnull Generic that) { return add(that.negate()); @@ -171,6 +167,8 @@ public abstract class Generic implements Arithmetic, Comparable { public abstract JsclInteger integerValue() throws NotIntegerException; + public abstract double doubleValue() throws NotDoubleException; + public abstract Variable variableValue() throws NotVariableException; public abstract Variable[] variables(); diff --git a/jscl/src/main/java/jscl/math/JsclInteger.java b/jscl/src/main/java/jscl/math/JsclInteger.java index 0c0034da..5339e192 100644 --- a/jscl/src/main/java/jscl/math/JsclInteger.java +++ b/jscl/src/main/java/jscl/math/JsclInteger.java @@ -4,13 +4,12 @@ import jscl.JsclMathEngine; import jscl.math.function.Constant; import jscl.mathml.MathML; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.math.BigInteger; import java.util.Collections; import java.util.Set; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - public final class JsclInteger extends Generic { public static final JsclInteger factory = new JsclInteger(BigInteger.valueOf(0)); @@ -162,8 +161,8 @@ public final class JsclInteger extends Generic { return 0; } - public JsclInteger mod(JsclInteger integer) { - return new JsclInteger(content.mod(integer.content)); + public JsclInteger mod(JsclInteger that) { + return new JsclInteger(content.mod(that.content)); } public JsclInteger modPow(JsclInteger exponent, JsclInteger integer) { @@ -372,7 +371,7 @@ public final class JsclInteger extends Generic { } @Override - public Double toDouble() { + public double doubleValue() throws NotDoubleException { return content.doubleValue(); } } diff --git a/jscl/src/main/java/jscl/math/JsclVector.java b/jscl/src/main/java/jscl/math/JsclVector.java index ee179a19..508c8062 100644 --- a/jscl/src/main/java/jscl/math/JsclVector.java +++ b/jscl/src/main/java/jscl/math/JsclVector.java @@ -247,7 +247,12 @@ public class JsclVector extends Generic { } public JsclInteger integerValue() throws NotIntegerException { - throw new NotIntegerException(); + throw NotIntegerException.get(); + } + + @Override + public double doubleValue() throws NotDoubleException { + throw NotDoubleException.get(); } @Override diff --git a/jscl/src/main/java/jscl/math/Matrix.java b/jscl/src/main/java/jscl/math/Matrix.java index 609f5974..054f66e8 100644 --- a/jscl/src/main/java/jscl/math/Matrix.java +++ b/jscl/src/main/java/jscl/math/Matrix.java @@ -329,7 +329,12 @@ public class Matrix extends Generic { } public JsclInteger integerValue() throws NotIntegerException { - throw new NotIntegerException(); + throw NotIntegerException.get(); + } + + @Override + public double doubleValue() throws NotDoubleException { + throw NotDoubleException.get(); } @Override diff --git a/jscl/src/main/java/jscl/math/ModularInteger.java b/jscl/src/main/java/jscl/math/ModularInteger.java index 29cdd666..e67f294b 100644 --- a/jscl/src/main/java/jscl/math/ModularInteger.java +++ b/jscl/src/main/java/jscl/math/ModularInteger.java @@ -151,6 +151,11 @@ public class ModularInteger extends Generic implements Field { return JsclInteger.valueOf(content); } + @Override + public double doubleValue() throws NotDoubleException { + return content; + } + @Override public boolean isInteger() { return true; diff --git a/jscl/src/main/java/jscl/math/NotDoubleException.java b/jscl/src/main/java/jscl/math/NotDoubleException.java new file mode 100644 index 00000000..de831f77 --- /dev/null +++ b/jscl/src/main/java/jscl/math/NotDoubleException.java @@ -0,0 +1,18 @@ +package jscl.math; + +import javax.annotation.Nonnull; + +public class NotDoubleException extends ArithmeticException { + + @SuppressWarnings("ThrowableInstanceNeverThrown") + private static final NotDoubleException INSTANCE = new NotDoubleException(); + + private NotDoubleException() { + super("Not double!"); + } + + @Nonnull + public static NotDoubleException get() { + return INSTANCE; + } +} diff --git a/jscl/src/main/java/jscl/math/NotIntegerException.java b/jscl/src/main/java/jscl/math/NotIntegerException.java index d6d96afd..ad49e667 100644 --- a/jscl/src/main/java/jscl/math/NotIntegerException.java +++ b/jscl/src/main/java/jscl/math/NotIntegerException.java @@ -1,11 +1,18 @@ package jscl.math; +import javax.annotation.Nonnull; + public class NotIntegerException extends ArithmeticException { - public NotIntegerException() { - this("Not integer!"); + + @SuppressWarnings("ThrowableInstanceNeverThrown") + private static final NotIntegerException INSTANCE = new NotIntegerException(); + + private NotIntegerException() { + super("Not integer!"); } - public NotIntegerException(String s) { - super(s); + @Nonnull + public static NotIntegerException get() { + return INSTANCE; } } diff --git a/jscl/src/main/java/jscl/math/NumericWrapper.java b/jscl/src/main/java/jscl/math/NumericWrapper.java index 09ed7ab4..03a3c426 100644 --- a/jscl/src/main/java/jscl/math/NumericWrapper.java +++ b/jscl/src/main/java/jscl/math/NumericWrapper.java @@ -1,22 +1,17 @@ package jscl.math; -import java.math.BigInteger; -import java.util.Collections; -import java.util.Set; - -import javax.annotation.Nonnull; - import jscl.JsclMathEngine; import jscl.math.function.Constant; import jscl.math.function.Constants; import jscl.math.function.IConstant; -import jscl.math.numeric.Complex; -import jscl.math.numeric.INumeric; -import jscl.math.numeric.Numeric; -import jscl.math.numeric.Real; -import jscl.math.numeric.Vector; +import jscl.math.numeric.*; import jscl.mathml.MathML; +import javax.annotation.Nonnull; +import java.math.BigInteger; +import java.util.Collections; +import java.util.Set; + public final class NumericWrapper extends Generic implements INumeric { @Nonnull @@ -189,19 +184,19 @@ public final class NumericWrapper extends Generic implements INumeric, INumeric, Comparable { @@ -332,7 +331,5 @@ public abstract class Numeric implements Arithmetic, INumeric, return null; } - public Double toDouble() { - return null; - } + public abstract double doubleValue(); } diff --git a/jscl/src/main/java/jscl/math/numeric/Real.java b/jscl/src/main/java/jscl/math/numeric/Real.java index c9756f71..300a088b 100644 --- a/jscl/src/main/java/jscl/math/numeric/Real.java +++ b/jscl/src/main/java/jscl/math/numeric/Real.java @@ -2,9 +2,8 @@ package jscl.math.numeric; import jscl.math.NotDivisibleException; -import java.math.BigInteger; - import javax.annotation.Nonnull; +import java.math.BigInteger; public final class Real extends Numeric { @@ -255,10 +254,6 @@ public final class Real extends Numeric { } else throw new ArithmeticException(); } - public double doubleValue() { - return content; - } - public int compareTo(@Nonnull Real that) { return Double.compare(this.content, that.content); } @@ -289,7 +284,7 @@ public final class Real extends Numeric { } @Override - public Double toDouble() { + public double doubleValue() { return content; } } diff --git a/jscl/src/main/java/jscl/math/numeric/Vector.java b/jscl/src/main/java/jscl/math/numeric/Vector.java index 9c5becf0..63cf174a 100644 --- a/jscl/src/main/java/jscl/math/numeric/Vector.java +++ b/jscl/src/main/java/jscl/math/numeric/Vector.java @@ -1,6 +1,7 @@ package jscl.math.numeric; import jscl.math.NotDivisibleException; +import jscl.math.NotDoubleException; import jscl.util.ArrayComparator; import javax.annotation.Nonnull; @@ -163,6 +164,11 @@ public class Vector extends Numeric { } } + @Override + public double doubleValue() { + throw NotDoubleException.get(); + } + public String toString() { final StringBuilder result = new StringBuilder(); diff --git a/jscl/src/main/java/jscl/math/operator/AbstractFunction.java b/jscl/src/main/java/jscl/math/operator/AbstractFunction.java index 38633e98..0906b343 100644 --- a/jscl/src/main/java/jscl/math/operator/AbstractFunction.java +++ b/jscl/src/main/java/jscl/math/operator/AbstractFunction.java @@ -128,7 +128,6 @@ public abstract class AbstractFunction extends Variable { public Generic numeric() { final AbstractFunction result = newNumericFunction(); - return result.selfNumeric(); } diff --git a/jscl/src/main/java/jscl/math/operator/DoubleFactorial.java b/jscl/src/main/java/jscl/math/operator/DoubleFactorial.java index 48054015..2789c90a 100644 --- a/jscl/src/main/java/jscl/math/operator/DoubleFactorial.java +++ b/jscl/src/main/java/jscl/math/operator/DoubleFactorial.java @@ -66,11 +66,11 @@ public class DoubleFactorial extends PostfixFunction { if (result instanceof JsclInteger) { return new NumericWrapper(((JsclInteger) result)); } else { - throw new NotIntegerException(); + throw NotIntegerException.get(); } } else { - throw new NotIntegerException(); + throw NotIntegerException.get(); } } diff --git a/jscl/src/main/java/jscl/math/operator/Factorial.java b/jscl/src/main/java/jscl/math/operator/Factorial.java index e92c6e18..d8c652db 100644 --- a/jscl/src/main/java/jscl/math/operator/Factorial.java +++ b/jscl/src/main/java/jscl/math/operator/Factorial.java @@ -47,11 +47,11 @@ public class Factorial extends PostfixFunction { if (result instanceof JsclInteger) { return new NumericWrapper(((JsclInteger) result)); } else { - throw new NotIntegerException(); + throw NotIntegerException.get(); } } else { - throw new NotIntegerException(); + throw NotIntegerException.get(); } } diff --git a/jscl/src/main/java/jscl/math/operator/Modulo.java b/jscl/src/main/java/jscl/math/operator/Modulo.java index c64ddd97..e1303044 100644 --- a/jscl/src/main/java/jscl/math/operator/Modulo.java +++ b/jscl/src/main/java/jscl/math/operator/Modulo.java @@ -1,9 +1,7 @@ package jscl.math.operator; -import jscl.math.Generic; -import jscl.math.JsclInteger; -import jscl.math.NotIntegerException; -import jscl.math.Variable; +import jscl.math.*; +import jscl.math.numeric.Real; import javax.annotation.Nonnull; @@ -26,14 +24,23 @@ public class Modulo extends Operator { public Generic selfExpand() { try { - final JsclInteger first = parameters[0].integerValue(); - final JsclInteger second = parameters[1].integerValue(); - - return first.mod(second); - + return tryIntegerMod(); } catch (NotIntegerException e) { } - return parameters[0].remainder(parameters[1]); + return tryRealMod(); + } + + private Generic tryRealMod() { + final double numerator = parameters[0].doubleValue(); + final double denominator = parameters[1].doubleValue(); + return new NumericWrapper(Real.valueOf(numerator % denominator)); + } + + @Nonnull + private Generic tryIntegerMod() throws NotIntegerException{ + final JsclInteger numerator = parameters[0].integerValue(); + final JsclInteger denominator = parameters[1].integerValue(); + return numerator.mod(denominator); } @Nonnull @@ -42,6 +49,16 @@ public class Modulo extends Operator { return new Modulo(parameters); } + @Override + public Generic numeric() { + return newNumericFunction().selfNumeric(); + } + + @Override + public Generic selfNumeric() { + return selfExpand(); + } + @Nonnull public Variable newInstance() { return new Modulo(null, null); diff --git a/jscl/src/main/java/jscl/math/polynomial/NestedPolynomial.java b/jscl/src/main/java/jscl/math/polynomial/NestedPolynomial.java index 9be03e37..ffd34852 100644 --- a/jscl/src/main/java/jscl/math/polynomial/NestedPolynomial.java +++ b/jscl/src/main/java/jscl/math/polynomial/NestedPolynomial.java @@ -178,7 +178,12 @@ final class PolynomialWrapper extends Generic { } public JsclInteger integerValue() throws NotIntegerException { - throw new NotIntegerException(); + throw NotIntegerException.get(); + } + + @Override + public double doubleValue() throws NotDoubleException { + throw NotDoubleException.get(); } @Override diff --git a/jscl/src/test/java/jscl/math/ExpressionTest.java b/jscl/src/test/java/jscl/math/ExpressionTest.java index 79d2a90e..d2c84412 100644 --- a/jscl/src/test/java/jscl/math/ExpressionTest.java +++ b/jscl/src/test/java/jscl/math/ExpressionTest.java @@ -8,7 +8,7 @@ import jscl.math.function.Constant; import jscl.math.function.ExtendedConstant; import jscl.math.function.IConstant; import jscl.text.ParseException; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Test; import javax.annotation.Nonnull; @@ -21,14 +21,10 @@ import java.net.URL; import java.net.URLConnection; import java.util.Set; -import static junit.framework.Assert.fail; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; -/** - * User: serso - * Date: 10/27/11 - * Time: 3:54 PM - */ public class ExpressionTest { private static final String expressions = "-24.37581129610191-((2699.798527427213-4032.781981216783)*√(4657.120529143301)/6202.47137988087-ln(4435.662292261872)*sin(5134.044125137488)-sin(5150.617980207194)+sin(1416.6029070906816))\n" + @@ -111,11 +107,11 @@ public class ExpressionTest { @Test public void testConstants() throws Exception { - Assert.assertTrue(Expression.valueOf("3+4").getConstants().isEmpty()); + assertTrue(Expression.valueOf("3+4").getConstants().isEmpty()); Set constants = Expression.valueOf("3+4*t").getConstants(); - Assert.assertTrue(constants.size() == 1); - Assert.assertTrue(constants.contains(new Constant("t"))); + assertTrue(constants.size() == 1); + assertTrue(constants.contains(new Constant("t"))); IConstant constant = null; @@ -125,9 +121,9 @@ public class ExpressionTest { constant = me.getConstantsRegistry().addOrUpdate(t_0.create()); constants = Expression.valueOf("3+4*t_0+t_0+t_1").getConstants(); - Assert.assertTrue(constants.size() == 2); - Assert.assertTrue(constants.contains(new Constant("t_0"))); - Assert.assertTrue(constants.contains(new Constant("t_1"))); + assertTrue(constants.size() == 2); + assertTrue(constants.contains(new Constant("t_0"))); + assertTrue(constants.contains(new Constant("t_1"))); final Expression expression = Expression.valueOf("2*t_0+5*t_1"); diff --git a/jscl/src/test/java/jscl/math/NumeralBaseConversionTest.java b/jscl/src/test/java/jscl/math/NumeralBaseConversionTest.java index 39532469..3f8f37dd 100644 --- a/jscl/src/test/java/jscl/math/NumeralBaseConversionTest.java +++ b/jscl/src/test/java/jscl/math/NumeralBaseConversionTest.java @@ -7,6 +7,7 @@ import jscl.text.ParseException; import jscl.util.ExpressionGeneratorWithInput; import org.junit.Assert; import org.junit.Test; +import org.solovyev.common.Functor; import javax.annotation.Nonnull; import java.io.InputStreamReader; @@ -20,14 +21,14 @@ import java.util.List; */ public class NumeralBaseConversionTest { - public static void testExpression(@Nonnull String[] line, @Nonnull Converter converter) throws ParseException { + public static void testExpression(@Nonnull String[] line, @Nonnull Functor converter) throws ParseException { final String dec = line[0].toUpperCase(); final String hex = "0x:" + line[1].toUpperCase(); final String bin = "0b:" + line[2].toUpperCase(); - final String decResult = Expression.valueOf(converter.convert(dec)).numeric().toString(); - final String hexResult = Expression.valueOf(converter.convert(hex)).numeric().toString(); - final String binResult = Expression.valueOf(converter.convert(bin)).numeric().toString(); + final String decResult = Expression.valueOf(converter.apply(dec)).numeric().toString(); + final String hexResult = Expression.valueOf(converter.apply(hex)).numeric().toString(); + final String binResult = Expression.valueOf(converter.apply(bin)).numeric().toString(); Assert.assertEquals(decResult, hexResult); Assert.assertEquals(decResult, binResult); @@ -95,38 +96,38 @@ public class NumeralBaseConversionTest { } } - private static class DummyExpression implements Converter { + private static class DummyExpression implements Functor { @Nonnull @Override - public String convert(@Nonnull String s) { + public String apply(@Nonnull String s) { return s; } } - private static class Expression1 implements Converter { + private static class Expression1 implements Functor { @Nonnull @Override - public String convert(@Nonnull String s) { + public String apply(@Nonnull String s) { return s + "*" + s; } } - private static class Expression2 implements Converter { + private static class Expression2 implements Functor { @Nonnull @Override - public String convert(@Nonnull String s) { + public String apply(@Nonnull String s) { return s + "*" + s + " * sin(" + s + ") - 0b:1101"; } } - private static class Expression3 implements Converter { + private static class Expression3 implements Functor { @Nonnull @Override - public String convert(@Nonnull String s) { + public String apply(@Nonnull String s) { return s + "*" + s + " * sin(" + s + ") - 0b:1101 + √(" + s + ") + exp ( " + s + ")"; } } diff --git a/jscl/src/test/java/jscl/math/function/DegTest.java b/jscl/src/test/java/jscl/math/function/DegTest.java index 4544e1d6..c5c62863 100644 --- a/jscl/src/test/java/jscl/math/function/DegTest.java +++ b/jscl/src/test/java/jscl/math/function/DegTest.java @@ -1,9 +1,8 @@ package jscl.math.function; import jscl.JsclMathEngine; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Test; -import org.solovyev.common.math.Maths; /** * User: serso @@ -28,6 +27,6 @@ public class DegTest { } private void assertEquals(double expected, Double actual) { - Assert.assertTrue("Expected=" + expected + ", actual=" + actual, Maths.equals(expected, actual, 8)); + Assert.assertEquals(expected, actual, Math.pow(10, -8)); } } diff --git a/jscl/src/test/java/jscl/math/operator/ModuloTest.java b/jscl/src/test/java/jscl/math/operator/ModuloTest.java new file mode 100644 index 00000000..3837aa24 --- /dev/null +++ b/jscl/src/test/java/jscl/math/operator/ModuloTest.java @@ -0,0 +1,57 @@ +package jscl.math.operator; + +import jscl.math.Expression; +import jscl.math.Generic; +import jscl.math.NumericWrapper; +import jscl.text.ParseException; +import org.junit.Assert; +import org.junit.Test; + +import javax.annotation.Nonnull; + +import static org.junit.Assert.assertEquals; + +public class ModuloTest { + + @Test + public void testNumeric() throws Exception { + assertMod(2, 2); + assertMod(1, 2); + assertMod(3.5, 2); + assertMod(3, 2); + assertMod(1.5, 2); + assertMod(1.5, "1.5", "2"); + assertMod(1.5, "3.5", "2"); + } + + private void assertMod(double expected, @Nonnull String numerator, @Nonnull String denominator) throws ParseException { + final Modulo mod = makeModulo(numerator, denominator); + final Generic numeric = mod.numeric(); + assertEquals(expected, numeric.doubleValue(), Math.pow(10, -8)); + } + + private void assertMod(int numerator, int denominator) { + final Modulo mod = makeModulo(numerator, denominator); + final Generic numeric = mod.numeric(); + Assert.assertTrue(numeric.isInteger()); + assertEquals(numerator % denominator, numeric.integerValue().intValue()); + } + + private void assertMod(double numerator, double denominator) { + final Modulo mod = makeModulo(numerator, denominator); + final Generic numeric = mod.numeric(); + assertEquals(numerator % denominator, numeric.doubleValue(), Math.pow(10, -8)); + } + + private Modulo makeModulo(int n, int d) { + return new Modulo(NumericWrapper.valueOf(n), NumericWrapper.valueOf(d)); + } + + private Modulo makeModulo(String n, String d) throws ParseException { + return new Modulo(Expression.valueOf(n), Expression.valueOf(d)); + } + + private Modulo makeModulo(double n, double d) { + return new Modulo(NumericWrapper.valueOf(n), NumericWrapper.valueOf(d)); + } +} \ No newline at end of file