xref: /freebsd/contrib/one-true-awk/testdir/T.gawk (revision 2008043f386721d58158e37e0d7e50df8095942d)
1echo T.gawk: tests adapted from gawk test suite
2# for which thanks.
3
4awk=${awk-../a.out}
5
6# arrayref:
7./echo '1
81' >foo1
9$awk '
10	BEGIN { # foo[10] = 0		# put this line in and it will work
11		test(foo); print foo[1]
12		test2(foo2); print foo2[1]
13	}
14	function test(foo) { test2(foo) }
15	function test2(bar) { bar[1] = 1 }
16' >foo2
17cmp -s foo1 foo2 || ./echo 'BAD: T.gawk arrayref'
18
19# asgext
20./echo '1 2 3
211
221 2 3 4' >foo
23./echo '3
241 2 3 a
25
261   a
273
281 2 3 a' >foo1
29$awk '{ print $3; $4 = "a"; print }' foo >foo2
30cmp -s foo1 foo2 || ./echo 'BAD: T.gawk asgext'
31
32# backgsub:
33./echo 'x\y
34x\\y' >foo
35./echo 'x\y
36xAy
37xAy
38xAAy' >foo1
39$awk '{	x = y = $0
40        gsub( /\\\\/, "A", x); print x
41        gsub( "\\\\", "A", y); print y
42}' foo >foo2
43cmp -s foo1 foo2 || ./echo 'BAD: T.gawk backgsub'
44
45
46# backgsub2:
47./echo 'x\y
48x\\y
49x\\\y' >foo
50./echo '	x\y
51	x\y
52	x\y
53	x\y
54	x\\y
55	x\\\y
56	x\\y
57	x\\\y
58	x\\\\y' >foo1
59$awk '{	w = x = y = z = $0
60        gsub( /\\\\/, "\\", w); print "	" w
61        gsub( /\\\\/, "\\\\", x); print "	" x
62        gsub( /\\\\/, "\\\\\\", y); print "	" y
63}
64' foo >foo2
65cmp -s foo1 foo2 || ./echo 'BAD: T.gawk backgsub2'
66
67
68# backgsub3:
69./echo 'xax
70xaax' >foo
71./echo '	xax
72	x&x
73	x&x
74	x\ax
75	x\ax
76	x\&x
77	xaax
78	x&&x
79	x&&x
80	x\a\ax
81	x\a\ax
82	x\&\&x' >foo1
83$awk '{	w = x = y = z = z1 = z2 = $0
84        gsub( /a/, "\&", w); print "	" w
85        gsub( /a/, "\\&", x); print "	" x
86        gsub( /a/, "\\\&", y); print "	" y
87        gsub( /a/, "\\\\&", z); print "	" z
88        gsub( /a/, "\\\\\&", z1); print "	" z1
89        gsub( /a/, "\\\\\\&", z2); print "	" z2
90}
91' foo >foo2
92cmp -s foo1 foo2 || ./echo 'BAD: T.gawk backgsub3'
93
94
95# backsub3:
96./echo 'xax
97xaax' >foo
98./echo '	xax
99	x&x
100	x&x
101	x\ax
102	x\ax
103	x\&x
104	xaax
105	x&ax
106	x&ax
107	x\aax
108	x\aax
109	x\&ax' >foo1
110$awk '{	w = x = y = z = z1 = z2 = $0
111        sub( /a/, "\&", w); print "	" w
112        sub( /a/, "\\&", x); print "	" x
113        sub( /a/, "\\\&", y); print "	" y
114        sub( /a/, "\\\\&", z); print "	" z
115        sub( /a/, "\\\\\&", z1); print "	" z1
116        sub( /a/, "\\\\\\&", z2); print "	" z2
117}
118' foo >foo2
119cmp -s foo1 foo2 || ./echo 'BAD: T.gawk backsub3'
120
121
122# backsub:
123./echo 'x\y
124x\\y' >foo
125./echo 'x\y
126x\\y
127x\\y
128x\\\y' >foo1
129$awk '{	x = y = $0
130        sub( /\\\\/, "\\\\", x); print x
131        sub( "\\\\", "\\\\", y); print y
132}' foo >foo2
133cmp -s foo1 foo2 || ./echo 'BAD: T.gawk backsub'
134
135
136
137
138# dynlj:
139./echo 'hello               world' >foo1
140$awk 'BEGIN { printf "%*sworld\n", -20, "hello" }' >foo2
141cmp -s foo1 foo2 || ./echo 'BAD: T.gawk dynlj'
142
143# fsrs:
144./echo 'a b
145c d
146e f
147
1481 2
1493 4
1505 6' >foo
151# note -n:
152./echo -n 'a b
153c d
154e f1 2
1553 4
1565 6' >foo1
157$awk '
158BEGIN {
159       RS=""; FS="\n";
160       ORS=""; OFS="\n";
161      }
162{
163        split ($2,f," ")
164        print $0;
165}' foo >foo2
166cmp -s foo1 foo2 || ./echo 'BAD: T.gawk fsrs'
167
168# intest
169./echo '0 1' >foo1
170$awk 'BEGIN {
171	bool = ((b = 1) in c);
172	print bool, b	# gawk-3.0.1 prints "0 "; should print "0 1"
173}' >foo2
174cmp -s foo1 foo2 || ./echo 'BAD: T.gawk intest'
175
176# intprec:
177./echo '0000000005:000000000e' >foo1
178$awk 'BEGIN { printf "%.10d:%.10x\n", 5, 14 }' >foo2
179cmp -s foo1 foo2 || ./echo 'BAD: T.gawk intprec'
180
181# litoct:
182./echo 'axb
183ab
184a*b' >foo
185./echo 'no match
186no match
187match' >foo1
188$awk '{ if (/a\52b/) print "match" ; else print "no match" }' foo >foo2
189cmp -s foo1 foo2 || ./echo 'BAD: T.gawk litoct'
190
191# math:
192./echo 'cos(0.785398) = 0.707107
193sin(0.785398) = 0.707107
194e = 2.718282
195log(e) = 1.000000
196sqrt(pi ^ 2) = 3.141593
197atan2(1, 1) = 0.785398' >foo1
198$awk 'BEGIN {
199	pi = 3.1415927
200	printf "cos(%f) = %f\n", pi/4, cos(pi/4)
201	printf "sin(%f) = %f\n", pi/4, sin(pi/4)
202	e = exp(1)
203	printf "e = %f\n", e
204	printf "log(e) = %f\n", log(e)
205	printf "sqrt(pi ^ 2) = %f\n", sqrt(pi ^ 2)
206	printf "atan2(1, 1) = %f\n", atan2(1, 1)
207}' >foo2
208cmp -s foo1 foo2 || ./echo 'BAD: T.gawk math'
209
210# nlfldsep:
211./echo 'some stuff
212more stuffA
213junk
214stuffA
215final' >foo
216./echo '4
217some
218stuff
219more
220stuff
221
2222
223junk
224stuff
225
2261
227final
228' >foo1
229$awk 'BEGIN { RS = "A" }
230{print NF; for (i = 1; i <= NF; i++) print $i ; print ""}
231' foo >foo2
232cmp -s foo1 foo2 || ./echo 'BAD: T.gawk nlfldsep'
233
234# numsubstr:
235./echo '5000
23610000
2375000' >foo
238./echo '000
2391000
240000' >foo1
241$awk '{ print substr(1000+$1, 2) }' foo >foo2
242cmp -s foo1 foo2 || ./echo 'BAD: T.gawk numsubstr'
243
244# pcntplus:
245./echo '+3 4' >foo1
246$awk 'BEGIN { printf "%+d %d\n", 3, 4 }' >foo2
247cmp -s foo1 foo2 || ./echo 'BAD: T.gawk pcntplus'
248
249# prt1eval:
250./echo 1 >foo1
251$awk 'function tst () {
252	sum += 1
253	return sum
254}
255BEGIN { OFMT = "%.0f" ; print tst() }
256' >foo2
257cmp -s foo1 foo2 || ./echo 'BAD: T.gawk prt1eval'
258
259# reparse:
260./echo '1 axbxc 2' >foo
261./echo '1
2621 a b c 2
2631 a b' >foo1
264$awk '{	gsub(/x/, " ")
265	$0 = $0
266	print $1
267	print $0
268	print $1, $2, $3
269}' foo >foo2
270cmp -s foo1 foo2 || ./echo 'BAD: T.gawk reparse'
271
272# rswhite:
273./echo '    a b
274c d' >foo
275./echo '<    a b
276c d>' >foo1
277$awk 'BEGIN { RS = "" }
278{ printf("<%s>\n", $0) }' foo  >foo2
279cmp -s foo1 foo2 || ./echo 'BAD: T.gawk rswhite'
280
281# splitvar:
282./echo 'Here===Is=Some=====Data' >foo
283./echo 4 >foo1
284$awk '{	sep = "=+"
285	n = split($0, a, sep)
286	print n
287}' foo >foo2
288cmp -s foo1 foo2 || ./echo 'BAD: T.gawk splitvar'
289
290# splitwht:
291./echo '4
2925' >foo1
293$awk 'BEGIN {
294	str = "a   b\t\tc d"
295	n = split(str, a, " ")
296	print n
297	m = split(str, b, / /)
298	print m
299}' >foo2
300cmp -s foo1 foo2 || ./echo 'BAD: T.gawk splitwht'
301
302# sprintfc:
303./echo '65
30466
305foo' >foo
306./echo 'A 65
307B 66
308f foo' >foo1
309$awk '{ print sprintf("%c", $1), $1 }' foo >foo2
310cmp -s foo1 foo2 || ./echo 'BAD: T.gawk sprintfc'
311
312# substr:
313./echo 'xxA
314xxab
315xxbc
316xxab
317xx
318xx
319xxab
320xx
321xxef
322xx' >foo1
323$awk 'BEGIN {
324	x = "A"
325	printf("xx%-39s\n", substr(x,1,39))
326	print "xx" substr("abcdef", 0, 2)
327	print "xx" substr("abcdef", 2.3, 2)
328	print "xx" substr("abcdef", -1, 2)
329	print "xx" substr("abcdef", 1, 0)
330	print "xx" substr("abcdef", 1, -3)
331	print "xx" substr("abcdef", 1, 2.3)
332	print "xx" substr("", 1, 2)
333	print "xx" substr("abcdef", 5, 5)
334	print "xx" substr("abcdef", 7, 2)
335	exit (0)
336}' >foo2
337cmp -s foo1 foo2 || ./echo 'BAD: T.gawk substr'
338
339# fldchg:
340./echo 'aa aab c d e f' >foo
341./echo '1: + +b c d e f
3422: + +b <c> d e f
3432a:%+%+b%<c>%d%e' >foo1
344$awk '{	gsub("aa", "+")
345	print "1:", $0
346	$3 = "<" $3 ">"
347	print "2:", $0
348	print "2a:" "%" $1 "%" $2 "%" $3 "%" $4 "%" $5
349}' foo >foo2
350cmp -s foo1 foo2 || ./echo 'BAD: T.gawk fldchg'
351
352# fldchgnf:
353./echo 'a b c d' >foo
354./echo 'a::c:d
3554' >foo1
356$awk '{ OFS = ":"; $2 = ""; print $0; print NF }' foo >foo2
357cmp -s foo1 foo2 || ./echo 'BAD: T.gawk fldchgnf'
358
359# funstack:
360# ./echo '	funstack test takes 5-10 sec, replicates part of T.beebe'
361$awk -f funstack.awk funstack.in >foo 2>&1
362cmp -s foo funstack.ok || ./echo 'BAD: T.gawk funstack'
363
364# OFMT from arnold robbins 6/02:
365#	5.7 with OFMT = %0.f is 6
366./echo '6' >foo1
367$awk 'BEGIN {
368	OFMT = "%.0f"
369	print 5.7
370}' >foo2
371cmp -s foo1 foo2 || ./echo 'BAD: T.gawk ofmt'
372
373
374### don't know what this is supposed to do now.
375### # convfmt:
376### ./echo 'a =  123.46
377### a =  123.456
378### a =  123.456' >foo1
379### $awk 'BEGIN {
380### 	CONVFMT = "%2.2f"
381### 	a = 123.456
382### 	b = a ""                # give a string value also
383### 	a += 0                  # make a numeric only again
384### 	print "a = ", a
385### 	CONVFMT = "%.6g"
386### 	print "a = ", a
387### 	a += 0                  # make a numeric only again
388### 	print "a = ", a    # use a as string
389### }' >foo2
390### cmp -s foo1 foo2 || ./echo 'BAD: T.gawk convfmt'
391