awk.tests 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #!/bin/sh
  2. # Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
  3. # Licensed under GPLv2, see file LICENSE in this source tree.
  4. . ./testing.sh
  5. # testing "description" "command" "result" "infile" "stdin"
  6. testing "awk -F case 0" "awk -F '[#]' '{ print NF }'" "" "" ""
  7. testing "awk -F case 1" "awk -F '[#]' '{ print NF }'" "0\n" "" "\n"
  8. testing "awk -F case 2" "awk -F '[#]' '{ print NF }'" "2\n" "" "#\n"
  9. testing "awk -F case 3" "awk -F '[#]' '{ print NF }'" "3\n" "" "#abc#\n"
  10. testing "awk -F case 4" "awk -F '[#]' '{ print NF }'" "3\n" "" "#abc#zz\n"
  11. testing "awk -F case 5" "awk -F '[#]' '{ print NF }'" "4\n" "" "#abc##zz\n"
  12. testing "awk -F case 6" "awk -F '[#]' '{ print NF }'" "4\n" "" "z#abc##zz\n"
  13. testing "awk -F case 7" "awk -F '[#]' '{ print NF }'" "5\n" "" "z##abc##zz\n"
  14. # conditions and operators
  15. testing "awk if operator == " "awk 'BEGIN{if(23==23) print \"foo\"}'" "foo\n" "" ""
  16. testing "awk if operator != " "awk 'BEGIN{if(23!=23) print \"bar\"}'" "" "" ""
  17. testing "awk if operator >= " "awk 'BEGIN{if(23>=23) print \"foo\"}'" "foo\n" "" ""
  18. testing "awk if operator < " "awk 'BEGIN{if(2 < 13) print \"foo\"}'" "foo\n" "" ""
  19. testing "awk if string == " "awk 'BEGIN{if(\"a\"==\"ab\") print \"bar\"}'" "" "" ""
  20. # 4294967295 = 0xffffffff
  21. testing "awk bitwise op" "awk '{ print or(4294967295,1) }'" "4294967295\n" "" "\n"
  22. # we were testing for a non-empty body when deciding if a function was
  23. # defined or not. The testcase below caused:
  24. # awk: cmd. line:8: Call to undefined function
  25. prg='
  26. function empty_fun(count) {
  27. # empty
  28. }
  29. END {
  30. i=1
  31. print "L" i "\n"
  32. empty_fun(i + i + ++i)
  33. print "L" i "\n"
  34. }'
  35. testing "awk handles empty function f(arg){}" \
  36. "awk '$prg'" \
  37. "L1\n\nL2\n\n" \
  38. "" ""
  39. prg='
  40. function outer_fun() {
  41. return 1
  42. }
  43. END {
  44. i=1
  45. print "L" i "\n"
  46. i += outer_fun()
  47. print "L" i "\n"
  48. }'
  49. testing "awk properly handles function from other scope" \
  50. "awk '$prg'" \
  51. "L1\n\nL2\n\n" \
  52. "" ""
  53. prg='
  54. END {
  55. i=1
  56. print "L" i "\n"
  57. i + trigger_error_fun()
  58. print "L" i "\n"
  59. }'
  60. testing "awk properly handles undefined function" \
  61. "awk '$prg' 2>&1" \
  62. "L1\n\nawk: cmd. line:5: Call to undefined function\n" \
  63. "" ""
  64. optional DESKTOP
  65. testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4294967295\n" "" "\n"
  66. testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2147483649\n" "" "\n"
  67. testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n"
  68. SKIP=
  69. # check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN
  70. testing "awk floating const with leading zeroes" \
  71. "awk '{ printf \"%f %f\n\", \"000.123\", \"009.123\" }'" \
  72. "0.123000 9.123000\n" \
  73. "" "\n"
  74. # long field seps requiring regex
  75. testing "awk long field sep" \
  76. "awk -F-- '{ print NF, length(\$NF), \$NF }'" \
  77. "2 0 \n3 0 \n4 0 \n5 0 \n" \
  78. "" \
  79. "a--\na--b--\na--b--c--\na--b--c--d--"
  80. testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \
  81. "a\n" \
  82. "" \
  83. "a!b\n"
  84. # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
  85. # but gawk 3.1.5 does not bail out on it.
  86. testing "awk gsub falls back to non-extended-regex" \
  87. "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
  88. optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
  89. test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
  90. testing "awk 'gcc build bug'" \
  91. "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \
  92. "f842e256461a5ab1ec60b58d16f1114f -\n" \
  93. "" ""
  94. rm -rf awk_t1_* 2>/dev/null
  95. SKIP=
  96. Q='":"'
  97. testing "awk NF in BEGIN" \
  98. "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
  99. ":0::::\n" \
  100. "" ""
  101. prg='
  102. function b(tmp) {
  103. tmp = 0;
  104. print "" tmp; #this line causes the bug
  105. return tmp;
  106. }
  107. function c(tmpc) {
  108. tmpc = b(); return tmpc;
  109. }
  110. BEGIN {
  111. print (c() ? "string" : "number");
  112. }'
  113. testing "awk string cast (bug 725)" \
  114. "awk '$prg'" \
  115. "0\nnumber\n" \
  116. "" ""
  117. testing "awk handles whitespace before array subscript" \
  118. "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
  119. # GNU awk 3.1.5's "print ERRNO" prints "No such file or directory" instead of "2",
  120. # do we need to emulate that as well?
  121. testing "awk handles non-existing file correctly" \
  122. "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \
  123. "2\n0\nOk\n" "" ""
  124. prg='
  125. BEGIN {
  126. u["a"]=1
  127. u["b"]=1
  128. u["c"]=1
  129. v["d"]=1
  130. v["e"]=1
  131. v["f"]=1
  132. for (l in u) {
  133. print "outer1", l;
  134. for (l in v) {
  135. print " inner", l;
  136. }
  137. print "outer2", l;
  138. }
  139. print "end", l;
  140. l="a"
  141. exit;
  142. }'
  143. testing "awk nested loops with the same variable" \
  144. "awk '$prg'" \
  145. "\
  146. outer1 a
  147. inner d
  148. inner e
  149. inner f
  150. outer2 f
  151. outer1 b
  152. inner d
  153. inner e
  154. inner f
  155. outer2 f
  156. outer1 c
  157. inner d
  158. inner e
  159. inner f
  160. outer2 f
  161. end f
  162. " \
  163. "" ""
  164. prg='
  165. BEGIN {
  166. u["a"]=1
  167. u["b"]=1
  168. u["c"]=1
  169. v["d"]=1
  170. v["e"]=1
  171. v["f"]=1
  172. for (l in u) {
  173. print "outer1", l;
  174. for (l in v) {
  175. print " inner", l;
  176. break;
  177. }
  178. print "outer2", l;
  179. }
  180. print "end", l;
  181. l="a"
  182. exit;
  183. }'
  184. # It's not just buggy, it enters infinite loop. Thus disabled
  185. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
  186. "awk '$prg'" \
  187. "\
  188. outer1 a
  189. inner d
  190. outer2 d
  191. outer1 b
  192. inner d
  193. outer2 d
  194. outer1 c
  195. inner d
  196. outer2 d
  197. end d
  198. " \
  199. "" ""
  200. prg='
  201. function f() {
  202. for (l in v) {
  203. print " inner", l;
  204. return;
  205. }
  206. }
  207. BEGIN {
  208. u["a"]=1
  209. u["b"]=1
  210. u["c"]=1
  211. v["d"]=1
  212. v["e"]=1
  213. v["f"]=1
  214. for (l in u) {
  215. print "outer1", l;
  216. f();
  217. print "outer2", l;
  218. }
  219. print "end", l;
  220. l="a"
  221. exit;
  222. }'
  223. # It's not just buggy, it enters infinite loop. Thus disabled
  224. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
  225. "awk '$prg'" \
  226. "\
  227. outer1 a
  228. inner d
  229. outer2 d
  230. outer1 b
  231. inner d
  232. outer2 d
  233. outer1 c
  234. inner d
  235. outer2 d
  236. end d
  237. " \
  238. "" ""
  239. prg='
  240. BEGIN{
  241. cnt = 0
  242. a[cnt] = "zeroth"
  243. a[++cnt] = "first"
  244. delete a[cnt--]
  245. print cnt
  246. print "[0]:" a[0]
  247. print "[1]:" a[1]
  248. }'
  249. testing "awk 'delete a[v--]' evaluates v-- once" \
  250. "awk '$prg'" \
  251. "\
  252. 0
  253. [0]:zeroth
  254. [1]:
  255. " \
  256. "" ""
  257. testing "awk func arg parsing 1" \
  258. "awk 'func f(,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  259. testing "awk func arg parsing 2" \
  260. "awk 'func f(a,,b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  261. testing "awk func arg parsing 3" \
  262. "awk 'func f(a,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  263. testing "awk func arg parsing 4" \
  264. "awk 'func f(a b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  265. testing "awk handles empty ()" \
  266. "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
  267. testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \
  268. "a:b\ne\n" \
  269. "" \
  270. "a:b c:d\ne:f g:h"
  271. optional FEATURE_AWK_LIBM
  272. testing "awk large integer" \
  273. "awk 'BEGIN{n=(2^31)-1; print n, int(n), n%1, ++n, int(n), n%1}'" \
  274. "2147483647 2147483647 0 2147483648 2147483648 0\n" \
  275. "" ""
  276. SKIP=
  277. testing "awk length(array)" \
  278. "awk 'BEGIN{ A[1]=2; A[\"qwe\"]=\"asd\"; print length(A)}'" \
  279. "2\n" \
  280. "" ""
  281. testing "awk length()" \
  282. "awk '{print length; print length(); print length(\"qwe\"); print length(99+9)}'" \
  283. "3\n3\n3\n3\n" \
  284. "" "qwe"
  285. testing "awk print length, 1" \
  286. "awk '{ print length, 1 }'" \
  287. "0 1\n" \
  288. "" "\n"
  289. testing "awk print length 1" \
  290. "awk '{ print length 1 }'" \
  291. "01\n" \
  292. "" "\n"
  293. testing "awk length == 0" \
  294. "awk 'length == 0 { print \"foo\" }'" \
  295. "foo\n" \
  296. "" "\n"
  297. testing "awk if (length == 0)" \
  298. "awk '{ if (length == 0) { print \"bar\" } }'" \
  299. "bar\n" \
  300. "" "\n"
  301. testing "awk -f and ARGC" \
  302. "awk -f - input" \
  303. "re\n2\n" \
  304. "do re mi\n" \
  305. '{print $2; print ARGC;}' \
  306. optional FEATURE_AWK_GNU_EXTENSIONS
  307. testing "awk -e and ARGC" \
  308. "awk -e '{print \$2; print ARGC;}' input" \
  309. "re\n2\n" \
  310. "do re mi\n" \
  311. ""
  312. SKIP=
  313. # The examples are in fact not valid awk programs (break/continue
  314. # can only be used inside loops).
  315. # But we do accept them outside of loops.
  316. # We had a bug with misparsing "break ; else" sequence.
  317. # Test that *that* bug is fixed, using simplest possible scripts:
  318. testing "awk break" \
  319. "awk -f - 2>&1; echo \$?" \
  320. "0\n" \
  321. "" \
  322. 'BEGIN { if (1) break; else a = 1 }'
  323. testing "awk continue" \
  324. "awk -f - 2>&1; echo \$?" \
  325. "0\n" \
  326. "" \
  327. 'BEGIN { if (1) continue; else a = 1 }'
  328. optional FEATURE_AWK_GNU_EXTENSIONS
  329. testing "awk handles invalid for loop" \
  330. "awk -e '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  331. SKIP=
  332. optional FEATURE_AWK_GNU_EXTENSIONS
  333. testing "awk handles colon not preceded by ternary" \
  334. "awk -e foo:bar: 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  335. SKIP=
  336. optional FEATURE_AWK_GNU_EXTENSIONS
  337. testing "awk errors on missing delete arg" \
  338. "awk -e '{delete}' 2>&1" "awk: cmd. line:1: Too few arguments\n" "" ""
  339. SKIP=
  340. # testing "description" "command" "result" "infile" "stdin"
  341. testing 'awk negative field access' \
  342. 'awk 2>&1 -- '\''{ $(-1) }'\' \
  343. "awk: cmd. line:1: Access to negative field\n" \
  344. '' \
  345. 'anything'
  346. # was misinterpreted as (("str"++) i) instead of ("str" (++i))
  347. # (and was executed: "str"++ is "0", thus concatenating "0" and "1"):
  348. testing 'awk do not allow "str"++' \
  349. 'awk -v i=1 "BEGIN {print \"str\" ++i}"' \
  350. "str2\n" \
  351. '' \
  352. 'anything'
  353. exit $FAILCOUNT