Files
rgbds/test/gfx/test.sh
T
RangiandEldred Habert 04bbe80ae0 Some shell script style improvements (#2087)
* Escape special characters in filenames when comparing gfx .err output
* Use `if` instead of `&&`
* Use `case` instead of `if` disjunction

---------

Co-authored-by: Eldred Habert <[email protected]>
2026-09-21 10:15:40 -04:00

143 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
[[ -e ./rgbgfx_test ]] || make -C ../.. test/gfx/rgbgfx_test Q= ${CXX:+"CXX=$CXX"} || exit
[[ -e ./randtilegen ]] || make -C ../.. test/gfx/randtilegen Q= ${CXX:+"CXX=$CXX"} || exit
export LC_ALL=C
# Screen width for help/usage text (for reproducible test results)
export COLUMNS=79
shopt -u checkwinsize # Prevent subsequent commands from resetting `COLUMNS`
errtmp="$(mktemp)"
# shellcheck disable=SC2064 # (Immediate expansion is the desired behavior.)
trap "rm -f ${errtmp@Q} result.{png,1bpp,2bpp,pal,tilemap,attrmap,palmap} out*.png" EXIT
tests=0
failed=0
rc=0
bold="$(tput bold)"
resbold="$(tput sgr0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
rescolors="$(tput op)"
RGBGFX=../../rgbgfx
newTest () {
cmdline="$*"
echo "${bold}${green}Testing: ${cmdline}${rescolors}${resbold}" >&2
}
runTest () {
(( tests++ ))
eval "$cmdline"
}
failTest () {
rc=1
(( failed++ ))
echo "${bold}${red}Test ${cmdline} failed!${1:+ (RC=$1)}${rescolors}${resbold}"
}
tryCmp () {
if ! cmp "$1" "$2"; then
../../contrib/gbdiff.bash "$1" "$2"
echo "${bold}${red}$1 mismatch!${rescolors}${resbold}"
false
fi
}
checkOutput () {
out_rc=0
for ext in 1bpp 2bpp pal tilemap attrmap palmap; do
if [[ -e "$1.out.$ext" ]]; then
tryCmp "$1.out.$ext" "result.$ext"
(( out_rc = out_rc || $? ))
fi
done
return $out_rc
}
# Draw a random tile offset and VRAM0 size
# Neither should change anything to how the image is displayed
while [[ "$ofs" -eq 0 ]]; do (( ofs = RANDOM % 256 )); done
while [[ "$size" -eq 0 ]]; do (( size = RANDOM % 256 )); done
for f in seed*.bin; do
for flags in ""{," -b $ofs"}{," -N $size,256"}; do
newTest ./rgbgfx_test "$f" $flags
runTest || failTest $?
done
done
for f in *.png; do
# Do not process outputs or palette inputs of other tests as test inputs themselves
case "$f" in
result.png | *.pal.png) continue;;
esac
flags=
if [[ -e "${f%.png}.flags" ]]; then
flags="@${f%.png}.flags"
fi
for f_ext in o_1bpp o_2bpp p_pal t_tilemap a_attrmap q_palmap; do
if [[ -e "${f%.png}.out.${f_ext#*_}" ]]; then
flags+=" -${f_ext%_*} result.${f_ext#*_}"
fi
done
newTest "$RGBGFX" $flags "$f"
if [[ -e "${f%.png}.err" ]]; then
runTest 2>"$errtmp"
diff -au --strip-trailing-cr "${f%.png}.err" "$errtmp" || failTest
else
runTest && checkOutput "${f%.png}" || failTest $?
fi
newTest "$RGBGFX" $flags - "<$f"
if [[ -e "${f%.png}.err" ]]; then
runTest 2>"$errtmp"
diff -au --strip-trailing-cr "${f%.png}.err" <(sed "s#<stdin>#${f//#/\\#}#g" "$errtmp") || failTest
else
runTest && checkOutput "${f%.png}" || failTest $?
fi
done
for f in *.[12]bpp; do
# Do not process outputs or sample outputs of other tests as test inputs themselves
case "$f" in
result.[12]bpp | *.in.[12]bpp | *.out.[12]bpp) continue;;
esac
flags=
if [[ -e "${f%.[12]bpp}.flags" ]]; then
flags="@${f%.[12]bpp}.flags"
if [[ -e "${f%.1bpp}.flags" ]]; then
flags="$flags -d 1"
fi
fi
if [[ -e "${f%.[12]bpp}.err" ]]; then
newTest "$RGBGFX $flags -o $f -r 1 result.png"
runTest 2>"$errtmp"
diff -au --strip-trailing-cr "${f%.[12]bpp}.err" <(sed "s#<stdin>#${f//#/\\#}#g" "$errtmp") || failTest
else
newTest "$RGBGFX $flags -o $f -r 1 result.png && $RGBGFX $flags -o result.2bpp result.png"
runTest && tryCmp "$f" result.2bpp || failTest $?
fi
done
# Test writing to stdout
newTest "$RGBGFX -m -o - write_stdout.bin > result.2bpp"
runTest && tryCmp write_stdout.out.2bpp result.2bpp || failTest $?
if [[ "$failed" -eq 0 ]]; then
echo "${bold}${green}All ${tests} tests passed!${rescolors}${resbold}"
else
echo "${bold}${red}${failed} of the tests failed!${rescolors}${resbold}"
fi
exit $rc