-TESTS=${TESTS:-`find . -name "*_test.c*"`}
-
-for test in $TESTS; do
- [ $VERBOSE -gt 0 ] && echo "Running $test..."
- case "$test" in
- *.cpp)
- $CXX $CXXFLAGS $test -o test || exit 1
- ./test || exit 1
- rm -f test
- ;;
- *.c)
- $CC $CFLAGS $test -o test || exit 1
- ./test || exit 1
- rm -f test
- ;;
+TESTS=${TESTS:-`find . \
+ \( -name .svn -prune -o -name .git -prune -o -name .hg -prune \) \
+ -o -name "*_test.c*" -print` }
+
+TESTOUT="testout"
+
+buildout='/dev/null'
+runout='/dev/null'
+[ "$VERBOSE" -ge 2 ] && buildout='/dev/stdout'
+[ "$VERBOSE" -ge 3 ] && runout='/dev/stdout'
+
+# Needed to get build/exec result code rather than tee's
+set -o pipefail
+
+rm -rf "${TESTOUT}.old"
+mv -f "${TESTOUT}" "$TESTOUT.old"
+mkdir -p "$TESTOUT"
+
+for src in $TESTS; do
+ name="`basename $src | sed -e 's/\.cpp$//' -e 's/\.c$//'`"
+ exe="./$TESTOUT/$name"
+
+ case "$src" in
+ *.cpp) BUILDCMD="$CXX $CXXFLAGS $src -o $exe" ;;
+ *.c) BUILDCMD="$CC $CXXFLAGS $src -o $exe" ;;