awk.tests 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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) }'" "4.29497e+09\n" "" "\n"
  22. optional DESKTOP
  23. testing "awk hex const 1" "awk '{ print or(0xffffffff,1) }'" "4.29497e+09\n" "" "\n"
  24. testing "awk hex const 2" "awk '{ print or(0x80000000,1) }'" "2.14748e+09\n" "" "\n"
  25. testing "awk oct const" "awk '{ print or(01234,1) }'" "669\n" "" "\n"
  26. SKIP=
  27. # check that "hex/oct integer" heuristic doesn't kick in on 00NN.NNN
  28. testing "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. # long field seps requiring regex
  33. testing "awk long field sep" "awk -F-- '{ print NF, length(\$NF), \$NF }'" \
  34. "2 0 \n3 0 \n4 0 \n5 0 \n" \
  35. "" \
  36. "a--\na--b--\na--b--c--\na--b--c--d--"
  37. testing "awk -F handles escapes" "awk -F'\\x21' '{print \$1}'" \
  38. "a\n" \
  39. "" \
  40. "a!b\n"
  41. # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
  42. # but gawk 3.1.5 does not bail out on it.
  43. testing "awk gsub falls back to non-extended-regex" \
  44. "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
  45. optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
  46. test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
  47. testing "awk 'gcc build bug'" \
  48. "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \
  49. "f842e256461a5ab1ec60b58d16f1114f -\n" \
  50. "" ""
  51. rm -rf awk_t1_* 2>/dev/null
  52. SKIP=
  53. Q='":"'
  54. testing "awk NF in BEGIN" \
  55. "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
  56. ":0::::\n" \
  57. "" ""
  58. prg='
  59. function b(tmp) {
  60. tmp = 0;
  61. print "" tmp; #this line causes the bug
  62. return tmp;
  63. }
  64. function c(tmpc) {
  65. tmpc = b(); return tmpc;
  66. }
  67. BEGIN {
  68. print (c() ? "string" : "number");
  69. }'
  70. testing "awk string cast (bug 725)" \
  71. "awk '$prg'" \
  72. "0\nnumber\n" \
  73. "" ""
  74. testing "awk handles whitespace before array subscript" \
  75. "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
  76. # GNU awk 3.1.5's "print ERRNO" prints "No such file or directory" instead of "2",
  77. # do we need to emulate that as well?
  78. testing "awk handles non-existing file correctly" \
  79. "awk 'BEGIN { getline line <\"doesnt_exist\"; print ERRNO; ERRNO=0; close(\"doesnt_exist\"); print ERRNO; print \"Ok\" }'" \
  80. "2\n0\nOk\n" "" ""
  81. prg='
  82. BEGIN {
  83. u["a"]=1
  84. u["b"]=1
  85. u["c"]=1
  86. v["d"]=1
  87. v["e"]=1
  88. v["f"]=1
  89. for (l in u) {
  90. print "outer1", l;
  91. for (l in v) {
  92. print " inner", l;
  93. }
  94. print "outer2", l;
  95. }
  96. print "end", l;
  97. l="a"
  98. exit;
  99. }'
  100. testing "awk nested loops with the same variable" \
  101. "awk '$prg'" \
  102. "\
  103. outer1 a
  104. inner d
  105. inner e
  106. inner f
  107. outer2 f
  108. outer1 b
  109. inner d
  110. inner e
  111. inner f
  112. outer2 f
  113. outer1 c
  114. inner d
  115. inner e
  116. inner f
  117. outer2 f
  118. end f
  119. " \
  120. "" ""
  121. prg='
  122. BEGIN {
  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
  142. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
  143. "awk '$prg'" \
  144. "\
  145. outer1 a
  146. inner d
  147. outer2 d
  148. outer1 b
  149. inner d
  150. outer2 d
  151. outer1 c
  152. inner d
  153. outer2 d
  154. end d
  155. " \
  156. "" ""
  157. prg='
  158. function f() {
  159. for (l in v) {
  160. print " inner", l;
  161. return;
  162. }
  163. }
  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. f();
  174. print "outer2", l;
  175. }
  176. print "end", l;
  177. l="a"
  178. exit;
  179. }'
  180. # It's not just buggy, it enters infinite loop. Thus disabled
  181. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
  182. "awk '$prg'" \
  183. "\
  184. outer1 a
  185. inner d
  186. outer2 d
  187. outer1 b
  188. inner d
  189. outer2 d
  190. outer1 c
  191. inner d
  192. outer2 d
  193. end d
  194. " \
  195. "" ""
  196. testing "awk handles empty ()" \
  197. "awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
  198. testing "awk FS assignment" "awk '{FS=\":\"; print \$1}'" \
  199. "a:b\ne\n" \
  200. "" \
  201. "a:b c:d\ne:f g:h"
  202. # testing "description" "command" "result" "infile" "stdin"
  203. exit $FAILCOUNT