xref: /aosp_15_r20/external/one-true-awk/testdir/T.expr (revision 9a7741de182b2776d7b30d6355f2585c0780a51b)
1*9a7741deSElliott Hughes#!/bin/sh
2*9a7741deSElliott Hughes
3*9a7741deSElliott Hughesecho T.expr: tests of miscellaneous expressions
4*9a7741deSElliott Hughes
5*9a7741deSElliott Hughesawk=${awk-../a.out}
6*9a7741deSElliott Hughes
7*9a7741deSElliott Hughes$awk '
8*9a7741deSElliott HughesBEGIN {
9*9a7741deSElliott Hughes	FS = "\t"
10*9a7741deSElliott Hughes	awk = "../a.out"
11*9a7741deSElliott Hughes}
12*9a7741deSElliott HughesNF == 0 || $1 ~ /^#/ {
13*9a7741deSElliott Hughes	next
14*9a7741deSElliott Hughes}
15*9a7741deSElliott Hughes$1 ~ /try/ {	# new test
16*9a7741deSElliott Hughes	nt++
17*9a7741deSElliott Hughes	sub(/try /, "")
18*9a7741deSElliott Hughes	prog = $0
19*9a7741deSElliott Hughes	printf("%3d  %s\n", nt, prog)
20*9a7741deSElliott Hughes	prog = sprintf("%s -F\"\\t\" '"'"'%s'"'"'", awk, prog)
21*9a7741deSElliott Hughes	# print "prog is", prog
22*9a7741deSElliott Hughes	nt2 = 0
23*9a7741deSElliott Hughes	while (getline > 0) {
24*9a7741deSElliott Hughes		if (NF == 0)	# blank line terminates a sequence
25*9a7741deSElliott Hughes			break
26*9a7741deSElliott Hughes		input = $1
27*9a7741deSElliott Hughes		for (i = 2; i < NF; i++)	# input data
28*9a7741deSElliott Hughes			input = input "\t" $i
29*9a7741deSElliott Hughes		test = sprintf("./echo '"'"'%s'"'"' | %s >foo1; ",
30*9a7741deSElliott Hughes			input, prog)
31*9a7741deSElliott Hughes		if ($NF == "\"\"")
32*9a7741deSElliott Hughes			output = ">foo2;"
33*9a7741deSElliott Hughes		else
34*9a7741deSElliott Hughes			output = sprintf("./echo '"'"'%s'"'"' >foo2; ", $NF)
35*9a7741deSElliott Hughes		gsub(/\\t/, "\t", output)
36*9a7741deSElliott Hughes		gsub(/\\n/, "\n", output)
37*9a7741deSElliott Hughes		run = sprintf("cmp foo1 foo2 || echo test %d.%d failed",
38*9a7741deSElliott Hughes			nt, ++nt2)
39*9a7741deSElliott Hughes		# print  "input is", input
40*9a7741deSElliott Hughes		# print  "test is", test
41*9a7741deSElliott Hughes		# print  "output is", output
42*9a7741deSElliott Hughes		# print  "run is", run
43*9a7741deSElliott Hughes		system(test output run)
44*9a7741deSElliott Hughes	}
45*9a7741deSElliott Hughes	tt += nt2
46*9a7741deSElliott Hughes}
47*9a7741deSElliott HughesEND { print tt, "tests" }
48*9a7741deSElliott Hughes' <<\!!!!
49*9a7741deSElliott Hughes# General format:
50*9a7741deSElliott Hughes# try program as rest of line
51*9a7741deSElliott Hughes# $1	$2	$3	output1  (\t for tab, \n for newline,
52*9a7741deSElliott Hughes# $1	$2	$3	output2  ("" for null)
53*9a7741deSElliott Hughes# ... terminated by blank line
54*9a7741deSElliott Hughes
55*9a7741deSElliott Hughes# try another program...
56*9a7741deSElliott Hughes
57*9a7741deSElliott Hughestry { print ($1 == 1) ? "yes" : "no" }
58*9a7741deSElliott Hughes1	yes
59*9a7741deSElliott Hughes1.0	yes
60*9a7741deSElliott Hughes1E0	yes
61*9a7741deSElliott Hughes0.1E1	yes
62*9a7741deSElliott Hughes10E-1	yes
63*9a7741deSElliott Hughes01	yes
64*9a7741deSElliott Hughes10	no
65*9a7741deSElliott Hughes10E-2	no
66*9a7741deSElliott Hughes
67*9a7741deSElliott Hughestry $1 > 0
68*9a7741deSElliott Hughes1	1
69*9a7741deSElliott Hughes2	2
70*9a7741deSElliott Hughes0	""
71*9a7741deSElliott Hughes-1	""
72*9a7741deSElliott Hughes1e0	1e0
73*9a7741deSElliott Hughes0e1	""
74*9a7741deSElliott Hughes-2e64	""
75*9a7741deSElliott Hughes3.1e4	3.1e4
76*9a7741deSElliott Hughes
77*9a7741deSElliott Hughestry { print NF }
78*9a7741deSElliott Hughes	0
79*9a7741deSElliott Hughesx	1
80*9a7741deSElliott Hughesx	y	2
81*9a7741deSElliott Hughes	y	2
82*9a7741deSElliott Hughesx		2
83*9a7741deSElliott Hughes
84*9a7741deSElliott Hughestry { print NF, $NF }
85*9a7741deSElliott Hughes	0
86*9a7741deSElliott Hughesx	1 x
87*9a7741deSElliott Hughesx	y	2 y
88*9a7741deSElliott Hughesx	yy	zzz	3 zzz
89*9a7741deSElliott Hughes
90*9a7741deSElliott Hughes# this horror prints $($2+1)
91*9a7741deSElliott Hughestry { i=1; print ($++$++i) }
92*9a7741deSElliott Hughes1	1
93*9a7741deSElliott Hughes1	2	3	3
94*9a7741deSElliott Hughesabc	abc
95*9a7741deSElliott Hughes
96*9a7741deSElliott Hughes# concatenate $1 and ++$2; print new $1 and concatenated value
97*9a7741deSElliott Hughestry { x = $1++++$2; print $1, x }
98*9a7741deSElliott Hughes1	3	2 14
99*9a7741deSElliott Hughes
100*9a7741deSElliott Hughes# do we get the precedence of ! right?
101*9a7741deSElliott Hughestry $1 !$2
102*9a7741deSElliott Hughes0	0	0\t0
103*9a7741deSElliott Hughes0	1	0\t1
104*9a7741deSElliott Hughes1	0	1\t0
105*9a7741deSElliott Hughes1	1	1\t1
106*9a7741deSElliott Hughes
107*9a7741deSElliott Hughes# another ava special
108*9a7741deSElliott Hughestry { print ($1~/abc/ !$2) }
109*9a7741deSElliott Hughes0	0	01
110*9a7741deSElliott Hughes0	1	00
111*9a7741deSElliott Hughesabc	0	11
112*9a7741deSElliott Hughesxabcd	1	10
113*9a7741deSElliott Hughes
114*9a7741deSElliott Hughestry { print !$1 + $2 }
115*9a7741deSElliott Hughes1	3	3
116*9a7741deSElliott Hughes0	3	4
117*9a7741deSElliott Hughes-1	3	3
118*9a7741deSElliott Hughes
119*9a7741deSElliott Hughes# aside:  !$1 = $2 is now a syntax error
120*9a7741deSElliott Hughes
121*9a7741deSElliott Hughes# the definition of "number" changes with isnumber.
122*9a7741deSElliott Hughes# 2e100 is ok according to strtod.
123*9a7741deSElliott Hughes# try 1
124*9a7741deSElliott Hughes
125*9a7741deSElliott Hughestry { print ($1 == $2) }
126*9a7741deSElliott Hughes0	0	1
127*9a7741deSElliott Hughes0	1	0
128*9a7741deSElliott Hughes0	00	1
129*9a7741deSElliott Hughes0	""	0
130*9a7741deSElliott Hughes+0	-0	1
131*9a7741deSElliott Hughes1	1.0	1
132*9a7741deSElliott Hughes1	1e0	1
133*9a7741deSElliott Hughes2e10	2.00e10	1
134*9a7741deSElliott Hughes2e10	2e+10	1
135*9a7741deSElliott Hughes2e-10	2e-10	1
136*9a7741deSElliott Hughes2e10	2e-10	0
137*9a7741deSElliott Hughes2e10	20e9	1
138*9a7741deSElliott Hughes2e100	2.000e100	1
139*9a7741deSElliott Hughes2e1000	2.0e1000	0
140*9a7741deSElliott Hughes
141*9a7741deSElliott Hughes# this one (3 & 4) may "fail" if a negative 0 is printed as -0,
142*9a7741deSElliott Hughes# but i think this might be a type-coercion problem.
143*9a7741deSElliott Hughes
144*9a7741deSElliott Hughestry { print $1, +$1, -$1, - -$1 }
145*9a7741deSElliott Hughes1	1 1 -1 1
146*9a7741deSElliott Hughes-1	-1 -1 1 -1
147*9a7741deSElliott Hughes0	0 0 0 0
148*9a7741deSElliott Hughesx	x 0 0 0
149*9a7741deSElliott Hughes
150*9a7741deSElliott Hughestry { printf("a%*sb\n", $1, $2) }
151*9a7741deSElliott Hughes1	x	axb
152*9a7741deSElliott Hughes2	x	a xb
153*9a7741deSElliott Hughes3	x	a  xb
154*9a7741deSElliott Hughes
155*9a7741deSElliott Hughestry { printf("a%-*sb\n", $1, $2) }
156*9a7741deSElliott Hughes1	x	axb
157*9a7741deSElliott Hughes2	x	ax b
158*9a7741deSElliott Hughes3	x	ax  b
159*9a7741deSElliott Hughes
160*9a7741deSElliott Hughestry { printf("a%*.*sb\n", $1, $2, "hello") }
161*9a7741deSElliott Hughes1	1	ahb
162*9a7741deSElliott Hughes2	1	a hb
163*9a7741deSElliott Hughes3	1	a  hb
164*9a7741deSElliott Hughes
165*9a7741deSElliott Hughestry { printf("a%-*.*sb\n", $1, $2, "hello") }
166*9a7741deSElliott Hughes1	1	ahb
167*9a7741deSElliott Hughes2	1	ah b
168*9a7741deSElliott Hughes3	1	ah  b
169*9a7741deSElliott Hughes
170*9a7741deSElliott Hughestry { printf("%d %ld %lld %zd %jd %hd %hhd\n", $1, $1, $1, $1, $1, $1, $1) }
171*9a7741deSElliott Hughes1	1 1 1 1 1 1 1
172*9a7741deSElliott Hughes10	10 10 10 10 10 10 10
173*9a7741deSElliott Hughes10000	10000 10000 10000 10000 10000 10000 10000
174*9a7741deSElliott Hughes
175*9a7741deSElliott Hughestry { printf("%x %lx %llx %zx %jx %hx %hhx\n", $1, $1, $1, $1, $1, $1, $1) }
176*9a7741deSElliott Hughes1	1 1 1 1 1 1 1
177*9a7741deSElliott Hughes10	a a a a a a a
178*9a7741deSElliott Hughes10000	2710 2710 2710 2710 2710 2710 2710
179*9a7741deSElliott Hughes
180*9a7741deSElliott Hughestry { if ($1 ~ $2) print 1; else print 0 }
181*9a7741deSElliott Hughesa	\141	1
182*9a7741deSElliott Hughesa	\142	0
183*9a7741deSElliott Hughesa	\x61	1
184*9a7741deSElliott Hughesa	\x061	0
185*9a7741deSElliott Hughesa	\x62	0
186*9a7741deSElliott Hughes0	\060	1
187*9a7741deSElliott Hughes0	\60	1
188*9a7741deSElliott Hughes0	\0060	0
189*9a7741deSElliott HughesZ	\x5a	1
190*9a7741deSElliott HughesZ	\x5A	1
191*9a7741deSElliott Hughes
192*9a7741deSElliott Hughestry { print $1 ~ $2 }
193*9a7741deSElliott Hughesa	\141	1
194*9a7741deSElliott Hughesa	\142	0
195*9a7741deSElliott Hughesa	\x61	1
196*9a7741deSElliott Hughesa	\x061	0
197*9a7741deSElliott Hughesa	\x62	0
198*9a7741deSElliott Hughes0	\060	1
199*9a7741deSElliott Hughes0	\60	1
200*9a7741deSElliott Hughes0	\0060	0
201*9a7741deSElliott HughesZ	\x5a	1
202*9a7741deSElliott HughesZ	\x5A	1
203*9a7741deSElliott Hughes
204*9a7741deSElliott Hughestry { print $1 || $2 }
205*9a7741deSElliott Hughes		0
206*9a7741deSElliott Hughes1		1
207*9a7741deSElliott Hughes0	0	0
208*9a7741deSElliott Hughes1	0	1
209*9a7741deSElliott Hughes0	1	1
210*9a7741deSElliott Hughes1	1	1
211*9a7741deSElliott Hughesa	b	1
212*9a7741deSElliott Hughes
213*9a7741deSElliott Hughestry { print $1 && $2 }
214*9a7741deSElliott Hughes		0
215*9a7741deSElliott Hughes1		0
216*9a7741deSElliott Hughes0	0	0
217*9a7741deSElliott Hughes1	0	0
218*9a7741deSElliott Hughes0	1	0
219*9a7741deSElliott Hughes1	1	1
220*9a7741deSElliott Hughesa	b	1
221*9a7741deSElliott Hughes
222*9a7741deSElliott Hughestry { $1 = $2; $1 = $1; print $1 }
223*9a7741deSElliott Hughesabc	def	def
224*9a7741deSElliott Hughesabc	def	ghi	def
225*9a7741deSElliott Hughes
226*9a7741deSElliott Hughes# $f++ => ($f)++
227*9a7741deSElliott Hughestry { f = 1; $f++; print f, $f }
228*9a7741deSElliott Hughes11	22	33	1 12
229*9a7741deSElliott Hughes
230*9a7741deSElliott Hughes# $f[1]++ => ($f[1])++
231*9a7741deSElliott Hughestry { f[1]=1; f[2]=2; print $f[1], $f[1]++, $f[2], f[1], f[2] }
232*9a7741deSElliott Hughes111	222	333	111 111 222 2 2
233*9a7741deSElliott Hughes
234*9a7741deSElliott Hughes
235*9a7741deSElliott Hughes!!!!
236