]> git.draconx.ca Git - cdecl99.git/blob - tests/crossparse-c-random.sh
Batch up crossparse tests to improve performance.
[cdecl99.git] / tests / crossparse-c-random.sh
1 #!/bin/sh
2 #
3 # Copyright © 2012 Nick Bowler
4 #
5 # Randomized tests that libcdecl can parse its own output.
6 #
7 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
8 # This is free software: you are free to do what the fuck you want to.
9 # There is NO WARRANTY, to the extent permitted by law.
10
11 : "${RANDOMSEED=$RANDOM}" "${TESTITER=500}"
12
13 randomdecl=test/randomdecl$EXEEXT
14 crossparse=test/crossparse$EXEEXT
15 test -x $randomdecl || exit 77
16
17 # Slow case: run tests one at a time to determine exactly which one failed.
18 first_failed() {
19         ff_count=0
20
21         while test $# -gt 0
22         do
23                 $crossparse "$1" 2>/dev/null || break
24                 ff_count=`expr $ff_count + 1`
25                 shift
26         done
27
28         echo "$ff_count"
29 }
30
31 proc() {
32         result=pass
33         count=0
34
35         set x
36         while read decl
37         do
38                 set "$@" "$decl"
39
40                 # Accumulate tests in $@ and run them in batches to avoid
41                 # significant startup costs.
42                 if test $# -gt 25; then
43                         shift
44                         $crossparse "$@" || { result=fail
45                                 tmp_count=`first_failed "$@"`
46                                 count=`expr $count + $tmp_count`
47                                 break
48                         }
49                         count=`expr $count + $#`
50                         set x
51                 fi
52         done
53
54         shift
55         if test $# -gt 0 && test x"$result" = x"pass"; then
56                 tmp_count=$#
57                 $crossparse "$@" || { result=fail
58                         tmp_count=`first_failed "$@"`
59                 }
60                 count=`expr $count + $tmp_count`
61         fi
62
63         echo "result=$result"
64         echo "count=$count"
65 }
66
67 printf '%s: randomized test using RANDOMSEED=%d\n' "$0" "$RANDOMSEED"
68
69 eval_cmd=`exec 3>&1
70         { $randomdecl -s "$RANDOMSEED" -n "$TESTITER" 3>&-
71                 echo gen_status=$? >&3
72         } | proc >&3`
73 eval "$eval_cmd"
74
75 expected_count=`expr "$TESTITER" + 0`
76 if test x"$count" != x"$expected_count"; then
77         printf '%s: failed after %d successful tests (out of %d)\n' \
78                 "$0" "$count" "$expected_count" 1>&2
79         exit 1
80 fi
81
82 test x"$result" = x"pass" && test x"$gen_status" = x"0" && exit 0
83 exit 1