⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 retest.java

📁 jakarta-regexp-1.5 正则表达式的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.regexp;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.PrintWriter;import java.io.StringReader;/** * Data driven (and optionally interactive) testing harness to exercise regular * expression compiler and matching engine. * * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a> * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> * @author <a href="mailto:gholam@xtra.co.nz">Michael McCallum</a> * @version $Id: RETest.java 518156 2007-03-14 14:31:26Z vgritsenko $ */public class RETest{    // True if we want to see output from success cases    static final boolean showSuccesses = false;    // A new line character.    static final String NEW_LINE = System.getProperty("line.separator");    // Construct a debug compiler    final REDebugCompiler compiler = new REDebugCompiler();    /**     * Main program entrypoint.  If an argument is given, it will be compiled     * and interactive matching will ensue.  If no argument is given, the     * file RETest.txt will be used as automated testing input.     * @param args Command line arguments (optional regular expression)     */    public static void main(String[] args)    {        try        {            if (!test( args )) {                System.exit(1);            }        }        catch (Exception e)        {            e.printStackTrace();            System.exit(1);        }    }    /**     * Testing entrypoint.     * @param args Command line arguments     * @exception Exception thrown in case of error     */    public static boolean test( String[] args ) throws Exception    {        RETest test = new RETest();        // Run interactive tests against a single regexp        if (args.length == 2)        {            test.runInteractiveTests(args[1]);        }        else if (args.length == 1)        {            // Run automated tests            test.runAutomatedTests(args[0]);        }        else        {            System.out.println( "Usage: RETest ([-i] [regex]) ([/path/to/testfile.txt])" );            System.out.println( "By Default will run automated tests from file 'docs/RETest.txt' ..." );            System.out.println();            test.runAutomatedTests("docs/RETest.txt");        }        return test.failures == 0;    }    /**     * Constructor     */    public RETest()    {    }    /**     * Compile and test matching against a single expression     * @param expr Expression to compile and test     */    void runInteractiveTests(String expr)    {        RE r = new RE();        try        {            // Compile expression            r.setProgram(compiler.compile(expr));            // Show expression            say("" + NEW_LINE + "" + expr + "" + NEW_LINE + "");            // Show program for compiled expression            PrintWriter writer = new PrintWriter( System.out );            compiler.dumpProgram( writer );            writer.flush();            boolean running = true;            // Test matching against compiled expression            while ( running )            {                // Read from keyboard                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                System.out.print("> ");                System.out.flush();                String match = br.readLine();                if ( match != null )                {                    // Try a match against the keyboard input                    if (r.match(match))                    {                        say("Match successful.");                    }                    else                    {                        say("Match failed.");                    }                    // Show subparen registers                    showParens(r);                }                else                {                    running = false;                    System.out.println();                }            }        }        catch (Exception e)        {            say("Error: " + e.toString());            e.printStackTrace();        }    }    /**     * Exit with a fatal error.     * @param s Last famous words before exiting     */    void die(String s)    {        say("FATAL ERROR: " + s);        System.exit(-1);    }    /**     * Fail with an error. Will print a big failure message to System.out.     *     * @param log Output before failure     * @param s Failure description     */    void fail(StringBuffer log, String s)    {        System.out.print(log.toString());        fail(s);    }    /**     * Fail with an error. Will print a big failure message to System.out.     *     * @param s Failure description     */    void fail(String s)    {        failures++;        say("" + NEW_LINE + "");        say("*******************************************************");        say("*********************  FAILURE!  **********************");        say("*******************************************************");        say("" + NEW_LINE + "");        say(s);        say("");        // make sure the writer gets flushed.        compiler.dumpProgram();        say("" + NEW_LINE + "");    }    /**     * Say something to standard out     * @param s What to say     */    void say(String s)    {        System.out.println(s);    }    /**     * Dump parenthesized subexpressions found by a regular expression matcher object     * @param r Matcher object with results to show     */    void showParens(RE r)    {        // Loop through each paren        for (int i = 0; i < r.getParenCount(); i++)        {            // Show paren register            say("$" + i + " = " + r.getParen(i));        }    }    /*     * number in automated test     */    int testCount = 0;    /*     * Count of failures in automated test     */    int failures = 0;    /**     * Run automated tests in RETest.txt file (from Perl 4.0 test battery)     * @exception Exception thrown in case of error     */    void runAutomatedTests(String testDocument) throws Exception    {        long ms = System.currentTimeMillis();        // Some unit tests        testPrecompiledRE();        testSplitAndGrep();        testSubst();        testOther();        // Test from script file        File testInput = new File(testDocument);        if (! testInput.exists()) {            throw new Exception ("Could not find: " + testDocument);        }        BufferedReader br = new BufferedReader(new FileReader(testInput));        try        {            // While input is available, parse lines            while (br.ready())            {                RETestCase testcase = getNextTestCase(br);                if (testcase != null) {                    testcase.runTest();                }            }        }        finally        {            br.close();        }        // Show match time        say(NEW_LINE + NEW_LINE + "Match time = " + (System.currentTimeMillis() - ms) + " ms.");        // Print final results        if (failures > 0) {            say("*************** THERE ARE FAILURES! *******************");        }        say("Tests complete.  " + testCount + " tests, " + failures + " failure(s).");    }    /**     * Run automated unit test     * @exception Exception thrown in case of error     */    void testOther() throws Exception    {        // Serialization test 1: Compile regexp and serialize/deserialize it        RE r = new RE("(a*)b");        say("Serialized/deserialized (a*)b");        ByteArrayOutputStream out = new ByteArrayOutputStream(128);        new ObjectOutputStream(out).writeObject(r);        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());        r = (RE)new ObjectInputStream(in).readObject();        if (!r.match("aaab"))        {            fail("Did not match 'aaab' with deserialized RE.");        } else {            say("aaaab = true");            showParens(r);        }        // Serialization test 2: serialize/deserialize used regexp        out.reset();        say("Deserialized (a*)b");        new ObjectOutputStream(out).writeObject(r);        in = new ByteArrayInputStream(out.toByteArray());        r = (RE)new ObjectInputStream(in).readObject();        if (r.getParenCount() != 0)        {            fail("Has parens after deserialization.");        }        if (!r.match("aaab"))        {            fail("Did not match 'aaab' with deserialized RE.");        } else {            say("aaaab = true");            showParens(r);        }        // Test MATCH_CASEINDEPENDENT        r = new RE("abc(\\w*)");        say("MATCH_CASEINDEPENDENT abc(\\w*)");        r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);        say("abc(d*)");        if (!r.match("abcddd"))        {            fail("Did not match 'abcddd'.");        } else {            say("abcddd = true");            showParens(r);        }        if (!r.match("aBcDDdd"))        {            fail("Did not match 'aBcDDdd'.");        } else {            say("aBcDDdd = true");            showParens(r);        }        if (!r.match("ABCDDDDD"))        {            fail("Did not match 'ABCDDDDD'.");        } else {            say("ABCDDDDD = true");            showParens(r);        }        r = new RE("(A*)b\\1");        r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);        if (!r.match("AaAaaaBAAAAAA"))        {            fail("Did not match 'AaAaaaBAAAAAA'.");        } else {            say("AaAaaaBAAAAAA = true");            showParens(r);        }        r = new RE("[A-Z]*");        r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);        if (!r.match("CaBgDe12"))        {            fail("Did not match 'CaBgDe12'.");        } else {            say("CaBgDe12 = true");            showParens(r);        }        // Test for eol/bol symbols.        r = new RE("^abc$");        if (r.match("\nabc")) {            fail("\"\\nabc\" matches \"^abc$\"");        }        // Test MATCH_MULTILINE. Test for eol/bol symbols.        r = new RE("^abc$", RE.MATCH_MULTILINE);        if (!r.match("\nabc")) {            fail("\"\\nabc\" doesn't match \"^abc$\"");        }        if (!r.match("\rabc")) {            fail("\"\\rabc\" doesn't match \"^abc$\"");        }        if (!r.match("\r\nabc")) {            fail("\"\\r\\nabc\" doesn't match \"^abc$\"");        }        if (!r.match("\u0085abc")) {            fail("\"\\u0085abc\" doesn't match \"^abc$\"");        }        if (!r.match("\u2028abc")) {            fail("\"\\u2028abc\" doesn't match \"^abc$\"");        }        if (!r.match("\u2029abc")) {            fail("\"\\u2029abc\" doesn't match \"^abc$\"");        }        // Test MATCH_MULTILINE. Test that '.' does not matches new line.        r = new RE("^a.*b$", RE.MATCH_MULTILINE);        if (r.match("a\nb")) {            fail("\"a\\nb\" matches \"^a.*b$\"");        }        if (r.match("a\rb")) {            fail("\"a\\rb\" matches \"^a.*b$\"");        }        if (r.match("a\r\nb")) {            fail("\"a\\r\\nb\" matches \"^a.*b$\"");        }        if (r.match("a\u0085b")) {            fail("\"a\\u0085b\" matches \"^a.*b$\"");        }        if (r.match("a\u2028b")) {            fail("\"a\\u2028b\" matches \"^a.*b$\"");        }        if (r.match("a\u2029b")) {            fail("\"a\\u2029b\" matches \"^a.*b$\"");        }        // Bug 38331: Large program        try {            REDebugCompiler c = new REDebugCompiler();            c.compile("(a{8192})?");            fail("(a{8192})? should fail to compile.");            c.dumpProgram();        } catch (RESyntaxException e) {            // expected        }    }    private void testPrecompiledRE()    {        // Pre-compiled regular expression "a*b"        char[] re1Instructions =        {            0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,            0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,            0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,            0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,            0x0000,        };        REProgram re1 = new REProgram(re1Instructions);        // Simple test of pre-compiled regular expressions        RE r = new RE(re1);        say("a*b");        boolean result = r.match("aaab");        say("aaab = " + result);        showParens(r);        if (!result) {            fail("\"aaab\" doesn't match to precompiled \"a*b\"");        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -