blob: 56b11ca46d3b63e0727a20557ce80d7c68d568ee [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
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
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020027# check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN
28testing "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 Vlasenko67b5eeb2009-04-12 13:54:13 +000033# long field seps requiring regex
34testing "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 Vlasenko7a676642009-03-15 22:20:31 +000039# '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
40# but gawk 3.1.5 does not bail out on it.
41testing "awk gsub falls back to non-extended-regex" \
42 "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
43
Denys Vlasenko8e3aff02010-05-10 11:00:11 +020044optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +020045test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +000046testing "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 Vlasenkoe3d90a92010-05-10 05:53:16 +020050rm -rf awk_t1_* 2>/dev/null
51SKIP=
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +000052
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +000053Q='":"'
54
55testing "awk NF in BEGIN" \
56 "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
57 ":0::::\n" \
58 "" ""
59
Denys Vlasenko12847742009-11-30 01:15:04 +010060prg='
61function b(tmp) {
62 tmp = 0;
63 print "" tmp; #this line causes the bug
64 return tmp;
65}
66function c(tmpc) {
67 tmpc = b(); return tmpc;
68}
69BEGIN {
70 print (c() ? "string" : "number");
71}'
72testing "awk string cast (bug 725)" \
73 "awk '$prg'" \
74 "0\nnumber\n" \
75 "" ""
76
Alexander Shishkind03cd3b2010-02-25 17:55:40 +020077testing "awk handles whitespace before array subscript" \
78 "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
79
Denys Vlasenko3cb60c32010-03-10 19:20:32 +010080prg='
81BEGIN {
Denys Vlasenko90f19fa2010-03-11 08:27:53 +010082 u["a"]=1
83 u["b"]=1
84 u["c"]=1
85 v["d"]=1
Denys Vlasenko3cb60c32010-03-10 19:20:32 +010086 v["e"]=1
Denys Vlasenko90f19fa2010-03-11 08:27:53 +010087 v["f"]=1
88 for (l in u) {
Denys Vlasenko3cb60c32010-03-10 19:20:32 +010089 print "outer1", l;
90 for (l in v) {
91 print " inner", l;
92 }
93 print "outer2", l;
94 }
95 print "end", l;
96 l="a"
97 exit;
98}'
99testing "awk nested loops with the same variable" \
100 "awk '$prg'" \
101 "\
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100102outer1 a
103 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100104 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100105 inner f
106outer2 f
107outer1 b
108 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100109 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100110 inner f
111outer2 f
112outer1 c
113 inner d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100114 inner e
Denys Vlasenko90f19fa2010-03-11 08:27:53 +0100115 inner f
116outer2 f
117end f
118" \
119 "" ""
120
121prg='
122BEGIN {
123 u["a"]=1
124 u["b"]=1
125 u["c"]=1
126 v["d"]=1
127 v["e"]=1
128 v["f"]=1
129 for (l in u) {
130 print "outer1", l;
131 for (l in v) {
132 print " inner", l;
133 break;
134 }
135 print "outer2", l;
136 }
137 print "end", l;
138 l="a"
139 exit;
140}'
141# It's not just buggy, it enters infinite loop. Thus disabled
142false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
143 "awk '$prg'" \
144 "\
145outer1 a
146 inner d
147outer2 d
148outer1 b
149 inner d
150outer2 d
151outer1 c
152 inner d
153outer2 d
154end d
155" \
156 "" ""
157
158prg='
159function f() {
160 for (l in v) {
161 print " inner", l;
162 return;
163 }
164}
165
166BEGIN {
167 u["a"]=1
168 u["b"]=1
169 u["c"]=1
170 v["d"]=1
171 v["e"]=1
172 v["f"]=1
173 for (l in u) {
174 print "outer1", l;
175 f();
176 print "outer2", l;
177 }
178 print "end", l;
179 l="a"
180 exit;
181}'
182# It's not just buggy, it enters infinite loop. Thus disabled
183false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
184 "awk '$prg'" \
185 "\
186outer1 a
187 inner d
188outer2 d
189outer1 b
190 inner d
191outer2 d
192outer1 c
193 inner d
194outer2 d
195end d
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100196" \
197 "" ""
198
Denis Vlasenko960eca62007-07-18 18:33:18 +0000199exit $FAILCOUNT