1#!/bin/bash
2
3if [[ -z "$AWK" ]]; then
4    printf '$AWK must be set\n' >&2
5    exit 1
6fi
7
8echo T.re: tests of regular expression code
9
10$AWK '
11BEGIN {
12	FS = "\t"
13	awk = ENVIRON["AWK"]
14}
15NF == 0 {
16	next
17}
18$1 != "" {	# new test
19	re = $1
20}
21$2 != "" {	# either ~ or !~
22	op = $2
23	if (op == "~")
24		neg = "!"
25	else if (op == "!~")
26		neg = ""
27}
28$3 != "" {	# new test string
29	str = $3
30}
31$3 == "\"\"" {	# explicit empty line
32	$3 = ""
33}
34NF > 2 {	# generate a test
35	input = $3
36	test = sprintf("printf %%s\\\\n '"'"'%s'"'"' | %s '"'"'%s/%s/ {print \"%d fails %s %s %s\"; exit 1}'"'"'",
37		input, awk, neg, re, NR, re, op, input)
38	# printf(" %3d   %s %s %s:\n", NR, re, op, input)
39	# print "test is |" test "|"
40	tcode = system(test)
41	if (tcode > 0) {
42		ecode = 1
43	}
44	# system("bprint -c ../a.out")
45	nt++
46}
47END {
48	print "	" nt, "tests"
49	exit ecode
50}
51' <<\!!!!
52	~	a
53		aa
54		aaa
55		""
56a	~	a
57		ba
58		bab
59	!~	""
60		x
61		xxxxx
62=	~	=
63		b=
64		b=b
65	!~	""
66		x
67		xxxxx
68.	~	x
69		xxx
70	!~	""			
71.a	~	xa
72		xxa
73		xax
74	!~	a
75		ax
76		""
77$	~	x
78		""
79.$	~	x
80		xx
81	!~	""
82a$	~	a
83		ba
84		bbba
85	!~	ab
86		x
87		""
88^	~	x
89		""
90		^
91^a$	~	a
92	!~	xa
93		ax
94		xax
95		""
96^a.$	~	ax
97		aa
98	!~	xa
99		aaa
100		axy
101		""
102^$	~	""
103	!~	x
104		^
105^.a	~	xa
106		xaa
107	!~	a
108		""
109^.*a	~	a
110		xa
111		xxxxxxa
112	!~	""
113^.+a	~	xa
114		xxxxxxa
115	!~	""
116		a
117		ax
118a*	~	""
119		a
120		aaaa
121		xa
122		xxxx
123aa*	~	a
124		aaa
125		xa
126	!~	xxxx
127		""
128\$	~	x$
129		$
130		$x
131		x$x
132	!~	""
133		x
134\.	~	.
135	!~	x
136		""
137xr+y	~	xry
138		xrry
139		xrrrrrry
140	!~	ry
141		xy
142		xr
143xr?y	~	xy
144		xry
145	!~	xrry
146a?b?c?	~	""
147		x
148^a?b?x	~	x
149		ax
150		bx
151		abx
152		xa
153	!~	""
154		ab
155		aba			
156[0-9]	~	1
157		567
158		x0y
159	!~	abc
160		""
161[^0-9]	!~	1
162		567
163		""
164	~	abc
165		x0y
166[0-25-69]	~	0
167		1
168		2
169		5
170		6
171		9
172	!~	3
173		4
174		7
175		8
176[[:lower:]]	~	a
177		b
178		z
179	!~	A
180		Z
181		1
182		:
183		[
184		]
185[[:upper:]]	~	A
186		B
187		Z
188	!~	a
189		z
190		1
191		:
192		[
193		]
194[[:lower:][:upper:]]	~	A
195		B
196		Z
197		a
198		b
199		z
200	!~	1
201		:
202		[
203		]
204[[:digit:]]	~	0
205		1
206		9
207	!~	d
208		:
209		[
210		]
211[[:xdigit:]]	~	0
212		1
213		9
214		a
215		d
216		f
217	!~	g
218		z
219		:
220		[
221		]
222[[:cntrl:]]	~	
223		
224		
225		
226	!~	.
227		Z
228		G
229		[
230		]
231[[:punct:]]	~	:
232		,
233		-
234		*
235		@
236	!~	a
237		1
238		U
239		 
240		
241x[0-9]+y	~	x0y
242		x23y
243		x12345y
244	!~	0y
245		xy
246x[0-9]?y	~	xy
247		x1y
248	!~	x23y
249x[[]y	~	x[y
250	!~	xy
251		x[[]y
252		x]y
253x[[-]y	~	x[y
254		x-y
255	!~	xy
256		x[[]y
257		x]y
258x[[-a]y	~	x[y
259		xay
260		x]y
261	!~	xy
262		x[[]y
263		x-y
264x[]-a]y	~	x]y
265		xay
266	!~	xy
267		x[y
268		x-y
269x[]]y	~	x]y
270	!~	xy
271		x[]]y
272		x[y
273x[^[]y	~	xay
274	!~	x[y
275x[-]y	~	x-y
276	!~	xy
277		x+y
278x[^-]y	~	x+y
279	!~	x-y
280		xy
281x[][]y	~	x[y
282		x]y
283	!~	xy
284		x][y
285		x[]y
286x[z-a]y	~	xy
287	!~	x
288		y
289		xay
290		xzy
291		x-y
292[0\-9]	~	0
293		-
294		9
295	!~	1
296		""
297[-1]	~	-
298		1
299	!~	0
300[0-]	~	0
301		-
302	!~	1
303[^-0]	~	x
304		^
305	!~	-
306		0
307		""
308[^0-]	~	x
309		^
310	!~	-
311		0
312		""
313x|y	~	x
314		y
315		xy
316	!~	a
317		""
318^abc|xyz$	~	abc
319		abcd
320		axyz
321		xyz
322	!~	xabc
323		xyza
324^(abc|xyz)$	~	abc
325		xyz
326	!~	abcxyz
327		abcx
328		cxyz
329^x\|y$	~	x|y
330	!~	xy
331^x\\y$	~	x\y
332	!~	xy
333		x\\y
334		xay
335\141\142	~	ab
336		xab
337		abx
338	!~	a
339		b
340		ax
341		axb
342x\056y	~	x.y
343	!~	x.
344		.x
345		xxx
346		xby  because \056 is not the metacharacter .
347		xcy  ditto
348[\60-\62\65-6\71]	~	0
349		1
350		2
351		5
352		6
353		9
354	!~	3
355		4
356		7
357		8
358[\60-2\65-6\71]	~	0
359		1
360		2
361		5
362		6
363		9
364	!~	3
365		4
366		7
367		8
368[\x30-\x32\x35-6\71]	~	0
369		1
370		2
371		5
372		6
373		9
374	!~	3
375		4
376		7
377		8
378[\x30-2\x35-6\x39]	~	0
379		1
380		2
381		5
382		6
383		9
384	!~	3
385		4
386		7
387		8
388\f	!~	x
389\b	!~	x
390\r	!~	x
391\n	!~	x
392!!!!
393