8 default_cmd
="sparse \$file"
9 default_args
="$SPARSE_TEST_ARGS"
11 prog_name
=`basename $0`
13 if [ ! -x "$default_path/sparse-llvm" ]; then
14 disabled_cmds
="sparsec sparsei sparse-llvm sparse-llvm-dis"
18 # - some tests gave an unexpected result
22 # - tests that have not been converted to test-suite format
23 # - tests that are disabled
26 # - tests that failed but are known to fail
33 # defaults to not verbose
41 # verbose(string) - prints string if we are in verbose mode
44 [ "$V" -eq "1" ] && echo " $1"
49 # warning(string) - prints a warning
52 [ "$quiet" -ne 1 ] && echo "warning: $1"
57 # error(string[, die]) - prints an error and exits with value die if given
60 [ "$quiet" -ne 1 ] && echo "error: $1"
61 [ -n "$2" ] && exit $2
67 # get_tag_value(file) - get the 'check-<...>' tags & values
71 check_command
="$default_cmd"
77 check_output_contains
=0
78 check_output_excludes
=0
79 check_output_pattern
=0
81 check_output_returns
=0
87 lines
=$
(grep '^ \* check-[a-z-]*' $1 | \
88 sed -e 's/^ \* \(check-[a-z-]*:*\) *\(.*\)$/\1 \2/')
90 while read tag val
; do
91 #echo "-> tag: '$tag'"
92 #echo "-> val: '$val'"
94 check-name
:) check_name
="$val" ;;
95 check-command
:) check_command
="$val" ;;
96 check-exit-value
:) check_exit_value
="$val" ;;
97 check-timeout
:) [ -z "$val" ] && val
=1
98 check_timeout
="$val" ;;
99 check-known-to-fail
) check_known_to_fail
=1 ;;
100 check-error-ignore
) check_error_ignore
=1 ;;
101 check-output-ignore
) check_output_ignore
=1 ;;
102 check-output-contains
:) check_output_contains
=1 ;;
103 check-output-excludes
:) check_output_excludes
=1 ;;
104 check-output-pattern
) check_output_pattern
=1 ;;
105 check-output-match
) check_output_match
=1 ;;
106 check-output-returns
:) check_output_returns
=1 ;;
107 check-arch-ignore
:) arch
=$
(uname
-m)
108 check_arch_ignore
="$val" ;;
109 check-arch-only
:) arch
=$
(uname
-m)
110 check_arch_only
="$val" ;;
111 check-assert
:) check_assert
="$val" ;;
112 check-cpp-if
:) check_cpp_if
="$val" ;;
114 check-description
:) ;; # ignore
115 check-note
:) ;; # ignore
116 check-warning
:) ;; # ignore
117 check-error-start
) ;; # ignore
118 check-error-end
) ;; # ignore
119 check-output-start
) ;; # ignore
120 check-output-end
) ;; # ignore
121 check-should-pass
) ;; # ignore, unused annotation
122 check-should-fail
) ;; # ignore, unused annotation
123 check-should-warn
) ;; # ignore, unused annotation
124 check-
*) error
"$1: unknown tag '$tag'" 1 ;;
132 # helper for has_(each|none)_patterns()
140 grep "$patt:" "$ifile" | \
141 sed -e "s/^.*$patt: *\(.*\)$/\1/" | \
143 grep -s -q "$val" "$ofile"
144 if [ "$?" $cmp 0 ]; then
145 error
" Pattern '$val' unexpectedly $msg"
154 # has_each_patterns(ifile tag ofile) - does ofile contains some
155 # of the patterns given by ifile's tags?
157 # returns 0 if all present, 1 otherwise
160 has_patterns
"$1" "$2" "$4" -ne "$3"
164 # has_none_patterns(ifile tag ofile) - does ofile contains some
165 # of the patterns given by ifile's tags?
167 # returns 1 if any present, 0 otherwise
170 has_patterns
"$1" "$2" "$4" -eq "$3"
174 # minmax_patterns(ifile tag ofile) - does ofile contains the
175 # the patterns given by ifile's tags
176 # the right number of time?
182 grep "$patt([0-9-]*\(, *\)*[0-9-]*):" "$ifile" | \
183 sed -e "s/^.*$patt(\([0-9]*\)): *\(.*\)/\1 eq \2/" \
184 -e "s/^.*$patt(\([0-9-]*\), *\([0-9-]*\)): *\(.*\)/\1 \2 \3/" | \
185 while read min max pat
; do
186 n
=$
(grep -s "$pat" "$ofile" |
wc -l)
187 if [ "$max" = "eq" ]; then
188 if [ "$n" -ne "$min" ]; then
189 error
" Pattern '$pat' expected $min times but got $n times"
194 if [ "$min" != '-' ]; then
195 if [ "$n" -lt "$min" ]; then
196 error
" Pattern '$pat' expected min $min times but got $n times"
200 if [ "$max" != '-' ]; then
201 if [ "$n" -gt "$max" ]; then
202 error
" Pattern '$pat' expected max $max times but got $n times"
217 grep "$patt" "$ifile" |
sed -e "s/^.*$patt(\(.*\)): *\(.*\)$/\1 \2/" | \
218 while read ins pat
; do
219 grep -s "^ $ins\\.*[0-9]* " "$ofile" |
grep -v -s -q "$pat"
220 if [ "$?" -ne 1 ]; then
221 error
" IR doesn't match '$pat'"
235 grep "$patt:" "$ifile" |
sed -e "s/^.*$patt: *\(.*\)$/\1/" | \
237 grep -s "^ ret\\.[0-9]" "$ofile" |
grep -v -s -q "[ \$]${ret}\$"
238 if [ "$?" -ne 1 ]; then
239 error
" Return doesn't match '$ret'"
248 # arg_file(filename) - checks if filename exists
256 error
"Can't open file $1"
266 echo "$prog_name - a tiny automatic testing script"
267 echo "Usage: $prog_name [option(s)] [command] [arguments]"
270 echo " -a|--abort Abort the tests as soon as one fails."
271 echo " -q|--quiet Be extra quiet while running the tests."
272 echo " --args='...' Add these options to the test command."
275 echo " [file ...] Runs the test suite on the given file(s)."
276 echo " If a directory is given, run only those files."
277 echo " If no file is given, run the whole testsuite."
278 echo " single file Run the test in 'file'."
279 echo " format file [name [cmd]] Help writing a new test case using cmd."
281 echo " [command] help Print usage."
286 disabled_tests
=$
(($disabled_tests + 1))
287 if [ -z "$vquiet" ]; then
293 # do_test(file) - tries to validate a test case
295 # it "parses" file, looking for check-* tags and tries to validate
296 # the test against an expected result
298 # - 0 if the test passed,
300 # - 2 if it is not a "test-suite" test.
301 # - 3 if the test is disabled.
310 # can this test be handled by test-suite ?
311 # (it has to have a check-name key in it)
312 if [ "$check_name" = "" ]; then
313 warning
"$file: test unhandled"
314 unhandled_tests
=$
(($unhandled_tests + 1))
317 test_name
="$check_name"
319 # does the test provide a specific command ?
320 if [ "$check_command" = "" ]; then
321 check_command
="$defaut_command"
324 # check for disabled commands
325 set -- $check_command
327 for i
in $disabled_cmds; do
328 if [ "$i" = "$base_cmd" ] ; then
329 disable
"$test_name" "$file"
333 if [ "$check_arch_ignore" != "" ]; then
334 if echo $arch |
egrep -q -w "$check_arch_ignore"; then
335 disable
"$test_name" "$file"
339 if [ "$check_arch_only" != "" ]; then
340 if ! (echo $arch |
egrep -q -w "$check_arch_only"); then
341 disable
"$test_name" "$file"
345 if [ "$check_assert" != "" ]; then
346 res
=$
(..
/sparse
- 2>&1 >/dev
/null
<<- EOF
347 _Static_assert($check_assert, "$check_assert");
350 if [ "$res" != "" ]; then
351 disable
"$test_name" "$file"
355 if [ "$check_cpp_if" != "" ]; then
356 res
=$
(..
/sparse
-E - 2>/dev
/null
<<- EOF
362 if [ "$res" != "" ]; then
363 disable
"$test_name" "$file"
368 if [ -z "$vquiet" ]; then
369 echo " TEST $test_name ($file)"
372 verbose
"Using command : $(echo "$@
")"
374 # grab the expected exit value
375 expected_exit_value
=$check_exit_value
376 verbose
"Expecting exit value: $expected_exit_value"
378 # do we want a timeout?
380 if [ $check_timeout -ne 0 ]; then
381 pre_cmd
="timeout $check_timeout"
385 # launch the test command and
386 # grab the actual output & exit value
387 eval $pre_cmd $default_path/$base_cmd $default_args "$@" \
388 1> $file.output.got
2> $file.error.got
391 must_fail
=$check_known_to_fail
392 [ $must_fail -eq 1 ] && [ $V -eq 0 ] && quiet
=1
393 known_ko_tests
=$
(($known_ko_tests + $must_fail))
395 for stream
in error output
; do
396 eval ignore
=\
$check_${stream}_ignore
397 [ $ignore -eq 1 ] && continue
399 # grab the expected output
400 sed -n "/check-$stream-start/,/check-$stream-end/p" $file \
401 |
grep -v check-
$stream > "$file".
$stream.expected
403 diff -u "$file".
$stream.expected
"$file".
$stream.got
> "$file".
$stream.
diff
404 if [ "$?" -ne "0" ]; then
405 error
"actual $stream text does not match expected $stream text."
406 error
"see $file.$stream.* for further investigation."
407 [ $quiet -ne 1 ] && cat "$file".
$stream.
diff
412 if [ "$actual_exit_value" -ne "$expected_exit_value" ]; then
413 error
"Actual exit value does not match the expected one."
414 error
"expected $expected_exit_value, got $actual_exit_value."
418 # verify the 'check-output-contains/excludes' tags
419 if [ $check_output_contains -eq 1 ]; then
420 has_each_patterns
"$file" 'check-output-contains' absent
$file.output.got
421 if [ "$?" -ne "0" ]; then
425 if [ $check_output_excludes -eq 1 ]; then
426 has_none_patterns
"$file" 'check-output-excludes' present
$file.output.got
427 if [ "$?" -ne "0" ]; then
431 if [ $check_output_pattern -eq 1 ]; then
432 # verify the 'check-output-pattern(...)' tags
433 minmax_patterns
"$file" 'check-output-pattern' $file.output.got
434 if [ "$?" -ne "0" ]; then
438 if [ $check_output_match -eq 1 ]; then
439 # verify the 'check-output-match($insn): $patt' tags
440 match_patterns
"$file" 'check-output-match' $file.output.got
441 if [ "$?" -ne "0" ]; then
445 if [ $check_output_returns -eq 1 ]; then
446 # verify the 'check-output-return: $value' tags
447 return_patterns
"$file" 'check-output-returns' $file.output.got
448 if [ "$?" -ne "0" ]; then
453 if [ "$must_fail" -eq "1" ]; then
454 if [ "$test_failed" -eq "1" ]; then
455 [ -z "$vquiet" ] && \
456 echo "info: XFAIL: test '$file' is known to fail"
458 echo "error: XPASS: test '$file' is known to fail but succeed!"
461 if [ "$test_failed" -eq "1" ]; then
462 echo "error: FAIL: test '$file' failed"
464 [ "$V" -ne "0" ] && \
465 echo "info: PASS: test '$file' passed"
469 if [ "$test_failed" -ne "$must_fail" ]; then
470 [ $abort -eq 1 ] && exit 1
475 if [ "$test_failed" -eq "1" ]; then
476 ko_tests
=$
(($ko_tests + 1))
478 ok_tests
=$
(($ok_tests + 1))
479 rm -f $file.
{error
,output
}.
{expected
,got
,diff}
486 for i
in $tests_list; do
491 [ $failed -eq 0 ] || OK
=KO
493 # prints some numbers
494 tests_nr
=$
(($ok_tests + $ko_tests))
495 echo "$OK: out of $tests_nr tests, $ok_tests passed, $ko_tests failed"
496 if [ "$known_ko_tests" -ne 0 ]; then
497 echo " $known_ko_tests of them are known to fail"
499 if [ "$unhandled_tests" -ne "0" ]; then
500 echo " $unhandled_tests tests could not be handled by $prog_name"
502 if [ "$disabled_tests" -ne "0" ]; then
503 echo " $disabled_tests tests were disabled"
509 echo "Usage: $prog_name [option(s)] [--]format file [name [cmd]]"
512 echo " -a append the created test to the input file"
513 echo " -f write a test known to fail"
514 echo " -l write a test for linearized code"
515 echo " -p write a test for pre-processing"
518 echo " file file containing the test case(s)"
519 echo " name name for the test case (defaults to file)"
520 echo " cmd command to be used (defaults to 'sparse \$file')"
524 # do_format([options,] file[, name[, cmd]]) - helps a test writer to format test-suite tags
527 def_cmd
="$default_cmd"
532 while [ $# -gt 0 ] ; do
539 def_cmd
='test-linearize -Wno-decl $file'
542 def_cmd
='sparse -E $file' ;;
554 if [ $# -lt 1 -o $# -gt 3 ]; then
559 arg_file
"$1" ||
return 1
563 [ -z "$fname" ] && fname
="$(basename "$1" .c)"
565 [ -z "$fcmd" ] && fcmd
="$def_cmd"
567 cmd
=`eval echo $default_path/$fcmd`
568 $cmd 1> $file.output.got
2> $file.error.got
570 [ $append != 0 ] && exec >> $file
576 if [ "$fcmd" != "$default_cmd" ]; then
577 echo " * check-command: $fcmd"
579 if [ "$fexit_value" -ne "0" ]; then
580 echo " * check-exit-value: $fexit_value"
582 if [ $fail != 0 ]; then
583 echo " * check-known-to-fail"
585 if [ $linear != 0 ]; then
587 echo ' * check-output-ignore'
588 echo ' * check-output-contains: xyz\\\\.'
589 echo ' * check-output-excludes: \\\\.'
591 for stream
in output error
; do
592 if [ -s "$file.$stream.got" ]; then
594 echo " * check-$stream-start"
595 cat "$file.$stream.got"
596 echo " * check-$stream-end"
603 ## allow flags from environment
604 set -- $SPARSE_TEST_FLAGS "$@"
607 while [ "$#" -gt "0" ]; do
616 default_args
="${1#--args=}";
623 0) echo "$2 passed !";;
624 1) echo "$2 failed !";;
625 2) echo "$2 can't be handled by $prog_name";;
640 tests_list
="$tests_list $1"
643 if [ ! -d "$1" ]; then
647 tests_list
="$tests_list $(find "$1" -name '*.c' | sort)"
653 if [ -z "$tests_list" ]; then
654 tests_list
=`find . -name '*.c' | sed -e 's#^\./\(.*\)#\1#' | sort`