xref: /freebsd/lib/libc/regex/grot/tests (revision 2008043f386721d58158e37e0d7e50df8095942d)
1# regular expression test set
2# Lines are at least three fields, separated by one or more tabs.  "" stands
3# for an empty field.  First field is an RE.  Second field is flags.  If
4# C flag given, regcomp() is expected to fail, and the third field is the
5# error name (minus the leading REG_).
6#
7# Otherwise it is expected to succeed, and the third field is the string to
8# try matching it against.  If there is no fourth field, the match is
9# expected to fail.  If there is a fourth field, it is the substring that
10# the RE is expected to match.  If there is a fifth field, it is a comma-
11# separated list of what the subexpressions should match, with - indicating
12# no match for that one.  In both the fourth and fifth fields, a (sub)field
13# starting with @ indicates that the (sub)expression is expected to match
14# a null string followed by the stuff after the @; this provides a way to
15# test where null strings match.  The character `N' in REs and strings
16# is newline, `S' is space, `T' is tab, `Z' is NUL.
17#
18# The full list of flags:
19#	-	placeholder, does nothing
20#	b	RE is a BRE, not an ERE
21#	&	try it as both an ERE and a BRE
22#	C	regcomp() error expected, third field is error name
23#	i	REG_ICASE
24#	m	("mundane") REG_NOSPEC
25#	s	REG_NOSUB (not really testable)
26#	n	REG_NEWLINE
27#	^	REG_NOTBOL
28#	$	REG_NOTEOL
29#	#	REG_STARTEND (see below)
30#	p	REG_PEND
31#
32# For REG_STARTEND, the start/end offsets are those of the substring
33# enclosed in ().
34
35# basics
36a		&	a	a
37abc		&	abc	abc
38abc|de		-	abc	abc
39a|b|c		-	abc	a
40
41# parentheses and perversions thereof
42a(b)c		-	abc	abc
43a\(b\)c		b	abc	abc
44a(		C	EPAREN
45a(		b	a(	a(
46a\(		-	a(	a(
47a\(		bC	EPAREN
48a\(b		bC	EPAREN
49a(b		C	EPAREN
50a(b		b	a(b	a(b
51# gag me with a right parenthesis -- 1003.2 goofed here (my fault, partly)
52a)		-	a)	a)
53)		-	)	)
54# end gagging (in a just world, those *should* give EPAREN)
55a)		b	a)	a)
56a\)		bC	EPAREN
57\)		bC	EPAREN
58a()b		-	ab	ab
59a\(\)b		b	ab	ab
60
61# anchoring and REG_NEWLINE
62^abc$		&	abc	abc
63a^b		-	a^b
64a^b		b	a^b	a^b
65a$b		-	a$b
66a$b		b	a$b	a$b
67^		&	abc	@abc
68$		&	abc	@
69^$		&	""	@
70$^		-	""	@
71\($\)\(^\)	b	""	@
72# stop retching, those are legitimate (although disgusting)
73^^		-	""	@
74$$		-	""	@
75b$		&	abNc
76b$		&n	abNc	b
77^b$		&	aNbNc
78^b$		&n	aNbNc	b
79^$		&n	aNNb	@Nb
80^$		n	abc
81^$		n	abcN	@
82$^		n	aNNb	@Nb
83\($\)\(^\)	bn	aNNb	@Nb
84^^		n^	aNNb	@Nb
85$$		n	aNNb	@NN
86^a		^	a
87a$		$	a
88^a		^n	aNb
89^b		^n	aNb	b
90a$		$n	bNa
91b$		$n	bNa	b
92a*(^b$)c*	-	b	b
93a*\(^b$\)c*	b	b	b
94
95# certain syntax errors and non-errors
96|		C	EMPTY
97|		b	|	|
98*		C	BADRPT
99*		b	*	*
100+		C	BADRPT
101?		C	BADRPT
102""		&C	EMPTY
103()		-	abc	@abc
104\(\)		b	abc	@abc
105a||b		C	EMPTY
106|ab		C	EMPTY
107ab|		C	EMPTY
108(|a)b		C	EMPTY
109(a|)b		C	EMPTY
110(*a)		C	BADRPT
111(+a)		C	BADRPT
112(?a)		C	BADRPT
113({1}a)		C	BADRPT
114\(\{1\}a\)	bC	BADRPT
115(a|*b)		C	BADRPT
116(a|+b)		C	BADRPT
117(a|?b)		C	BADRPT
118(a|{1}b)	C	BADRPT
119^*		C	BADRPT
120^*		b	*	*
121^+		C	BADRPT
122^?		C	BADRPT
123^{1}		C	BADRPT
124^\{1\}		bC	BADRPT
125
126# metacharacters, backslashes
127a.c		&	abc	abc
128a[bc]d		&	abd	abd
129a\*c		&	a*c	a*c
130a\\b		&	a\b	a\b
131a\\\*b		&	a\*b	a\*b
132a\bc		&	abc	abc
133a\		&C	EESCAPE
134a\\bc		&	a\bc	a\bc
135\{		bC	BADRPT
136# trailing $ is a peculiar special case for the BRE code
137a$		&	a	a
138a$		&	a$
139a\$		&	a
140a\$		&	a$	a$
141a\\$		&	a
142a\\$		&	a$
143a\\$		&	a\$
144a\\$		&	a\	a\
145
146# back references, ugh
147a\(b\)\2c	bC	ESUBREG
148a\(b\1\)c	bC	ESUBREG
149a\(b*\)c\1d	b	abbcbbd	abbcbbd	bb
150a\(b*\)c\1d	b	abbcbd
151a\(b*\)c\1d	b	abbcbbbd
152^\(.\)\1	b	abc
153a\([bc]\)\1d	b	abcdabbd	abbd	b
154a\(\([bc]\)\2\)*d	b	abbccd	abbccd
155a\(\([bc]\)\2\)*d	b	abbcbd
156# actually, this next one probably ought to fail, but the spec is unclear
157a\(\(b\)*\2\)*d		b	abbbd	abbbd
158# here is a case that no NFA implementation does right
159\(ab*\)[ab]*\1	b	ababaaa	ababaaa	a
160# check out normal matching in the presence of back refs
161\(a\)\1bcd	b	aabcd	aabcd
162\(a\)\1bc*d	b	aabcd	aabcd
163\(a\)\1bc*d	b	aabd	aabd
164\(a\)\1bc*d	b	aabcccd	aabcccd
165\(a\)\1bc*[ce]d	b	aabcccd	aabcccd
166^\(a\)\1b\(c\)*cd$	b	aabcccd	aabcccd
167\(b*\)\(a*\1\)*	b	ab	a
168\([^_]*\)\(_*\1\)*	b	foo_foo_bar_bar_bar_baz	foo_foo	foo,_foo
169\([^_]*\)\(_*\1\)*	b	bar_bar_bar_baz	bar_bar_bar	bar,_bar
170\([^_]*\)\(_*\1\)*	b	foo_bar_baz	foo	foo
171\(.*\)\1	b	""	""
172\(.*\)\1	b	a	""
173\(.*\)\1	b	aa	aa
174\(.*\)\1	b	aaa	aa
175\(.*\)\1	b	aaaa	aaaa
176\([^_]*\)\1	b	""	""
177\([^_]*\)\1	b	a	""
178\([^_]*\)\1	b	aa	aa
179\([^_]*\)\1	b	aaa	aa
180\([^_]*\)\1	b	aaaa	aaaa
181foo\(.*\)bar\1	b	foolbarl	foolbarl	l
182foo\(.*\)bar\1	b	foobar	foobar	""
183\(\(.\)b\)*\1	b	aba
184\(\(.\)b\)*\1	b	abba
185\(\(.\)b\)*\1	b	abbba
186\(\(.\)b\)*\1	b	abbbba	bbbb	bb,b
187\(\(.\)b\)*\1	b	abbbbba	abbbbb	bb,b
188\(\(.\)b\)*\1	b	abbbbbba	abbbbb	bb,b
189\(\(.\)b\)*\1	b	abbbbbbbbbbbbbba	abbbbbbbbbbbbb	bb,b
190\(\(.\)b\)*\1	b	abbbbbbbbbbbbbbba	abbbbbbbbbbbbbbb	bb,b
191
192# ordinary repetitions
193ab*c		&	abc	abc
194ab+c		-	abc	abc
195ab?c		-	abc	abc
196a\(*\)b		b	a*b	a*b
197a\(**\)b	b	ab	ab
198a\(***\)b	bC	BADRPT
199*a		b	*a	*a
200**a		b	a	a
201***a		bC	BADRPT
202
203# the dreaded bounded repetitions
204{		&	{	{
205{abc		&	{abc	{abc
206{1		C	BADRPT
207{1}		C	BADRPT
208a{b		&	a{b	a{b
209a{1}b		-	ab	ab
210a\{1\}b		b	ab	ab
211a{1,}b		-	ab	ab
212a\{1,\}b	b	ab	ab
213a{1,2}b		-	aab	aab
214a\{1,2\}b	b	aab	aab
215a{1		C	EBRACE
216a\{1		bC	EBRACE
217a{1a		C	EBRACE
218a\{1a		bC	EBRACE
219a{1a}		C	BADBR
220a\{1a\}		bC	BADBR
221a{,2}		-	a{,2}	a{,2}
222a\{,2\}		bC	BADBR
223a{,}		-	a{,}	a{,}
224a\{,\}		bC	BADBR
225a{1,x}		C	BADBR
226a\{1,x\}	bC	BADBR
227a{1,x		C	EBRACE
228a\{1,x		bC	EBRACE
229a{300}		C	BADBR
230a\{300\}	bC	BADBR
231a{1,0}		C	BADBR
232a\{1,0\}	bC	BADBR
233ab{0,0}c	-	abcac	ac
234ab\{0,0\}c	b	abcac	ac
235ab{0,1}c	-	abcac	abc
236ab\{0,1\}c	b	abcac	abc
237ab{0,3}c	-	abbcac	abbc
238ab\{0,3\}c	b	abbcac	abbc
239ab{1,1}c	-	acabc	abc
240ab\{1,1\}c	b	acabc	abc
241ab{1,3}c	-	acabc	abc
242ab\{1,3\}c	b	acabc	abc
243ab{2,2}c	-	abcabbc	abbc
244ab\{2,2\}c	b	abcabbc	abbc
245ab{2,4}c	-	abcabbc	abbc
246ab\{2,4\}c	b	abcabbc	abbc
247((a{1,10}){1,10}){1,10}	-	a	a	a,a
248((a{1,10}){1,10}){1,10}bb	-	aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb	aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb
249
250# multiple repetitions
251a**		&C	BADRPT
252a++		C	BADRPT
253a??		C	BADRPT
254a*+		C	BADRPT
255a*?		C	BADRPT
256a+*		C	BADRPT
257a+?		C	BADRPT
258a?*		C	BADRPT
259a?+		C	BADRPT
260a{1}{1}		C	BADRPT
261a*{1}		C	BADRPT
262a+{1}		C	BADRPT
263a?{1}		C	BADRPT
264a{1}*		C	BADRPT
265a{1}+		C	BADRPT
266a{1}?		C	BADRPT
267a*{b}		-	a{b}	a{b}
268a\{1\}\{1\}	bC	BADRPT
269a*\{1\}		bC	BADRPT
270a\{1\}*		bC	BADRPT
271
272# brackets, and numerous perversions thereof
273a[b]c		&	abc	abc
274a[ab]c		&	abc	abc
275a[^ab]c		&	adc	adc
276a[]b]c		&	a]c	a]c
277a[[b]c		&	a[c	a[c
278a[-b]c		&	a-c	a-c
279a[^]b]c		&	adc	adc
280a[^-b]c		&	adc	adc
281a[b-]c		&	a-c	a-c
282a[b		&C	EBRACK
283a[]		&C	EBRACK
284a[1-3]c		&	a2c	a2c
285a[3-1]c		&C	ERANGE
286a[1-3-5]c	&C	ERANGE
287a[[.-.]--]c	&	a-c	a-c
288a[1-		&C	ERANGE
289a[[.		&C	EBRACK
290a[[.x		&C	EBRACK
291a[[.x.		&C	EBRACK
292a[[.x.]		&C	EBRACK
293a[[.x.]]	&	ax	ax
294a[[.x,.]]	&C	ECOLLATE
295a[[.one.]]b	&	a1b	a1b
296a[[.notdef.]]b	&C	ECOLLATE
297a[[.].]]b	&	a]b	a]b
298a[[:alpha:]]c	&	abc	abc
299a[[:notdef:]]c	&C	ECTYPE
300a[[:		&C	EBRACK
301a[[:alpha	&C	EBRACK
302a[[:alpha:]	&C	EBRACK
303a[[:alpha,:]	&C	ECTYPE
304a[[:]:]]b	&C	ECTYPE
305a[[:-:]]b	&C	ECTYPE
306a[[:alph:]]	&C	ECTYPE
307a[[:alphabet:]]	&C	ECTYPE
308[[:alnum:]]+	-	-%@a0X-	a0X
309[[:alpha:]]+	-	-%@aX0-	aX
310[[:blank:]]+	-	aSSTb	SST
311[[:cntrl:]]+	-	aNTb	NT
312[[:digit:]]+	-	a019b	019
313[[:graph:]]+	-	Sa%bS	a%b
314[[:lower:]]+	-	AabC	ab
315[[:print:]]+	-	NaSbN	aSb
316[[:punct:]]+	-	S%-&T	%-&
317[[:space:]]+	-	aSNTb	SNT
318[[:upper:]]+	-	aBCd	BC
319[[:xdigit:]]+	-	p0f3Cq	0f3C
320a[[=b=]]c	&	abc	abc
321a[[=		&C	EBRACK
322a[[=b		&C	EBRACK
323a[[=b=		&C	EBRACK
324a[[=b=]		&C	EBRACK
325a[[=b,=]]	&C	ECOLLATE
326a[[=one=]]b	&	a1b	a1b
327
328# complexities
329a(((b)))c	-	abc	abc
330a(b|(c))d	-	abd	abd
331a(b*|c)d	-	abbd	abbd
332# just gotta have one DFA-buster, of course
333a[ab]{20}	-	aaaaabaaaabaaaabaaaab	aaaaabaaaabaaaabaaaab
334# and an inline expansion in case somebody gets tricky
335a[ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab]	-	aaaaabaaaabaaaabaaaab	aaaaabaaaabaaaabaaaab
336# and in case somebody just slips in an NFA...
337a[ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab](wee|week)(knights|night)	-	aaaaabaaaabaaaabaaaabweeknights	aaaaabaaaabaaaabaaaabweeknights
338# fish for anomalies as the number of states passes 32
33912345678901234567890123456789	-	a12345678901234567890123456789b	12345678901234567890123456789
340123456789012345678901234567890	-	a123456789012345678901234567890b	123456789012345678901234567890
3411234567890123456789012345678901	-	a1234567890123456789012345678901b	1234567890123456789012345678901
34212345678901234567890123456789012	-	a12345678901234567890123456789012b	12345678901234567890123456789012
343123456789012345678901234567890123	-	a123456789012345678901234567890123b	123456789012345678901234567890123
344# and one really big one, beyond any plausible word width
3451234567890123456789012345678901234567890123456789012345678901234567890	-	a1234567890123456789012345678901234567890123456789012345678901234567890b	1234567890123456789012345678901234567890123456789012345678901234567890
346# fish for problems as brackets go past 8
347[ab][cd][ef][gh][ij][kl][mn]	-	xacegikmoq	acegikm
348[ab][cd][ef][gh][ij][kl][mn][op]	-	xacegikmoq	acegikmo
349[ab][cd][ef][gh][ij][kl][mn][op][qr]	-	xacegikmoqy	acegikmoq
350[ab][cd][ef][gh][ij][kl][mn][op][q]	-	xacegikmoqy	acegikmoq
351
352# subtleties of matching
353abc		&	xabcy	abc
354a\(b\)?c\1d	b	acd
355aBc		i	Abc	Abc
356a[Bc]*d		i	abBCcd	abBCcd
3570[[:upper:]]1	&i	0a1	0a1
3580[[:lower:]]1	&i	0A1	0A1
359a[^b]c		&i	abc
360a[^b]c		&i	aBc
361a[^b]c		&i	adc	adc
362[a]b[c]		-	abc	abc
363[a]b[a]		-	aba	aba
364[abc]b[abc]	-	abc	abc
365[abc]b[abd]	-	abd	abd
366a(b?c)+d	-	accd	accd
367(wee|week)(knights|night)	-	weeknights	weeknights
368(we|wee|week|frob)(knights|night|day)	-	weeknights	weeknights
369a[bc]d		-	xyzaaabcaababdacd	abd
370a[ab]c		-	aaabc	abc
371abc		s	abc	abc
372
373# subexpressions
374a(b)(c)d	-	abcd	abcd	b,c
375a(((b)))c	-	abc	abc	b,b,b
376a(b|(c))d	-	abd	abd	b,-
377a(b*|c|e)d	-	abbd	abbd	bb
378a(b*|c|e)d	-	acd	acd	c
379a(b*|c|e)d	-	ad	ad	@d
380a(b?)c		-	abc	abc	b
381a(b?)c		-	ac	ac	@c
382a(b+)c		-	abc	abc	b
383a(b+)c		-	abbbc	abbbc	bbb
384a(b*)c		-	ac	ac	@c
385(a|ab)(bc([de]+)f|cde)	-	abcdef	abcdef	a,bcdef,de
386# the regression tester only asks for 9 subexpressions
387a(b)(c)(d)(e)(f)(g)(h)(i)(j)k	-	abcdefghijk	abcdefghijk	b,c,d,e,f,g,h,i,j
388a(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)l	-	abcdefghijkl	abcdefghijkl	b,c,d,e,f,g,h,i,j,k
389a([bc]?)c	-	abc	abc	b
390a([bc]?)c	-	ac	ac	@c
391a([bc]+)c	-	abc	abc	b
392a([bc]+)c	-	abcc	abcc	bc
393a([bc]+)bc	-	abcbc	abcbc	bc
394a(bb+|b)b	-	abb	abb	b
395a(bbb+|bb+|b)b	-	abb	abb	b
396a(bbb+|bb+|b)b	-	abbb	abbb	bb
397a(bbb+|bb+|b)bb	-	abbb	abbb	b
398(.*).*		-	abcdef	abcdef	abcdef
399(a*)*		-	bc	@b	@b
400
401# do we get the right subexpression when it is used more than once?
402a(b|c)*d	-	ad	ad	-
403a(b|c)*d	-	abcd	abcd	c
404a(b|c)+d	-	abd	abd	b
405a(b|c)+d	-	abcd	abcd	c
406a(b|c?)+d	-	ad	ad	@d
407a(b|c?)+d	-	abcd	abcd	@d
408a(b|c){0,0}d	-	ad	ad	-
409a(b|c){0,1}d	-	ad	ad	-
410a(b|c){0,1}d	-	abd	abd	b
411a(b|c){0,2}d	-	ad	ad	-
412a(b|c){0,2}d	-	abcd	abcd	c
413a(b|c){0,}d	-	ad	ad	-
414a(b|c){0,}d	-	abcd	abcd	c
415a(b|c){1,1}d	-	abd	abd	b
416a(b|c){1,1}d	-	acd	acd	c
417a(b|c){1,2}d	-	abd	abd	b
418a(b|c){1,2}d	-	abcd	abcd	c
419a(b|c){1,}d	-	abd	abd	b
420a(b|c){1,}d	-	abcd	abcd	c
421a(b|c){2,2}d	-	acbd	acbd	b
422a(b|c){2,2}d	-	abcd	abcd	c
423a(b|c){2,4}d	-	abcd	abcd	c
424a(b|c){2,4}d	-	abcbd	abcbd	b
425a(b|c){2,4}d	-	abcbcd	abcbcd	c
426a(b|c){2,}d	-	abcd	abcd	c
427a(b|c){2,}d	-	abcbd	abcbd	b
428a(b+|((c)*))+d	-	abd	abd	@d,@d,-
429a(b+|((c)*))+d	-	abcd	abcd	@d,@d,-
430
431# check out the STARTEND option
432[abc]		&#	a(b)c	b
433[abc]		&#	a(d)c
434[abc]		&#	a(bc)d	b
435[abc]		&#	a(dc)d	c
436.		&#	a()c
437b.*c		&#	b(bc)c	bc
438b.*		&#	b(bc)c	bc
439.*c		&#	b(bc)c	bc
440
441# plain strings, with the NOSPEC flag
442abc		m	abc	abc
443abc		m	xabcy	abc
444abc		m	xyz
445a*b		m	aba*b	a*b
446a*b		m	ab
447""		mC	EMPTY
448
449# cases involving NULs
450aZb		&	a	a
451aZb		&p	a
452aZb		&p#	(aZb)	aZb
453aZ*b		&p#	(ab)	ab
454a.b		&#	(aZb)	aZb
455a.*		&#	(aZb)c	aZb
456
457# word boundaries (ick)
458[[:<:]]a	&	a	a
459[[:<:]]a	&	ba
460[[:<:]]a	&	-a	a
461a[[:>:]]	&	a	a
462a[[:>:]]	&	ab
463a[[:>:]]	&	a-	a
464[[:<:]]a.c[[:>:]]	&	axcd-dayc-dazce-abc	abc
465[[:<:]]a.c[[:>:]]	&	axcd-dayc-dazce-abc-q	abc
466[[:<:]]a.c[[:>:]]	&	axc-dayc-dazce-abc	axc
467
468# past problems
469(A[1])|(A[2])|(A[3])|(A[4])|(A[5])|(A[6])|(A[7])|(A[8])|(A[9])|(A[A])	-	A1	A1
470abcdefghijklmnop	i	abcdefghijklmnop	abcdefghijklmnop
471abcdefghijklmnopqrstuv	i	abcdefghijklmnopqrstuv	abcdefghijklmnopqrstuv
472(ALAK)|(ALT[AB])|(CC[123]1)|(CM[123]1)|(GAMC)|(LC[23][EO ])|(SEM[1234])|(SL[ES][12])|(SLWW)|(SLF )|(SLDT)|(VWH[12])|(WH[34][EW])|(WP1[ESN])	-	CC11	CC11
473CC[13]1|a{21}[23][EO][123][Es][12]a{15}aa[34][EW]aaaaaaa[X]a	-	CC11	CC11
474# PR 130504
475(.|())(b)	-	ab	ab
476(()|.)(b)	-	ab	ab
477