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 |
| 27 | testing "awk bitwise op" "awk '{ print or(4294967295,1) }'" "4.29497e+09\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 | |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 47 | optional DESKTOP |
Denis Vlasenko | 66496d7 | 2008-08-29 08:37:07 +0000 | [diff] [blame] | 48 | testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4.29497e+09\n" "" "\n" |
| 49 | testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2.14748e+09\n" "" "\n" |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 50 | testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n" |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 51 | SKIP= |
Denis Vlasenko | 66496d7 | 2008-08-29 08:37:07 +0000 | [diff] [blame] | 52 | |
Denys Vlasenko | d527e0c | 2010-10-05 13:22:11 +0200 | [diff] [blame] | 53 | # check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN |
| 54 | testing "awk floating const with leading zeroes" \ |
| 55 | "awk '{ printf \"%f %f\n\", \"000.123\", \"009.123\" }'" \ |
| 56 | "0.123000 9.123000\n" \ |
| 57 | "" "\n" |
| 58 | |
Denis Vlasenko | 67b5eeb | 2009-04-12 13:54:13 +0000 | [diff] [blame] | 59 | # long field seps requiring regex |
| 60 | testing "awk long field sep" "awk -F-- '{ print NF, length(\$NF), \$NF }'" \ |
| 61 | "2 0 \n3 0 \n4 0 \n5 0 \n" \ |
| 62 | "" \ |
| 63 | "a--\na--b--\na--b--c--\na--b--c--d--" |
| 64 | |
Denys Vlasenko | ea664dd | 2012-06-22 18:41:01 +0200 | [diff] [blame] | 65 | testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \ |
| 66 | "a\n" \ |
| 67 | "" \ |
| 68 | "a!b\n" |
| 69 | |
Denis Vlasenko | 7a67664 | 2009-03-15 22:20:31 +0000 | [diff] [blame] | 70 | # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'), |
| 71 | # but gawk 3.1.5 does not bail out on it. |
| 72 | testing "awk gsub falls back to non-extended-regex" \ |
| 73 | "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n" |
| 74 | |
Denys Vlasenko | 8e3aff0 | 2010-05-10 11:00:11 +0200 | [diff] [blame] | 75 | optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2 |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 76 | test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2 |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 77 | testing "awk 'gcc build bug'" \ |
| 78 | "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \ |
| 79 | "f842e256461a5ab1ec60b58d16f1114f -\n" \ |
| 80 | "" "" |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 81 | rm -rf awk_t1_* 2>/dev/null |
| 82 | SKIP= |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | 41d5ebe | 2009-01-25 01:00:15 +0000 | [diff] [blame] | 84 | Q='":"' |
| 85 | |
| 86 | testing "awk NF in BEGIN" \ |
| 87 | "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \ |
| 88 | ":0::::\n" \ |
| 89 | "" "" |
| 90 | |
Denys Vlasenko | 1284774 | 2009-11-30 01:15:04 +0100 | [diff] [blame] | 91 | prg=' |
| 92 | function b(tmp) { |
| 93 | tmp = 0; |
| 94 | print "" tmp; #this line causes the bug |
| 95 | return tmp; |
| 96 | } |
| 97 | function c(tmpc) { |
| 98 | tmpc = b(); return tmpc; |
| 99 | } |
| 100 | BEGIN { |
| 101 | print (c() ? "string" : "number"); |
| 102 | }' |
| 103 | testing "awk string cast (bug 725)" \ |
| 104 | "awk '$prg'" \ |
| 105 | "0\nnumber\n" \ |
| 106 | "" "" |
| 107 | |
Alexander Shishkin | d03cd3b | 2010-02-25 17:55:40 +0200 | [diff] [blame] | 108 | testing "awk handles whitespace before array subscript" \ |
| 109 | "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" "" |
| 110 | |
Denys Vlasenko | 6a0d749 | 2010-10-23 21:02:15 +0200 | [diff] [blame] | 111 | # GNU awk 3.1.5's "print ERRNO" prints "No such file or directory" instead of "2", |
| 112 | # do we need to emulate that as well? |
| 113 | testing "awk handles non-existing file correctly" \ |
| 114 | "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \ |
| 115 | "2\n0\nOk\n" "" "" |
| 116 | |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 117 | prg=' |
| 118 | BEGIN { |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 119 | u["a"]=1 |
| 120 | u["b"]=1 |
| 121 | u["c"]=1 |
| 122 | v["d"]=1 |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 123 | v["e"]=1 |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 124 | v["f"]=1 |
| 125 | for (l in u) { |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 126 | print "outer1", l; |
| 127 | for (l in v) { |
| 128 | print " inner", l; |
| 129 | } |
| 130 | print "outer2", l; |
| 131 | } |
| 132 | print "end", l; |
| 133 | l="a" |
| 134 | exit; |
| 135 | }' |
| 136 | testing "awk nested loops with the same variable" \ |
| 137 | "awk '$prg'" \ |
| 138 | "\ |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 139 | outer1 a |
| 140 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 141 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 142 | inner f |
| 143 | outer2 f |
| 144 | outer1 b |
| 145 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 146 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 147 | inner f |
| 148 | outer2 f |
| 149 | outer1 c |
| 150 | inner d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 151 | inner e |
Denys Vlasenko | 90f19fa | 2010-03-11 08:27:53 +0100 | [diff] [blame] | 152 | inner f |
| 153 | outer2 f |
| 154 | end f |
| 155 | " \ |
| 156 | "" "" |
| 157 | |
| 158 | prg=' |
| 159 | BEGIN { |
| 160 | u["a"]=1 |
| 161 | u["b"]=1 |
| 162 | u["c"]=1 |
| 163 | v["d"]=1 |
| 164 | v["e"]=1 |
| 165 | v["f"]=1 |
| 166 | for (l in u) { |
| 167 | print "outer1", l; |
| 168 | for (l in v) { |
| 169 | print " inner", l; |
| 170 | break; |
| 171 | } |
| 172 | print "outer2", l; |
| 173 | } |
| 174 | print "end", l; |
| 175 | l="a" |
| 176 | exit; |
| 177 | }' |
| 178 | # It's not just buggy, it enters infinite loop. Thus disabled |
| 179 | false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \ |
| 180 | "awk '$prg'" \ |
| 181 | "\ |
| 182 | outer1 a |
| 183 | inner d |
| 184 | outer2 d |
| 185 | outer1 b |
| 186 | inner d |
| 187 | outer2 d |
| 188 | outer1 c |
| 189 | inner d |
| 190 | outer2 d |
| 191 | end d |
| 192 | " \ |
| 193 | "" "" |
| 194 | |
| 195 | prg=' |
| 196 | function f() { |
| 197 | for (l in v) { |
| 198 | print " inner", l; |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | BEGIN { |
| 204 | u["a"]=1 |
| 205 | u["b"]=1 |
| 206 | u["c"]=1 |
| 207 | v["d"]=1 |
| 208 | v["e"]=1 |
| 209 | v["f"]=1 |
| 210 | for (l in u) { |
| 211 | print "outer1", l; |
| 212 | f(); |
| 213 | print "outer2", l; |
| 214 | } |
| 215 | print "end", l; |
| 216 | l="a" |
| 217 | exit; |
| 218 | }' |
| 219 | # It's not just buggy, it enters infinite loop. Thus disabled |
| 220 | false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \ |
| 221 | "awk '$prg'" \ |
| 222 | "\ |
| 223 | outer1 a |
| 224 | inner d |
| 225 | outer2 d |
| 226 | outer1 b |
| 227 | inner d |
| 228 | outer2 d |
| 229 | outer1 c |
| 230 | inner d |
| 231 | outer2 d |
| 232 | end d |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 233 | " \ |
| 234 | "" "" |
| 235 | |
Denys Vlasenko | 7b46d11 | 2011-09-11 00:30:56 +0200 | [diff] [blame] | 236 | testing "awk handles empty ()" \ |
| 237 | "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" "" |
| 238 | |
Denys Vlasenko | df8066a | 2012-07-11 01:27:15 +0200 | [diff] [blame] | 239 | testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \ |
| 240 | "a:b\ne\n" \ |
| 241 | "" \ |
| 242 | "a:b c:d\ne:f g:h" |
| 243 | |
| 244 | # testing "description" "command" "result" "infile" "stdin" |
| 245 | |
Denis Vlasenko | 960eca6 | 2007-07-18 18:33:18 +0000 | [diff] [blame] | 246 | exit $FAILCOUNT |