1 package org.apache.regexp; 2 3 59 60 import java.io.BufferedReader ; 61 import java.io.FileReader ; 62 import java.io.InputStreamReader ; 63 import java.io.PrintWriter ; 64 import java.io.File ; 65 import java.io.ByteArrayOutputStream ; 66 import java.io.ObjectOutputStream ; 67 import java.io.ByteArrayInputStream ; 68 import java.io.ObjectInputStream ; 69 70 79 public class RETest 80 { 81 static final boolean showSuccesses = false; 83 84 static final String NEW_LINE = System.getProperty( "line.separator" ); 86 87 RE r = new RE(); 89 REDebugCompiler compiler = new REDebugCompiler(); 90 91 97 public static void main(String [] args) 98 { 99 try 100 { 101 test( args ); 102 } 103 catch (Exception e) 104 { 105 e.printStackTrace(); 106 } 107 } 108 109 114 public static boolean test( String [] args ) throws Exception 115 { 116 RETest test = new RETest(); 117 if (args.length == 2) 119 { 120 test.runInteractiveTests(args[1]); 121 } 122 else if (args.length == 1) 123 { 124 test.runAutomatedTests(args[0]); 126 } 127 else 128 { 129 System.out.println( "Usage: RETest ([-i] [regex]) ([/path/to/testfile.txt])" ); 130 System.out.println( "By Default will run automated tests from file 'docs/RETest.txt' ..." ); 131 System.out.println(); 132 test.runAutomatedTests("docs/RETest.txt"); 133 } 134 return test.failures == 0; 135 } 136 137 140 public RETest() 141 { 142 } 143 144 148 void runInteractiveTests(String expr) 149 { 150 try 151 { 152 r.setProgram(compiler.compile(expr)); 154 155 say("" + NEW_LINE + "" + expr + "" + NEW_LINE + ""); 157 158 PrintWriter writer = new PrintWriter ( System.out ); 160 compiler.dumpProgram( writer ); 161 writer.flush(); 162 163 boolean running = true; 164 while ( running ) 166 { 167 BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 169 System.out.print("> "); 170 System.out.flush(); 171 String match = br.readLine(); 172 173 if ( match != null ) 174 { 175 if (r.match(match)) 177 { 178 say("Match successful."); 179 } 180 else 181 { 182 say("Match failed."); 183 } 184 185 showParens(r); 187 } 188 else 189 { 190 running = false; 191 System.out.println(); 192 } 193 } 194 } 195 catch (Exception e) 196 { 197 say("Error: " + e.toString()); 198 e.printStackTrace(); 199 } 200 } 201 202 206 void die(String s) 207 { 208 say("FATAL ERROR: " + s); 209 System.exit(0); 210 } 211 212 217 void fail(String s) 218 { 219 failures++; 220 say("" + NEW_LINE + ""); 221 say("*******************************************************"); 222 say("********************* FAILURE! **********************"); 223 say("*******************************************************"); 224 say("" + NEW_LINE + ""); 225 say(s); 226 say(""); 227 PrintWriter writer = new PrintWriter ( System.out ); 229 compiler.dumpProgram( writer ); 230 writer.flush(); 231 say("" + NEW_LINE + ""); 232 } 233 234 238 void success(String s) 239 { 240 if (showSuccesses) 241 { 242 show(); 243 say("Success: " + s); 244 } 245 } 246 247 251 void say(String s) 252 { 253 System.out.println (s); 254 } 255 256 259 void show() 260 { 261 say("" + NEW_LINE + "-----------------------" + NEW_LINE + ""); 262 say("Expression #" + (n) + " \"" + expr + "\" "); 263 } 264 265 269 void showParens(RE r) 270 { 271 for (int i = 0; i < r.getParenCount(); i++) 273 { 274 say("$" + i + " = " + r.getParen(i)); 276 } 277 } 278 279 char[] re1Instructions = 281 { 282 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041, 283 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047, 284 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000, 285 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000, 286 0x0000, 287 }; 288 289 REProgram re1 = new REProgram(re1Instructions); 290 291 294 String expr; 295 int n = 0; 296 297 300 int failures = 0; 301 302 306 void runAutomatedTests(String testDocument) throws Exception 307 { 308 long ms = System.currentTimeMillis(); 309 310 RE r = new RE(re1); 312 say("a*b"); 313 say("aaaab = " + r.match("aaab")); 314 showParens(r); 315 say("b = " + r.match("b")); 316 showParens(r); 317 say("c = " + r.match("c")); 318 showParens(r); 319 say("ccccaaaaab = " + r.match("ccccaaaaab")); 320 showParens(r); 321 322 r = new RE("a*b"); 323 String [] s = r.split("xxxxaabxxxxbyyyyaaabzzz"); 324 r = new RE("x+"); 325 s = r.grep(s); 326 for (int i = 0; i < s.length; i++) 327 { 328 System.out.println ("s[" + i + "] = " + s[i]); 329 } 330 331 r = new RE("a*b"); 332 String s1 = r.subst("aaaabfooaaabgarplyaaabwackyb", "-"); 333 System.out.println ("s = " + s1); 334 335 runAutomatedTests(); 337 338 File testInput = new File (testDocument); 340 if (! testInput.exists()) 341 throw new Exception ("Could not find: " + testDocument); 342 BufferedReader br = new BufferedReader (new FileReader (testInput)); 343 try 344 { 345 while (br.ready()) 347 { 348 String number = ""; 350 String yesno; 351 while (br.ready()) 352 { 353 number = br.readLine(); 354 if (number == null) 355 { 356 break; 357 } 358 number = number.trim(); 359 if (number.startsWith("#")) 360 { 361 break; 362 } 363 if (!number.equals("")) 364 { 365 System.out.println ("Script error. Line = " + number); 366 System.exit(0); 367 } 368 } 369 370 if (!br.ready()) 372 { 373 break; 374 } 375 376 expr = br.readLine(); 378 n++; 379 say(""); 380 say(n + ". " + expr); 381 say(""); 382 383 try 385 { 386 r.setProgram(compiler.compile(expr)); 387 } 388 389 catch (Exception e) 391 { 392 yesno = br.readLine().trim(); 394 395 if (yesno.equals("ERR")) 397 { 398 say(" Match: ERR"); 399 success("Produces an error (" + e.toString() + "), as expected."); 400 continue; 401 } 402 403 String message = e.getMessage() == null ? e.toString() : e.getMessage(); 405 fail("Produces an unexpected exception \"" + message + "\""); 406 e.printStackTrace(); 407 } 408 catch (Error e) 409 { 410 fail("Compiler threw fatal error \"" + e.getMessage() + "\""); 412 e.printStackTrace(); 413 } 414 415 String matchAgainst = br.readLine().trim(); 417 say(" Match against: '" + matchAgainst + "'"); 418 419 if (matchAgainst.equals("ERR")) 421 { 422 fail("Was expected to be an error, but wasn't."); 423 continue; 424 } 425 426 try 428 { 429 boolean b = r.match(matchAgainst); 431 432 yesno = br.readLine().trim(); 434 435 if (b) 437 { 438 say(" Match: YES"); 440 441 if (yesno.equals("NO")) 443 { 444 fail("Matched \"" + matchAgainst + "\", when not expected to."); 445 } 446 else 447 if (yesno.equals("YES")) 448 { 449 success("Matched \"" + matchAgainst + "\", as expected:"); 451 452 if (showSuccesses) 454 { 455 showParens(r); 456 } 457 458 say(" Paren count: " + r.getParenCount()); 459 460 for (int p = 0; p < r.getParenCount(); p++) 462 { 463 String register = br.readLine().trim(); 465 say(" Paren " + p + " : " + r.getParen(p)); 466 467 if (register.length() == 0 && r.getParen(p) == null) 469 { 470 } else 472 if (!register.equals(r.getParen(p))) 473 { 474 fail("Register " + p + " should be = \"" + register + "\", but is \"" + r.getParen(p) + "\" instead."); 476 } 477 } 478 } 479 else 480 { 481 die("Test script error!"); 483 } 484 } 485 else 486 { 487 say(" Match: NO"); 489 490 if (yesno.equals("YES")) 492 { 493 fail("Did not match \"" + matchAgainst + "\", when expected to."); 495 } 496 else 497 if (yesno.equals("NO")) 498 { 499 success("Did not match \"" + matchAgainst + "\", as expected."); 501 } 502 else 503 { 504 die("Test script error!"); 506 } 507 } 508 } 509 510 catch(Exception e) 512 { 513 fail("Matcher threw exception: " + e.toString()); 514 e.printStackTrace(); 515 } 516 517 catch(Error e) 519 { 520 fail("Matcher threw fatal error \"" + e.getMessage() + "\""); 521 e.printStackTrace(); 522 } 523 } 524 } 525 finally 526 { 527 br.close(); 528 } 529 530 System.out.println( NEW_LINE + NEW_LINE + "Match time = " + (System.currentTimeMillis() - ms) + " ms."); 532 533 System.out.println( NEW_LINE + "Tests complete. " + n + " tests, " + failures + " failure(s)."); 535 } 536 537 541 void runAutomatedTests() throws Exception 542 { 543 RE r = new RE("(a*)b"); 545 say("Serialized/deserialized (a*)b"); 546 ByteArrayOutputStream out = new ByteArrayOutputStream (128); 547 new ObjectOutputStream (out).writeObject(r); 548 ByteArrayInputStream in = new ByteArrayInputStream (out.toByteArray()); 549 r = (RE)new ObjectInputStream (in).readObject(); 550 if (!r.match("aaab")) { 551 fail("Did not match 'aaab' with deserialized RE."); 552 } 553 say("aaaab = true"); 554 showParens(r); 555 556 out.reset(); 558 say("Deserialized (a*)b"); 559 new ObjectOutputStream (out).writeObject(r); 560 in = new ByteArrayInputStream (out.toByteArray()); 561 r = (RE)new ObjectInputStream (in).readObject(); 562 if (r.getParenCount() != 0) { 563 fail("Has parens after deserialization."); 564 } 565 if (!r.match("aaab")) { 566 fail("Did not match 'aaab' with deserialized RE."); 567 } 568 say("aaaab = true"); 569 showParens(r); 570 571 r = new RE("abc(\\w*)"); 573 say("MATCH_CASEINDEPENDENT abc(\\w*)"); 574 r.setMatchFlags(RE.MATCH_CASEINDEPENDENT); 575 say("abc(d*)"); 576 if (!r.match("abcddd")) { 577 fail("Did not match 'abcddd'."); 578 } 579 say("abcddd = true"); 580 showParens(r); 581 582 if (!r.match("aBcDDdd")) { 583 fail("Did not match 'aBcDDdd'."); 584 } 585 say("aBcDDdd = true"); 586 showParens(r); 587 588 if (!r.match("ABCDDDDD")) { 589 fail("Did not match 'ABCDDDDD'."); 590 } 591 say("ABCDDDDD = true"); 592 showParens(r); 593 } 594 } 595 | Popular Tags |