Remove reflection tests as they are in FastMathStrictComparisonTest
Enable fail if not all StrictMath methods have been implemented git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1062902 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6e9f0c1dbb
commit
fa0d87fd62
|
@ -16,11 +16,9 @@
|
|||
*/
|
||||
package org.apache.commons.math.util;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.math.dfp.Dfp;
|
||||
import org.apache.commons.math.dfp.DfpField;
|
||||
|
@ -37,25 +35,6 @@ public class FastMathTest {
|
|||
private static final double MAX_ERROR_ULP = 0.51;
|
||||
private static final int NUMBER_OF_TRIALS = 1000;
|
||||
|
||||
// Values which often need special handling
|
||||
private static final Double[] DOUBLE_SPECIAL_VALUES = {
|
||||
-0.0, +0.0, // 1,2
|
||||
Double.NaN, // 3
|
||||
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, // 4,5
|
||||
-Double.MAX_VALUE, Double.MAX_VALUE, // 6,7
|
||||
// decreasing order of absolute value to help catch first failure
|
||||
-MathUtils.EPSILON, MathUtils.EPSILON, // 8,9
|
||||
-MathUtils.SAFE_MIN, MathUtils.SAFE_MIN, // 10,11
|
||||
-Double.MIN_VALUE, Double.MIN_VALUE, // 12,13
|
||||
};
|
||||
|
||||
private static final Float [] FLOAT_SPECIAL_VALUES = {
|
||||
-0.0f, +0.0f, // 1,2
|
||||
Float.NaN, // 3
|
||||
Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, // 4,5
|
||||
Float.MIN_VALUE, Float.MAX_VALUE, // 6,7
|
||||
-Float.MIN_VALUE, -Float.MAX_VALUE, // 8,9
|
||||
};
|
||||
|
||||
private DfpField field;
|
||||
private RandomGenerator generator;
|
||||
|
@ -1100,124 +1079,6 @@ public class FastMathTest {
|
|||
Assert.assertEquals(Float.NEGATIVE_INFINITY, FastMath.scalb(-1.1e-7f, 152), 0F);
|
||||
}
|
||||
|
||||
private static void reportError(String message) {
|
||||
final boolean fatal = false;
|
||||
if (fatal) {
|
||||
Assert.fail(message);
|
||||
} else {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static void check(Method mathMethod, Object[] params, Object expected, Object actual, int[] entries){
|
||||
if (!expected.equals(actual)){
|
||||
if (expected instanceof Double) {
|
||||
double exp = (Double) expected;
|
||||
// Need to discount 0 otherwise +0.0 == -0.0
|
||||
if (exp != 0 && MathUtils.equals(exp, (Double) actual, 1)) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
float exp = (Float) expected;
|
||||
// Need to discount 0 otherwise +0.0 == -0.0
|
||||
if (exp != 0 && MathUtils.equals(exp, ((Float) actual), 1)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(mathMethod.getReturnType().getSimpleName());
|
||||
sb.append(" ");
|
||||
sb.append(mathMethod.getName());
|
||||
sb.append("(");
|
||||
String sep = "";
|
||||
for(Object o : params){
|
||||
sb.append(sep);
|
||||
sb.append(o);
|
||||
sep=", ";
|
||||
}
|
||||
sb.append(") expected ");
|
||||
sb.append(expected);
|
||||
sb.append(" actual ");
|
||||
sb.append(actual);
|
||||
sb.append(" entries ");
|
||||
sb.append(Arrays.toString(entries));
|
||||
reportError(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static class CompareSpecials {
|
||||
private final Object[] specialvalues;
|
||||
CompareSpecials(Object[] values){
|
||||
specialvalues=values;
|
||||
}
|
||||
void compareSpecials(Method mathMethod, Method fastMethod, Type[] types) throws Exception {
|
||||
Object[] params = new Object[types.length];
|
||||
int entry1 = 0;
|
||||
int[] entries = new int[types.length];
|
||||
for(Object d : specialvalues) {
|
||||
entry1++;
|
||||
params[0] = d;
|
||||
entries[0] = entry1;
|
||||
if (params.length > 1){
|
||||
int entry2 = 0;
|
||||
for(Object d1 : specialvalues) {
|
||||
entry2++;
|
||||
params[1] = d1;
|
||||
entries[1] = entry2;
|
||||
compare(mathMethod, fastMethod, params, entries);
|
||||
}
|
||||
} else {
|
||||
compare(mathMethod, fastMethod, params, entries);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void compare(Method mathMethod, Method fastMethod,
|
||||
Object[] params, int[] entries) throws IllegalAccessException,
|
||||
InvocationTargetException {
|
||||
try {
|
||||
Object expected = mathMethod.invoke(mathMethod, params);
|
||||
Object actual = fastMethod.invoke(mathMethod, params);
|
||||
check(mathMethod, params, expected, actual, entries);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Assert.fail(mathMethod+" "+e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void testSpecialCases(Class<?> type, CompareSpecials comparer) throws Exception {
|
||||
Method math[] = StrictMath.class.getDeclaredMethods();
|
||||
for(Method mathMethod : math) {
|
||||
Type ret = mathMethod.getGenericReturnType();
|
||||
if (ret.equals(type) && Modifier.isPublic(mathMethod.getModifiers())){
|
||||
Type []params = mathMethod.getGenericParameterTypes();
|
||||
if (params.length >=1 && params[0].equals(type)) {
|
||||
Method fastMethod = null;
|
||||
String name = mathMethod.getName();
|
||||
try {
|
||||
fastMethod = FastMath.class.getDeclaredMethod(name, (Class[]) params);
|
||||
if (Modifier.isPublic(fastMethod.getModifiers())) {
|
||||
comparer.compareSpecials(mathMethod, fastMethod, params);
|
||||
} else {
|
||||
System.out.println("Cannot find public FastMath method corresponding to: "+mathMethod);
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
System.out.println("Cannot find FastMath method corresponding to: "+mathMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloatSpecialCases() throws Exception {
|
||||
testSpecialCases(float.class, new CompareSpecials(FLOAT_SPECIAL_VALUES));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleSpecialCases() throws Exception {
|
||||
testSpecialCases(double.class, new CompareSpecials(DOUBLE_SPECIAL_VALUES));
|
||||
}
|
||||
|
||||
private boolean compareClassMethods(Class<?> class1, Class<?> class2){
|
||||
boolean allfound = true;
|
||||
for(Method method1 : class1.getDeclaredMethods()){
|
||||
|
@ -1237,7 +1098,7 @@ public class FastMathTest {
|
|||
@Test
|
||||
public void checkMissingFastMathClasses() {
|
||||
boolean ok = compareClassMethods(StrictMath.class, FastMath.class);
|
||||
// TODO Assert.assertTrue("FastMath should implement all StrictMath methods", ok);
|
||||
Assert.assertTrue("FastMath should implement all StrictMath methods", ok);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
|
|
Loading…
Reference in New Issue