001: /*
002: * gnu/regexp/util/Tests.java -- Simple testsuite for gnu.regexp package
003: * Copyright (C) 1998 Wes Biggs
004: *
005: * This file is in the public domain. However, the gnu.regexp library
006: * proper is licensed under the terms of the GNU Library General Public
007: * License (see the file LICENSE for details).
008: */
009:
010: package gnu.regexp.util;
011:
012: import gnu.regexp.*;
013:
014: /**
015: * This is a very basic testsuite application for gnu.regexp.
016: *
017: * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
018: * @version 1.01
019: */
020: public class Tests {
021: private Tests() {
022: }
023:
024: private static void check(REMatch m, String expect, int x) {
025: if ((m == null) || !m.toString().equals(expect))
026: System.out.print("Failed");
027: else
028: System.out.print("Passed");
029: System.out.println(" test #" + x);
030: }
031:
032: /**
033: * Runs the testsuite. No command line arguments are necessary.
034: *
035: * @exception REException An error occurred compiling a regular expression.
036: */
037: public static void main(String[] argv) throws REException {
038: RE e;
039:
040: e = new RE("(.*)z");
041: check(e.getMatch("xxz"), "xxz", 1);
042:
043: e = new RE(".*z");
044: check(e.getMatch("xxz"), "xxz", 2);
045:
046: e = new RE("(x|xy)z");
047: check(e.getMatch("xz"), "xz", 3);
048: check(e.getMatch("xyz"), "xyz", 4);
049:
050: e = new RE("(x)+z");
051: check(e.getMatch("xxz"), "xxz", 5);
052:
053: e = new RE("abc");
054: check(e.getMatch("xyzabcdef"), "abc", 6);
055:
056: e = new RE("^start.*end$");
057: check(e.getMatch("start here and go to the end"),
058: "start here and go to the end", 7);
059:
060: e = new RE("(x|xy)+z");
061: check(e.getMatch("xxyz"), "xxyz", 8);
062:
063: e = new RE("type=([^ \t]+)[ \t]+exts=([^ \t\n\r]+)");
064: check(e.getMatch("type=text/html exts=htm,html"),
065: "type=text/html exts=htm,html", 9);
066:
067: e = new RE("(x)\\1");
068: check(e.getMatch("zxxz"), "xx", 10);
069:
070: e = new RE("(x*)(y)\\2\\1");
071: check(e.getMatch("xxxyyxx"), "xxyyxx", 11);
072:
073: e = new RE("[-go]+");
074: check(e.getMatch("go-go"), "go-go", 12);
075:
076: e = new RE("[\\w-]+");
077: check(e.getMatch("go-go"), "go-go", 13);
078:
079: e = new RE("^start.*?end");
080: check(
081: e
082: .getMatch("start here and end in the middle, not the very end"),
083: "start here and end", 14);
084:
085: e = new RE("\\d\\s\\w\\n\\r");
086: check(e.getMatch(" 9\tX\n\r "), "9\tX\n\r", 15);
087:
088: e = new RE("zow", RE.REG_ICASE);
089: check(e.getMatch("ZoW"), "ZoW", 16);
090:
091: e = new RE("(\\d+)\\D*(\\d+)\\D*(\\d)+");
092: check(e.getMatch("size--10 by 20 by 30 feet"),
093: "10 by 20 by 30", 17);
094:
095: e = new RE("(ab)(.*?)(d)");
096: REMatch m = e.getMatch("abcd");
097: check(m, "abcd", 18);
098: System.out.println(((m.toString(2).equals("c")) ? "Pass"
099: : "Fail")
100: + "ed test #19");
101: }
102: }
|