3
0

awk.tests 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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" "awk -F-- '{ print NF, length(\$NF), \$NF }'" \
  76. "2 0 \n3 0 \n4 0 \n5 0 \n" \
  77. "" \
  78. "a--\na--b--\na--b--c--\na--b--c--d--"
  79. testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \
  80. "a\n" \
  81. "" \
  82. "a!b\n"
  83. # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
  84. # but gawk 3.1.5 does not bail out on it.
  85. testing "awk gsub falls back to non-extended-regex" \
  86. "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
  87. optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
  88. test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
  89. testing "awk 'gcc build bug'" \
  90. "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \
  91. "f842e256461a5ab1ec60b58d16f1114f -\n" \
  92. "" ""
  93. rm -rf awk_t1_* 2>/dev/null
  94. SKIP=
  95. Q='":"'
  96. testing "awk NF in BEGIN" \
  97. "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
  98. ":0::::\n" \
  99. "" ""
  100. prg='
  101. function b(tmp) {
  102. tmp = 0;
  103. print "" tmp; #this line causes the bug
  104. return tmp;
  105. }
  106. function c(tmpc) {
  107. tmpc = b(); return tmpc;
  108. }
  109. BEGIN {
  110. print (c() ? "string" : "number");
  111. }'
  112. testing "awk string cast (bug 725)" \
  113. "awk '$prg'" \
  114. "0\nnumber\n" \
  115. "" ""
  116. testing "awk handles whitespace before array subscript" \
  117. "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
  118. # GNU awk 3.1.5's "print ERRNO" prints "No such file or directory" instead of "2",
  119. # do we need to emulate that as well?
  120. testing "awk handles non-existing file correctly" \
  121. "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \
  122. "2\n0\nOk\n" "" ""
  123. prg='
  124. BEGIN {
  125. u["a"]=1
  126. u["b"]=1
  127. u["c"]=1
  128. v["d"]=1
  129. v["e"]=1
  130. v["f"]=1
  131. for (l in u) {
  132. print "outer1", l;
  133. for (l in v) {
  134. print " inner", l;
  135. }
  136. print "outer2", l;
  137. }
  138. print "end", l;
  139. l="a"
  140. exit;
  141. }'
  142. testing "awk nested loops with the same variable" \
  143. "awk '$prg'" \
  144. "\
  145. outer1 a
  146. inner d
  147. inner e
  148. inner f
  149. outer2 f
  150. outer1 b
  151. inner d
  152. inner e
  153. inner f
  154. outer2 f
  155. outer1 c
  156. inner d
  157. inner e
  158. inner f
  159. outer2 f
  160. end f
  161. " \
  162. "" ""
  163. prg='
  164. BEGIN {
  165. u["a"]=1
  166. u["b"]=1
  167. u["c"]=1
  168. v["d"]=1
  169. v["e"]=1
  170. v["f"]=1
  171. for (l in u) {
  172. print "outer1", l;
  173. for (l in v) {
  174. print " inner", l;
  175. break;
  176. }
  177. print "outer2", l;
  178. }
  179. print "end", l;
  180. l="a"
  181. exit;
  182. }'
  183. # It's not just buggy, it enters infinite loop. Thus disabled
  184. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
  185. "awk '$prg'" \
  186. "\
  187. outer1 a
  188. inner d
  189. outer2 d
  190. outer1 b
  191. inner d
  192. outer2 d
  193. outer1 c
  194. inner d
  195. outer2 d
  196. end d
  197. " \
  198. "" ""
  199. prg='
  200. function f() {
  201. for (l in v) {
  202. print " inner", l;
  203. return;
  204. }
  205. }
  206. BEGIN {
  207. u["a"]=1
  208. u["b"]=1
  209. u["c"]=1
  210. v["d"]=1
  211. v["e"]=1
  212. v["f"]=1
  213. for (l in u) {
  214. print "outer1", l;
  215. f();
  216. print "outer2", l;
  217. }
  218. print "end", l;
  219. l="a"
  220. exit;
  221. }'
  222. # It's not just buggy, it enters infinite loop. Thus disabled
  223. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
  224. "awk '$prg'" \
  225. "\
  226. outer1 a
  227. inner d
  228. outer2 d
  229. outer1 b
  230. inner d
  231. outer2 d
  232. outer1 c
  233. inner d
  234. outer2 d
  235. end d
  236. " \
  237. "" ""
  238. testing "awk handles empty ()" \
  239. "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
  240. testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \
  241. "a:b\ne\n" \
  242. "" \
  243. "a:b c:d\ne:f g:h"
  244. optional FEATURE_AWK_LIBM
  245. testing "awk large integer" \
  246. "awk 'BEGIN{n=(2^31)-1; print n, int(n), n%1, ++n, int(n), n%1}'" \
  247. "2147483647 2147483647 0 2147483648 2147483648 0\n" \
  248. "" ""
  249. SKIP=
  250. testing "awk length(array)" \
  251. "awk 'BEGIN{ A[1]=2; A[\"qwe\"]=\"asd\"; print length(A)}'" \
  252. "2\n" \
  253. "" ""
  254. testing "awk -f and ARGC" \
  255. "awk -f - input" \
  256. "re\n2\n" \
  257. "do re mi\n" \
  258. '{print $2; print ARGC;}' \
  259. optional FEATURE_AWK_GNU_EXTENSIONS
  260. testing "awk -e and ARGC" \
  261. "awk -e '{print \$2; print ARGC;}' input" \
  262. "re\n2\n" \
  263. "do re mi\n" \
  264. ""
  265. SKIP=
  266. # testing "description" "command" "result" "infile" "stdin"
  267. exit $FAILCOUNT