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