1 // Copyright (c) 1999-2018 David Muse
2 // See the file COPYING for more information
3 
4 #include <rudiments/regularexpression.h>
5 #include <rudiments/charstring.h>
6 #include <rudiments/sys.h>
7 #include <rudiments/stdio.h>
8 #include "test.cpp"
9 
printMatches(regularexpression * re)10 void printMatches(regularexpression *re) {
11 	for (int32_t i=0; i<re->getSubstringCount(); i++) {
12 		stdoutput.printf("match %d start: \"%s\"\n",
13 					i,re->getSubstringStart(i));
14 		stdoutput.printf("match %d end  : \"%s\"\n",
15 					i,re->getSubstringEnd(i));
16 	}
17 	stdoutput.printf("%d matches\n\n",re->getSubstringCount());
18 }
19 
main(int argc,const char ** argv)20 int main(int argc, const char **argv) {
21 
22 	header("regularexpression");
23 
24         const char	*mtc="Dave";
25         const char	*str=NULL;
26 
27         stdoutput.printf("static match %s\n",mtc);
28         str="Hello";
29         test(str,!regularexpression::match(str,mtc));
30         str="Hello Dave";
31         test(str,regularexpression::match(str,mtc));
32         str="Hello Dave Goodbye";
33         test(str,regularexpression::match(str,mtc));
34         str="Dave Goodbye";
35         test(str,regularexpression::match(str,mtc));
36         stdoutput.printf("\n");
37 
38 
39         stdoutput.printf("static match %s (with length)\n",mtc);
40         str="Hello";
41         test(str,!regularexpression::match(str,4,mtc));
42         str="Hello Dave";
43         test(str,!regularexpression::match(str,4,mtc));
44         test(str,regularexpression::match(str,10,mtc));
45         str="Hello Dave Goodbye";
46         test(str,!regularexpression::match(str,4,mtc));
47         test(str,regularexpression::match(str,11,mtc));
48         str="Dave Goodbye";
49         test(str,!regularexpression::match(str,3,mtc));
50         test(str,regularexpression::match(str,4,mtc));
51         stdoutput.printf("\n");
52 
53 
54         stdoutput.printf("instance match %s\n",mtc);
55         regularexpression       re;
56 	test("setPattern",re.setPattern(mtc));
57         str="Hello";
58         test(str,!re.match(str));
59 	//printMatches(&re);
60 	test("match count",!re.getSubstringCount());
61 	test("match start",!re.getSubstringStart(0));
62 	test("match end",!re.getSubstringEnd(0));
63 
64         str="Hello Dave";
65         test(str,re.match(str));
66 	//printMatches(&re);
67 	test("match count",re.getSubstringCount()==1);
68 	test("match start",re.getSubstringStart(0)==(str+6));
69 	test("match end",re.getSubstringEnd(0)==(str+10));
70 
71 	test("study",re.study());
72         str="Hello Dave Goodbye";
73         test(str,re.match(str));
74 	//printMatches(&re);
75 	test("match count",re.getSubstringCount()==1);
76 	test("match start",re.getSubstringStart(0)==(str+6));
77 	test("match end",re.getSubstringEnd(0)==(str+10));
78 
79         str="Dave Goodbye";
80         test(str,re.match(str));
81 	//printMatches(&re);
82 	test("match count",re.getSubstringCount()==1);
83 	test("match start",re.getSubstringStart(0)==str);
84 	test("match end",re.getSubstringEnd(0)==(str+4));
85 
86         str="Dave Dave Dave";
87         test(str,re.match(str));
88 	//printMatches(&re);
89 	test("match count",re.getSubstringCount()==1);
90 	test("match start",re.getSubstringStart(0)==str);
91 	test("match end",re.getSubstringEnd(0)==(str+4));
92         stdoutput.printf("\n");
93 
94 
95         stdoutput.printf("optional whitespace match\n");
96 	mtc="^[ 	\r\n]*((create|CREATE|drop|DROP)[ 	\r\n]+)|"
97 			"((begin|BEGIN|rollback|ROLLBACK)[ 	\r\n]*)";
98 	test("setPattern",re.setPattern(mtc));
99 	test("study",re.study());
100         str="create table";
101         test(str,re.match(str));
102         str=" create  table";
103         test(str,re.match(str));
104         str="\n create  table";
105         test(str,re.match(str));
106         str="\n	 create\n	 table";
107         test(str,re.match(str));
108         str="drop table";
109         test(str,re.match(str));
110         str=" drop  table";
111         test(str,re.match(str));
112         str="\n drop  table";
113         test(str,re.match(str));
114         str="\n	 drop\n	 table";
115         test(str,re.match(str));
116         str="begin";
117         test(str,re.match(str));
118         str="begin ";
119         test(str,re.match(str));
120         str=" begin ";
121         test(str,re.match(str));
122         str="rollback";
123         test(str,re.match(str));
124         str="rollback ";
125         test(str,re.match(str));
126         str=" rollback ";
127         test(str,re.match(str));
128         stdoutput.printf("\n");
129 
130 
131 	char	*osname=sys::getOperatingSystemName();
132 
133 	// solaris regex can't handle this, apparently
134 	if (charstring::compare(osname,"SunOS")) {
135 
136         	stdoutput.printf("wildcard word match\n");
137 		test("setPattern",re.setPattern("(\\w+) (\\w+)"));
138         	str="hello world";
139         	test(str,re.match(str));
140 		//printMatches(&re);
141 		test("match count",re.getSubstringCount()==3);
142 		test("match 0 start",re.getSubstringStart(0)==str);
143 		test("match 0 end",re.getSubstringEnd(0)==(str+11));
144 		test("match 1 start",re.getSubstringStart(1)==str);
145 		test("match 1 end",re.getSubstringEnd(1)==(str+5));
146 		test("match 2 start",re.getSubstringStart(2)==(str+6));
147 		test("match 2 end",re.getSubstringEnd(2)==(str+11));
148         	str="hello world hello world";
149         	test(str,re.match(str));
150 		//printMatches(&re);
151 		test("match count",re.getSubstringCount()==3);
152 		test("match 0 start",re.getSubstringStart(0)==str);
153 		test("match 0 end",re.getSubstringEnd(0)==(str+11));
154 		test("match 1 start",re.getSubstringStart(1)==str);
155 		test("match 1 end",re.getSubstringEnd(1)==(str+5));
156 		test("match 2 start",re.getSubstringStart(2)==(str+6));
157 		test("match 2 end",re.getSubstringEnd(2)==(str+11));
158         	stdoutput.printf("\n");
159 	}
160 	delete[] osname;
161 
162 
163         stdoutput.printf("NULLs\n");
164 	test("setPattern",re.setPattern(NULL));
165 	test("study",re.study());
166         test("match",re.match(NULL));
167 	//printMatches(&re);
168 	test("match count",!re.getSubstringCount());
169 	test("match start",!re.getSubstringStart(0));
170 	test("match end",!re.getSubstringEnd(0));
171         stdoutput.printf("\n");
172 }
173