File: T.expr

package info (click to toggle)
original-awk 2025-01-16-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,400 kB
  • sloc: ansic: 6,367; awk: 1,271; sh: 1,214; yacc: 422; makefile: 73; fortran: 1
file content (235 lines) | stat: -rwxr-xr-x 3,583 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/sh

echo T.expr: tests of miscellaneous expressions

awk=${awk-../a.out}

$awk '
BEGIN {
	FS = "\t"
	awk = "../a.out"
}
NF == 0 || $1 ~ /^#/ {
	next
}
$1 ~ /try/ {	# new test
	nt++
	sub(/try /, "")
	prog = $0
	printf("%3d  %s\n", nt, prog)
	prog = sprintf("%s -F\"\\t\" '"'"'%s'"'"'", awk, prog)
	# print "prog is", prog
	nt2 = 0
	while (getline > 0) {
		if (NF == 0)	# blank line terminates a sequence
			break
		input = $1
		for (i = 2; i < NF; i++)	# input data
			input = input "\t" $i
		test = sprintf("./echo '"'"'%s'"'"' | %s >foo1; ",
			input, prog)
		if ($NF == "\"\"")
			output = ">foo2;"
		else
			output = sprintf("./echo '"'"'%s'"'"' >foo2; ", $NF)
		gsub(/\\t/, "\t", output)
		gsub(/\\n/, "\n", output)
		run = sprintf("cmp foo1 foo2 || echo test %d.%d failed",
			nt, ++nt2)
		# print  "input is", input
		# print  "test is", test
		# print  "output is", output
		# print  "run is", run
		system(test output run)
	}
	tt += nt2
}
END { print tt, "tests" }
' <<\!!!!
# General format:
# try program as rest of line
# $1	$2	$3	output1  (\t for tab, \n for newline,
# $1	$2	$3	output2  ("" for null)
# ... terminated by blank line

# try another program...

try { print ($1 == 1) ? "yes" : "no" }
1	yes
1.0	yes
1E0	yes
0.1E1	yes
10E-1	yes
01	yes
10	no
10E-2	no

try $1 > 0
1	1
2	2
0	""
-1	""
1e0	1e0
0e1	""
-2e64	""
3.1e4	3.1e4

try { print NF }
	0
x	1
x	y	2
	y	2
x		2

try { print NF, $NF }
	0 
x	1 x
x	y	2 y
x	yy	zzz	3 zzz

# this horror prints $($2+1)
try { i=1; print ($++$++i) }
1	1
1	2	3	3
abc	abc

# concatenate $1 and ++$2; print new $1 and concatenated value
try { x = $1++++$2; print $1, x }
1	3	2 14

# do we get the precedence of ! right?
try $1 !$2
0	0	0\t0
0	1	0\t1
1	0	1\t0
1	1	1\t1

# another ava special
try { print ($1~/abc/ !$2) }
0	0	01
0	1	00
abc	0	11
xabcd	1	10

try { print !$1 + $2 }
1	3	3
0	3	4
-1	3	3

# aside:  !$1 = $2 is now a syntax error

# the definition of "number" changes with isnumber.
# 2e100 is ok according to strtod.
# try 1 

try { print ($1 == $2) }
0	0	1
0	1	0
0	00	1
0	""	0
+0	-0	1
1	1.0	1
1	1e0	1
2e10	2.00e10	1
2e10	2e+10	1
2e-10	2e-10	1
2e10	2e-10	0
2e10	20e9	1
2e100	2.000e100	1
2e1000	2.0e1000	0

# this one (3 & 4) may "fail" if a negative 0 is printed as -0,
# but i think this might be a type-coercion problem.

try { print $1, +$1, -$1, - -$1 }
1	1 1 -1 1
-1	-1 -1 1 -1
0	0 0 0 0
x	x 0 0 0

try { printf("a%*sb\n", $1, $2) }
1	x	axb
2	x	a xb
3	x	a  xb

try { printf("a%-*sb\n", $1, $2) }
1	x	axb
2	x	ax b
3	x	ax  b

try { printf("a%*.*sb\n", $1, $2, "hello") }
1	1	ahb
2	1	a hb
3	1	a  hb

try { printf("a%-*.*sb\n", $1, $2, "hello") }
1	1	ahb
2	1	ah b
3	1	ah  b

try { printf("%d %ld %lld %zd %jd %hd %hhd\n", $1, $1, $1, $1, $1, $1, $1) }
1	1 1 1 1 1 1 1
10	10 10 10 10 10 10 10
10000	10000 10000 10000 10000 10000 10000 10000

try { printf("%x %lx %llx %zx %jx %hx %hhx\n", $1, $1, $1, $1, $1, $1, $1) }
1	1 1 1 1 1 1 1
10	a a a a a a a
10000	2710 2710 2710 2710 2710 2710 2710

try { if ($1 ~ $2) print 1; else print 0 }
a	\141	1
a	\142	0
a	\x61	1
a	\x061	0
a	\x62	0
0	\060	1
0	\60	1
0	\0060	0
Z	\x5a	1
Z	\x5A	1

try { print $1 ~ $2 }
a	\141	1
a	\142	0
a	\x61	1
a	\x061	0
a	\x62	0
0	\060	1
0	\60	1
0	\0060	0
Z	\x5a	1
Z	\x5A	1

try { print $1 || $2 }
		0
1		1
0	0	0
1	0	1
0	1	1
1	1	1
a	b	1

try { print $1 && $2 }
		0
1		0
0	0	0
1	0	0
0	1	0
1	1	1
a	b	1

try { $1 = $2; $1 = $1; print $1 }
abc	def	def
abc	def	ghi	def

# $f++ => ($f)++
try { f = 1; $f++; print f, $f }
11	22	33	1 12

# $f[1]++ => ($f[1])++
try { f[1]=1; f[2]=2; print $f[1], $f[1]++, $f[2], f[1], f[2] }
111	222	333	111 111 222 2 2


!!!!