3 # Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
4 # Licensed under GPLv2, see file LICENSE in this source tree.
8 # testing "description" "command" "result" "infile" "stdin"
10 testing "awk -F case 0" "awk -F '[#]' '{ print NF }'" "" "" ""
11 testing "awk -F case 1" "awk -F '[#]' '{ print NF }'" "0\n" "" "\n"
12 testing "awk -F case 2" "awk -F '[#]' '{ print NF }'" "2\n" "" "#\n"
13 testing "awk -F case 3" "awk -F '[#]' '{ print NF }'" "3\n" "" "#abc#\n"
14 testing "awk -F case 4" "awk -F '[#]' '{ print NF }'" "3\n" "" "#abc#zz\n"
15 testing "awk -F case 5" "awk -F '[#]' '{ print NF }'" "4\n" "" "#abc##zz\n"
16 testing "awk -F case 6" "awk -F '[#]' '{ print NF }'" "4\n" "" "z#abc##zz\n"
17 testing "awk -F case 7" "awk -F '[#]' '{ print NF }'" "5\n" "" "z##abc##zz\n"
19 # conditions and operators
20 testing "awk if operator == " "awk 'BEGIN{if(23==23) print \"foo\"}'" "foo\n" "" ""
21 testing "awk if operator != " "awk 'BEGIN{if(23!=23) print \"bar\"}'" "" "" ""
22 testing "awk if operator >= " "awk 'BEGIN{if(23>=23) print \"foo\"}'" "foo\n" "" ""
23 testing "awk if operator < " "awk 'BEGIN{if(2 < 13) print \"foo\"}'" "foo\n" "" ""
24 testing "awk if string == " "awk 'BEGIN{if(\"a\"==\"ab\") print \"bar\"}'" "" "" ""
26 # 4294967295 = 0xffffffff
27 testing "awk bitwise op" "awk '{ print or(4294967295,1) }'" "4294967295\n" "" "\n"
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
33 function empty_fun(count) {
39 empty_fun(i + i + ++i)
42 testing "awk handles empty function f(arg){}" \
48 function outer_fun() {
57 testing "awk properly handles function from other scope" \
66 i + trigger_error_fun()
69 testing "awk properly handles undefined function" \
71 "L1\n\nawk: cmd. line:5: Call to undefined function\n" \
76 testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4294967295\n" "" "\n"
77 testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2147483649\n" "" "\n"
78 testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n"
81 # check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN
82 testing "awk floating const with leading zeroes" \
83 "awk '{ printf \"%f %f\n\", \"000.123\", \"009.123\" }'" \
84 "0.123000 9.123000\n" \
87 # long field seps requiring regex
88 testing "awk long field sep" \
89 "awk -F-- '{ print NF, length(\$NF), \$NF }'" \
90 "2 0 \n3 0 \n4 0 \n5 0 \n" \
92 "a--\na--b--\na--b--c--\na--b--c--d--"
94 testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \
99 # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
100 # but gawk 3.1.5 does not bail out on it.
101 testing "awk gsub falls back to non-extended-regex" \
102 "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
104 optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
105 test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
106 testing "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" \
110 rm -rf awk_t1_* 2>/dev/null
115 testing "awk NF in BEGIN" \
116 "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
123 print "" tmp; #this line causes the bug
127 tmpc = b(); return tmpc;
130 print (c() ? "string" : "number");
132 testing "awk string cast (bug 725)" \
137 testing "awk handles whitespace before array subscript" \
138 "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
140 # 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?
142 testing "awk handles non-existing file correctly" \
143 "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \
165 testing "awk nested loops with the same variable" \
207 # It's not just buggy, it enters infinite loop. Thus disabled
208 false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
248 # It's not just buggy, it enters infinite loop. Thus disabled
249 false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
275 testing "awk 'delete a[v--]' evaluates v-- once" \
284 testing "awk func arg parsing 1" \
285 "awk 'func f(,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
287 testing "awk func arg parsing 2" \
288 "awk 'func f(a,,b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
290 testing "awk func arg parsing 3" \
291 "awk 'func f(a,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
293 testing "awk func arg parsing 4" \
294 "awk 'func f(a b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
296 testing "awk handles empty ()" \
297 "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
299 testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \
304 optional FEATURE_AWK_LIBM
305 testing "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" \
311 testing "awk length(array)" \
312 "awk 'BEGIN{ A[1]=2; A[\"qwe\"]=\"asd\"; print length(A)}'" \
316 testing "awk length()" \
317 "awk '{print length; print length(); print length(\"qwe\"); print length(99+9)}'" \
321 testing "awk print length, 1" \
322 "awk '{ print length, 1 }'" \
326 testing "awk print length 1" \
327 "awk '{ print length 1 }'" \
331 testing "awk length == 0" \
332 "awk 'length == 0 { print \"foo\" }'" \
336 testing "awk if (length == 0)" \
337 "awk '{ if (length == 0) { print \"bar\" } }'" \
341 testing "awk -f and ARGC" \
345 '{print $2; print ARGC;}' \
347 optional FEATURE_AWK_GNU_EXTENSIONS
348 testing "awk -e and ARGC" \
349 "awk -e '{print \$2; print ARGC;}' input" \
355 # 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:
360 testing "awk break" \
361 "awk -f - 2>&1; echo \$?" \
364 'BEGIN { if (1) break; else a = 1 }'
365 testing "awk continue" \
366 "awk -f - 2>&1; echo \$?" \
369 'BEGIN { if (1) continue; else a = 1 }'
371 optional FEATURE_AWK_GNU_EXTENSIONS
372 testing "awk handles invalid for loop" \
373 "awk -e '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
376 optional FEATURE_AWK_GNU_EXTENSIONS
377 testing "awk handles colon not preceded by ternary" \
378 "awk -e foo:bar: 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
381 optional FEATURE_AWK_GNU_EXTENSIONS
382 testing "awk errors on missing delete arg" \
383 "awk -e '{delete}' 2>&1" "awk: cmd. line:1: Too few arguments\n" "" ""
386 # testing "description" "command" "result" "infile" "stdin"
387 testing 'awk negative field access' \
388 'awk 2>&1 -- '\''{ $(-1) }'\' \
389 "awk: cmd. line:1: Access to negative field\n" \
393 # was misinterpreted as (("str"++) i) instead of ("str" (++i))
394 # (and was executed: "str"++ is "0", thus concatenating "0" and "1"):
395 testing 'awk do not allow "str"++' \
396 'awk -v i=1 "BEGIN {print \"str\" ++i}"' \