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"
16 if [ ! -x "$default_path/scheck" ]; then
17 disabled_cmds
="$disabled_cmds scheck"
21 # - some tests gave an unexpected result
25 # - tests that have not been converted to test-suite format
26 # - tests that are disabled
29 # - tests that failed but are known to fail
36 # defaults to not verbose
44 # verbose(string) - prints string if we are in verbose mode
47 [ "$V" -eq "1" ] && echo " $1"
52 # warning(string) - prints a warning
55 [ "$quiet" -ne 1 ] && echo "warning: $1"
60 # error(string[, die]) - prints an error and exits with value die if given
63 [ "$quiet" -ne 1 ] && echo "error: $1"
64 [ -n "$2" ] && exit $2
70 # get_tag_value(file) - get the 'check-<...>' tags & values
74 check_command
="$default_cmd"
80 check_output_contains
=0
81 check_output_excludes
=0
82 check_output_pattern
=0
84 check_output_returns
=0
90 lines
=$
(grep '^ \* check-[a-z-]*' $1 | \
91 sed -e 's/^ \* \(check-[a-z-]*:*\) *\(.*\)$/\1 \2/')
93 while read tag val
; do
94 #echo "-> tag: '$tag'"
95 #echo "-> val: '$val'"
97 check-name
:) check_name
="$val" ;;
98 check-command
:) check_command
="$val" ;;
99 check-exit-value
:) check_exit_value
="$val" ;;
100 check-timeout
:) [ -z "$val" ] && val
=1
101 check_timeout
="$val" ;;
102 check-known-to-fail
) check_known_to_fail
=1 ;;
103 check-error-ignore
) check_error_ignore
=1 ;;
104 check-output-ignore
) check_output_ignore
=1 ;;
105 check-output-contains
:) check_output_contains
=1 ;;
106 check-output-excludes
:) check_output_excludes
=1 ;;
107 check-output-pattern
) check_output_pattern
=1 ;;
108 check-output-match
) check_output_match
=1 ;;
109 check-output-returns
:) check_output_returns
=1 ;;
110 check-arch-ignore
:) arch
=$
(uname
-m)
111 check_arch_ignore
="$val" ;;
112 check-arch-only
:) arch
=$
(uname
-m)
113 check_arch_only
="$val" ;;
114 check-assert
:) check_assert
="$val" ;;
115 check-cpp-if
:) check_cpp_if
="$val" ;;
117 check-description
:) ;; # ignore
118 check-note
:) ;; # ignore
119 check-warning
:) ;; # ignore
120 check-error-start
) ;; # ignore
121 check-error-end
) ;; # ignore
122 check-output-start
) ;; # ignore
123 check-output-end
) ;; # ignore
124 check-should-pass
) ;; # ignore, unused annotation
125 check-should-fail
) ;; # ignore, unused annotation
126 check-should-warn
) ;; # ignore, unused annotation
127 check-
*) error
"$1: unknown tag '$tag'" 1 ;;
135 # helper for has_(each|none)_patterns()
143 grep "$patt:" "$ifile" | \
144 sed -e "s/^.*$patt: *\(.*\)$/\1/" | \
146 grep -s -q "$val" "$ofile"
147 if [ "$?" $cmp 0 ]; then
148 error
" Pattern '$val' unexpectedly $msg"
157 # has_each_patterns(ifile tag ofile) - does ofile contains some
158 # of the patterns given by ifile's tags?
160 # returns 0 if all present, 1 otherwise
163 has_patterns
"$1" "$2" "$4" -ne "$3"
167 # has_none_patterns(ifile tag ofile) - does ofile contains some
168 # of the patterns given by ifile's tags?
170 # returns 1 if any present, 0 otherwise
173 has_patterns
"$1" "$2" "$4" -eq "$3"
177 # minmax_patterns(ifile tag ofile) - does ofile contains the
178 # the patterns given by ifile's tags
179 # the right number of time?
185 grep "$patt([0-9-]*\(, *\)*[0-9-]*):" "$ifile" | \
186 sed -e "s/^.*$patt(\([0-9]*\)): *\(.*\)/\1 eq \2/" \
187 -e "s/^.*$patt(\([0-9-]*\), *\([0-9-]*\)): *\(.*\)/\1 \2 \3/" | \
188 while read min max pat
; do
189 n
=$
(grep -s "$pat" "$ofile" |
wc -l)
190 if [ "$max" = "eq" ]; then
191 if [ "$n" -ne "$min" ]; then
192 error
" Pattern '$pat' expected $min times but got $n times"
197 if [ "$min" != '-' ]; then
198 if [ "$n" -lt "$min" ]; then
199 error
" Pattern '$pat' expected min $min times but got $n times"
203 if [ "$max" != '-' ]; then
204 if [ "$n" -gt "$max" ]; then
205 error
" Pattern '$pat' expected max $max times but got $n times"
220 grep "$patt" "$ifile" |
sed -e "s/^.*$patt(\(.*\)): *\(.*\)$/\1 \2/" | \
221 while read ins pat
; do
222 grep -s "^ $ins\\.*[0-9]* " "$ofile" |
grep -v -s -q "$pat"
223 if [ "$?" -ne 1 ]; then
224 error
" IR doesn't match '$pat'"
238 grep "$patt:" "$ifile" |
sed -e "s/^.*$patt: *\(.*\)$/\1/" | \
240 grep -s "^ ret\\.[0-9]" "$ofile" |
grep -v -s -q "[ \$]${ret}\$"
241 if [ "$?" -ne 1 ]; then
242 error
" Return doesn't match '$ret'"
251 # arg_file(filename) - checks if filename exists
259 error
"Can't open file $1"
269 echo "$prog_name - a tiny automatic testing script"
270 echo "Usage: $prog_name [option(s)] [command] [arguments]"
273 echo " -a|--abort Abort the tests as soon as one fails."
274 echo " -q|--quiet Be extra quiet while running the tests."
275 echo " --args='...' Add these options to the test command."
278 echo " [file ...] Runs the test suite on the given file(s)."
279 echo " If a directory is given, run only those files."
280 echo " If no file is given, run the whole testsuite."
281 echo " single file Run the test in 'file'."
282 echo " format file [name [cmd]] Help writing a new test case using cmd."
284 echo " [command] help Print usage."
289 disabled_tests
=$
(($disabled_tests + 1))
290 if [ -z "$vquiet" ]; then
296 # do_test(file) - tries to validate a test case
298 # it "parses" file, looking for check-* tags and tries to validate
299 # the test against an expected result
301 # - 0 if the test passed,
303 # - 2 if it is not a "test-suite" test.
304 # - 3 if the test is disabled.
313 # can this test be handled by test-suite ?
314 # (it has to have a check-name key in it)
315 if [ "$check_name" = "" ]; then
316 warning
"$file: test unhandled"
317 unhandled_tests
=$
(($unhandled_tests + 1))
320 test_name
="$check_name"
322 # does the test provide a specific command ?
323 if [ "$check_command" = "" ]; then
324 check_command
="$defaut_command"
327 # check for disabled commands
328 set -- $check_command
330 for i
in $disabled_cmds; do
331 if [ "$i" = "$base_cmd" ] ; then
332 disable
"$test_name" "$file"
336 if [ "$check_arch_ignore" != "" ]; then
337 if echo $arch |
egrep -q -w "$check_arch_ignore"; then
338 disable
"$test_name" "$file"
342 if [ "$check_arch_only" != "" ]; then
343 if ! (echo $arch |
egrep -q -w "$check_arch_only"); then
344 disable
"$test_name" "$file"
348 if [ "$check_assert" != "" ]; then
349 res
=$
(..
/sparse
- 2>&1 >/dev
/null
<<- EOF
350 _Static_assert($check_assert, "$check_assert");
353 if [ "$res" != "" ]; then
354 disable
"$test_name" "$file"
358 if [ "$check_cpp_if" != "" ]; then
359 res
=$
(..
/sparse
-E - 2>/dev
/null
<<- EOF
365 if [ "$res" != "" ]; then
366 disable
"$test_name" "$file"
371 if [ -z "$vquiet" ]; then
372 echo " TEST $test_name ($file)"
375 verbose
"Using command : $(echo "$@
")"
377 # grab the expected exit value
378 expected_exit_value
=$check_exit_value
379 verbose
"Expecting exit value: $expected_exit_value"
381 # do we want a timeout?
383 if [ $check_timeout -ne 0 ]; then
384 pre_cmd
="timeout $check_timeout"
388 # launch the test command and
389 # grab the actual output & exit value
390 eval $pre_cmd $default_path/$base_cmd $default_args "$@" \
391 1> $file.output.got
2> $file.error.got
394 must_fail
=$check_known_to_fail
395 [ $must_fail -eq 1 ] && [ $V -eq 0 ] && quiet
=1
396 known_ko_tests
=$
(($known_ko_tests + $must_fail))
398 for stream
in error output
; do
399 eval ignore
=\
$check_${stream}_ignore
400 [ $ignore -eq 1 ] && continue
402 # grab the expected output
403 sed -n "/check-$stream-start/,/check-$stream-end/p" $file \
404 |
grep -v check-
$stream > "$file".
$stream.expected
406 diff -u "$file".
$stream.expected
"$file".
$stream.got
> "$file".
$stream.
diff
407 if [ "$?" -ne "0" ]; then
408 error
"actual $stream text does not match expected $stream text."
409 error
"see $file.$stream.* for further investigation."
410 [ $quiet -ne 1 ] && cat "$file".
$stream.
diff
415 if [ "$actual_exit_value" -ne "$expected_exit_value" ]; then
416 error
"Actual exit value does not match the expected one."
417 error
"expected $expected_exit_value, got $actual_exit_value."
421 # verify the 'check-output-contains/excludes' tags
422 if [ $check_output_contains -eq 1 ]; then
423 has_each_patterns
"$file" 'check-output-contains' absent
$file.output.got
424 if [ "$?" -ne "0" ]; then
428 if [ $check_output_excludes -eq 1 ]; then
429 has_none_patterns
"$file" 'check-output-excludes' present
$file.output.got
430 if [ "$?" -ne "0" ]; then
434 if [ $check_output_pattern -eq 1 ]; then
435 # verify the 'check-output-pattern(...)' tags
436 minmax_patterns
"$file" 'check-output-pattern' $file.output.got
437 if [ "$?" -ne "0" ]; then
441 if [ $check_output_match -eq 1 ]; then
442 # verify the 'check-output-match($insn): $patt' tags
443 match_patterns
"$file" 'check-output-match' $file.output.got
444 if [ "$?" -ne "0" ]; then
448 if [ $check_output_returns -eq 1 ]; then
449 # verify the 'check-output-return: $value' tags
450 return_patterns
"$file" 'check-output-returns' $file.output.got
451 if [ "$?" -ne "0" ]; then
456 if [ "$must_fail" -eq "1" ]; then
457 if [ "$test_failed" -eq "1" ]; then
458 [ -z "$vquiet" ] && \
459 echo "info: XFAIL: test '$file' is known to fail"
461 echo "error: XPASS: test '$file' is known to fail but succeed!"
464 if [ "$test_failed" -eq "1" ]; then
465 echo "error: FAIL: test '$file' failed"
467 [ "$V" -ne "0" ] && \
468 echo "info: PASS: test '$file' passed"
472 if [ "$test_failed" -ne "$must_fail" ]; then
473 [ $abort -eq 1 ] && exit 1
478 if [ "$test_failed" -eq "1" ]; then
479 ko_tests
=$
(($ko_tests + 1))
481 ok_tests
=$
(($ok_tests + 1))
482 rm -f $file.
{error
,output
}.
{expected
,got
,diff}
489 for i
in $tests_list; do
494 [ $failed -eq 0 ] || OK
=KO
496 # prints some numbers
497 tests_nr
=$
(($ok_tests + $ko_tests))
498 echo "$OK: out of $tests_nr tests, $ok_tests passed, $ko_tests failed"
499 if [ "$known_ko_tests" -ne 0 ]; then
500 echo " $known_ko_tests of them are known to fail"
502 if [ "$unhandled_tests" -ne "0" ]; then
503 echo " $unhandled_tests tests could not be handled by $prog_name"
505 if [ "$disabled_tests" -ne "0" ]; then
506 echo " $disabled_tests tests were disabled"
512 echo "Usage: $prog_name [option(s)] [--]format file [name [cmd]]"
515 echo " -a append the created test to the input file"
516 echo " -f write a test known to fail"
517 echo " -l write a test for linearized code"
518 echo " -r write a test for linearized code returning 1"
519 echo " -p write a test for pre-processing"
520 echo " -s write a test for symbolic checking"
523 echo " file file containing the test case(s)"
524 echo " name name for the test case (defaults to file)"
525 echo " cmd command to be used (defaults to 'sparse \$file')"
529 # do_format([options,] file[, name[, cmd]]) - helps a test writer to format test-suite tags
532 def_cmd
="$default_cmd"
538 while [ $# -gt 0 ] ; do
545 def_cmd
='test-linearize -Wno-decl $file'
548 def_cmd
='test-linearize -Wno-decl $file'
551 def_cmd
='sparse -E $file' ;;
553 def_cmd
='scheck $file' ;;
565 if [ $# -lt 1 -o $# -gt 3 ]; then
570 arg_file
"$1" ||
return 1
574 [ -z "$fname" ] && fname
="$(basename "$1" .c)"
576 [ -z "$fcmd" ] && fcmd
="$def_cmd"
578 cmd
=`eval echo $default_path/$fcmd`
579 $cmd 1> $file.output.got
2> $file.error.got
581 [ $append != 0 ] && exec >> $file
587 if [ "$fcmd" != "$default_cmd" ]; then
588 echo " * check-command: $fcmd"
590 if [ "$fexit_value" -ne "0" ]; then
591 echo " * check-exit-value: $fexit_value"
593 if [ $fail != 0 ]; then
594 echo " * check-known-to-fail"
596 if [ "$ret" != '' ]; then
598 echo ' * check-output-ignore'
599 echo " * check-output-returns: $ret"
600 rm -f "$file.output.got"
602 if [ $linear != 0 ]; then
604 echo ' * check-output-ignore'
605 echo ' * check-output-contains: xyz\\\\.'
606 echo ' * check-output-excludes: \\\\.'
608 for stream
in output error
; do
609 if [ -s "$file.$stream.got" ]; then
611 echo " * check-$stream-start"
612 cat "$file.$stream.got"
613 echo " * check-$stream-end"
620 ## allow flags from environment
621 set -- $SPARSE_TEST_FLAGS "$@"
624 while [ "$#" -gt "0" ]; do
633 default_args
="${1#--args=}";
640 0) echo "$2 passed !";;
641 1) echo "$2 failed !";;
642 2) echo "$2 can't be handled by $prog_name";;
657 tests_list
="$tests_list $1"
660 if [ ! -d "$1" ]; then
664 tests_list
="$tests_list $(find "$1" -name '*.c' | sort)"
670 if [ -z "$tests_list" ]; then
671 tests_list
=`find . -name '*.c' | sed -e 's#^\./\(.*\)#\1#' | sort`