xref: /freebsd/contrib/one-true-awk/testdir/T.expr (revision 23f24377)
1#!/bin/sh
2
3echo T.expr: tests of miscellaneous expressions
4
5awk=${awk-../a.out}
6
7$awk '
8BEGIN {
9	FS = "\t"
10	awk = "../a.out"
11}
12NF == 0 || $1 ~ /^#/ {
13	next
14}
15$1 ~ /try/ {	# new test
16	nt++
17	sub(/try /, "")
18	prog = $0
19	printf("%3d  %s\n", nt, prog)
20	prog = sprintf("%s -F\"\\t\" '"'"'%s'"'"'", awk, prog)
21	# print "prog is", prog
22	nt2 = 0
23	while (getline > 0) {
24		if (NF == 0)	# blank line terminates a sequence
25			break
26		input = $1
27		for (i = 2; i < NF; i++)	# input data
28			input = input "\t" $i
29		test = sprintf("./echo '"'"'%s'"'"' | %s >foo1; ",
30			input, prog)
31		if ($NF == "\"\"")
32			output = ">foo2;"
33		else
34			output = sprintf("./echo '"'"'%s'"'"' >foo2; ", $NF)
35		gsub(/\\t/, "\t", output)
36		gsub(/\\n/, "\n", output)
37		run = sprintf("cmp foo1 foo2 || echo test %d.%d failed",
38			nt, ++nt2)
39		# print  "input is", input
40		# print  "test is", test
41		# print  "output is", output
42		# print  "run is", run
43		system(test output run)
44	}
45	tt += nt2
46}
47END { print tt, "tests" }
48' <<\!!!!
49# General format:
50# try program as rest of line
51# $1	$2	$3	output1  (\t for tab, \n for newline,
52# $1	$2	$3	output2  ("" for null)
53# ... terminated by blank line
54
55# try another program...
56
57try { print ($1 == 1) ? "yes" : "no" }
581	yes
591.0	yes
601E0	yes
610.1E1	yes
6210E-1	yes
6301	yes
6410	no
6510E-2	no
66
67try $1 > 0
681	1
692	2
700	""
71-1	""
721e0	1e0
730e1	""
74-2e64	""
753.1e4	3.1e4
76
77try { print NF }
78	0
79x	1
80x	y	2
81	y	2
82x		2
83
84try { print NF, $NF }
85	0
86x	1 x
87x	y	2 y
88x	yy	zzz	3 zzz
89
90# this horror prints $($2+1)
91try { i=1; print ($++$++i) }
921	1
931	2	3	3
94abc	abc
95
96# concatenate $1 and ++$2; print new $1 and concatenated value
97try { x = $1++++$2; print $1, x }
981	3	2 14
99
100# do we get the precedence of ! right?
101try $1 !$2
1020	0	0\t0
1030	1	0\t1
1041	0	1\t0
1051	1	1\t1
106
107# another ava special
108try { print ($1~/abc/ !$2) }
1090	0	01
1100	1	00
111abc	0	11
112xabcd	1	10
113
114try { print !$1 + $2 }
1151	3	3
1160	3	4
117-1	3	3
118
119# aside:  !$1 = $2 is now a syntax error
120
121# the definition of "number" changes with isnumber.
122# 2e100 is ok according to strtod.
123# try 1
124
125try { print ($1 == $2) }
1260	0	1
1270	1	0
1280	00	1
1290	""	0
130+0	-0	1
1311	1.0	1
1321	1e0	1
1332e10	2.00e10	1
1342e10	2e+10	1
1352e-10	2e-10	1
1362e10	2e-10	0
1372e10	20e9	1
1382e100	2.000e100	1
1392e1000	2.0e1000	0
140
141# this one (3 & 4) may "fail" if a negative 0 is printed as -0,
142# but i think this might be a type-coercion problem.
143
144try { print $1, +$1, -$1, - -$1 }
1451	1 1 -1 1
146-1	-1 -1 1 -1
1470	0 0 0 0
148x	x 0 0 0
149
150try { printf("a%*sb\n", $1, $2) }
1511	x	axb
1522	x	a xb
1533	x	a  xb
154
155try { printf("a%-*sb\n", $1, $2) }
1561	x	axb
1572	x	ax b
1583	x	ax  b
159
160try { printf("a%*.*sb\n", $1, $2, "hello") }
1611	1	ahb
1622	1	a hb
1633	1	a  hb
164
165try { printf("a%-*.*sb\n", $1, $2, "hello") }
1661	1	ahb
1672	1	ah b
1683	1	ah  b
169
170try { printf("%d %ld %lld %zd %jd %hd %hhd\n", $1, $1, $1, $1, $1, $1, $1) }
1711	1 1 1 1 1 1 1
17210	10 10 10 10 10 10 10
17310000	10000 10000 10000 10000 10000 10000 10000
174
175try { printf("%x %lx %llx %zx %jx %hx %hhx\n", $1, $1, $1, $1, $1, $1, $1) }
1761	1 1 1 1 1 1 1
17710	a a a a a a a
17810000	2710 2710 2710 2710 2710 2710 2710
179
180try { if ($1 ~ $2) print 1; else print 0 }
181a	\141	1
182a	\142	0
183a	\x61	1
184a	\x061	0
185a	\x62	0
1860	\060	1
1870	\60	1
1880	\0060	0
189Z	\x5a	1
190Z	\x5A	1
191
192try { print $1 ~ $2 }
193a	\141	1
194a	\142	0
195a	\x61	1
196a	\x061	0
197a	\x62	0
1980	\060	1
1990	\60	1
2000	\0060	0
201Z	\x5a	1
202Z	\x5A	1
203
204try { print $1 || $2 }
205		0
2061		1
2070	0	0
2081	0	1
2090	1	1
2101	1	1
211a	b	1
212
213try { print $1 && $2 }
214		0
2151		0
2160	0	0
2171	0	0
2180	1	0
2191	1	1
220a	b	1
221
222try { $1 = $2; $1 = $1; print $1 }
223abc	def	def
224abc	def	ghi	def
225
226# $f++ => ($f)++
227try { f = 1; $f++; print f, $f }
22811	22	33	1 12
229
230# $f[1]++ => ($f[1])++
231try { f[1]=1; f[2]=2; print $f[1], $f[1]++, $f[2], f[1], f[2] }
232111	222	333	111 111 222 2 2
233
234
235!!!!
236