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