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=' |
Denys Vlasenko | d150710 | 2021-06-30 12:23:51 +0200 | [diff] [blame] | 48 | function empty_fun(){} |
| 49 | END {empty_fun() |
| 50 | print "Ok" |
| 51 | }' |
| 52 | testing "awk handles empty function f(){}" \ |
| 53 | "awk '$prg'" \ |
| 54 | "Ok\n" \ |
| 55 | "" "" |
| 56 | |
| 57 | prg=' |
Bernhard Reutner-Fischer | a060a1a | 2013-07-31 15:29:20 +0200 | [diff] [blame] | 58 | function outer_fun() { |
| 59 | return 1 |
| 60 | } |
| 61 | END { |
| 62 | i=1 |
| 63 | print "L" i "\n" |
| 64 | i += outer_fun() |
| 65 | print "L" i "\n" |
| 66 | }' |
| 67 | testing "awk properly handles function from other scope" \ |
| 68 | "awk '$prg'" \ |
| 69 | "L1\n\nL2\n\n" \ |
| 70 | "" "" |
| 71 | |
| 72 | prg=' |
| 73 | END { |
| 74 | i=1 |
| 75 | print "L" i "\n" |
| 76 | i + trigger_error_fun() |
| 77 | print "L" i "\n" |
| 78 | }' |
| 79 | testing "awk properly handles undefined function" \ |
| 80 | "awk '$prg' 2>&1" \ |
| 81 | "L1\n\nawk: cmd. line:5: Call to undefined function\n" \ |
| 82 | "" "" |
| 83 | |
Denys Vlasenko | 6872c19 | 2021-06-29 12:16:36 +0200 | [diff] [blame] | 84 | prg=' |
| 85 | BEGIN { |
| 86 | v=1 |
| 87 | a=2 |
| 88 | print v (a) |
| 89 | }' |
Denys Vlasenko | d7354df | 2021-06-30 12:52:51 +0200 | [diff] [blame] | 90 | testing "awk 'v (a)' is not a function call, it is a concatenation" \ |
Denys Vlasenko | 6872c19 | 2021-06-29 12:16:36 +0200 | [diff] [blame] | 91 | "awk '$prg' 2>&1" \ |
| 92 | "12\n" \ |
| 93 | "" "" |
| 94 | |
Denys Vlasenko | d7354df | 2021-06-30 12:52:51 +0200 | [diff] [blame] | 95 | prg='func f(){print"F"};func g(){print"G"};BEGIN{f(g(),g())}' |
| 96 | testing "awk unused function args are evaluated" \ |
| 97 | "awk '$prg' 2>&1" \ |
| 98 | "G\nG\nF\n" \ |
| 99 | "" "" |
| 100 | |
Bernhard Reutner-Fischer | a060a1a | 2013-07-31 15:29:20 +0200 | [diff] [blame] | 101 | |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 102 | optional DESKTOP |
Denys Vlasenko | b855460 | 2013-07-22 11:49:06 +0200 | [diff] [blame] | 103 | testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4294967295\n" "" "\n" |
| 104 | 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] | 105 | testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n" |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 106 | SKIP= |
Denis Vlasenko | 66496d7 | 2008-08-29 08:37:07 +0000 | [diff] [blame] | 107 | |
Denys Vlasenko | d527e0c | 2010-10-05 13:22:11 +0200 | [diff] [blame] | 108 | # check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN |
| 109 | testing "awk floating const with leading zeroes" \ |
| 110 | "awk '{ printf \"%f %f\n\", \"000.123\", \"009.123\" }'" \ |
| 111 | "0.123000 9.123000\n" \ |
| 112 | "" "\n" |
| 113 | |
Denis Vlasenko | 67b5eeb | 2009-04-12 13:54:13 +0000 | [diff] [blame] | 114 | # long field seps requiring regex |
Denys Vlasenko | bd8b05b | 2020-02-02 23:28:55 +0100 | [diff] [blame] | 115 | testing "awk long field sep" \ |
| 116 | "awk -F-- '{ print NF, length(\$NF), \$NF }'" \ |
Denis Vlasenko | 67b5eeb | 2009-04-12 13:54:13 +0000 | [diff] [blame] | 117 | "2 0 \n3 0 \n4 0 \n5 0 \n" \ |
| 118 | "" \ |
| 119 | "a--\na--b--\na--b--c--\na--b--c--d--" |
| 120 | |
Denys Vlasenko | ea664dd | 2012-06-22 18:41:01 +0200 | [diff] [blame] | 121 | testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \ |
| 122 | "a\n" \ |
| 123 | "" \ |
| 124 | "a!b\n" |
| 125 | |
Denis Vlasenko | 7a67664 | 2009-03-15 22:20:31 +0000 | [diff] [blame] | 126 | # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'), |
| 127 | # but gawk 3.1.5 does not bail out on it. |
| 128 | testing "awk gsub falls back to non-extended-regex" \ |
| 129 | "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n" |
| 130 | |
Denys Vlasenko | 8e3aff0 | 2010-05-10 11:00:11 +0200 | [diff] [blame] | 131 | optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2 |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 132 | test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2 |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 133 | testing "awk 'gcc build bug'" \ |
| 134 | "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \ |
| 135 | "f842e256461a5ab1ec60b58d16f1114f -\n" \ |
| 136 | "" "" |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 137 | rm -rf awk_t1_* 2>/dev/null |
| 138 | SKIP= |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | 41d5ebe | 2009-01-25 01:00:15 +0000 | [diff] [blame] | 140 | Q='":"' |
| 141 | |
| 142 | testing "awk NF in BEGIN" \ |
| 143 | "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \ |
| 144 | ":0::::\n" \ |
| 145 | "" "" |
| 146 | |
Denys Vlasenko | 1284774 | 2009-11-30 01:15:04 +0100 | [diff] [blame] | 147 | prg=' |
| 148 | function b(tmp) { |
| 149 | tmp = 0; |
| 150 | print "" tmp; #this line causes the bug |
| 151 | return tmp; |
| 152 | } |
| 153 | function c(tmpc) { |
| 154 | tmpc = b(); return tmpc; |
| 155 | } |
| 156 | BEGIN { |
| 157 | print (c() ? "string" : "number"); |
| 158 | }' |
| 159 | testing "awk string cast (bug 725)" \ |
| 160 | "awk '$prg'" \ |
| 161 | "0\nnumber\n" \ |
| 162 | "" "" |
| 163 | |
Alexander Shishkin | d03cd3b | 2010-02-25 17:55:40 +0200 | [diff] [blame] | 164 | testing "awk handles whitespace before array subscript" \ |
| 165 | "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" "" |
| 166 | |
Denys Vlasenko | 6a0d749 | 2010-10-23 21:02:15 +0200 | [diff] [blame] | 167 | # GNU awk 3.1.5's "print ERRNO" prints "No such file or directory" instead of "2", |
| 168 | # do we need to emulate that as well? |
| 169 | testing "awk handles non-existing file correctly" \ |
| 170 | "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \ |
| 171 | "2\n0\nOk\n" "" "" |
| 172 | |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 173 | prg=' |
| 174 | BEGIN { |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 175 | u["a"]=1 |
| 176 | u["b"]=1 |
| 177 | u["c"]=1 |
| 178 | v["d"]=1 |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 179 | v["e"]=1 |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 180 | v["f"]=1 |
| 181 | for (l in u) { |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 182 | print "outer1", l; |
| 183 | for (l in v) { |
| 184 | print " inner", l; |
| 185 | } |
| 186 | print "outer2", l; |
| 187 | } |
| 188 | print "end", l; |
| 189 | l="a" |
| 190 | exit; |
| 191 | }' |
| 192 | testing "awk nested loops with the same variable" \ |
| 193 | "awk '$prg'" \ |
| 194 | "\ |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 195 | outer1 a |
| 196 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 197 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 198 | inner f |
| 199 | outer2 f |
| 200 | outer1 b |
| 201 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 202 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 203 | inner f |
| 204 | outer2 f |
| 205 | outer1 c |
| 206 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 207 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 208 | inner f |
| 209 | outer2 f |
| 210 | end f |
| 211 | " \ |
| 212 | "" "" |
| 213 | |
| 214 | prg=' |
| 215 | BEGIN { |
| 216 | u["a"]=1 |
| 217 | u["b"]=1 |
| 218 | u["c"]=1 |
| 219 | v["d"]=1 |
| 220 | v["e"]=1 |
| 221 | v["f"]=1 |
| 222 | for (l in u) { |
| 223 | print "outer1", l; |
| 224 | for (l in v) { |
| 225 | print " inner", l; |
| 226 | break; |
| 227 | } |
| 228 | print "outer2", l; |
| 229 | } |
| 230 | print "end", l; |
| 231 | l="a" |
| 232 | exit; |
| 233 | }' |
| 234 | # It's not just buggy, it enters infinite loop. Thus disabled |
| 235 | false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \ |
| 236 | "awk '$prg'" \ |
| 237 | "\ |
| 238 | outer1 a |
| 239 | inner d |
| 240 | outer2 d |
| 241 | outer1 b |
| 242 | inner d |
| 243 | outer2 d |
| 244 | outer1 c |
| 245 | inner d |
| 246 | outer2 d |
| 247 | end d |
| 248 | " \ |
| 249 | "" "" |
| 250 | |
| 251 | prg=' |
| 252 | function f() { |
| 253 | for (l in v) { |
| 254 | print " inner", l; |
| 255 | return; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | BEGIN { |
| 260 | u["a"]=1 |
| 261 | u["b"]=1 |
| 262 | u["c"]=1 |
| 263 | v["d"]=1 |
| 264 | v["e"]=1 |
| 265 | v["f"]=1 |
| 266 | for (l in u) { |
| 267 | print "outer1", l; |
| 268 | f(); |
| 269 | print "outer2", l; |
| 270 | } |
| 271 | print "end", l; |
| 272 | l="a" |
| 273 | exit; |
| 274 | }' |
| 275 | # It's not just buggy, it enters infinite loop. Thus disabled |
| 276 | false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \ |
| 277 | "awk '$prg'" \ |
| 278 | "\ |
| 279 | outer1 a |
| 280 | inner d |
| 281 | outer2 d |
| 282 | outer1 b |
| 283 | inner d |
| 284 | outer2 d |
| 285 | outer1 c |
| 286 | inner d |
| 287 | outer2 d |
| 288 | end d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 289 | " \ |
| 290 | "" "" |
| 291 | |
Denys Vlasenko | 6f4a785 | 2018-01-07 01:19:08 +0100 | [diff] [blame] | 292 | prg=' |
| 293 | BEGIN{ |
| 294 | cnt = 0 |
| 295 | a[cnt] = "zeroth" |
| 296 | a[++cnt] = "first" |
| 297 | delete a[cnt--] |
| 298 | print cnt |
| 299 | print "[0]:" a[0] |
| 300 | print "[1]:" a[1] |
| 301 | }' |
| 302 | testing "awk 'delete a[v--]' evaluates v-- once" \ |
| 303 | "awk '$prg'" \ |
| 304 | "\ |
| 305 | 0 |
| 306 | [0]:zeroth |
| 307 | [1]: |
| 308 | " \ |
| 309 | "" "" |
| 310 | |
Brian Foley | 1c42c18 | 2019-01-06 18:32:59 -0800 | [diff] [blame] | 311 | testing "awk func arg parsing 1" \ |
| 312 | "awk 'func f(,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 313 | |
| 314 | testing "awk func arg parsing 2" \ |
| 315 | "awk 'func f(a,,b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 316 | |
| 317 | testing "awk func arg parsing 3" \ |
| 318 | "awk 'func f(a,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 319 | |
| 320 | testing "awk func arg parsing 4" \ |
| 321 | "awk 'func f(a b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
| 322 | |
Denys Vlasenko | 7b46d11 | 2011-09-11 00:30:56 +0200 | [diff] [blame] | 323 | testing "awk handles empty ()" \ |
| 324 | "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" "" |
| 325 | |
Denys Vlasenko | df8066a | 2012-07-11 01:27:15 +0200 | [diff] [blame] | 326 | testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \ |
| 327 | "a:b\ne\n" \ |
| 328 | "" \ |
| 329 | "a:b c:d\ne:f g:h" |
| 330 | |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 331 | optional FEATURE_AWK_LIBM |
Denys Vlasenko | b855460 | 2013-07-22 11:49:06 +0200 | [diff] [blame] | 332 | testing "awk large integer" \ |
| 333 | "awk 'BEGIN{n=(2^31)-1; print n, int(n), n%1, ++n, int(n), n%1}'" \ |
| 334 | "2147483647 2147483647 0 2147483648 2147483648 0\n" \ |
| 335 | "" "" |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 336 | SKIP= |
Denys Vlasenko | b855460 | 2013-07-22 11:49:06 +0200 | [diff] [blame] | 337 | |
Denys Vlasenko | 7985bc1 | 2013-10-12 04:51:54 +0200 | [diff] [blame] | 338 | testing "awk length(array)" \ |
Denys Vlasenko | bd0e221 | 2013-11-21 15:09:55 +0100 | [diff] [blame] | 339 | "awk 'BEGIN{ A[1]=2; A[\"qwe\"]=\"asd\"; print length(A)}'" \ |
Denys Vlasenko | 7985bc1 | 2013-10-12 04:51:54 +0200 | [diff] [blame] | 340 | "2\n" \ |
| 341 | "" "" |
| 342 | |
Denys Vlasenko | 28b00ce | 2015-10-02 02:41:39 +0200 | [diff] [blame] | 343 | testing "awk length()" \ |
| 344 | "awk '{print length; print length(); print length(\"qwe\"); print length(99+9)}'" \ |
| 345 | "3\n3\n3\n3\n" \ |
| 346 | "" "qwe" |
| 347 | |
Denys Vlasenko | bd8b05b | 2020-02-02 23:28:55 +0100 | [diff] [blame] | 348 | testing "awk print length, 1" \ |
| 349 | "awk '{ print length, 1 }'" \ |
| 350 | "0 1\n" \ |
| 351 | "" "\n" |
| 352 | |
| 353 | testing "awk print length 1" \ |
| 354 | "awk '{ print length 1 }'" \ |
| 355 | "01\n" \ |
| 356 | "" "\n" |
| 357 | |
| 358 | testing "awk length == 0" \ |
| 359 | "awk 'length == 0 { print \"foo\" }'" \ |
| 360 | "foo\n" \ |
| 361 | "" "\n" |
| 362 | |
| 363 | testing "awk if (length == 0)" \ |
| 364 | "awk '{ if (length == 0) { print \"bar\" } }'" \ |
| 365 | "bar\n" \ |
| 366 | "" "\n" |
| 367 | |
Denys Vlasenko | bd0e221 | 2013-11-21 15:09:55 +0100 | [diff] [blame] | 368 | testing "awk -f and ARGC" \ |
| 369 | "awk -f - input" \ |
| 370 | "re\n2\n" \ |
| 371 | "do re mi\n" \ |
| 372 | '{print $2; print ARGC;}' \ |
| 373 | |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 374 | optional FEATURE_AWK_GNU_EXTENSIONS |
Denys Vlasenko | bd0e221 | 2013-11-21 15:09:55 +0100 | [diff] [blame] | 375 | testing "awk -e and ARGC" \ |
| 376 | "awk -e '{print \$2; print ARGC;}' input" \ |
| 377 | "re\n2\n" \ |
| 378 | "do re mi\n" \ |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 379 | "" |
| 380 | SKIP= |
Denys Vlasenko | bd0e221 | 2013-11-21 15:09:55 +0100 | [diff] [blame] | 381 | |
Denys Vlasenko | 5f8daef | 2014-06-26 16:40:28 +0200 | [diff] [blame] | 382 | testing "awk break" \ |
| 383 | "awk -f - 2>&1; echo \$?" \ |
Denys Vlasenko | d3480dd | 2021-07-14 14:33:37 +0200 | [diff] [blame] | 384 | "awk: -:1: 'break' not in a loop\n1\n" \ |
Denys Vlasenko | 5f8daef | 2014-06-26 16:40:28 +0200 | [diff] [blame] | 385 | "" \ |
| 386 | 'BEGIN { if (1) break; else a = 1 }' |
| 387 | testing "awk continue" \ |
| 388 | "awk -f - 2>&1; echo \$?" \ |
Denys Vlasenko | d3480dd | 2021-07-14 14:33:37 +0200 | [diff] [blame] | 389 | "awk: -:1: 'continue' not in a loop\n1\n" \ |
Denys Vlasenko | 5f8daef | 2014-06-26 16:40:28 +0200 | [diff] [blame] | 390 | "" \ |
| 391 | 'BEGIN { if (1) continue; else a = 1 }' |
| 392 | |
Denys Vlasenko | 22cc928 | 2019-06-08 13:00:46 +0200 | [diff] [blame] | 393 | optional FEATURE_AWK_GNU_EXTENSIONS |
Brian Foley | 61d5997 | 2016-10-15 14:45:40 +0100 | [diff] [blame] | 394 | testing "awk handles invalid for loop" \ |
Brian Foley | 08a514c | 2019-01-01 13:40:59 -0800 | [diff] [blame] | 395 | "awk -e '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
Denys Vlasenko | 22cc928 | 2019-06-08 13:00:46 +0200 | [diff] [blame] | 396 | SKIP= |
Brian Foley | 61d5997 | 2016-10-15 14:45:40 +0100 | [diff] [blame] | 397 | |
Denys Vlasenko | 22cc928 | 2019-06-08 13:00:46 +0200 | [diff] [blame] | 398 | optional FEATURE_AWK_GNU_EXTENSIONS |
Brian Foley | dac15a1 | 2019-01-01 13:40:58 -0800 | [diff] [blame] | 399 | testing "awk handles colon not preceded by ternary" \ |
Brian Foley | 08a514c | 2019-01-01 13:40:59 -0800 | [diff] [blame] | 400 | "awk -e foo:bar: 2>&1" "awk: cmd. line:1: Unexpected token\n" "" "" |
Denys Vlasenko | 22cc928 | 2019-06-08 13:00:46 +0200 | [diff] [blame] | 401 | SKIP= |
Brian Foley | 08a514c | 2019-01-01 13:40:59 -0800 | [diff] [blame] | 402 | |
Denys Vlasenko | 22cc928 | 2019-06-08 13:00:46 +0200 | [diff] [blame] | 403 | optional FEATURE_AWK_GNU_EXTENSIONS |
Brian Foley | 08a514c | 2019-01-01 13:40:59 -0800 | [diff] [blame] | 404 | testing "awk errors on missing delete arg" \ |
| 405 | "awk -e '{delete}' 2>&1" "awk: cmd. line:1: Too few arguments\n" "" "" |
Denys Vlasenko | 22cc928 | 2019-06-08 13:00:46 +0200 | [diff] [blame] | 406 | SKIP= |
Brian Foley | dac15a1 | 2019-01-01 13:40:58 -0800 | [diff] [blame] | 407 | |
Ron Yorston | e8fe9f9 | 2021-01-27 11:19:14 +0000 | [diff] [blame] | 408 | optional FEATURE_AWK_GNU_EXTENSIONS |
| 409 | testing "awk printf('%c') can output NUL" \ |
| 410 | "awk '{printf(\"hello%c null\n\", 0)}'" "hello\0 null\n" "" "\n" |
| 411 | SKIP= |
| 412 | |
Denys Vlasenko | 8e8cea2 | 2021-08-16 07:37:22 +0200 | [diff] [blame] | 413 | optional FEATURE_AWK_GNU_EXTENSIONS DESKTOP |
Denys Vlasenko | 8d269ef | 2021-07-12 11:27:11 +0200 | [diff] [blame] | 414 | testing "awk printf('%-10c') can output NUL" \ |
| 415 | "awk 'BEGIN { printf \"[%-10c]\n\", 0 }' | od -tx1" "\ |
| 416 | 0000000 5b 00 20 20 20 20 20 20 20 20 20 5d 0a |
| 417 | 0000015 |
| 418 | " "" "" |
| 419 | SKIP= |
| 420 | |
Denys Vlasenko | df8066a | 2012-07-11 01:27:15 +0200 | [diff] [blame] | 421 | # testing "description" "command" "result" "infile" "stdin" |
Denys Vlasenko | 2454e67 | 2018-04-23 10:53:18 +0200 | [diff] [blame] | 422 | testing 'awk negative field access' \ |
| 423 | 'awk 2>&1 -- '\''{ $(-1) }'\' \ |
| 424 | "awk: cmd. line:1: Access to negative field\n" \ |
| 425 | '' \ |
| 426 | 'anything' |
| 427 | |
Denys Vlasenko | 6f7a009 | 2020-06-09 01:33:54 +0200 | [diff] [blame] | 428 | # was misinterpreted as (("str"++) i) instead of ("str" (++i)) |
| 429 | # (and was executed: "str"++ is "0", thus concatenating "0" and "1"): |
| 430 | testing 'awk do not allow "str"++' \ |
| 431 | 'awk -v i=1 "BEGIN {print \"str\" ++i}"' \ |
| 432 | "str2\n" \ |
| 433 | '' \ |
| 434 | 'anything' |
| 435 | |
Denys Vlasenko | 758c2bd | 2020-12-04 19:00:06 +0100 | [diff] [blame] | 436 | # gawk compat: FS regex matches only non-empty separators: |
| 437 | # with -*, the splitting is NOT f o o b a r, but foo bar: |
Denys Vlasenko | 665a659 | 2020-12-02 19:07:31 +0100 | [diff] [blame] | 438 | testing 'awk FS regex which can match empty string' \ |
| 439 | "awk -F '-*' '{print \$1 \"-\" \$2 \"=\" \$3 \"*\" \$4}'" \ |
| 440 | "foo-bar=*\n" \ |
| 441 | '' \ |
| 442 | 'foo--bar' |
Denys Vlasenko | df8066a | 2012-07-11 01:27:15 +0200 | [diff] [blame] | 443 | |
Denys Vlasenko | 758c2bd | 2020-12-04 19:00:06 +0100 | [diff] [blame] | 444 | # last+1 field should be empty (had a bug where it wasn't) |
| 445 | testing 'awk $NF is empty' \ |
| 446 | "awk -F '=+' '{print \$NF}'" \ |
| 447 | "\n" \ |
| 448 | '' \ |
| 449 | 'a=====123=' |
| 450 | |
Denys Vlasenko | 4d902ea | 2021-07-02 22:28:51 +0200 | [diff] [blame] | 451 | testing "awk exit N propagates through END's exit" \ |
| 452 | "awk 'BEGIN { exit 42 } END { exit }'; echo \$?" \ |
| 453 | "42\n" \ |
| 454 | '' '' |
| 455 | |
Denys Vlasenko | 3d57a84 | 2021-07-11 12:00:31 +0200 | [diff] [blame] | 456 | testing "awk print + redirect" \ |
| 457 | "awk 'BEGIN { print \"STDERR %s\" >\"/dev/stderr\" }' 2>&1" \ |
| 458 | "STDERR %s\n" \ |
| 459 | '' '' |
| 460 | |
Denys Vlasenko | 39aabfe | 2021-07-11 12:51:43 +0200 | [diff] [blame] | 461 | testing "awk \"cmd\" | getline" \ |
| 462 | "awk 'BEGIN { \"echo HELLO\" | getline; print }'" \ |
| 463 | "HELLO\n" \ |
| 464 | '' '' |
| 465 | |
Daniel Thau | 7d06d6e | 2021-09-02 07:41:08 -0400 | [diff] [blame] | 466 | # printf %% should print one % (had a bug where it didn't) |
| 467 | testing 'awk printf %% prints one %' \ |
| 468 | "awk 'BEGIN { printf \"%%\n\" }'" \ |
| 469 | "%\n" \ |
| 470 | '' '' |
| 471 | |
Denis Vlasenko | 960eca6 | 2007-07-18 18:33:18 +0000 | [diff] [blame] | 472 | exit $FAILCOUNT |