#! /bin/sh -
### ====================================================================
### Test implementations of the Pochhammer symbol computation, including
### creation of gnuplot files in EPS form.
###
### Usage:
###	env [CC=...] [CFLAGS=...] [FILES="poch1.c poch2.c ...] ./poch-test.sh
###
### Executables, test data, and plots are store in $TMPDIR to reduce
### local clutter.
###
### [01-Aug-2026]
### ====================================================================

### Customize this for your local default prefix
test -d /usr/uumath && prefix=${prefix-/usr/uumath}
test -d ${prefix} || prefix=/usr/local

CC=${CC-"cc"}
CFLAGS=${CFLAGS-"-O3"}
FILES=${FILES-"
	poch-randlab.c
	poch.c
        pocharf-randlab.c
	pocharf.c
	pochcp-randlab.c
	pochcp.c
	pochcp2-randlab.c
	pochcp2.c
	pochfx-randlab.c
	pochfx.c
	pochgmp-randlab.c
	pochgmp.c
	pochmpfr-randlab.c
	pochmpfr.c
	pochpp1-randlab.c
	pochpp1.c
	pochpp2.c
	"}
LDFLAGS=${LDFLAGS-"-Wl,-rpath,${prefix}/lib64 -L${prefix}/lib64"}
LIBS=${LIBS-"-lflint -lfx -lgmp -lmpfr -lm"}
TMPDIR=${TMPDIR-"/tmp"}

test_poch()
{   # Usage: test_poch -DTEST1 test1
    # Usage: test_poch -DTEST2 test2
    o=${b}-${2}
    exe=${TMPDIR}/${o}.exe
    ${CC} ${CFLAGS} -I${prefix}/include ${1} ${f} ${LDFLAGS} ${LIBS} -o ${exe}
    test -x ${exe} || continue
    ${exe} > ${TMPDIR}/${o}.dat
    cat >${TMPDIR}/${o}.gnu <<EOF
	XFMT     = "%.0f"
	YFMT     = "${YFMT}"
	YMAX     = ${YMAX}
	YTICS    = ${YTICS}
	set key off
	set pointsize  0.25
	set size ratio 0.5
	set ytics YTICS
	set ylabel "ulps"
	set format x XFMT
	set format y YFMT
	set yrange [-YMAX:YMAX]

	set xlabel "n"
	set xtics 50
	set title "${o}-n"

	set output "${TMPDIR}/${o}-n.eps"
	set terminal postscript portrait enhanced color "Arial" 22 size 10,4
	plot "${TMPDIR}/${o}.dat" using (\$1):(\$3) lt rgb "red", -0.5 lt rgb "blue", 0 lt rgb "blue", 0.5 lt rgb "blue"

	set output "${TMPDIR}/${o}-n.png"
	set terminal png font "Arial,22" enhanced size 600,480
	plot "${TMPDIR}/${o}.dat" using (\$1):(\$3) lt rgb "red", -0.5 lt rgb "blue", 0 lt rgb "blue", 0.5 lt rgb "blue"

	set xlabel "x"
	set xtics 250
	set title "${o}-x"

	set output "${TMPDIR}/${o}-x.eps"
	set terminal postscript portrait enhanced color "Arial" 22 size 10,4
	plot "${TMPDIR}/${o}.dat" using (\$2):(\$3) lt rgb "red", -0.5 lt rgb "blue", 0 lt rgb "blue", 0.5 lt rgb "blue"

	set output "${TMPDIR}/${o}-x.png"
	set terminal png font "Arial,22" enhanced size 600,480
	plot "${TMPDIR}/${o}.dat" using (\$2):(\$3) lt rgb "red", -0.5 lt rgb "blue", 0 lt rgb "blue", 0.5 lt rgb "blue"

EOF
    gnuplot ${TMPDIR}/${o}.gnu 
    ls -l ${TMPDIR}/${o}.*
}

for f in ${FILES}
do
    echo ==================== ${f}
    b=`basename ${f} .c`

    case $b in
    poch)
	YFMT="%.0f"
	YMAX=10
	YTICS=5
	;;
    pochpp1)
	YFMT="%.1f"
	YMAX=1
	YTICS=0.5
	;;
    pocharf-* | pochgmp-* | pochfx-* | pochmpfr-* | pochpp*-* )
	YFMT="%.1f"
	YMAX=1
	YTICS=0.5
	;;
    *randlab*)
	YFMT="%.0f"
	YMAX=50
	YTICS=25
	;;
    *)
	YFMT="%.1f"
	YMAX=1
	YTICS=0.5
	;;
    esac

    test_poch -DTEST1 test1

    YFMT="%.0f"
    YMAX=50
    YTICS=25

    test_poch -DTEST2 test2
done
