Module operator fix
This commit is contained in:
@@ -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<? extends Constant> 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");
|
||||
|
||||
|
@@ -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<String, String> converter) throws ParseException {
|
||||
public static void testExpression(@Nonnull String[] line, @Nonnull Functor<String, String> 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<String, String> {
|
||||
private static class DummyExpression implements Functor<String, String> {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String convert(@Nonnull String s) {
|
||||
public String apply(@Nonnull String s) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Expression1 implements Converter<String, String> {
|
||||
private static class Expression1 implements Functor<String, String> {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String convert(@Nonnull String s) {
|
||||
public String apply(@Nonnull String s) {
|
||||
return s + "*" + s;
|
||||
}
|
||||
}
|
||||
|
||||
private static class Expression2 implements Converter<String, String> {
|
||||
private static class Expression2 implements Functor<String, String> {
|
||||
|
||||
@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<String, String> {
|
||||
private static class Expression3 implements Functor<String, String> {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String convert(@Nonnull String s) {
|
||||
public String apply(@Nonnull String s) {
|
||||
return s + "*" + s + " * sin(" + s + ") - 0b:1101 + √(" + s + ") + exp ( " + s + ")";
|
||||
}
|
||||
}
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
||||
|
57
jscl/src/test/java/jscl/math/operator/ModuloTest.java
Normal file
57
jscl/src/test/java/jscl/math/operator/ModuloTest.java
Normal file
@@ -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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user