1#!/bin/sh -
2#	$Id: testone,v 12.0 2004/11/17 03:44:48 bostic Exp $
3#
4# Run just one C++ regression test, the single argument
5# is the basename of the test, e.g. TestRpcServer
6
7error()
8{
9	echo '' >&2
10	echo "C++ regression error: $@" >&2
11	echo '' >&2
12	ecode=1
13}
14
15# compares the result against the good version,
16# reports differences, and removes the result file
17# if there are no differences.
18#
19compare_result()
20{
21	good="$1"
22	latest="$2"
23	if [ ! -e "$good" ]; then
24		echo "Note: $good does not exist"
25		return
26	fi
27	tmpout=/tmp/blddb$$.tmp
28	diff "$good" "$latest" > $tmpout
29	if [ -s $tmpout ]; then
30		nbad=`grep '^[0-9]' $tmpout | wc -l`
31		error "$good and $latest differ in $nbad places."
32	else
33		rm $latest
34	fi
35	rm -f $tmpout
36}
37
38ecode=0
39stdinflag=n
40gdbflag=n
41CXX=${CXX:-c++}
42LIBS=${LIBS:-}
43
44# remove any -c option in the CXXFLAGS
45CXXFLAGS="`echo " ${CXXFLAGS} " | sed -e 's/ -c //g'`"
46
47# determine the prefix of the install tree
48prefix=""
49while :
50do
51	case "$1" in
52	--prefix=* )
53		prefix="`echo $1 | sed -e 's/--prefix=//'`"; shift
54		LIBS="-L$prefix/lib -ldb_cxx $LIBS"
55		CXXFLAGS="-I$prefix/include $CXXFLAGS"
56	        export LD_LIBRARY_PATH="$prefix/lib:$LD_LIBRARY_PATH"
57		;;
58	--stdin )
59		stdinflag=y; shift
60		;;
61	--gdb )
62		CXXFLAGS="-g $CXXFLAGS"
63		gdbflag=y; shift
64		;;
65        * )
66		break
67                ;;
68	esac
69done
70
71if [ "$#" = 0 ]; then
72	echo 'Usage: testone [ --prefix=<dir> | --stdin ] TestName'
73        exit 1
74fi
75name="$1"
76
77# compile
78rm -rf TESTDIR; mkdir TESTDIR
79cd ./TESTDIR
80
81${CXX} ${CXXFLAGS} -o $name ../$name.cpp ${LIBS} > ../$name.compileout 2>&1
82if [ $? != 0 -o -s ../$name.compileout ]; then
83	error "compilation of $name failed, see $name.compileout"
84	exit 1
85fi
86rm -f ../$name.compileout
87
88# find input and error file
89infile=../$name.testin
90if [ ! -f $infile ]; then
91	infile=/dev/null
92fi
93
94# run and diff results
95rm -rf TESTDIR
96if [ "$gdbflag" = y ]; then
97	if [ -s $infile ]; then
98		echo "Input file is $infile"
99	fi
100	gdb ./$name
101        exit 0
102elif [ "$stdinflag" = y ]; then
103	./$name           >../$name.out 2>../$name.err
104else
105	./$name  <$infile >../$name.out 2>../$name.err
106fi
107cd ..
108
109testerr=$name.testerr
110if [ ! -f $testerr ]; then
111	testerr=/dev/null
112fi
113
114testout=$name.testout
115if [ ! -f $testout ]; then
116	testout=/dev/null
117fi
118
119compare_result $testout $name.out
120compare_result $testerr $name.err
121rm -rf TESTDIR
122exit $ecode
123