1 package com.sun.org.apache.regexp.internal; 2 3 59 60 import java.io.*; 61 62 70 public class RETest 71 { 72 RE r = new RE(); 74 REDebugCompiler compiler = new REDebugCompiler(); 75 76 static final boolean showSuccesses = false; 78 79 85 public static void _main(String [] arg) 86 { 87 try 88 { 89 test(); 91 } 92 catch (Exception e) 93 { 94 e.printStackTrace(); 95 } 96 } 97 98 103 public static boolean test() throws Exception 104 { 105 RETest test = new RETest(); 106 test.runAutomatedTests("docs/RETest.txt"); 107 return test.failures == 0; 108 } 109 110 113 public RETest() 114 { 115 } 116 117 121 public RETest(String [] arg) 122 { 123 try 124 { 125 if (arg.length == 2) 127 { 128 runInteractiveTests(arg[1]); 129 } 130 else if (arg.length == 1) 131 { 132 runAutomatedTests(arg[0]); 134 } 135 else 136 { 137 System.out.println ( "Usage: RETest ([-i] [regex]) ([/path/to/testfile.txt])" ); 138 } 139 } 140 catch (Exception e) 141 { 142 e.printStackTrace(); 143 } 144 } 145 146 150 void runInteractiveTests(String expr) 151 { 152 try 153 { 154 r.setProgram(compiler.compile(expr)); 156 157 say("\n" + expr + "\n"); 159 160 compiler.dumpProgram(new PrintWriter(System.out)); 162 163 while (true) 165 { 166 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 168 System.out.print("> "); 169 System.out.flush(); 170 String match = br.readLine(); 171 172 if (r.match(match)) 174 { 175 say("Match successful."); 176 } 177 else 178 { 179 say("Match failed."); 180 } 181 182 showParens(r); 184 } 185 } 186 catch (Exception e) 187 { 188 say("Error: " + e.toString()); 189 e.printStackTrace(); 190 } 191 } 192 193 197 void die(String s) 198 { 199 say("FATAL ERROR: " + s); 200 System.exit(0); 201 } 202 203 207 void fail(String s) 208 { 209 failures++; 210 say("\n"); 211 say("*******************************************************"); 212 say("********************* FAILURE! **********************"); 213 say("*******************************************************"); 214 say("\n"); 215 say(s); 216 say(""); 217 compiler.dumpProgram(new PrintWriter(System.out)); 218 say("\n"); 219 } 220 221 225 void success(String s) 226 { 227 if (showSuccesses) 228 { 229 show(); 230 say("Success: " + s); 231 } 232 } 233 234 238 void say(String s) 239 { 240 System.out.println (s); 241 } 242 243 246 void show() 247 { 248 say("\n-----------------------\n"); 249 say("Expression #" + (n) + " \"" + expr + "\" "); 250 } 251 252 256 void showParens(RE r) 257 { 258 for (int i = 0; i < r.getParenCount(); i++) 260 { 261 say("$" + i + " = " + r.getParen(i)); 263 } 264 } 265 266 char[] re1Instructions = 268 { 269 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041, 270 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047, 271 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000, 272 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000, 273 0x0000, 274 }; 275 276 REProgram re1 = new REProgram(re1Instructions); 277 278 281 String expr; 282 int n = 0; 283 284 287 int failures = 0; 288 289 293 void runAutomatedTests(String testDocument) throws Exception 294 { 295 long ms = System.currentTimeMillis(); 296 297 RE r = new RE(re1); 299 say("a*b"); 300 say("aaaab = " + r.match("aaab")); 301 showParens(r); 302 say("b = " + r.match("b")); 303 showParens(r); 304 say("c = " + r.match("c")); 305 showParens(r); 306 say("ccccaaaaab = " + r.match("ccccaaaaab")); 307 showParens(r); 308 309 r = new RE("a*b"); 310 String [] s = r.split("xxxxaabxxxxbyyyyaaabzzz"); 311 r = new RE("x+"); 312 s = r.grep(s); 313 for (int i = 0; i < s.length; i++) 314 { 315 System.out.println ("s[" + i + "] = " + s[i]); 316 } 317 318 r = new RE("a*b"); 319 String s1 = r.subst("aaaabfooaaabgarplyaaabwackyb", "-"); 320 System.out.println ("s = " + s1); 321 322 File testInput = new File(testDocument); 324 if (! testInput.exists()) 325 throw new Exception ("Could not find: " + testDocument); 326 BufferedReader br = new BufferedReader(new FileReader(testInput)); 327 try 328 { 329 while (br.ready()) 331 { 332 String number = ""; 334 String yesno; 335 while (br.ready()) 336 { 337 number = br.readLine(); 338 if (number == null) 339 { 340 break; 341 } 342 number = number.trim(); 343 if (number.startsWith("#")) 344 { 345 break; 346 } 347 if (!number.equals("")) 348 { 349 System.out.println ("Script error. Line = " + number); 350 System.exit(0); 351 } 352 } 353 354 if (!br.ready()) 356 { 357 break; 358 } 359 360 expr = br.readLine(); 362 n++; 363 say(""); 364 say(n + ". " + expr); 365 say(""); 366 367 try 369 { 370 r.setProgram(compiler.compile(expr)); 371 } 372 373 catch (Exception e) 375 { 376 yesno = br.readLine().trim(); 378 379 if (yesno.equals("ERR")) 381 { 382 say(" Match: ERR"); 383 success("Produces an error (" + e.toString() + "), as expected."); 384 continue; 385 } 386 387 fail("Produces the unexpected error \"" + e.getMessage() + "\""); 389 } 390 catch (Error e) 391 { 392 fail("Compiler threw fatal error \"" + e.getMessage() + "\""); 394 e.printStackTrace(); 395 } 396 397 String matchAgainst = br.readLine().trim(); 399 say(" Match against: '" + matchAgainst + "'"); 400 401 if (matchAgainst.equals("ERR")) 403 { 404 fail("Was expected to be an error, but wasn't."); 405 continue; 406 } 407 408 try 410 { 411 boolean b = r.match(matchAgainst); 413 414 yesno = br.readLine().trim(); 416 417 if (b) 419 { 420 say(" Match: YES"); 422 423 if (yesno.equals("NO")) 425 { 426 fail("Matched \"" + matchAgainst + "\", when not expected to."); 427 } 428 else 429 if (yesno.equals("YES")) 430 { 431 success("Matched \"" + matchAgainst + "\", as expected:"); 433 434 if (showSuccesses) 436 { 437 showParens(r); 438 } 439 440 say(" Paren count: " + r.getParenCount()); 441 442 for (int p = 0; p < r.getParenCount(); p++) 444 { 445 String register = br.readLine().trim(); 447 say(" Paren " + p + " : " + r.getParen(p)); 448 449 if (!register.equals(r.getParen(p))) 451 { 452 fail("Register " + p + " should be = \"" + register + "\", but is \"" + r.getParen(p) + "\" instead."); 454 } 455 } 456 } 457 else 458 { 459 die("Test script error!"); 461 } 462 } 463 else 464 { 465 say(" Match: NO"); 467 468 if (yesno.equals("YES")) 470 { 471 fail("Did not match \"" + matchAgainst + "\", when expected to."); 473 } 474 else 475 if (yesno.equals("NO")) 476 { 477 success("Did not match \"" + matchAgainst + "\", as expected."); 479 } 480 else 481 { 482 die("Test script error!"); 484 } 485 } 486 } 487 488 catch (Exception e) 490 { 491 fail("Matcher threw exception: " + e.toString()); 492 e.printStackTrace(); 493 } 494 495 catch (Error e) 497 { 498 fail("Matcher threw fatal error \"" + e.getMessage() + "\""); 499 e.printStackTrace(); 500 } 501 } 502 } 503 finally 504 { 505 br.close(); 506 } 507 508 System.out.println ("\n\nMatch time = " + (System.currentTimeMillis() - ms) + " ms."); 510 511 System.out.println ("\nTests complete. " + n + " tests, " + failures + " failure(s)."); 513 } 514 } 515 | Popular Tags |