1#!/bin/bash
2
3if [[ -z "$AWK" ]]; then
4    printf '$AWK must be set\n' >&2
5    exit 1
6fi
7
8echo T.sub: tests of sub and gsub code
9
10# input lines are of form
11#	pattern  replacement  input-string  sub-output  gsub-output
12
13$AWK '
14BEGIN {
15	FS = "\t"
16	awk = ENVIRON["AWK"]
17}
18NF == 0		{ next }
19$1 ~ /^#/	{ next }
20
21$1 != "" {	# new pattern
22	pat = $1
23}
24$2 != "" {	# new replacement
25	repl = $2
26}
27$3 != "" {	# new input string
28	str = $3
29}
30$4 != "" {	# new sub output
31	subout = $4
32}
33$5 != "" {	# new gsub output
34	gsubout = $5
35}
36NF < 5 {	# weird input line
37	printf("weird test spec `%s` ignored\n", $0) | "cat 1>&2"
38	next
39}
40{		# "" => explicitly empty
41	# printf(" %3d:   %s %s %s %s %s:\n", NR, pat, repl, str, subout, gsubout)
42	if (pat == "\"\"") pat = ""
43	if (repl == "\"\"") repl = ""
44	if (str == "\"\"") str = ""
45	if (subout == "\"\"") subout = ""
46	if (gsubout == "\"\"") gsubout = ""
47}
48{		# generate a test
49	nt++
50	gsub(/\\/, "&&", repl)	# in case of \ enclosed
51	test = sprintf("echo '"'"'%s'"'"' | %s '"'\n"'", str, awk) \
52	  sprintf("{ temp = $0; sub(/%s/, \"%s\", temp)\n", pat, repl) \
53	  sprintf("  if (temp != \"%s\") print \" sub %d fails:\", temp, \"should be %s in %s\"\n",
54		subout, nt, subout, (pat " " repl " " str " " subout)) \
55	  sprintf("  gsub(/%s/, \"%s\")\n", pat, repl) \
56	  sprintf("  if ($0 != \"%s\") print \"gsub %d fails:\", $0, \"should be %s in %s\"\n}",
57		gsubout, nt, gsubout, (pat " " repl " " str " " gsubout)) \
58	  "'" '"'"
59	# if (nt >= 55) print "test is: " test
60	tcode = system(test)
61	if (tcode > 0) {
62		ecode = 1
63	}
64	# system("bprint -c ../a.out")
65}
66END {
67	print nt, "tests"
68	exit ecode
69}
70
71' <<\!!!!
72a	x	aaa	xaa	xxx
73		axa	xxa	xxx
74		bbb	bbb	bbb
75		""	""	""
76a	xy	aaa	xyaa	xyxyxy
77		axa	xyxa	xyxxy
78		bbb	bbb	bbb
79		""	""	""
80.	x	aaa	xaa	xxx
81		axa	xxa	xxx
82		bbb	xbb	xxx
83		""	""	""
84.a	x	a	a	a
85		ax	ax	ax
86		aa	x	x
87		aaab	xab	xab
88		aaaa	xaa	xx
89		""	""	""
90$	x	a	ax	ax
91		""	x	x
92.$	x	""	""	""
93		a	x	x
94		ab	ax	ax
95a$	x	""	""	""
96		a	x	x
97		b	b	b
98		ab	ab	ab
99^	x	""	x	x
100		a	xa	xa
101^a$	xx	a	xx	xx
102		""	""	""
103		b	b	b
104		aa	aa	aa
105^a.$	xy	a	a	a
106		""	""	""
107		ab	xy	xy
108		ba	ba	ba
109^$	x	""	x	x
110		a	a	a
111^.a	x	aa	x	x
112		ba	x	x
113		ab	ab	ab
114		a	a	a	
115^.*a	xy	""	""	""
116		a	xy	xy
117		b	b	b
118		ba	xy	xy
119^.+a	xy	""	""	""
120		a	a	a
121		bb	bb	bb
122		ba	xy	xy
123a	&x&y	a	axay	axay
124		aa	axaya	axayaxay
125
126a*	x	""	x	x
127		z	xz	xzx
128		az	xz	xzx
129		aza	xza	xzx
130	b	xxx	bxxx	bxbxbxb
131	x&	paq	xpaq	xpxaqx
132	x\&	paq	x&paq	x&px&qx&
133	x&y	paq	xypaq	xypxayqxy
134	x\&y	paq	x&ypaq	x&ypx&yqx&y
135a+	x&	paq	pxaq	pxaq
136	x\&	paq	px&q	px&q
137	x&y	paq	pxayq	pxayq
138	x\&y	paq	px&yq	px&yq
139aa*	x	a	x	x
140		aa	x	x
141		wawa	wxwa	wxwx
142\$	x	""	""	""
143		a	a	a
144		a$	ax	ax
145		$$$	x$$	xxx
146		z$z$z	zxz$z	zxzxz
147\.	x	""	""	""
148		a	a	a
149		a.	ax	ax
150		...	x..	xxx
151		z.z.z	zxz.z	zxzxz
152xr+y	q	xy	xy	xy
153		xry	q	q
154		xrry	q	q
155		xryWxry	qWxry	qWq
156xr?y	q	AxyB	AqB	AqB
157		AxryB	AqB	AqB
158		Axrry	Axrry	Axrry
159a?b?c?	x	""	x	x
160		a	x	x
161		b	x	x
162		c	x	x
163		ac	x	x
164		acc	xc	xx
165^a?b?q	x	""	""	""
166		q	x	x
167		a	a	a
168		aq	x	x
169		bq	x	x
170		abq	x	x
171		qab	xab	xab
172		abqabq	xabq	xabq
173[0-9]	xyz	0	xyz	xyz
174		00	xyz0	xyzxyz
175		000	xyz00	xyzxyzxyz
176		0a	xyza	xyza
177		a0	axyz	axyz
178		0a0	xyza0	xyzaxyz
179		xx	xx	xx
180		""	""	""
181^[0-9]	xyz	0	xyz	xyz
182		00	xyz0	xyz0
183		000	xyz00	xyz00
184		0a	xyza	xyza
185		a0	a0	a0
186		xx	xx	xx
187		""	""	""
188[0-9]$	xyz	0	xyz	xyz
189		00	0xyz	0xyz
190		000	00xyz	00xyz
191		0a	0a	0a
192		a0	axyz	axyz
193		xx	xx	xx
194		""	""	""
195[0-9]*	xyz	0	xyz	xyz
196		000	xyz	xyz
197		0a	xyza	xyzaxyz
198		a0	xyza0	xyzaxyz
199		0a0	xyza0	xyzaxyz
200		pq	xyzpq	xyzpxyzqxyz
201		""	xyz	xyz
202
203""	<&>	abc	<>abc	<>a<>b<>c<>	fixed 2/07, we think
204""	<\&>	abc	<&>abc	<&>a<&>b<&>c<&>
205""	<&&>	abc	<>abc	<>a<>b<>c<>
206""	<&>	""	<>	<>
207
208d?abc	<&>	abc	<abc>	<abc>
209d?	<&>	abc	<>abc	<>a<>b<>c<>
210
211x[0-9]+y	Q	xy	xy	xy	no change
212		x0y	Q	Q
213		x12y	Q	Q
214		x1y2	Q2	Q2
215		x1yax23y	Qax23y	QaQ
216
217# x[0-9]?y	~	xy
218# 		x1y
219# 	!~	x23y
220# x[[]y	~	x[y
221# 	!~	xy
222# 		x[[]y
223# 		x]y
224# x[^[]y	~	xay
225# 	!~	x[y
226# x[-]y	~	x-y
227# 	!~	xy
228# 		x+y
229# x[^-]y	~	x+y
230# 	!~	x-y
231# 		xy
232# [0\-9]	~	0
233# 		-
234# 		9
235# 	!~	1
236# 		""
237# [-1]	~	-
238# 		1
239# 	!~	0
240# [0-]	~	0
241# 		-
242# 	!~	1
243# [^-0]	~	x
244# 		^
245# 	!~	-
246# 		0
247# 		""
248# [^0-]	~	x
249# 		^
250# 	!~	-
251# 		0
252# 		""
253# x|y	~	x
254# 		y
255# 		xy
256# 	!~	a
257# 		""
258# ^abc|xyz$	~	abc
259# 		abcd
260# 		axyz
261# 		xyz
262# 	!~	xabc
263# 		xyza
264# ^(abc|xyz)$	~	abc
265# 		xyz
266# 	!~	abcxyz
267# 		abcx
268# 		cxyz
269# ^x\|y$	~	x|y
270# 	!~	xy
271# ^x\\y$	~	x\y
272# 	!~	xy
273# 		x\\y
274# 		xay
275# \141\142	~	ab
276# 		xab
277# 		abx
278# 	!~	a
279# 		b
280# 		ax
281# 		axb
282# x\056y	~	x.y
283# 	!~	x.
284# 		.x
285# 		xxx
286# 		xby  because \056 is not the metacharacter .
287# 		xcy  ditto
288# [\60-\62\65-6\71]	~	0
289# 		1
290# 		2
291# 		5
292# 		6
293# 		9
294# 	!~	3
295# 		4
296# 		7
297# 		8
298# [\60-2\65-6\71]	~	0
299# 		1
300# 		2
301# 		5
302# 		6
303# 		9
304# 	!~	3
305# 		4
306# 		7
307# 		8
308# [\x30-\x32\x35-6\71]	~	0
309# 		1
310# 		2
311# 		5
312# 		6
313# 		9
314# 	!~	3
315# 		4
316# 		7
317# 		8
318# [\x30-2\x35-6\x39]	~	0
319# 		1
320# 		2
321# 		5
322# 		6
323# 		9
324# 	!~	3
325# 		4
326# 		7
327# 		8
328!!!!
329