3 # somewhat portable way to detect CPU count
5 if [ "$CPUS" = "" ]; then
6 # Windows standard environment variable
7 CPUS="$NUMBER_OF_PROCESSORS"
9 if [ "$CPUS" = "" ]; then
11 CPUS=`getconf _NPROCESSORS_ONLN 2>/dev/null`
13 if [ "$CPUS" = "" ]; then
15 CPUS=`getconf NPROCESSORS_ONLN 2>/dev/null`
17 if [ "$CPUS" = "" ]; then
33 threads=`detect_cpu_count; echo $CPUS`
38 "--valgrind" ) valgrind=`which valgrind` ;;
39 "--skeleton" ) skeleton=1 ;;
40 "--keep-tmp-files" ) keep_tmp_files=1 ;;
44 diff_prog="${diff_prog} -b" # ignore whitespace at the end of line
48 number_pattern='^[0-9]+$'
49 if [[ $number =~ $number_pattern ]]
54 * ) tests[${#tests[@]}]="$arg" ;; # array is continuous (old bash lacks +=)
57 echo "Running in ${threads} thread(s)"
59 if test ! -x "${re2c}"; then
60 echo "Cannot find re2c executable (${re2c})."
64 test_blddir="test_"`date +%y%m%d%H%M%S`
65 rm -rf $test_blddir && mkdir $test_blddir
67 # preserve directory structure unless given explicit args
68 if [ ${#tests[@]} -eq 0 ]; then
69 cp -R "@top_srcdir@/test/"* "@top_srcdir@/examples/"* $test_blddir
71 for f in ${tests[@]}; do
72 cp $f ${f%.re}.c $test_blddir
76 chmod -R u+w $test_blddir
78 find $test_blddir -type f \
84 # if not a debug build, remove all debug subdirs
85 $re2c --version | grep -q "debug" \
86 || find "$test_blddir" -type d -name debug | xargs rm -rf
88 tests=(`find $test_blddir -name '*.re' | sort`)
90 # set include paths, relative to build directory
92 && incpaths=$(find * -type d -exec echo "-I ../{}" \;) \
95 tests_per_thread=$((${#tests[@]} / threads + 1))
97 for ((i = 0; i < threads; i++))
99 j=$((i * tests_per_thread))
100 packs[$i]=${tests[@]:j:tests_per_thread}
103 if test -n "${valgrind}"
107 "--track-origins=yes"
110 "--show-reachable=yes"
114 valgrind="${valgrind} ${valgrind_options[@]}"
128 local outx=${x:$((${#test_blddir} + 1))}
129 local outc="${outx%.re}.c"
131 # filename (dot short* (long arg?)*)? ext
132 # must keep to POSIX standard: no syntactic sugar like +,?, etc.
133 # if you change this regexp, try it with 'sed --posix'
134 local switches=`basename "$x" | lc_run sed \
135 -e 's/^[^.]*\.re$//g' \
136 -e 's/^[^.]*\.\(.*\)\.re$/\1/g' \
137 -e 's/^\([^-]\)/-\1/' \
138 -e 's/--\([^ (-]*\)/ --\1/g' \
139 -e 's/(\([^)]*\))/ \1/g'`
140 # check that flags do not contain uppercase letters:
141 # file extensions are case-insensitive on some platforms
142 printf "%s" "$switches" | lc_run grep -q "[A-Z]" \
143 && { echo "bad file extension: '$outx' (uppercase letters are not allowed)"; exit 1; }
144 local switches="$switches -o $outc --no-version --no-generation-date"
145 # enable warnings globally
146 local switches="$switches -W"
148 if [ $skeleton -eq 1 ]
152 local switches="$switches --skeleton -Werror-undefined-control-flow"
154 ${valgrind} ${wine} ../${re2c} $incpaths $switches "../$x" 2>"$outc.stderr"
155 local status=$(echo $?)
156 [ $status -eq 0 ] && { @CC@ -Wall -Wextra -o "$outc.out" "$outc" 2>>"$outc.stderr" || status=2; }
157 [ $status -eq 0 ] && { ./"$outc.out" 2>>"$outc.stderr" || status=3; }
160 0 ) local msg="OK" ;;
161 1 ) local msg="OK (expected re2c error)" ;;
162 2 ) local msg="FAIL (compilation error)" ;;
163 3 ) local msg="FAIL (runtime error)" ;;
164 * ) local msg="FAIL (unknown error)" ;;
166 printf "%-25s $outx\n" "$msg"
168 if [ $status -le 1 ]; then
169 [ $keep_tmp_files -eq 0 ] && rm -f "$outx" "$outc"{,.line*.{input,keys},.stderr,.out}
171 local errcnt=$(($errcnt + 1))
174 local c="${outx%.re}.c.orig"
181 && mkdir -p `dirname "$outx"` \
182 && cp "../../$x" "$outx"
184 $valgrind $wine ../../$re2c $incpaths $switches "$outx" 2>"$outc.stderr" 1>&2
185 # on windows output contains CR LF, cut CR to match test results
186 for f in "$outc" "$outc.stderr"; do
188 && cat "$f" | lc_run tr -d '\r' > "$f".mod \
191 # paste all files dropped by re2c into output file
192 rm "$outx" && find . -type f \
202 [ -z $status ] && status=`[ -f "$c" ] || echo "MISSING"`
203 [ -z $status ] && status=`$diff_prog "$c" "$outc" > "$outc.diff" || echo "FAIL"`
204 [ -z $status ] && status="OK"
205 if [ $status = "OK" ]
207 rm "$outc" "$outc.diff" "$c" "$outx"
209 local errcnt=$(($errcnt + 1))
212 printf "%-10s $outx\n" "$status"
230 for ((i = 0; i < ${#packs[@]}; i++))
232 logs[$i]=`date +%y%m%d%H%M%S`_$i
233 run_pack ${logs[i]} ${packs[i]} &
234 wait_pids[${#wait_pids[@]}]=$! # array is continuous (old bash lacks +=)
239 for ((i = 0; i < ${#logs[@]}; i++))
241 error=`cat ${logs[i]}`
242 errors=$((errors + error))
246 # remove directories that are empty or contain only .inc files
247 for d in $(find $test_blddir -depth -type d)
249 [ -z "$(find $d -type f ! -name '*.inc')" ] && rm -rf "$d"
254 echo "All ${#tests[@]} tests passed successfully."
257 echo "Error: $errors out ${#tests[@]} tests failed."