awk.tests 4.5 KB

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