1package libcore.java.math; 2 3import java.lang.reflect.Method; 4import java.util.Collections; 5import java.util.HashMap; 6import java.util.Map; 7 8/** 9 * Tests functions in java.lang.Math 10 * Looks for the filenames in csvFileNames in tests/resources 11 * Tests functions and numbers found in those files. 12 * Run: vogar --classpath out/target/common/obj/JAVA_LIBRARIES/core-tests-support_intermediates/javalib.jar 13 * libcore/luni/src/test/java/libcore/java/math/RunCSVTests.java 14 */ 15public class RunCSVTests extends CSVTest { 16 /** Stores ulps of error allowed for each function, if not 1 ulp.*/ 17 private static final Map<String, Double> UlpMap; 18 static { 19 final HashMap<String, Double> funcUlps = new HashMap<String, Double>(); 20 funcUlps.put("sinh", 2.5); 21 funcUlps.put("cosh", 2.5); 22 funcUlps.put("tanh", 2.5); 23 funcUlps.put("abs", 0.0); 24 funcUlps.put("signum", 0.0); 25 funcUlps.put("getExponent", 0.0); 26 funcUlps.put("toRadians", 0.0); 27 funcUlps.put("toDegrees", 0.0); 28 funcUlps.put("sqrt", 0.0); 29 funcUlps.put("ceil", 0.0); 30 funcUlps.put("floor", 0.0); 31 funcUlps.put("rint", 0.0); 32 funcUlps.put("atan2", 2.0); 33 funcUlps.put("round", 0.0); 34 funcUlps.put("max", 0.0); 35 funcUlps.put("min", 0.0); 36 funcUlps.put("copySign", 0.0); 37 funcUlps.put("nextAfter", 0.0); 38 funcUlps.put("scalb", 0.0); 39 UlpMap = Collections.unmodifiableMap(funcUlps); 40 } 41 42 public static final String[] csvFileNames = { "/math_tests.csv", 43 "/math_important_numbers.csv", "/math_java_only.csv" }; 44 45 public void test_csv() throws Exception { 46 this.TestCSVInputs(csvFileNames); 47 } 48 49 /** 50 * Runs a standard single-input test using assertEquals. 51 * Allows error based on UlpMap, but defaults to 1 ulp. 52 */ 53 @Override 54 void runTest(String func, double expectedOutput, double input, String extra) 55 throws Exception { 56 Class<Math> mathClass = Math.class; 57 Method m = mathClass.getMethod(func, new Class[] { Double.TYPE }); 58 Object returnValue = m.invoke(null, input); 59 60 double allowedError; 61 if (UlpMap.containsKey(func)) { 62 allowedError = UlpMap.get(func)*Math.ulp(expectedOutput); 63 } else { 64 allowedError = Math.ulp(expectedOutput); 65 } 66 67 try { 68 assertEquals(extra + ": " + m + ": " + input + ": ", expectedOutput, 69 (double) returnValue, allowedError); 70 } catch (ClassCastException e) { 71 assertEquals(extra + ": " + m + ": " + input + ": ", (int) expectedOutput, 72 (int) returnValue, allowedError); 73 } 74 } 75 76 /** 77 * Runs a 2-input test using assertEquals. 78 * Allows error based on UlpMap, but defaults to 1 ulp. 79 */ 80 @Override 81 void run2InputTest(String func, double expectedOutput, double input1, 82 double input2, String extra) throws Exception { 83 Class<Math> mathClass = Math.class; 84 Method m; 85 Object returnValue; 86 if (func.equals("scalb")) { 87 m = mathClass.getMethod(func, new Class[] { Double.TYPE, Integer.TYPE }); 88 returnValue = m.invoke(null, input1, (int) input2); 89 } else { 90 m = mathClass.getMethod(func, new Class[] { Double.TYPE, Double.TYPE }); 91 returnValue = m.invoke(null, input1, input2); 92 } 93 94 double allowedError; 95 if (UlpMap.containsKey(func)) { 96 allowedError = UlpMap.get(func)*Math.ulp(expectedOutput); 97 } else { 98 allowedError = Math.ulp(expectedOutput); 99 } 100 101 try { 102 assertEquals(extra + ": " + m + ": ", expectedOutput, (double) returnValue, 103 allowedError); 104 } catch (ClassCastException e) { 105 assertEquals(extra + ": " + m + ": ", (int) expectedOutput, (int) returnValue, 106 allowedError); 107 } 108 } 109} 110