001: /*
002: * gnu/regexp/util/Tests.java -- Simple testsuite for gnu.regexp package
003: * Copyright (C) 1998-2001 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 Lesser General Public
007: * License (see the file COPYING.LIB for details).
008: */
009:
010: package gnu.regexp.util;
011:
012: import gnu.regexp.*;
013: import java.io.StringBufferInputStream;
014: import java.io.StringReader;
015:
016: /**
017: * This is a very basic testsuite application for gnu.regexp.
018: *
019: * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
020: * @version 1.1.1
021: */
022: public class Tests {
023: private Tests() {
024: }
025:
026: private static void check(RE expr, String input, String expect,
027: int id) {
028: // Test it using all possible input types
029: check(expr.getMatch(input), expect, id, "String");
030: check(expr.getMatch(new StringBuffer(input)), expect, id,
031: "StringBuffer");
032: check(expr.getMatch(input.toCharArray()), expect, id, "char[]");
033: check(expr.getMatch(new StringReader(input)), expect, id,
034: "Reader");
035: check(expr.getMatch(new StringBufferInputStream(input)),
036: expect, id, "InputStream");
037: }
038:
039: private static void check(REMatch m, String expect, int x,
040: String type) {
041: if ((m == null) || !m.toString().equals(expect))
042: System.out.print("*** Failed");
043: else
044: System.out.print("Passed");
045: System.out.println(" test #" + x + " (" + type + ")");
046: }
047:
048: /**
049: * Runs the testsuite. No command line arguments are necessary.
050: *
051: * @exception REException An error occurred compiling a regular expression.
052: */
053: public static void main(String[] argv) throws REException {
054: RE e;
055:
056: e = new RE("(.*)z");
057: check(e, ("xxz"), "xxz", 1);
058:
059: e = new RE(".*z");
060: check(e, ("xxz"), "xxz", 2);
061:
062: e = new RE("(x|xy)z");
063: check(e, ("xz"), "xz", 3);
064: check(e, ("xyz"), "xyz", 4);
065:
066: e = new RE("(x)+z");
067: check(e, ("xxz"), "xxz", 5);
068:
069: e = new RE("abc");
070: check(e, ("xyzabcdef"), "abc", 6);
071:
072: e = new RE("^start.*end$");
073: check(e, ("start here and go to the end"),
074: "start here and go to the end", 7);
075:
076: e = new RE("(x|xy)+z");
077: check(e, ("xxyz"), "xxyz", 8);
078:
079: e = new RE("type=([^ \t]+)[ \t]+exts=([^ \t\n\r]+)");
080: check(e, ("type=text/html exts=htm,html"),
081: "type=text/html exts=htm,html", 9);
082:
083: e = new RE("(x)\\1");
084: check(e, ("zxxz"), "xx", 10);
085:
086: e = new RE("(x*)(y)\\2\\1");
087: check(e, ("xxxyyxx"), "xxyyxx", 11);
088:
089: e = new RE("[-go]+");
090: check(e, ("go-go"), "go-go", 12);
091:
092: e = new RE("[\\w-]+");
093: check(e, ("go-go"), "go-go", 13);
094:
095: e = new RE("^start.*?end");
096: check(e,
097: ("start here and end in the middle, not the very end"),
098: "start here and end", 14);
099:
100: e = new RE("\\d\\s\\w\\n\\r");
101: check(e, (" 9\tX\n\r "), "9\tX\n\r", 15);
102:
103: e = new RE("zow", RE.REG_ICASE);
104: check(e, ("ZoW"), "ZoW", 16);
105:
106: e = new RE("(\\d+)\\D*(\\d+)\\D*(\\d)+");
107: check(e, ("size--10 by 20 by 30 feet"), "10 by 20 by 30", 17);
108:
109: e = new RE("(ab)(.*?)(d)");
110: REMatch m = e.getMatch("abcd");
111: check(m, "abcd", 18, "String");
112: System.out.println(((m.toString(2).equals("c")) ? "Pass"
113: : "*** Fail")
114: + "ed test #19");
115:
116: e = new RE("^$");
117: check(e, (""), "", 20);
118:
119: e = new RE("a*");
120: check(e, (""), "", 21);
121: check(e, ("a"), "a", 22);
122: check(e, ("aa"), "aa", 23);
123:
124: e = new RE("(([12]))?");
125: check(e, ("12"), "1", 24);
126:
127: e = new RE("(.*)?b");
128: check(e, ("ab"), "ab", 25);
129:
130: e = new RE("(.*)?-(.*)");
131: check(e, ("a-b"), "a-b", 26);
132:
133: e = new RE("(a)b");
134: check(e, ("aab"), "ab", 27);
135:
136: e = new RE("[M]iss");
137: check(e, ("one Mississippi"), "Miss", 28);
138:
139: e = new RE("\\S Miss");
140: check(e, ("one Mississippi"), "e Miss", 29);
141:
142: e = new RE("a*");
143: check(e, ("b"), "", 30);
144: check(e, ("ab"), "a", 31);
145: check(e, ("aab"), "aa", 32);
146:
147: // Single character should match anywhere in String
148: e = new RE("a");
149: check(e, ("a"), "a", 33);
150: check(e, ("ab"), "a", 34);
151: check(e, ("ba"), "a", 35);
152: check(e, ("bab"), "a", 36);
153:
154: }
155: }
|