-#!/bin/sh
-#
-# Copyright © 2012 Nick Bowler
-#
-# Randomized tests that libcdecl can parse its own output.
-#
-# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
-# This is free software: you are free to do what the fuck you want to.
-# There is NO WARRANTY, to the extent permitted by law.
-
-: "${RANDOMSEED=$RANDOM}" "${TESTITER=500}"
-
-randomdecl=test/randomdecl$EXEEXT
-crossparse=test/crossparse$EXEEXT
-test -x $randomdecl || exit 77
-
-# Slow case: run tests one at a time to determine exactly which one failed.
-first_failed() {
- ff_count=0
-
- while test $# -gt 0
- do
- $crossparse "$1" 2>/dev/null || break
- ff_count=`expr $ff_count + 1`
- shift
- done
-
- echo "$ff_count"
-}
-
-proc() {
- result=pass
- count=0
-
- set x
- while read decl
- do
- set "$@" "$decl"
-
- # Accumulate tests in $@ and run them in batches to avoid
- # significant startup costs.
- if test $# -gt 25; then
- shift
- $crossparse "$@" || { result=fail
- tmp_count=`first_failed "$@"`
- count=`expr $count + $tmp_count`
- break
- }
- count=`expr $count + $#`
- set x
- fi
- done
-
- shift
- if test $# -gt 0 && test x"$result" = x"pass"; then
- tmp_count=$#
- $crossparse "$@" || { result=fail
- tmp_count=`first_failed "$@"`
- }
- count=`expr $count + $tmp_count`
- fi
-
- echo "result=$result"
- echo "count=$count"
-}
-
-printf '%s: randomized test using RANDOMSEED=%d\n' "$0" "$RANDOMSEED"
-
-eval_cmd=`exec 3>&1
- { $randomdecl -s "$RANDOMSEED" -n "$TESTITER" 3>&-
- echo gen_status=$? >&3
- } | proc >&3`
-eval "$eval_cmd"
-
-if test x"$gen_status" != x"0"; then
- printf '%s: %s failed with status=%d\n' \
- "$0" "$randomdecl" "$gen_status" 1>&2
- exit 1
-fi
-
-expected_count=`expr "$TESTITER" + 0`
-if test x"$count" != x"$expected_count"; then
- printf '%s: failed after %d successful tests (out of %d)\n' \
- "$0" "$count" "$expected_count" 1>&2
- exit 1
-fi
-
-test x"$result" = x"pass" && exit 0
-exit 1