/torture/utils/testutils.sh

# · Shell · 216 lines · 134 code · 28 blank · 54 comment · 25 complexity · 72ac902c3a6c090d03debf5f052b0e5f MD5 · raw file

  1. #!/bin/sh
  2. #
  3. # testutils.sh - Misc utilities for testing the GNU recutils.
  4. #
  5. # Copyright (C) 2010, 2011, 2012 Jose E. Marchesi.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. : ${builddir=.}
  20. . $builddir/config.sh
  21. # Create an input file.
  22. #
  23. # $1 => Name of the file.
  24. # $2 => Contents of the file.
  25. test_declare_input_file ()
  26. {
  27. # Check parameters.
  28. if test "$#" -ne "2"
  29. then
  30. echo "error: testutils: invalid parameters to test_declare_input_file"
  31. exit 1
  32. fi
  33. filename="${test_suite}-$1.in"
  34. contents="$2"
  35. # Create the input file.
  36. printf "%s" "$contents" > $filename
  37. # Add the file to the global list of temp files.
  38. test_tmpfiles="$test_tmpfiles $filename"
  39. }
  40. # Generate an input file.
  41. #
  42. # $1 => Name of the file.
  43. # $2 => Number of records.
  44. # $3 => Number of fields in each record.
  45. test_gen_input_file ()
  46. {
  47. # Check parameters.
  48. if test "$#" -ne "3"
  49. then
  50. echo "error: testutils: invalid parameters to test_gen_input_file"
  51. exit 1
  52. fi
  53. filename="$1.in"
  54. numrecords="$2"
  55. numfields="$3"
  56. # Create the contents of the file if it does not already exist.
  57. # This is important, since the generated test files will usually
  58. # be very big.
  59. echo -n " Creating input file $filename ..."
  60. if test -f $filename; then
  61. echo " skipped"
  62. else
  63. recno=0
  64. while test "$recno" -lt "$numrecords"
  65. do
  66. fieldno=0
  67. while test "$fieldno" -lt "$numfields"
  68. do
  69. echo "field$fieldno: value$recno$fieldno" >> $filename
  70. fieldno=`expr $fieldno + 1`
  71. done
  72. echo "" >> $filename
  73. recno=`expr $recno + 1`
  74. done
  75. echo " done"
  76. fi
  77. }
  78. # Initialize the testing environment.
  79. #
  80. # $1 => Name of the test suite.
  81. test_init ()
  82. {
  83. if test "$#" -ne "1"; then
  84. echo "error: testutils: invalid parameters to test_init"
  85. exit 1
  86. fi
  87. test_suite=$1
  88. test_result="0"
  89. trap 'rm -fr $test_tmpfiles' EXIT 1 2 3 15
  90. echo "Running $test_suite test(s): "
  91. }
  92. # Delete temporary files and other cleanup.
  93. #
  94. # No parameters.
  95. test_cleanup ()
  96. {
  97. rm -fr $test_tmpfiles
  98. return $test_result
  99. }
  100. # Test a recutil.
  101. #
  102. # $1 => Test name.
  103. # $2 => Expected result: ok, xfail, perf
  104. # $3 => Utility to test.
  105. # $4 => Parameters.
  106. # $5 => Input file to use.
  107. # $6 => Expected result. Only for 'ok' tests.
  108. test_tool ()
  109. {
  110. # Check parameters.
  111. if test "$#" -lt "2" || \
  112. { test "$2" != "ok" && test "$2" != "xfail" && test "$2" != "perf"; } || \
  113. { test "$2" = "ok" && test "$#" -ne "6"; } || \
  114. { test "$2" = "perf" && test "$#" -ne "5"; } || \
  115. { test "$2" = "xfail" && test "$#" -ne "5"; }
  116. then
  117. echo "error: testutils: invalid parameters to test_tool"
  118. exit 1
  119. fi
  120. printf " %s " $1
  121. status=$2
  122. utility=$3$EXEEXT
  123. parameters=$4
  124. input_file="${test_suite}-$5.in"
  125. ok_file="$1.ok"
  126. output_file="$1.out"
  127. error_file="$1.err"
  128. timing_file="$1.tim"
  129. postprocessed_output_file="$1.put"
  130. expected=$6
  131. test_tmpfiles="$test_tmpfiles $output_file $ok_file"
  132. if test "$status" = "perf"; then
  133. test_tmpfiles="$test_tmpfiles $timing_file"
  134. fi
  135. # Run the tool.
  136. if test "$status" = "perf"; then
  137. eval "cat $input_file | time -f %E -o $timing_file \
  138. $utility $parameters > $output_file 2> $error_file"
  139. else
  140. eval "cat $input_file | $utility $parameters > $output_file 2> $error_file"
  141. fi
  142. res=$?
  143. if test "$status" = "ok" || test "$status" = "perf"
  144. then
  145. if test "$res" -ne "0"
  146. then
  147. printf "%s (see %s)\n" "error" "$error_file"
  148. else
  149. if test "$status" = "ok"; then
  150. # Check for the result in output_file.
  151. LC_ALL=C tr -d '\r' < $output_file > $postprocessed_output_file
  152. printf "%s" "$expected" > $ok_file
  153. cmp $ok_file $postprocessed_output_file > /dev/null 2>&1
  154. res=$?
  155. if test "$res" -eq "0"
  156. then
  157. echo $status
  158. else
  159. printf "%s (see %s)\n" "fail" "$1.diff"
  160. diff $ok_file $postprocessed_output_file > $1.diff
  161. fi
  162. rm $error_file $postprocessed_output_file
  163. else
  164. elapsed_time=`cat $timing_file`
  165. echo $elapsed_time
  166. rm $error_file
  167. fi
  168. fi
  169. fi
  170. if test "$status" = "xfail"
  171. then
  172. if test "$res" -eq "0"
  173. then
  174. echo "error (expected failure)"
  175. res=1
  176. else
  177. echo $status
  178. # Don't accumulate any error.
  179. res=0
  180. fi
  181. rm $error_file
  182. fi
  183. # Accumulate the error.
  184. test_result=`expr $test_result + $res`
  185. }
  186. PATH=$srcdir:$PATH
  187. export PATH
  188. # End of testutils.sh