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