Home | History | Annotate | Download | only in math
      1 package libcore.java.math;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.InputStreamReader;
      5 import java.util.Collections;
      6 import java.util.HashSet;
      7 import java.util.Set;
      8 
      9 /**
     10  * Standard single-input test framework for csv math tests
     11  */
     12 public abstract class CSVTest extends junit.framework.TestCase {
     13     /*
     14      * csv file should have the following format:
     15      * function,expected_output,input,extra_info
     16      * e.g. cos,-0x1.0000000000000p+0,0x1.921fb54442d18p+1,cos(pi)
     17      * for two input: function,expected_output,input1,input2,extra
     18      * vogar classpath: obj/JAVA_LIBRARIES/core-tests-support_intermediates/javalib.jar
     19      */
     20 
     21     /**
     22      * This is a set of functions in java.Math/StrictMath that take two inputs.
     23      * These functions will call run2InputTest; others will call runTest.
     24      */
     25     protected static final Set<String> twoInputFunctions;
     26     static {
     27         Set<String> twoInFunc = new HashSet<String>();
     28         twoInFunc.add("atan2");
     29         twoInFunc.add("copySign");
     30         twoInFunc.add("hypot");
     31         twoInFunc.add("IEEEremainder");
     32         twoInFunc.add("max");
     33         twoInFunc.add("min");
     34         twoInFunc.add("nextAfter");
     35         twoInFunc.add("pow");
     36         twoInFunc.add("scalb");
     37         twoInputFunctions = Collections.unmodifiableSet(twoInFunc);
     38     }
     39 
     40     void TestCSVInputs(String[] csvFileNames) throws Exception {
     41         int totalTests = 0;
     42         for (String csvFileName : csvFileNames) {
     43             String line = "";
     44             BufferedReader br = null;
     45 
     46             try {
     47                 br = new BufferedReader(new InputStreamReader(
     48                         getClass().getResourceAsStream(csvFileName)));
     49                 while ((line = br.readLine()) != null) {
     50                     if (line.charAt(0) != '#') {
     51                         String[] testCase = line.split(",");
     52                         runTest(testCase);
     53                         totalTests++;
     54                     }
     55                 }
     56             } finally {
     57                 if (br != null) {
     58                     br.close();
     59                 }
     60             }
     61         }
     62         System.out.println("Completed running " + totalTests + " tests");
     63     }
     64 
     65     protected void runTest(String[] testCase) throws Exception {
     66         String function = testCase[0];
     67         double expectedOutput = Double.parseDouble(testCase[1]);
     68         double input = Double.parseDouble(testCase[2]);
     69         String extra = "";
     70         if (twoInputFunctions.contains(function)) {
     71             double input2 = Double.parseDouble(testCase[3]);
     72             if (testCase.length > 4) {
     73                 extra = testCase[4];
     74             }
     75             run2InputTest(function, expectedOutput, input, input2, extra);
     76         } else {
     77             if (testCase.length > 3) {
     78                 extra = testCase[3];
     79             }
     80             runTest(function, expectedOutput, input, extra);
     81         }
     82     }
     83 
     84     abstract void runTest(String func, double expectedOutput, double input,
     85             String extra) throws Exception;
     86 
     87     abstract void run2InputTest(String func, double expectedOutput, double input1, double input2, String extra) throws Exception;
     88 }