ip link: add VLAN support
[oweals/busybox.git] / testsuite / awk.tests
1 #!/bin/sh
2
3 # Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
4 # Licensed under GPLv2, see file LICENSE in this source tree.
5
6 . ./testing.sh
7
8 # testing "description" "command" "result" "infile" "stdin"
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
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
26 # 4294967295 = 0xffffffff
27 testing "awk bitwise op"  "awk '{ print or(4294967295,1) }'" "4.29497e+09\n" "" "\n"
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
47 optional DESKTOP
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"
50 testing "awk oct const"   "awk '{ print or(01234,1) }'"      "669\n"         "" "\n"
51 SKIP=
52
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
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
65 testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \
66         "a\n" \
67         "" \
68         "a!b\n"
69
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
75 optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
76 test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
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         "" ""
81 rm -rf awk_t1_* 2>/dev/null
82 SKIP=
83
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
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
108 testing "awk handles whitespace before array subscript" \
109         "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
110
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
117 prg='
118 BEGIN {
119   u["a"]=1
120   u["b"]=1
121   u["c"]=1
122   v["d"]=1
123   v["e"]=1
124   v["f"]=1
125   for (l in u) {
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         "\
139 outer1 a
140  inner d
141  inner e
142  inner f
143 outer2 f
144 outer1 b
145  inner d
146  inner e
147  inner f
148 outer2 f
149 outer1 c
150  inner d
151  inner e
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
233 " \
234         "" ""
235
236 testing "awk handles empty ()" \
237         "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
238
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
246 exit $FAILCOUNT