summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/math/RunCSVTests.java
blob: 0c107f1e16738dbe293cf73c1d35acad8e5cd094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package libcore.java.math;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Tests functions in java.lang.Math
 * Looks for the filenames in csvFileNames in tests/resources
 * Tests functions and numbers found in those files.
 * Run: vogar --classpath out/target/common/obj/JAVA_LIBRARIES/core-tests-support_intermediates/javalib.jar
 * libcore/luni/src/test/java/libcore/java/math/RunCSVTests.java
 */
public class RunCSVTests extends CSVTest {
    /** Stores ulps of error allowed for each function, if not 1 ulp.*/
    private static final Map<String, Double> UlpMap;
    static {
        final HashMap<String, Double> funcUlps = new HashMap<String, Double>();
        funcUlps.put("sinh", 2.5);
        funcUlps.put("cosh", 2.5);
        funcUlps.put("tanh", 2.5);
        funcUlps.put("abs", 0.0);
        funcUlps.put("signum", 0.0);
        funcUlps.put("getExponent", 0.0);
        funcUlps.put("toRadians", 0.0);
        funcUlps.put("toDegrees", 0.0);
        funcUlps.put("sqrt", 0.0);
        funcUlps.put("ceil", 0.0);
        funcUlps.put("floor", 0.0);
        funcUlps.put("rint", 0.0);
        funcUlps.put("atan2", 2.0);
        funcUlps.put("round", 0.0);
        funcUlps.put("max", 0.0);
        funcUlps.put("min", 0.0);
        funcUlps.put("copySign", 0.0);
        funcUlps.put("nextAfter", 0.0);
        funcUlps.put("scalb", 0.0);
        UlpMap = Collections.unmodifiableMap(funcUlps);
    }

    public static final String[] csvFileNames = { "/math_tests.csv",
            "/math_important_numbers.csv", "/math_java_only.csv" };

    public void test_csv() throws Exception {
        this.TestCSVInputs(csvFileNames);
    }

    /**
     * Runs a standard single-input test using assertEquals.
     * Allows error based on UlpMap, but defaults to 1 ulp.
     */
    @Override
    void runTest(String func, double expectedOutput, double input, String extra)
            throws Exception {
        Class<Math> mathClass = Math.class;
        Method m = mathClass.getMethod(func, new Class[] { Double.TYPE });
        Object returnValue = m.invoke(null, input);

        double allowedError;
        if (UlpMap.containsKey(func)) {
            allowedError = UlpMap.get(func)*Math.ulp(expectedOutput);
        } else {
            allowedError = Math.ulp(expectedOutput);
        }

        try {
            assertEquals(extra + ": " + m + ": " + input + ": ", expectedOutput,
                         (double) returnValue, allowedError);
        } catch (ClassCastException e) {
            assertEquals(extra + ": " + m + ": " + input + ": ", (int) expectedOutput,
                         (int) returnValue, allowedError);
        }
    }

    /**
     * Runs a 2-input test using assertEquals.
     * Allows error based on UlpMap, but defaults to 1 ulp.
     */
    @Override
    void run2InputTest(String func, double expectedOutput, double input1,
            double input2, String extra) throws Exception {
        Class<Math> mathClass = Math.class;
        Method m;
        Object returnValue;
        if (func.equals("scalb")) {
            m = mathClass.getMethod(func, new Class[] { Double.TYPE, Integer.TYPE });
            returnValue = m.invoke(null, input1, (int) input2);
        } else {
            m = mathClass.getMethod(func, new Class[] { Double.TYPE, Double.TYPE });
            returnValue = m.invoke(null, input1, input2);
        }

        double allowedError;
        if (UlpMap.containsKey(func)) {
            allowedError = UlpMap.get(func)*Math.ulp(expectedOutput);
        } else {
            allowedError = Math.ulp(expectedOutput);
        }

        try {
            assertEquals(extra + ": " + m + ": ", expectedOutput, (double) returnValue,
                         allowedError);
        } catch (ClassCastException e) {
            assertEquals(extra + ": " + m + ": ", (int) expectedOutput, (int) returnValue,
                         allowedError);
        }
    }
}