fork download
  1. import java.util.Arrays;
  2. import java.util.ArrayList;
  3. import java.util.Random;
  4.  
  5. import java.util.regex.Pattern;
  6. import java.util.regex.Matcher;
  7.  
  8. import java.lang.reflect.Method;
  9.  
  10. import java.lang.annotation.Retention;
  11. import java.lang.annotation.RetentionPolicy;
  12. import java.lang.annotation.Target;
  13. import java.lang.annotation.ElementType;
  14.  
  15. class Test_15187583 {
  16. @Retention(RetentionPolicy.RUNTIME)
  17. @Target(ElementType.METHOD)
  18. private @interface Test {};
  19.  
  20. // Number of times to run all methods
  21. private static final int NUM_OUTER = 3;
  22. // Number of times to run each method
  23. private static final int NUM_REPEAT = 10;
  24.  
  25. public static void main(String args[]) {
  26. runTestReflection();
  27. }
  28.  
  29. private static Pattern p1 = Pattern.compile("[0123]123456|98765");
  30.  
  31. @Test
  32. public static int match_1(String input) {
  33. Matcher m = p1.matcher(input);
  34. int count = 0;
  35.  
  36. while (m.find()) {
  37. count++;
  38. }
  39.  
  40. return count;
  41. }
  42.  
  43. private static Pattern p2a = Pattern.compile("[0123]123456");
  44. private static Pattern p2b = Pattern.compile("98765");
  45.  
  46. @Test
  47. public static int match_2(String input) {
  48. int count = 0;
  49.  
  50. Matcher m1 = p2a.matcher(input);
  51.  
  52. while (m1.find()) {
  53. count++;
  54. }
  55.  
  56. Matcher m2 = p2b.matcher(input);
  57.  
  58. while (m2.find()) {
  59. count++;
  60. }
  61.  
  62. return count;
  63. }
  64.  
  65. // Half definitely has a match
  66. private static final double HAS_PATTERN = -0.50;
  67. private static Random rand = new Random();
  68.  
  69. // Number of strings to test on
  70. private static final int NUM_INPUT = 10000;
  71. // From 10 - 90 characters
  72. private static final int LENGTH = 50;
  73. private static final int LENGTH_FLUCTUATION = 40;
  74.  
  75. private static final String[] matchPatterns = {"0123456","1123456","2123456","3123456","98765"};
  76.  
  77. // When the string should have a match, the number of matches in the string will be at least 1 (and on average around 1)
  78. // When the string shouldn't have a match, there might be a match if the digit generated coincide with the match (low probability)
  79. public static String[] generateInput() {
  80. String output[] = new String[NUM_INPUT];
  81.  
  82. for (int i = 0; i < NUM_INPUT; i++) {
  83. StringBuilder s = new StringBuilder();
  84. int length = LENGTH - rand.nextInt(LENGTH_FLUCTUATION * 2 + 1) + LENGTH_FLUCTUATION;
  85.  
  86. if (rand.nextDouble() < HAS_PATTERN) {
  87. int patternToUse = rand.nextInt(matchPatterns.length);
  88. int randGenCount = length - matchPatterns[patternToUse].length();
  89.  
  90. for (int j = 0; j < randGenCount; j++) {
  91. s.append((char) ('0' + rand.nextInt(10)));
  92. }
  93.  
  94. int insertPos = rand.nextInt(randGenCount);
  95. s.insert(insertPos, matchPatterns[patternToUse]);
  96.  
  97. } else {
  98. for (int j = 0; j < length; j++) {
  99. s.append((char) ('0' + rand.nextInt(10)));
  100. }
  101. }
  102.  
  103. assert(s.length() == length);
  104.  
  105. output[i] = s.toString();
  106. }
  107.  
  108. return output;
  109. }
  110.  
  111. private static ArrayList<Method> getTestMethods() {
  112. Class<?> klass = null;
  113. try {
  114. klass = Class.forName(Thread.currentThread().getStackTrace()[1].getClassName());
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. System.err.println("Something really bad happened. Bailling out...");
  118. System.exit(1);
  119. }
  120. Method[] methods = klass.getMethods();
  121. // System.out.println(klass);
  122. // System.out.println(Arrays.toString(methods));
  123.  
  124. ArrayList<Method> testMethods = new ArrayList<Method>();
  125.  
  126. for (Method method: methods) {
  127. if (method.isAnnotationPresent(Test.class)) {
  128. testMethods.add(method);
  129. }
  130. }
  131.  
  132. // System.out.println(testMethods);
  133.  
  134. return testMethods;
  135. }
  136.  
  137. public static void runTestReflection() {
  138. ArrayList<Method> methods = getTestMethods();
  139.  
  140. for (int t = 0; t < NUM_OUTER; t++) {
  141. String inputSet[] = generateInput();
  142. Integer compareSet[] = null;
  143.  
  144. for (Method method: methods) {
  145. System.out.print(method.getName() + ": ");
  146. if (compareSet == null) {
  147. compareSet = new Integer[inputSet.length];
  148.  
  149. try {
  150. for (int i = 0; i < inputSet.length; i++) {
  151. compareSet[i] = (Integer) method.invoke(null, inputSet[i]);
  152. }
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. System.exit(1);
  156. }
  157. } else {
  158. try {
  159. for (int i = 0; i < inputSet.length; i++) {
  160. Integer result = (Integer) method.invoke(null, inputSet[i]);
  161. if (!compareSet[i].equals(result)) {
  162. System.err.println(compareSet[i] + " != " + result + " for input " + inputSet[i]);
  163. throw new AssertionError();
  164. }
  165. }
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. System.exit(1);
  169. }
  170. }
  171.  
  172. long sum = 0;
  173. for (int i = 0; i < NUM_REPEAT; i++) {
  174. long start, end;
  175. Object result;
  176.  
  177. try {
  178.  
  179. start = System.nanoTime();
  180. for (String input: inputSet) {
  181. result = method.invoke(null, input);
  182. }
  183. end = System.nanoTime();
  184.  
  185. System.out.print((end - start) / 1000 + " ");
  186. sum += (end - start) / 1000;
  187. } catch (Exception e) {
  188. e.printStackTrace();
  189. }
  190. }
  191.  
  192. System.out.println("| " + sum / NUM_REPEAT);
  193. }
  194.  
  195. System.out.println();
  196. }
  197. }
  198. }
Success #stdin #stdout 2.65s 380864KB
stdin
Standard input is empty
stdout
match_1: 55167 42285 44841 41449 40406 42426 41126 41579 40329 40737 | 43034
match_2: 29754 29971 29839 29852 29835 29828 29616 30063 30269 29980 | 29900

match_1: 48032 43107 45191 41120 41625 41216 42163 41300 41322 41666 | 42674
match_2: 30319 29777 30203 30175 30353 30570 30514 30050 30491 30539 | 30299

match_1: 43698 45289 41547 41292 42003 41359 42032 41330 41990 41332 | 42187
match_2: 30408 30442 30453 29811 30455 30490 30481 30523 30460 29856 | 30337