blob: 0691a7972c5685ddaa0cfc3092fcddf31be93bf3 [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>
4# Licensed under GPL v2, see file LICENSE for details.
5
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
Denis Vlasenko66496d72008-08-29 08:37:07 +000019# 4294967295 = 0xffffffff
20testing "awk bitwise op" "awk '{ print or(4294967295,1) }'" "4.29497e+09\n" "" "\n"
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020021optional DESKTOP
Denis Vlasenko66496d72008-08-29 08:37:07 +000022testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4.29497e+09\n" "" "\n"
23testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2.14748e+09\n" "" "\n"
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +000024testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n"
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020025SKIP=
Denis Vlasenko66496d72008-08-29 08:37:07 +000026
Denis Vlasenko67b5eeb2009-04-12 13:54:13 +000027# long field seps requiring regex
28testing "awk long field sep" "awk -F-- '{ print NF, length(\$NF), \$NF }'" \
29 "2 0 \n3 0 \n4 0 \n5 0 \n" \
30 "" \
31 "a--\na--b--\na--b--c--\na--b--c--d--"
32
Denis Vlasenko7a676642009-03-15 22:20:31 +000033# '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
34# but gawk 3.1.5 does not bail out on it.
35testing "awk gsub falls back to non-extended-regex" \
36 "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
37
Denys Vlasenko8e3aff02010-05-10 11:00:11 +020038optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +020039test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +000040testing "awk 'gcc build bug'" \
41 "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \
42 "f842e256461a5ab1ec60b58d16f1114f -\n" \
43 "" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +020044rm -rf awk_t1_* 2>/dev/null
45SKIP=
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +000046
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +000047Q='":"'
48
49testing "awk NF in BEGIN" \
50 "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
51 ":0::::\n" \
52 "" ""
53
Denys Vlasenko12847742009-11-30 01:15:04 +010054prg='
55function b(tmp) {
56 tmp = 0;
57 print "" tmp; #this line causes the bug
58 return tmp;
59}
60function c(tmpc) {
61 tmpc = b(); return tmpc;
62}
63BEGIN {
64 print (c() ? "string" : "number");
65}'
66testing "awk string cast (bug 725)" \
67 "awk '$prg'" \
68 "0\nnumber\n" \
69 "" ""
70
Alexander Shishkind03cd3b2010-02-25 17:55:40 +020071testing "awk handles whitespace before array subscript" \
72 "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
73
Denys Vlasenko3cb60c32010-03-10 19:20:32 +010074prg='
75BEGIN {
Denys Vlasenko90f19fa2010-03-11 08:27:53 +010076 u["a"]=1
77 u["b"]=1
78 u["c"]=1
79 v["d"]=1
Denys Vlasenko3cb60c32010-03-10 19:20:32 +010080 v["e"]=1
Denys Vlasenko90f19fa2010-03-11 08:27:53 +010081 v["f"]=1
82 for (l in u) {
Denys Vlasenko3cb60c32010-03-10 19:20:32 +010083 print "outer1", l;
84 for (l in v) {
85 print " inner", l;
86 }
87 print "outer2", l;
88 }
89 print "end", l;
90 l="a"
91 exit;
92}'
93testing "awk nested loops with the same variable" \
94 "awk '$prg'" \
95 "\
Denys Vlasenko90f19fa2010-03-11 08:27:53 +010096outer1 a
97 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +010098 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +010099 inner f
100outer2 f
101outer1 b
102 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100103 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100104 inner f
105outer2 f
106outer1 c
107 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100108 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100109 inner f
110outer2 f
111end f
112" \
113 "" ""
114
115prg='
116BEGIN {
117 u["a"]=1
118 u["b"]=1
119 u["c"]=1
120 v["d"]=1
121 v["e"]=1
122 v["f"]=1
123 for (l in u) {
124 print "outer1", l;
125 for (l in v) {
126 print " inner", l;
127 break;
128 }
129 print "outer2", l;
130 }
131 print "end", l;
132 l="a"
133 exit;
134}'
135# It's not just buggy, it enters infinite loop. Thus disabled
136false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
137 "awk '$prg'" \
138 "\
139outer1 a
140 inner d
141outer2 d
142outer1 b
143 inner d
144outer2 d
145outer1 c
146 inner d
147outer2 d
148end d
149" \
150 "" ""
151
152prg='
153function f() {
154 for (l in v) {
155 print " inner", l;
156 return;
157 }
158}
159
160BEGIN {
161 u["a"]=1
162 u["b"]=1
163 u["c"]=1
164 v["d"]=1
165 v["e"]=1
166 v["f"]=1
167 for (l in u) {
168 print "outer1", l;
169 f();
170 print "outer2", l;
171 }
172 print "end", l;
173 l="a"
174 exit;
175}'
176# It's not just buggy, it enters infinite loop. Thus disabled
177false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
178 "awk '$prg'" \
179 "\
180outer1 a
181 inner d
182outer2 d
183outer1 b
184 inner d
185outer2 d
186outer1 c
187 inner d
188outer2 d
189end d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100190" \
191 "" ""
192
Denis Vlasenko960eca62007-07-18 18:33:18 +0000193exit $FAILCOUNT