awk.tests 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 empty_fun(){}
  41. END {empty_fun()
  42. print "Ok"
  43. }'
  44. testing "awk handles empty function f(){}" \
  45. "awk '$prg'" \
  46. "Ok\n" \
  47. "" ""
  48. prg='
  49. function outer_fun() {
  50. return 1
  51. }
  52. END {
  53. i=1
  54. print "L" i "\n"
  55. i += outer_fun()
  56. print "L" i "\n"
  57. }'
  58. testing "awk properly handles function from other scope" \
  59. "awk '$prg'" \
  60. "L1\n\nL2\n\n" \
  61. "" ""
  62. prg='
  63. END {
  64. i=1
  65. print "L" i "\n"
  66. i + trigger_error_fun()
  67. print "L" i "\n"
  68. }'
  69. testing "awk properly handles undefined function" \
  70. "awk '$prg' 2>&1" \
  71. "L1\n\nawk: cmd. line:5: Call to undefined function\n" \
  72. "" ""
  73. prg='
  74. BEGIN {
  75. v=1
  76. a=2
  77. print v (a)
  78. }'
  79. testing "awk 'v (a)' is not a function call, it is a concatenation" \
  80. "awk '$prg' 2>&1" \
  81. "12\n" \
  82. "" ""
  83. prg='func f(){print"F"};func g(){print"G"};BEGIN{f(g(),g())}'
  84. testing "awk unused function args are evaluated" \
  85. "awk '$prg' 2>&1" \
  86. "G\nG\nF\n" \
  87. "" ""
  88. optional DESKTOP
  89. testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4294967295\n" "" "\n"
  90. testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2147483649\n" "" "\n"
  91. testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n"
  92. SKIP=
  93. # check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN
  94. testing "awk floating const with leading zeroes" \
  95. "awk '{ printf \"%f %f\n\", \"000.123\", \"009.123\" }'" \
  96. "0.123000 9.123000\n" \
  97. "" "\n"
  98. # long field seps requiring regex
  99. testing "awk long field sep" \
  100. "awk -F-- '{ print NF, length(\$NF), \$NF }'" \
  101. "2 0 \n3 0 \n4 0 \n5 0 \n" \
  102. "" \
  103. "a--\na--b--\na--b--c--\na--b--c--d--"
  104. testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \
  105. "a\n" \
  106. "" \
  107. "a!b\n"
  108. # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
  109. # but gawk 3.1.5 does not bail out on it.
  110. testing "awk gsub falls back to non-extended-regex" \
  111. "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
  112. optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
  113. test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
  114. testing "awk 'gcc build bug'" \
  115. "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \
  116. "f842e256461a5ab1ec60b58d16f1114f -\n" \
  117. "" ""
  118. rm -rf awk_t1_* 2>/dev/null
  119. SKIP=
  120. Q='":"'
  121. testing "awk NF in BEGIN" \
  122. "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
  123. ":0::::\n" \
  124. "" ""
  125. prg='
  126. function b(tmp) {
  127. tmp = 0;
  128. print "" tmp; #this line causes the bug
  129. return tmp;
  130. }
  131. function c(tmpc) {
  132. tmpc = b(); return tmpc;
  133. }
  134. BEGIN {
  135. print (c() ? "string" : "number");
  136. }'
  137. testing "awk string cast (bug 725)" \
  138. "awk '$prg'" \
  139. "0\nnumber\n" \
  140. "" ""
  141. testing "awk handles whitespace before array subscript" \
  142. "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
  143. # GNU awk 3.1.5's "print ERRNO" prints "No such file or directory" instead of "2",
  144. # do we need to emulate that as well?
  145. testing "awk handles non-existing file correctly" \
  146. "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \
  147. "2\n0\nOk\n" "" ""
  148. prg='
  149. BEGIN {
  150. u["a"]=1
  151. u["b"]=1
  152. u["c"]=1
  153. v["d"]=1
  154. v["e"]=1
  155. v["f"]=1
  156. for (l in u) {
  157. print "outer1", l;
  158. for (l in v) {
  159. print " inner", l;
  160. }
  161. print "outer2", l;
  162. }
  163. print "end", l;
  164. l="a"
  165. exit;
  166. }'
  167. testing "awk nested loops with the same variable" \
  168. "awk '$prg'" \
  169. "\
  170. outer1 a
  171. inner d
  172. inner e
  173. inner f
  174. outer2 f
  175. outer1 b
  176. inner d
  177. inner e
  178. inner f
  179. outer2 f
  180. outer1 c
  181. inner d
  182. inner e
  183. inner f
  184. outer2 f
  185. end f
  186. " \
  187. "" ""
  188. prg='
  189. BEGIN {
  190. u["a"]=1
  191. u["b"]=1
  192. u["c"]=1
  193. v["d"]=1
  194. v["e"]=1
  195. v["f"]=1
  196. for (l in u) {
  197. print "outer1", l;
  198. for (l in v) {
  199. print " inner", l;
  200. break;
  201. }
  202. print "outer2", l;
  203. }
  204. print "end", l;
  205. l="a"
  206. exit;
  207. }'
  208. # It's not just buggy, it enters infinite loop. Thus disabled
  209. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
  210. "awk '$prg'" \
  211. "\
  212. outer1 a
  213. inner d
  214. outer2 d
  215. outer1 b
  216. inner d
  217. outer2 d
  218. outer1 c
  219. inner d
  220. outer2 d
  221. end d
  222. " \
  223. "" ""
  224. prg='
  225. function f() {
  226. for (l in v) {
  227. print " inner", l;
  228. return;
  229. }
  230. }
  231. BEGIN {
  232. u["a"]=1
  233. u["b"]=1
  234. u["c"]=1
  235. v["d"]=1
  236. v["e"]=1
  237. v["f"]=1
  238. for (l in u) {
  239. print "outer1", l;
  240. f();
  241. print "outer2", l;
  242. }
  243. print "end", l;
  244. l="a"
  245. exit;
  246. }'
  247. # It's not just buggy, it enters infinite loop. Thus disabled
  248. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
  249. "awk '$prg'" \
  250. "\
  251. outer1 a
  252. inner d
  253. outer2 d
  254. outer1 b
  255. inner d
  256. outer2 d
  257. outer1 c
  258. inner d
  259. outer2 d
  260. end d
  261. " \
  262. "" ""
  263. prg='
  264. BEGIN{
  265. cnt = 0
  266. a[cnt] = "zeroth"
  267. a[++cnt] = "first"
  268. delete a[cnt--]
  269. print cnt
  270. print "[0]:" a[0]
  271. print "[1]:" a[1]
  272. }'
  273. testing "awk 'delete a[v--]' evaluates v-- once" \
  274. "awk '$prg'" \
  275. "\
  276. 0
  277. [0]:zeroth
  278. [1]:
  279. " \
  280. "" ""
  281. testing "awk func arg parsing 1" \
  282. "awk 'func f(,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  283. testing "awk func arg parsing 2" \
  284. "awk 'func f(a,,b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  285. testing "awk func arg parsing 3" \
  286. "awk 'func f(a,) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  287. testing "awk func arg parsing 4" \
  288. "awk 'func f(a b) { }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  289. testing "awk handles empty ()" \
  290. "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
  291. testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \
  292. "a:b\ne\n" \
  293. "" \
  294. "a:b c:d\ne:f g:h"
  295. optional FEATURE_AWK_LIBM
  296. testing "awk large integer" \
  297. "awk 'BEGIN{n=(2^31)-1; print n, int(n), n%1, ++n, int(n), n%1}'" \
  298. "2147483647 2147483647 0 2147483648 2147483648 0\n" \
  299. "" ""
  300. SKIP=
  301. testing "awk length(array)" \
  302. "awk 'BEGIN{ A[1]=2; A[\"qwe\"]=\"asd\"; print length(A)}'" \
  303. "2\n" \
  304. "" ""
  305. testing "awk length()" \
  306. "awk '{print length; print length(); print length(\"qwe\"); print length(99+9)}'" \
  307. "3\n3\n3\n3\n" \
  308. "" "qwe"
  309. testing "awk print length, 1" \
  310. "awk '{ print length, 1 }'" \
  311. "0 1\n" \
  312. "" "\n"
  313. testing "awk print length 1" \
  314. "awk '{ print length 1 }'" \
  315. "01\n" \
  316. "" "\n"
  317. testing "awk length == 0" \
  318. "awk 'length == 0 { print \"foo\" }'" \
  319. "foo\n" \
  320. "" "\n"
  321. testing "awk if (length == 0)" \
  322. "awk '{ if (length == 0) { print \"bar\" } }'" \
  323. "bar\n" \
  324. "" "\n"
  325. testing "awk -f and ARGC" \
  326. "awk -f - input" \
  327. "re\n2\n" \
  328. "do re mi\n" \
  329. '{print $2; print ARGC;}' \
  330. optional FEATURE_AWK_GNU_EXTENSIONS
  331. testing "awk -e and ARGC" \
  332. "awk -e '{print \$2; print ARGC;}' input" \
  333. "re\n2\n" \
  334. "do re mi\n" \
  335. ""
  336. SKIP=
  337. testing "awk break" \
  338. "awk -f - 2>&1; echo \$?" \
  339. "awk: -:1: 'break' not in a loop\n1\n" \
  340. "" \
  341. 'BEGIN { if (1) break; else a = 1 }'
  342. testing "awk continue" \
  343. "awk -f - 2>&1; echo \$?" \
  344. "awk: -:1: 'continue' not in a loop\n1\n" \
  345. "" \
  346. 'BEGIN { if (1) continue; else a = 1 }'
  347. optional FEATURE_AWK_GNU_EXTENSIONS
  348. testing "awk handles invalid for loop" \
  349. "awk -e '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  350. SKIP=
  351. optional FEATURE_AWK_GNU_EXTENSIONS
  352. testing "awk handles colon not preceded by ternary" \
  353. "awk -e foo:bar: 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
  354. SKIP=
  355. optional FEATURE_AWK_GNU_EXTENSIONS
  356. testing "awk errors on missing delete arg" \
  357. "awk -e '{delete}' 2>&1" "awk: cmd. line:1: Too few arguments\n" "" ""
  358. SKIP=
  359. optional FEATURE_AWK_GNU_EXTENSIONS
  360. testing "awk printf('%c') can output NUL" \
  361. "awk '{printf(\"hello%c null\n\", 0)}'" "hello\0 null\n" "" "\n"
  362. SKIP=
  363. optional FEATURE_AWK_GNU_EXTENSIONS DESKTOP
  364. testing "awk printf('%-10c') can output NUL" \
  365. "awk 'BEGIN { printf \"[%-10c]\n\", 0 }' | od -tx1" "\
  366. 0000000 5b 00 20 20 20 20 20 20 20 20 20 5d 0a
  367. 0000015
  368. " "" ""
  369. SKIP=
  370. # testing "description" "command" "result" "infile" "stdin"
  371. testing 'awk negative field access' \
  372. 'awk 2>&1 -- '\''{ $(-1) }'\' \
  373. "awk: cmd. line:1: Access to negative field\n" \
  374. '' \
  375. 'anything'
  376. # was misinterpreted as (("str"++) i) instead of ("str" (++i))
  377. # (and was executed: "str"++ is "0", thus concatenating "0" and "1"):
  378. testing 'awk do not allow "str"++' \
  379. 'awk -v i=1 "BEGIN {print \"str\" ++i}"' \
  380. "str2\n" \
  381. '' \
  382. 'anything'
  383. # gawk compat: FS regex matches only non-empty separators:
  384. # with -*, the splitting is NOT f o o b a r, but foo bar:
  385. testing 'awk FS regex which can match empty string' \
  386. "awk -F '-*' '{print \$1 \"-\" \$2 \"=\" \$3 \"*\" \$4}'" \
  387. "foo-bar=*\n" \
  388. '' \
  389. 'foo--bar'
  390. # last+1 field should be empty (had a bug where it wasn't)
  391. testing 'awk $NF is empty' \
  392. "awk -F '=+' '{print \$NF}'" \
  393. "\n" \
  394. '' \
  395. 'a=====123='
  396. testing "awk exit N propagates through END's exit" \
  397. "awk 'BEGIN { exit 42 } END { exit }'; echo \$?" \
  398. "42\n" \
  399. '' ''
  400. testing "awk print + redirect" \
  401. "awk 'BEGIN { print \"STDERR %s\" >\"/dev/stderr\" }' 2>&1" \
  402. "STDERR %s\n" \
  403. '' ''
  404. testing "awk \"cmd\" | getline" \
  405. "awk 'BEGIN { \"echo HELLO\" | getline; print }'" \
  406. "HELLO\n" \
  407. '' ''
  408. # printf %% should print one % (had a bug where it didn't)
  409. testing 'awk printf %% prints one %' \
  410. "awk 'BEGIN { printf \"%%\n\" }'" \
  411. "%\n" \
  412. '' ''
  413. exit $FAILCOUNT