KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > regexp > util > Tests


1 /*
2  * gnu/regexp/util/Tests.java -- Simple testsuite for gnu.regexp package
3  * Copyright (C) 1998-2001 Wes Biggs
4  *
5  * This file is in the public domain. However, the gnu.regexp library
6  * proper is licensed under the terms of the GNU Lesser General Public
7  * License (see the file COPYING.LIB for details).
8  */

9
10 package gnu.regexp.util;
11 import gnu.regexp.*;
12 import java.io.StringBufferInputStream JavaDoc;
13 import java.io.StringReader JavaDoc;
14
15 /**
16  * This is a very basic testsuite application for gnu.regexp.
17  *
18  * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
19  * @version 1.1.1
20  */

21 public class Tests {
22   private Tests() { }
23
24     private static void check(RE expr, String JavaDoc input, String JavaDoc expect, int id) {
25     // Test it using all possible input types
26
check(expr.getMatch(input),expect,id, "String");
27     check(expr.getMatch(new StringBuffer JavaDoc(input)),expect,id, "StringBuffer");
28     check(expr.getMatch(input.toCharArray()),expect,id, "char[]");
29     check(expr.getMatch(new StringReader JavaDoc(input)),expect,id, "Reader");
30     check(expr.getMatch(new StringBufferInputStream JavaDoc(input)),expect,id, "InputStream");
31     }
32   private static void check(REMatch m, String JavaDoc expect, int x, String JavaDoc type) {
33     if ((m == null) || !m.toString().equals(expect)) System.out.print("*** Failed");
34     else System.out.print("Passed");
35     System.out.println(" test #"+x + " (" + type + ")");
36   }
37
38   /**
39    * Runs the testsuite. No command line arguments are necessary.
40    *
41    * @exception REException An error occurred compiling a regular expression.
42    */

43   public static void main(String JavaDoc[] argv) throws REException {
44     RE e;
45
46     
47     e = new RE("(.*)z");
48     check(e,("xxz"),"xxz",1);
49
50     e = new RE(".*z");
51     check(e,("xxz"),"xxz",2);
52     
53     e = new RE("(x|xy)z");
54     check(e,("xz"),"xz",3);
55     check(e,("xyz"),"xyz",4);
56
57     e = new RE("(x)+z");
58     check(e,("xxz"),"xxz",5);
59
60     e = new RE("abc");
61     check(e,("xyzabcdef"),"abc",6);
62
63     e = new RE("^start.*end$");
64     check(e,("start here and go to the end"),"start here and go to the end",7);
65
66     e = new RE("(x|xy)+z");
67     check(e,("xxyz"),"xxyz",8);
68
69     e = new RE("type=([^ \t]+)[ \t]+exts=([^ \t\n\r]+)");
70     check(e,("type=text/html exts=htm,html"),"type=text/html exts=htm,html",9);
71
72     e = new RE("(x)\\1");
73     check(e,("zxxz"),"xx", 10);
74
75     e = new RE("(x*)(y)\\2\\1");
76     check(e,("xxxyyxx"),"xxyyxx",11);
77
78     e = new RE("[-go]+");
79     check(e,("go-go"),"go-go",12);
80
81     e = new RE("[\\w-]+");
82     check(e,("go-go"),"go-go",13);
83
84     e = new RE("^start.*?end");
85     check(e,("start here and end in the middle, not the very end"),"start here and end",14);
86     
87     e = new RE("\\d\\s\\w\\n\\r");
88     check(e,(" 9\tX\n\r "),"9\tX\n\r",15);
89
90     e = new RE("zow",RE.REG_ICASE);
91     check(e,("ZoW"),"ZoW",16);
92
93     e = new RE("(\\d+)\\D*(\\d+)\\D*(\\d)+");
94     check(e,("size--10 by 20 by 30 feet"),"10 by 20 by 30",17);
95
96     e = new RE("(ab)(.*?)(d)");
97     REMatch m = e.getMatch("abcd");
98     check(m,"abcd",18, "String");
99     System.out.println(((m.toString(2).equals("c")) ? "Pass" : "*** Fail")
100                + "ed test #19");
101
102     e = new RE("^$");
103     check(e,(""),"",20);
104
105     e = new RE("a*");
106     check(e,(""),"",21);
107     check(e,("a"),"a",22);
108     check(e,("aa"),"aa",23);
109
110     e = new RE("(([12]))?");
111     check(e,("12"),"1",24);
112
113     e = new RE("(.*)?b");
114     check(e,("ab"),"ab",25);
115
116     e = new RE("(.*)?-(.*)");
117     check(e,("a-b"), "a-b", 26);
118
119     e = new RE("(a)b");
120     check(e,("aab"), "ab", 27);
121
122     e = new RE("[M]iss");
123     check(e,("one Mississippi"), "Miss", 28);
124
125     e = new RE("\\S Miss");
126     check(e,("one Mississippi"), "e Miss", 29);
127
128     e = new RE("a*");
129     check(e,("b"),"",30);
130     check(e,("ab"),"a",31);
131     check(e,("aab"),"aa",32);
132
133     // Single character should match anywhere in String
134
e = new RE("a");
135     check(e,("a"),"a", 33);
136     check(e,("ab"),"a", 34);
137     check(e,("ba"),"a", 35);
138     check(e,("bab"),"a", 36);
139     
140   }
141 }
142
Popular Tags