Guest User

RETester.java

a guest
Nov 19th, 2011
49
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3. import java.lang.reflect.Modifier;
  4. import java.text.DecimalFormat;
  5. import java.text.NumberFormat;
  6. import java.util.Locale;
  7. import java.util.regex.Pattern;
  8.  
  9. public class RETester {
  10.     public static final long TIME_LIMIT = 2000;
  11.  
  12.     public static void test(Class clazz, String pattern) {
  13.         Pattern p = Pattern.compile(pattern);
  14.         Method[] ms = clazz.getMethods();
  15.         int total = 0;
  16.         int success = 0;
  17.         for (Method m : ms)
  18.             if (Modifier.isPublic(m.getModifiers()) && p.matcher(m.getName()).matches()) {
  19.                 total++;
  20.                 if (test(clazz, m))
  21.                     success++;
  22.             }
  23.         System.out.println("Completed " + success + " of " + total + " tests successfully.");
  24.         if (success < total)
  25.             System.out.println("SOME TESTS HAVE FAILED.");
  26.     }
  27.  
  28.     public static boolean test(Class clazz, Method method) {
  29.         long time = System.currentTimeMillis();
  30.         Throwable outcome;
  31.         try {
  32.             Object instance = clazz.newInstance();
  33.             method.invoke(instance);
  34.             outcome = null;
  35.         } catch (InvocationTargetException e) {
  36.             outcome = e.getCause();
  37.         } catch (Throwable e) {
  38.             outcome = e;
  39.         }
  40.         time = System.currentTimeMillis() - time;
  41.         NumberFormat fmt = DecimalFormat.getInstance(Locale.US);
  42.         fmt.setMinimumFractionDigits(3);
  43.         System.out.print("Done " + method.getName() + " in " + fmt.format(time / 1000.0) + " sec - ");
  44.         if (outcome instanceof MismatchException) {
  45.             System.out.println("FAILED: " + outcome.getMessage());
  46.             return false;
  47.         } else if (outcome != null) {
  48.             System.out.println("FAILED: " + outcome);
  49.             outcome.printStackTrace(System.out);
  50.             return false;
  51.         } else if (time > TIME_LIMIT) {
  52.             System.out.println("TIME LIMIT EXCEEDED");
  53.             return false;
  54.         } else {
  55.             System.out.println("success");
  56.             return true;
  57.         }
  58.     }
  59.  
  60.     public static class MismatchException extends RuntimeException {
  61.         public MismatchException(String message) {
  62.             super(message);
  63.         }
  64.     }
  65.  
  66.     public static final double MAX_DOUBLE_ERROR = 1E-9;
  67.  
  68.     public static boolean doubleCompare(double a, double b) {
  69.         if (Double.isNaN(a)) {
  70.             return Double.isNaN(b);
  71.         } else if (Double.isInfinite(a)) {
  72.             if (a > 0) {
  73.                 return b > 0 && Double.isInfinite(b);
  74.             } else {
  75.                 return b < 0 && Double.isInfinite(b);
  76.             }
  77.         } else if (Double.isNaN(b) || Double.isInfinite(b)) {
  78.             return false;
  79.         } else if (Math.abs(b - a) < MAX_DOUBLE_ERROR) {
  80.             //always allow it to be off a little, regardless of scale
  81.             return true;
  82.         } else {
  83.             double min = Math.min(a * (1.0 - MAX_DOUBLE_ERROR),
  84.                     a * (1.0 + MAX_DOUBLE_ERROR));
  85.             double max = Math.max(a * (1.0 - MAX_DOUBLE_ERROR),
  86.                     a * (1.0 + MAX_DOUBLE_ERROR));
  87.             return b > min && b < max;
  88.         }
  89.     }
  90.  
  91.     public static boolean objectCompare(Object a, Object b) {
  92.         return a == null ? b == null : a.equals(b);
  93.     }
  94.  
  95.     public static void eq(double a, double b) {
  96.         if (!doubleCompare(a, b))
  97.             throw new MismatchException("expected " + b + ", received " + a);
  98.     }
  99.  
  100.     public static void eq(int a, int b) {
  101.         if (a != b)
  102.             throw new MismatchException("expected " + b + ", received " + a);
  103.     }
  104.  
  105.     public static void eq(char a, char b) {
  106.         if (a != b)
  107.             throw new MismatchException("expected '" + b + "', received '" + a + "'");
  108.     }
  109.  
  110.     public static void eq(long a, long b) {
  111.         if (a != b)
  112.             throw new MismatchException("expected " + b + "L, received " + a + "L");
  113.     }
  114.  
  115.     public static void eq(boolean a, boolean b) {
  116.         if (a != b)
  117.             throw new MismatchException("expected " + b + ", received " + a);
  118.     }
  119.  
  120.     public static void eq(String a, String b) {
  121.         if (!objectCompare(a, b))
  122.             throw new MismatchException("expected \"" + b + "\", received \"" + a + "\"");
  123.     }
  124.  
  125.     public static void eq(double[] a, double[] b) {
  126.         if (a.length != b.length)
  127.             throw new MismatchException("expected " + b.length + " elements, received " + a.length + " elements");
  128.         for (int i = 0; i < a.length; i++)
  129.             try {
  130.                 eq(a[i], b[i]);
  131.             } catch (MismatchException e) {
  132.                 throw new MismatchException("differ in position " + i + ": " + e.getMessage());
  133.             }
  134.     }
  135.  
  136.     public static void eq(int[] a, int[] b) {
  137.         if (a.length != b.length)
  138.             throw new MismatchException("expected " + b.length + " elements, received " + a.length + " elements");
  139.         for (int i = 0; i < a.length; i++)
  140.             try {
  141.                 eq(a[i], b[i]);
  142.             } catch (MismatchException e) {
  143.                 throw new MismatchException("differ in position " + i + ": " + e.getMessage());
  144.             }
  145.     }
  146.  
  147.     public static void eq(char[] a, char[] b) {
  148.         if (a.length != b.length)
  149.             throw new MismatchException("expected " + b.length + " elements, received " + a.length + " elements");
  150.         for (int i = 0; i < a.length; i++)
  151.             try {
  152.                 eq(a[i], b[i]);
  153.             } catch (MismatchException e) {
  154.                 throw new MismatchException("differ in position " + i + ": " + e.getMessage());
  155.             }
  156.     }
  157.  
  158.     public static void eq(long[] a, long[] b) {
  159.         if (a.length != b.length)
  160.             throw new MismatchException("expected " + b.length + " elements, received " + a.length + " elements");
  161.         for (int i = 0; i < a.length; i++)
  162.             try {
  163.                 eq(a[i], b[i]);
  164.             } catch (MismatchException e) {
  165.                 throw new MismatchException("differ in position " + i + ": " + e.getMessage());
  166.             }
  167.     }
  168.  
  169.     public static void eq(boolean[] a, boolean[] b) {
  170.         if (a.length != b.length)
  171.             throw new MismatchException("expected " + b.length + " elements, received " + a.length + " elements");
  172.         for (int i = 0; i < a.length; i++)
  173.             try {
  174.                 eq(a[i], b[i]);
  175.             } catch (MismatchException e) {
  176.                 throw new MismatchException("differ in position " + i + ": " + e.getMessage());
  177.             }
  178.     }
  179.  
  180.     public static void eq(String[] a, String[] b) {
  181.         if (a.length != b.length)
  182.             throw new MismatchException("expected " + b.length + " elements, received " + a.length + " elements");
  183.         for (int i = 0; i < a.length; i++)
  184.             try {
  185.                 eq(a[i], b[i]);
  186.             } catch (MismatchException e) {
  187.                 throw new MismatchException("differ in position " + i + ": " + e.getMessage());
  188.             }
  189.     }
  190. }
  191.  
  192.  
RAW Paste Data