Denis Vlasenko | 960eca6 | 2007-07-18 18:33:18 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Denis Vlasenko | 960eca6 | 2007-07-18 18:33:18 +0000 | [diff] [blame] | 3 | # Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com> |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | # Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 960eca6 | 2007-07-18 18:33:18 +0000 | [diff] [blame] | 5 | |
Mike Frysinger | caa7940 | 2009-11-04 18:41:22 -0500 | [diff] [blame] | 6 | . ./testing.sh |
Denis Vlasenko | 960eca6 | 2007-07-18 18:33:18 +0000 | [diff] [blame] | 7 | |
Denis Vlasenko | bb13079 | 2008-07-15 06:45:14 +0000 | [diff] [blame] | 8 | # testing "description" "command" "result" "infile" "stdin" |
Denis Vlasenko | 960eca6 | 2007-07-18 18:33:18 +0000 | [diff] [blame] | 9 | |
| 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" |
| 18 | |
Tanguy Pruvot | 0a393cf | 2012-06-11 10:33:45 +0200 | [diff] [blame] | 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\"}'" "" "" "" |
| 25 | |
Denis Vlasenko | 66496d7 | 2008-08-29 08:37:07 +0000 | [diff] [blame] | 26 | # 4294967295 = 0xffffffff |
Denys Vlasenko | b855460 | 2013-07-22 11:49:06 +0200 | [diff] [blame] | 27 | testing "awk bitwise op" "awk '{ print or(4294967295,1) }'" "4294967295\n" "" "\n" |
Bernhard Reutner-Fischer | b79a0fe | 2013-03-06 21:01:05 +0100 | [diff] [blame] | 28 | |
| 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 |
| 32 | prg=' |
| 33 | function empty_fun(count) { |
| 34 | # empty |
| 35 | } |
| 36 | END { |
| 37 | i=1 |
| 38 | print "L" i "\n" |
| 39 | empty_fun(i + i + ++i) |
| 40 | print "L" i "\n" |
| 41 | }' |
| 42 | testing "awk handles empty function f(arg){}" \ |
| 43 | "awk '$prg'" \ |
| 44 | "L1\n\nL2\n\n" \ |
| 45 | "" "" |
| 46 | |
Bernhard Reutner-Fischer | a060a1a | 2013-07-31 15:29:20 +0200 | [diff] [blame] | 47 | prg=' |
| 48 | function outer_fun() { |
| 49 | return 1 |
| 50 | } |
| 51 | END { |
| 52 | i=1 |
| 53 | print "L" i "\n" |
| 54 | i += outer_fun() |
| 55 | print "L" i "\n" |
| 56 | }' |
| 57 | testing "awk properly handles function from other scope" \ |
| 58 | "awk '$prg'" \ |
| 59 | "L1\n\nL2\n\n" \ |
| 60 | "" "" |
| 61 | |
| 62 | prg=' |
| 63 | END { |
| 64 | i=1 |
| 65 | print "L" i "\n" |
| 66 | i + trigger_error_fun() |
| 67 | print "L" i "\n" |
| 68 | }' |
| 69 | testing "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 Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 75 | optional DESKTOP |
Denys Vlasenko | b855460 | 2013-07-22 11:49:06 +0200 | [diff] [blame] | 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" |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 78 | testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n" |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 79 | SKIP= |
Denis Vlasenko | 66496d7 | 2008-08-29 08:37:07 +0000 | [diff] [blame] | 80 | |
Denys Vlasenko | d527e0c | 2010-10-05 13:22:11 +0200 | [diff] [blame] | 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" \ |
| 85 | "" "\n" |
| 86 | |
Denis Vlasenko | 67b5eeb | 2009-04-12 13:54:13 +0000 | [diff] [blame] | 87 | # long field seps requiring regex |
| 88 | testing "awk long field sep" "awk -F-- '{ print NF, length(\$NF), \$NF }'" \ |
| 89 | "2 0 \n3 0 \n4 0 \n5 0 \n" \ |
| 90 | "" \ |
| 91 | "a--\na--b--\na--b--c--\na--b--c--d--" |
| 92 | |
Denys Vlasenko | ea664dd | 2012-06-22 18:41:01 +0200 | [diff] [blame] | 93 | testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \ |
| 94 | "a\n" \ |
| 95 | "" \ |
| 96 | "a!b\n" |
| 97 | |
Denis Vlasenko | 7a67664 | 2009-03-15 22:20:31 +0000 | [diff] [blame] | 98 | # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'), |
| 99 | # but gawk 3.1.5 does not bail out on it. |
| 100 | testing "awk gsub falls back to non-extended-regex" \ |
| 101 | "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n" |
| 102 | |
Denys Vlasenko | 8e3aff0 | 2010-05-10 11:00:11 +0200 | [diff] [blame] | 103 | optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2 |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 104 | test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2 |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 105 | testing "awk 'gcc build bug'" \ |
| 106 | "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \ |
| 107 | "f842e256461a5ab1ec60b58d16f1114f -\n" \ |
| 108 | "" "" |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 109 | rm -rf awk_t1_* 2>/dev/null |
| 110 | SKIP= |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | 41d5ebe | 2009-01-25 01:00:15 +0000 | [diff] [blame] | 112 | Q='":"' |
| 113 | |
| 114 | testing "awk NF in BEGIN" \ |
| 115 | "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \ |
| 116 | ":0::::\n" \ |
| 117 | "" "" |
| 118 | |
Denys Vlasenko | 1284774 | 2009-11-30 01:15:04 +0100 | [diff] [blame] | 119 | prg=' |
| 120 | function b(tmp) { |
| 121 | tmp = 0; |
| 122 | print "" tmp; #this line causes the bug |
| 123 | return tmp; |
| 124 | } |
| 125 | function c(tmpc) { |
| 126 | tmpc = b(); return tmpc; |
| 127 | } |
| 128 | BEGIN { |
| 129 | print (c() ? "string" : "number"); |
| 130 | }' |
| 131 | testing "awk string cast (bug 725)" \ |
| 132 | "awk '$prg'" \ |
| 133 | "0\nnumber\n" \ |
| 134 | "" "" |
| 135 | |
Alexander Shishkin | d03cd3b | 2010-02-25 17:55:40 +0200 | [diff] [blame] | 136 | testing "awk handles whitespace before array subscript" \ |
| 137 | "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" "" |
| 138 | |
Denys Vlasenko | 6a0d749 | 2010-10-23 21:02:15 +0200 | [diff] [blame] | 139 | # GNU awk 3.1.5's "print ERRNO" prints "No such file or directory" instead of "2", |
| 140 | # do we need to emulate that as well? |
| 141 | testing "awk handles non-existing file correctly" \ |
| 142 | "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \ |
| 143 | "2\n0\nOk\n" "" "" |
| 144 | |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 145 | prg=' |
| 146 | BEGIN { |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 147 | u["a"]=1 |
| 148 | u["b"]=1 |
| 149 | u["c"]=1 |
| 150 | v["d"]=1 |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 151 | v["e"]=1 |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 152 | v["f"]=1 |
| 153 | for (l in u) { |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 154 | print "outer1", l; |
| 155 | for (l in v) { |
| 156 | print " inner", l; |
| 157 | } |
| 158 | print "outer2", l; |
| 159 | } |
| 160 | print "end", l; |
| 161 | l="a" |
| 162 | exit; |
| 163 | }' |
| 164 | testing "awk nested loops with the same variable" \ |
| 165 | "awk '$prg'" \ |
| 166 | "\ |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 167 | outer1 a |
| 168 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 169 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 170 | inner f |
| 171 | outer2 f |
| 172 | outer1 b |
| 173 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 174 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 175 | inner f |
| 176 | outer2 f |
| 177 | outer1 c |
| 178 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 179 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 180 | inner f |
| 181 | outer2 f |
| 182 | end f |
| 183 | " \ |
| 184 | "" "" |
| 185 | |
| 186 | prg=' |
| 187 | BEGIN { |
| 188 | u["a"]=1 |
| 189 | u["b"]=1 |
| 190 | u["c"]=1 |
| 191 | v["d"]=1 |
| 192 | v["e"]=1 |
| 193 | v["f"]=1 |
| 194 | for (l in u) { |
| 195 | print "outer1", l; |
| 196 | for (l in v) { |
| 197 | print " inner", l; |
| 198 | break; |
| 199 | } |
| 200 | print "outer2", l; |
| 201 | } |
| 202 | print "end", l; |
| 203 | l="a" |
| 204 | exit; |
| 205 | }' |
| 206 | # It's not just buggy, it enters infinite loop. Thus disabled |
| 207 | false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \ |
| 208 | "awk '$prg'" \ |
| 209 | "\ |
| 210 | outer1 a |
| 211 | inner d |
| 212 | outer2 d |
| 213 | outer1 b |
| 214 | inner d |
| 215 | outer2 d |
| 216 | outer1 c |
| 217 | inner d |
| 218 | outer2 d |
| 219 | end d |
| 220 | " \ |
| 221 | "" "" |
| 222 | |
| 223 | prg=' |
| 224 | function f() { |
| 225 | for (l in v) { |
| 226 | print " inner", l; |
| 227 | return; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | BEGIN { |
| 232 | u["a"]=1 |
| 233 | u["b"]=1 |
| 234 | u["c"]=1 |
| 235 | v["d"]=1 |
| 236 | v["e"]=1 |
| 237 | v["f"]=1 |
| 238 | for (l in u) { |
| 239 | print "outer1", l; |
| 240 | f(); |
| 241 | print "outer2", l; |
| 242 | } |
| 243 | print "end", l; |
| 244 | l="a" |
| 245 | exit; |
| 246 | }' |
| 247 | # It's not just buggy, it enters infinite loop. Thus disabled |
| 248 | false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \ |
| 249 | "awk '$prg'" \ |
| 250 | "\ |
| 251 | outer1 a |
| 252 | inner d |
| 253 | outer2 d |
| 254 | outer1 b |
| 255 | inner d |
| 256 | outer2 d |
| 257 | outer1 c |
| 258 | inner d |
| 259 | outer2 d |
| 260 | end d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 261 | " \ |
| 262 | "" "" |
| 263 | |
Denys Vlasenko | 6f4a785 | 2018-01-07 01:19:08 +0100 | [diff] [blame] | 264 | prg=' |
| 265 | BEGIN{ |
| 266 | cnt = 0 |
| 267 | a[cnt] = "zeroth" |
| 268 | a[++cnt] = "first" |
| 269 | delete a[cnt--] |
| 270 | print cnt |
| 271 | print "[0]:" a[0] |
| 272 | print "[1]:" a[1] |
| 273 | }' |
| 274 | testing "awk 'delete a[v--]' evaluates v-- once" \ |
| 275 | "awk '$prg'" \ |
| 276 | "\ |
| 277 | 0 |
| 278 | [0]:zeroth |
| 279 | [1]: |
| 280 | " \ |
| 281 | "" "" |
| 282 | |
Brian Foley | 1c42c18 | 2019-01-06 18:32:59 -0800 | [diff] [blame] | 283 | testing "awk func arg parsing 1" \ |
| 284 | "awk 'func f(,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 285 | |
| 286 | testing "awk func arg parsing 2" \ |
| 287 | "awk 'func f(a,,b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 288 | |
| 289 | testing "awk func arg parsing 3" \ |
| 290 | "awk 'func f(a,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 291 | |
| 292 | testing "awk func arg parsing 4" \ |
| 293 | "awk 'func f(a b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 294 | |
Denys Vlasenko | 7b46d11 | 2011-09-11 00:30:56 +0200 | [diff] [blame] | 295 | testing "awk handles empty ()" \ |
| 296 | "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" "" |
| 297 | |
Denys Vlasenko | df8066a | 2012-07-11 01:27:15 +0200 | [diff] [blame] | 298 | testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \ |
| 299 | "a:b\ne\n" \ |
| 300 | "" \ |
| 301 | "a:b c:d\ne:f g:h" |
| 302 | |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 303 | optional FEATURE_AWK_LIBM |
Denys Vlasenko | b855460 | 2013-07-22 11:49:06 +0200 | [diff] [blame] | 304 | testing "awk large integer" \ |
| 305 | "awk 'BEGIN{n=(2^31)-1; print n, int(n), n%1, ++n, int(n), n%1}'" \ |
| 306 | "2147483647 2147483647 0 2147483648 2147483648 0\n" \ |
| 307 | "" "" |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 308 | SKIP= |
Denys Vlasenko | b855460 | 2013-07-22 11:49:06 +0200 | [diff] [blame] | 309 | |
Denys Vlasenko | 7985bc1 | 2013-10-12 04:51:54 +0200 | [diff] [blame] | 310 | testing "awk length(array)" \ |
Denys Vlasenko | bd0e221 | 2013-11-21 15:09:55 +0100 | [diff] [blame] | 311 | "awk 'BEGIN{ A[1]=2; A[\"qwe\"]=\"asd\"; print length(A)}'" \ |
Denys Vlasenko | 7985bc1 | 2013-10-12 04:51:54 +0200 | [diff] [blame] | 312 | "2\n" \ |
| 313 | "" "" |
| 314 | |
Denys Vlasenko | 28b00ce | 2015-10-02 02:41:39 +0200 | [diff] [blame] | 315 | testing "awk length()" \ |
| 316 | "awk '{print length; print length(); print length(\"qwe\"); print length(99+9)}'" \ |
| 317 | "3\n3\n3\n3\n" \ |
| 318 | "" "qwe" |
| 319 | |
Denys Vlasenko | bd0e221 | 2013-11-21 15:09:55 +0100 | [diff] [blame] | 320 | testing "awk -f and ARGC" \ |
| 321 | "awk -f - input" \ |
| 322 | "re\n2\n" \ |
| 323 | "do re mi\n" \ |
| 324 | '{print $2; print ARGC;}' \ |
| 325 | |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 326 | optional FEATURE_AWK_GNU_EXTENSIONS |
Denys Vlasenko | bd0e221 | 2013-11-21 15:09:55 +0100 | [diff] [blame] | 327 | testing "awk -e and ARGC" \ |
| 328 | "awk -e '{print \$2; print ARGC;}' input" \ |
| 329 | "re\n2\n" \ |
| 330 | "do re mi\n" \ |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 331 | "" |
| 332 | SKIP= |
Denys Vlasenko | bd0e221 | 2013-11-21 15:09:55 +0100 | [diff] [blame] | 333 | |
Denys Vlasenko | 5f8daef | 2014-06-26 16:40:28 +0200 | [diff] [blame] | 334 | # The examples are in fact not valid awk programs (break/continue |
| 335 | # can only be used inside loops). |
| 336 | # But we do accept them outside of loops. |
| 337 | # We had a bug with misparsing "break ; else" sequence. |
| 338 | # Test that *that* bug is fixed, using simplest possible scripts: |
| 339 | testing "awk break" \ |
| 340 | "awk -f - 2>&1; echo \$?" \ |
| 341 | "0\n" \ |
| 342 | "" \ |
| 343 | 'BEGIN { if (1) break; else a = 1 }' |
| 344 | testing "awk continue" \ |
| 345 | "awk -f - 2>&1; echo \$?" \ |
| 346 | "0\n" \ |
| 347 | "" \ |
| 348 | 'BEGIN { if (1) continue; else a = 1 }' |
| 349 | |
Brian Foley | 61d5997 | 2016-10-15 14:45:40 +0100 | [diff] [blame] | 350 | testing "awk handles invalid for loop" \ |
Brian Foley | 08a514c | 2019-01-01 13:40:59 -0800 | [diff] [blame] | 351 | "awk -e '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
Brian Foley | 61d5997 | 2016-10-15 14:45:40 +0100 | [diff] [blame] | 352 | |
Brian Foley | dac15a1 | 2019-01-01 13:40:58 -0800 | [diff] [blame] | 353 | testing "awk handles colon not preceded by ternary" \ |
Brian Foley | 08a514c | 2019-01-01 13:40:59 -0800 | [diff] [blame] | 354 | "awk -e foo:bar: 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 355 | |
| 356 | testing "awk errors on missing delete arg" \ |
| 357 | "awk -e '{delete}' 2>&1" "awk: cmd. line:1: Too few arguments\n" "" "" |
Brian Foley | dac15a1 | 2019-01-01 13:40:58 -0800 | [diff] [blame] | 358 | |
Denys Vlasenko | df8066a | 2012-07-11 01:27:15 +0200 | [diff] [blame] | 359 | # testing "description" "command" "result" "infile" "stdin" |
Denys Vlasenko | 2454e67 | 2018-04-23 10:53:18 +0200 | [diff] [blame] | 360 | testing 'awk negative field access' \ |
| 361 | 'awk 2>&1 -- '\''{ $(-1) }'\' \ |
| 362 | "awk: cmd. line:1: Access to negative field\n" \ |
| 363 | '' \ |
| 364 | 'anything' |
| 365 | |
Denys Vlasenko | df8066a | 2012-07-11 01:27:15 +0200 | [diff] [blame] | 366 | |
Denis Vlasenko | 960eca6 | 2007-07-18 18:33:18 +0000 | [diff] [blame] | 367 | exit $FAILCOUNT |