awk.tests 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/bin/sh
  2. # Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
  3. # Licensed under GPL v2, see file LICENSE for details.
  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. # long field seps requiring regex
  22. testing "awk long field sep" "awk -F-- '{ print NF, length(\$NF), \$NF }'" \
  23. "2 0 \n3 0 \n4 0 \n5 0 \n" \
  24. "" \
  25. "a--\na--b--\na--b--c--\na--b--c--d--"
  26. # '@(samp|code|file)\{' is an invalid extended regex (unmatched '{'),
  27. # but gawk 3.1.5 does not bail out on it.
  28. testing "awk gsub falls back to non-extended-regex" \
  29. "awk 'gsub(\"@(samp|code|file)\{\",\"\");'; echo \$?" "0\n" "" "Hi\n"
  30. optional TAR BUNZIP2 FEATURE_SEAMLESS_BZ2
  31. test x"$SKIP" != x"1" && tar xjf awk_t1.tar.bz2
  32. testing "awk 'gcc build bug'" \
  33. "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \
  34. "f842e256461a5ab1ec60b58d16f1114f -\n" \
  35. "" ""
  36. rm -rf awk_t1_* 2>/dev/null
  37. SKIP=
  38. Q='":"'
  39. testing "awk NF in BEGIN" \
  40. "awk 'BEGIN { print ${Q} NF ${Q} \$0 ${Q} \$1 ${Q} \$2 ${Q} }'" \
  41. ":0::::\n" \
  42. "" ""
  43. prg='
  44. function b(tmp) {
  45. tmp = 0;
  46. print "" tmp; #this line causes the bug
  47. return tmp;
  48. }
  49. function c(tmpc) {
  50. tmpc = b(); return tmpc;
  51. }
  52. BEGIN {
  53. print (c() ? "string" : "number");
  54. }'
  55. testing "awk string cast (bug 725)" \
  56. "awk '$prg'" \
  57. "0\nnumber\n" \
  58. "" ""
  59. testing "awk handles whitespace before array subscript" \
  60. "awk 'BEGIN { arr [3] = 1; print arr [3] }'" "1\n" "" ""
  61. prg='
  62. BEGIN {
  63. u["a"]=1
  64. u["b"]=1
  65. u["c"]=1
  66. v["d"]=1
  67. v["e"]=1
  68. v["f"]=1
  69. for (l in u) {
  70. print "outer1", l;
  71. for (l in v) {
  72. print " inner", l;
  73. }
  74. print "outer2", l;
  75. }
  76. print "end", l;
  77. l="a"
  78. exit;
  79. }'
  80. testing "awk nested loops with the same variable" \
  81. "awk '$prg'" \
  82. "\
  83. outer1 a
  84. inner d
  85. inner e
  86. inner f
  87. outer2 f
  88. outer1 b
  89. inner d
  90. inner e
  91. inner f
  92. outer2 f
  93. outer1 c
  94. inner d
  95. inner e
  96. inner f
  97. outer2 f
  98. end f
  99. " \
  100. "" ""
  101. prg='
  102. BEGIN {
  103. u["a"]=1
  104. u["b"]=1
  105. u["c"]=1
  106. v["d"]=1
  107. v["e"]=1
  108. v["f"]=1
  109. for (l in u) {
  110. print "outer1", l;
  111. for (l in v) {
  112. print " inner", l;
  113. break;
  114. }
  115. print "outer2", l;
  116. }
  117. print "end", l;
  118. l="a"
  119. exit;
  120. }'
  121. # It's not just buggy, it enters infinite loop. Thus disabled
  122. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and break" \
  123. "awk '$prg'" \
  124. "\
  125. outer1 a
  126. inner d
  127. outer2 d
  128. outer1 b
  129. inner d
  130. outer2 d
  131. outer1 c
  132. inner d
  133. outer2 d
  134. end d
  135. " \
  136. "" ""
  137. prg='
  138. function f() {
  139. for (l in v) {
  140. print " inner", l;
  141. return;
  142. }
  143. }
  144. BEGIN {
  145. u["a"]=1
  146. u["b"]=1
  147. u["c"]=1
  148. v["d"]=1
  149. v["e"]=1
  150. v["f"]=1
  151. for (l in u) {
  152. print "outer1", l;
  153. f();
  154. print "outer2", l;
  155. }
  156. print "end", l;
  157. l="a"
  158. exit;
  159. }'
  160. # It's not just buggy, it enters infinite loop. Thus disabled
  161. false && test x"$SKIP_KNOWN_BUGS" = x"" && testing "awk nested loops with the same variable and return" \
  162. "awk '$prg'" \
  163. "\
  164. outer1 a
  165. inner d
  166. outer2 d
  167. outer1 b
  168. inner d
  169. outer2 d
  170. outer1 c
  171. inner d
  172. outer2 d
  173. end d
  174. " \
  175. "" ""
  176. exit $FAILCOUNT