blob: dad49c3f54d9c10c15c88ca0970346e583916513 [file] [log] [blame]
Denis Vlasenko960eca62007-07-18 18:33:18 +00001#!/bin/sh
2
Denis Vlasenko960eca62007-07-18 18:33:18 +00003# Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004# Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko960eca62007-07-18 18:33:18 +00005
Mike Frysingercaa79402009-11-04 18:41:22 -05006. ./testing.sh
Denis Vlasenko960eca62007-07-18 18:33:18 +00007
Denis Vlasenkobb130792008-07-15 06:45:14 +00008# testing "description" "command" "result" "infile" "stdin"
Denis Vlasenko960eca62007-07-18 18:33:18 +00009
10testing "awk -F case 0" "awk -F '[#]' '{ print NF }'" "" "" ""
11testing "awk -F case 1" "awk -F '[#]' '{ print NF }'" "0\n" "" "\n"
12testing "awk -F case 2" "awk -F '[#]' '{ print NF }'" "2\n" "" "#\n"
13testing "awk -F case 3" "awk -F '[#]' '{ print NF }'" "3\n" "" "#abc#\n"
14testing "awk -F case 4" "awk -F '[#]' '{ print NF }'" "3\n" "" "#abc#zz\n"
15testing "awk -F case 5" "awk -F '[#]' '{ print NF }'" "4\n" "" "#abc##zz\n"
16testing "awk -F case 6" "awk -F '[#]' '{ print NF }'" "4\n" "" "z#abc##zz\n"
17testing "awk -F case 7" "awk -F '[#]' '{ print NF }'" "5\n" "" "z##abc##zz\n"
18
Tanguy Pruvot0a393cf2012-06-11 10:33:45 +020019# conditions and operators
20testing "awk if operator == " "awk 'BEGIN{if(23==23) print \"foo\"}'" "foo\n" "" ""
21testing "awk if operator != " "awk 'BEGIN{if(23!=23) print \"bar\"}'" "" "" ""
22testing "awk if operator >= " "awk 'BEGIN{if(23>=23) print \"foo\"}'" "foo\n" "" ""
23testing "awk if operator < " "awk 'BEGIN{if(2 < 13) print \"foo\"}'" "foo\n" "" ""
24testing "awk if string == " "awk 'BEGIN{if(\"a\"==\"ab\") print \"bar\"}'" "" "" ""
25
Denis Vlasenko66496d72008-08-29 08:37:07 +000026# 4294967295 = 0xffffffff
27testing "awk bitwise op" "awk '{ print or(4294967295,1) }'" "4.29497e+09\n" "" "\n"
Bernhard Reutner-Fischerb79a0fe2013-03-06 21:01:05 +010028
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
32prg='
33function empty_fun(count) {
34 # empty
35}
36END {
37 i=1
38 print "L" i "\n"
39 empty_fun(i + i + ++i)
40 print "L" i "\n"
41}'
42testing "awk handles empty function f(arg){}" \
43 "awk '$prg'" \
44 "L1\n\nL2\n\n" \
45 "" ""
46
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020047optional DESKTOP
Denis Vlasenko66496d72008-08-29 08:37:07 +000048testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4.29497e+09\n" "" "\n"
49testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2.14748e+09\n" "" "\n"
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +000050testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n"
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020051SKIP=
Denis Vlasenko66496d72008-08-29 08:37:07 +000052
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020053# check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN
54testing "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 Vlasenko67b5eeb2009-04-12 13:54:13 +000059# long field seps requiring regex
60testing "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 Vlasenkoea664dd2012-06-22 18:41:01 +020065testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \
66 "a\n" \
67 "" \
68 "a!b\n"
69
Denis Vlasenko7a676642009-03-15 22:20:31 +000070# '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
71# but gawk 3.1.5 does not bail out on it.
72testing "awk gsub falls back to non-extended-regex" \
73 "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
74
Denys Vlasenko8e3aff02010-05-10 11:00:11 +020075optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +020076test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +000077testing "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 Vlasenkoe3d90a92010-05-10 05:53:16 +020081rm -rf awk_t1_* 2>/dev/null
82SKIP=
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +000083
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +000084Q='":"'
85
86testing "awk NF in BEGIN" \
87 "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
88 ":0::::\n" \
89 "" ""
90
Denys Vlasenko12847742009-11-30 01:15:04 +010091prg='
92function b(tmp) {
93 tmp = 0;
94 print "" tmp; #this line causes the bug
95 return tmp;
96}
97function c(tmpc) {
98 tmpc = b(); return tmpc;
99}
100BEGIN {
101 print (c() ? "string" : "number");
102}'
103testing "awk string cast (bug 725)" \
104 "awk '$prg'" \
105 "0\nnumber\n" \
106 "" ""
107
Alexander Shishkind03cd3b2010-02-25 17:55:40 +0200108testing "awk handles whitespace before array subscript" \
109 "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
110
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200111# 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?
113testing "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 Vlasenko3cb60c32010-03-10 19:20:32 +0100117prg='
118BEGIN {
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100119 u["a"]=1
120 u["b"]=1
121 u["c"]=1
122 v["d"]=1
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100123 v["e"]=1
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100124 v["f"]=1
125 for (l in u) {
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100126 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}'
136testing "awk nested loops with the same variable" \
137 "awk '$prg'" \
138 "\
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100139outer1 a
140 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100141 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100142 inner f
143outer2 f
144outer1 b
145 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100146 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100147 inner f
148outer2 f
149outer1 c
150 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100151 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100152 inner f
153outer2 f
154end f
155" \
156 "" ""
157
158prg='
159BEGIN {
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
179false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
180 "awk '$prg'" \
181 "\
182outer1 a
183 inner d
184outer2 d
185outer1 b
186 inner d
187outer2 d
188outer1 c
189 inner d
190outer2 d
191end d
192" \
193 "" ""
194
195prg='
196function f() {
197 for (l in v) {
198 print " inner", l;
199 return;
200 }
201}
202
203BEGIN {
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
220false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
221 "awk '$prg'" \
222 "\
223outer1 a
224 inner d
225outer2 d
226outer1 b
227 inner d
228outer2 d
229outer1 c
230 inner d
231outer2 d
232end d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100233" \
234 "" ""
235
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200236testing "awk handles empty ()" \
237 "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
238
Denys Vlasenkodf8066a2012-07-11 01:27:15 +0200239testing "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 Vlasenko960eca62007-07-18 18:33:18 +0000246exit $FAILCOUNT