blob: cf9b722dc203f5918d6c2af9148e703c43423f34 [file] [log] [blame]
Denis Vlasenko960eca62007-07-18 18:33:18 +00001#!/bin/sh
2
Denis Vlasenko960eca62007-07-18 18:33:18 +00003# Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004# Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko960eca62007-07-18 18:33:18 +00005
Mike Frysingercaa79402009-11-04 18:41:22 -05006. ./testing.sh
Denis Vlasenko960eca62007-07-18 18:33:18 +00007
Denis Vlasenkobb130792008-07-15 06:45:14 +00008# testing "description" "command" "result" "infile" "stdin"
Denis Vlasenko960eca62007-07-18 18:33:18 +00009
10testing "awk -F case 0" "awk -F '[#]' '{ print NF }'" "" "" ""
11testing "awk -F case 1" "awk -F '[#]' '{ print NF }'" "0\n" "" "\n"
12testing "awk -F case 2" "awk -F '[#]' '{ print NF }'" "2\n" "" "#\n"
13testing "awk -F case 3" "awk -F '[#]' '{ print NF }'" "3\n" "" "#abc#\n"
14testing "awk -F case 4" "awk -F '[#]' '{ print NF }'" "3\n" "" "#abc#zz\n"
15testing "awk -F case 5" "awk -F '[#]' '{ print NF }'" "4\n" "" "#abc##zz\n"
16testing "awk -F case 6" "awk -F '[#]' '{ print NF }'" "4\n" "" "z#abc##zz\n"
17testing "awk -F case 7" "awk -F '[#]' '{ print NF }'" "5\n" "" "z##abc##zz\n"
18
Tanguy Pruvot0a393cf2012-06-11 10:33:45 +020019# conditions and operators
20testing "awk if operator == " "awk 'BEGIN{if(23==23) print \"foo\"}'" "foo\n" "" ""
21testing "awk if operator != " "awk 'BEGIN{if(23!=23) print \"bar\"}'" "" "" ""
22testing "awk if operator >= " "awk 'BEGIN{if(23>=23) print \"foo\"}'" "foo\n" "" ""
23testing "awk if operator < " "awk 'BEGIN{if(2 < 13) print \"foo\"}'" "foo\n" "" ""
24testing "awk if string == " "awk 'BEGIN{if(\"a\"==\"ab\") print \"bar\"}'" "" "" ""
25
Denis Vlasenko66496d72008-08-29 08:37:07 +000026# 4294967295 = 0xffffffff
Denys Vlasenkob8554602013-07-22 11:49:06 +020027testing "awk bitwise op" "awk '{ print or(4294967295,1) }'" "4294967295\n" "" "\n"
Bernhard Reutner-Fischerb79a0fe2013-03-06 21:01:05 +010028
29# we were testing for a non-empty body when deciding if a function was
30# defined or not. The testcase below caused:
31# awk: cmd. line:8: Call to undefined function
32prg='
33function empty_fun(count) {
34 # empty
35}
36END {
37 i=1
38 print "L" i "\n"
39 empty_fun(i + i + ++i)
40 print "L" i "\n"
41}'
42testing "awk handles empty function f(arg){}" \
43 "awk '$prg'" \
44 "L1\n\nL2\n\n" \
45 "" ""
46
Bernhard Reutner-Fischera060a1a2013-07-31 15:29:20 +020047prg='
48function outer_fun() {
49 return 1
50}
51END {
52 i=1
53 print "L" i "\n"
54 i += outer_fun()
55 print "L" i "\n"
56}'
57testing "awk properly handles function from other scope" \
58 "awk '$prg'" \
59 "L1\n\nL2\n\n" \
60 "" ""
61
62prg='
63END {
64 i=1
65 print "L" i "\n"
66 i + trigger_error_fun()
67 print "L" i "\n"
68}'
69testing "awk properly handles undefined function" \
70 "awk '$prg' 2>&1" \
71 "L1\n\nawk: cmd. line:5: Call to undefined function\n" \
72 "" ""
73
74
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020075optional DESKTOP
Denys Vlasenkob8554602013-07-22 11:49:06 +020076testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4294967295\n" "" "\n"
77testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2147483649\n" "" "\n"
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +000078testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n"
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020079SKIP=
Denis Vlasenko66496d72008-08-29 08:37:07 +000080
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020081# check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN
82testing "awk floating const with leading zeroes" \
83 "awk '{ printf \"%f %f\n\", \"000.123\", \"009.123\" }'" \
84 "0.123000 9.123000\n" \
85 "" "\n"
86
Denis Vlasenko67b5eeb2009-04-12 13:54:13 +000087# long field seps requiring regex
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +010088testing "awk long field sep" \
89 "awk -F-- '{ print NF, length(\$NF), \$NF }'" \
Denis Vlasenko67b5eeb2009-04-12 13:54:13 +000090 "2 0 \n3 0 \n4 0 \n5 0 \n" \
91 "" \
92 "a--\na--b--\na--b--c--\na--b--c--d--"
93
Denys Vlasenkoea664dd2012-06-22 18:41:01 +020094testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \
95 "a\n" \
96 "" \
97 "a!b\n"
98
Denis Vlasenko7a676642009-03-15 22:20:31 +000099# '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
100# but gawk 3.1.5 does not bail out on it.
101testing "awk gsub falls back to non-extended-regex" \
102 "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
103
Denys Vlasenko8e3aff02010-05-10 11:00:11 +0200104optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +0200105test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +0000106testing "awk 'gcc build bug'" \
107 "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \
108 "f842e256461a5ab1ec60b58d16f1114f -\n" \
109 "" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +0200110rm -rf awk_t1_* 2>/dev/null
111SKIP=
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +0000112
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000113Q='":"'
114
115testing "awk NF in BEGIN" \
116 "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
117 ":0::::\n" \
118 "" ""
119
Denys Vlasenko12847742009-11-30 01:15:04 +0100120prg='
121function b(tmp) {
122 tmp = 0;
123 print "" tmp; #this line causes the bug
124 return tmp;
125}
126function c(tmpc) {
127 tmpc = b(); return tmpc;
128}
129BEGIN {
130 print (c() ? "string" : "number");
131}'
132testing "awk string cast (bug 725)" \
133 "awk '$prg'" \
134 "0\nnumber\n" \
135 "" ""
136
Alexander Shishkind03cd3b2010-02-25 17:55:40 +0200137testing "awk handles whitespace before array subscript" \
138 "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
139
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200140# GNU awk 3.1.5's "print ERRNO" prints "No such file or directory" instead of "2",
141# do we need to emulate that as well?
142testing "awk handles non-existing file correctly" \
143 "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \
144 "2\n0\nOk\n" "" ""
145
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100146prg='
147BEGIN {
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100148 u["a"]=1
149 u["b"]=1
150 u["c"]=1
151 v["d"]=1
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100152 v["e"]=1
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100153 v["f"]=1
154 for (l in u) {
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100155 print "outer1", l;
156 for (l in v) {
157 print " inner", l;
158 }
159 print "outer2", l;
160 }
161 print "end", l;
162 l="a"
163 exit;
164}'
165testing "awk nested loops with the same variable" \
166 "awk '$prg'" \
167 "\
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100168outer1 a
169 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100170 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100171 inner f
172outer2 f
173outer1 b
174 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100175 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100176 inner f
177outer2 f
178outer1 c
179 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100180 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100181 inner f
182outer2 f
183end f
184" \
185 "" ""
186
187prg='
188BEGIN {
189 u["a"]=1
190 u["b"]=1
191 u["c"]=1
192 v["d"]=1
193 v["e"]=1
194 v["f"]=1
195 for (l in u) {
196 print "outer1", l;
197 for (l in v) {
198 print " inner", l;
199 break;
200 }
201 print "outer2", l;
202 }
203 print "end", l;
204 l="a"
205 exit;
206}'
207# It's not just buggy, it enters infinite loop. Thus disabled
208false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
209 "awk '$prg'" \
210 "\
211outer1 a
212 inner d
213outer2 d
214outer1 b
215 inner d
216outer2 d
217outer1 c
218 inner d
219outer2 d
220end d
221" \
222 "" ""
223
224prg='
225function f() {
226 for (l in v) {
227 print " inner", l;
228 return;
229 }
230}
231
232BEGIN {
233 u["a"]=1
234 u["b"]=1
235 u["c"]=1
236 v["d"]=1
237 v["e"]=1
238 v["f"]=1
239 for (l in u) {
240 print "outer1", l;
241 f();
242 print "outer2", l;
243 }
244 print "end", l;
245 l="a"
246 exit;
247}'
248# It's not just buggy, it enters infinite loop. Thus disabled
249false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
250 "awk '$prg'" \
251 "\
252outer1 a
253 inner d
254outer2 d
255outer1 b
256 inner d
257outer2 d
258outer1 c
259 inner d
260outer2 d
261end d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100262" \
263 "" ""
264
Denys Vlasenko6f4a7852018-01-07 01:19:08 +0100265prg='
266BEGIN{
267cnt = 0
268a[cnt] = "zeroth"
269a[++cnt] = "first"
270delete a[cnt--]
271print cnt
272print "[0]:" a[0]
273print "[1]:" a[1]
274}'
275testing "awk 'delete a[v--]' evaluates v-- once" \
276 "awk '$prg'" \
277 "\
2780
279[0]:zeroth
280[1]:
281" \
282 "" ""
283
Brian Foley1c42c182019-01-06 18:32:59 -0800284testing "awk func arg parsing 1" \
285 "awk 'func f(,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
286
287testing "awk func arg parsing 2" \
288 "awk 'func f(a,,b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
289
290testing "awk func arg parsing 3" \
291 "awk 'func f(a,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
292
293testing "awk func arg parsing 4" \
294 "awk 'func f(a b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
295
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200296testing "awk handles empty ()" \
297 "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
298
Denys Vlasenkodf8066a2012-07-11 01:27:15 +0200299testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \
300 "a:b\ne\n" \
301 "" \
302 "a:b c:d\ne:f g:h"
303
Denys Vlasenko198b02f2013-12-31 23:22:36 +0100304optional FEATURE_AWK_LIBM
Denys Vlasenkob8554602013-07-22 11:49:06 +0200305testing "awk large integer" \
306 "awk 'BEGIN{n=(2^31)-1; print n, int(n), n%1, ++n, int(n), n%1}'" \
307 "2147483647 2147483647 0 2147483648 2147483648 0\n" \
308 "" ""
Denys Vlasenko198b02f2013-12-31 23:22:36 +0100309SKIP=
Denys Vlasenkob8554602013-07-22 11:49:06 +0200310
Denys Vlasenko7985bc12013-10-12 04:51:54 +0200311testing "awk length(array)" \
Denys Vlasenkobd0e2212013-11-21 15:09:55 +0100312 "awk 'BEGIN{ A[1]=2; A[\"qwe\"]=\"asd\"; print length(A)}'" \
Denys Vlasenko7985bc12013-10-12 04:51:54 +0200313 "2\n" \
314 "" ""
315
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200316testing "awk length()" \
317 "awk '{print length; print length(); print length(\"qwe\"); print length(99+9)}'" \
318 "3\n3\n3\n3\n" \
319 "" "qwe"
320
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +0100321testing "awk print length, 1" \
322 "awk '{ print length, 1 }'" \
323 "0 1\n" \
324 "" "\n"
325
326testing "awk print length 1" \
327 "awk '{ print length 1 }'" \
328 "01\n" \
329 "" "\n"
330
331testing "awk length == 0" \
332 "awk 'length == 0 { print \"foo\" }'" \
333 "foo\n" \
334 "" "\n"
335
336testing "awk if (length == 0)" \
337 "awk '{ if (length == 0) { print \"bar\" } }'" \
338 "bar\n" \
339 "" "\n"
340
Denys Vlasenkobd0e2212013-11-21 15:09:55 +0100341testing "awk -f and ARGC" \
342 "awk -f - input" \
343 "re\n2\n" \
344 "do re mi\n" \
345 '{print $2; print ARGC;}' \
346
Denys Vlasenko198b02f2013-12-31 23:22:36 +0100347optional FEATURE_AWK_GNU_EXTENSIONS
Denys Vlasenkobd0e2212013-11-21 15:09:55 +0100348testing "awk -e and ARGC" \
349 "awk -e '{print \$2; print ARGC;}' input" \
350 "re\n2\n" \
351 "do re mi\n" \
Denys Vlasenko198b02f2013-12-31 23:22:36 +0100352 ""
353SKIP=
Denys Vlasenkobd0e2212013-11-21 15:09:55 +0100354
Denys Vlasenko5f8daef2014-06-26 16:40:28 +0200355# The examples are in fact not valid awk programs (break/continue
356# can only be used inside loops).
357# But we do accept them outside of loops.
358# We had a bug with misparsing "break ; else" sequence.
359# Test that *that* bug is fixed, using simplest possible scripts:
360testing "awk break" \
361 "awk -f - 2>&1; echo \$?" \
362 "0\n" \
363 "" \
364 'BEGIN { if (1) break; else a = 1 }'
365testing "awk continue" \
366 "awk -f - 2>&1; echo \$?" \
367 "0\n" \
368 "" \
369 'BEGIN { if (1) continue; else a = 1 }'
370
Denys Vlasenko22cc9282019-06-08 13:00:46 +0200371optional FEATURE_AWK_GNU_EXTENSIONS
Brian Foley61d59972016-10-15 14:45:40 +0100372testing "awk handles invalid for loop" \
Brian Foley08a514c2019-01-01 13:40:59 -0800373 "awk -e '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
Denys Vlasenko22cc9282019-06-08 13:00:46 +0200374SKIP=
Brian Foley61d59972016-10-15 14:45:40 +0100375
Denys Vlasenko22cc9282019-06-08 13:00:46 +0200376optional FEATURE_AWK_GNU_EXTENSIONS
Brian Foleydac15a12019-01-01 13:40:58 -0800377testing "awk handles colon not preceded by ternary" \
Brian Foley08a514c2019-01-01 13:40:59 -0800378 "awk -e foo:bar: 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
Denys Vlasenko22cc9282019-06-08 13:00:46 +0200379SKIP=
Brian Foley08a514c2019-01-01 13:40:59 -0800380
Denys Vlasenko22cc9282019-06-08 13:00:46 +0200381optional FEATURE_AWK_GNU_EXTENSIONS
Brian Foley08a514c2019-01-01 13:40:59 -0800382testing "awk errors on missing delete arg" \
383 "awk -e '{delete}' 2>&1" "awk: cmd. line:1: Too few arguments\n" "" ""
Denys Vlasenko22cc9282019-06-08 13:00:46 +0200384SKIP=
Brian Foleydac15a12019-01-01 13:40:58 -0800385
Ron Yorstone8fe9f92021-01-27 11:19:14 +0000386optional FEATURE_AWK_GNU_EXTENSIONS
387testing "awk printf('%c') can output NUL" \
388 "awk '{printf(\"hello%c null\n\", 0)}'" "hello\0 null\n" "" "\n"
389SKIP=
390
Denys Vlasenkodf8066a2012-07-11 01:27:15 +0200391# testing "description" "command" "result" "infile" "stdin"
Denys Vlasenko2454e672018-04-23 10:53:18 +0200392testing 'awk negative field access' \
393 'awk 2>&1 -- '\''{ $(-1) }'\' \
394 "awk: cmd. line:1: Access to negative field\n" \
395 '' \
396 'anything'
397
Denys Vlasenko6f7a0092020-06-09 01:33:54 +0200398# was misinterpreted as (("str"++) i) instead of ("str" (++i))
399# (and was executed: "str"++ is "0", thus concatenating "0" and "1"):
400testing 'awk do not allow "str"++' \
401 'awk -v i=1 "BEGIN {print \"str\" ++i}"' \
402 "str2\n" \
403 '' \
404 'anything'
405
Denys Vlasenko758c2bd2020-12-04 19:00:06 +0100406# gawk compat: FS regex matches only non-empty separators:
407# with -*, the splitting is NOT f o o b a r, but foo bar:
Denys Vlasenko665a6592020-12-02 19:07:31 +0100408testing 'awk FS regex which can match empty string' \
409 "awk -F '-*' '{print \$1 \"-\" \$2 \"=\" \$3 \"*\" \$4}'" \
410 "foo-bar=*\n" \
411 '' \
412 'foo--bar'
Denys Vlasenkodf8066a2012-07-11 01:27:15 +0200413
Denys Vlasenko758c2bd2020-12-04 19:00:06 +0100414# last+1 field should be empty (had a bug where it wasn't)
415testing 'awk $NF is empty' \
416 "awk -F '=+' '{print \$NF}'" \
417 "\n" \
418 '' \
419 'a=====123='
420
Denis Vlasenko960eca62007-07-18 18:33:18 +0000421exit $FAILCOUNT