bmxconverter/test.sh

422 lines
8.8 KiB
Bash
Raw Normal View History

2023-11-17 12:30:24 -08:00
#!/bin/bash
2023-12-07 13:04:14 -08:00
BLACK=0
RED=1
GREEN=2
YELLOW=3
BLUE=4
MAGENTA=5
CYAN=6
WHITE=7
GRAY=8
DEFAULT=9
2023-12-01 15:46:43 -08:00
bool_true() {
[ "$1" = "true" ] 2>/dev/null || [ "$1" -ne 0 ] 2>/dev/null
2023-12-07 13:04:14 -08:00
[ $? -eq 0 ] # Convert $? to 1 or 0 value
2023-12-01 15:46:43 -08:00
return $?
}
bool_false() {
2023-12-07 13:04:14 -08:00
! bool_true "$1"
2023-12-01 15:46:43 -08:00
return $?
}
cmd_avail() {
[ -x "$(command -v "$1")" ] && return 0 || return 1
}
tests=0
failed=0
succeeded=0
2023-12-07 13:04:14 -08:00
failed_ids=()
2023-12-01 15:46:43 -08:00
color=1
tput_avail=0
if cmd_avail tput; then
tput_avail=1
fi
if bool_true $tput_avail; then
2023-12-07 13:04:14 -08:00
COLORS=$(tput colors 2>/dev/null)
2023-12-01 15:46:43 -08:00
if [ $? -eq 0 ] && [ $COLORS -gt 2 ]; then
color=1
2023-12-07 13:04:14 -08:00
if [ $COLORS -lt 8 ]; then
GRAY=9
fi
2023-12-01 15:46:43 -08:00
else
color=0
fi
fi
2023-11-17 14:12:46 -08:00
usage() {
cat << EOF
$0 usage:
2023-12-07 13:04:14 -08:00
-o|--output-dir <output directory>
2023-11-17 14:12:46 -08:00
-p|--use-program <program path>
Uses the specified already-compiled binary rather than compiling manually
-i|--input-image <image path>
Adds an image input at the specified path to the image list.
-b|--output-bpp <bpp>
Adds a bitdepth to the list to test with
-r|--resize <width>x<height>
2023-11-17 14:12:46 -08:00
Adds a resize to the list to test with
2023-12-01 15:46:43 -08:00
-R|--no-reverse
2023-11-17 14:12:46 -08:00
Disables reverse operation testing
2023-12-01 15:46:43 -08:00
-D|--no-dither
2023-11-17 14:12:46 -08:00
Disables dithering testing
2023-12-01 15:46:43 -08:00
-S|--no-compress
Disables compression testing
--no-generate-palette
Disables palette generation when testing and uses prebuilt palette files only
-p|--palette-file
Uses the specified palette file.
-d|--debug
Use debug flags with the converter program.
2023-11-17 14:12:46 -08:00
-n|--no-defaults
Disables default settings
2023-12-01 15:46:43 -08:00
-c|--color
Enables color.
-C|--no-color
Disables color.
-h|--help
Shows this message.
2023-12-07 13:04:14 -08:00
-O|--reverse-dir <directory for reverse conversion files>
Sets the directory for files that have been converted to PNG from BMX
No effect when reverse conversion is disabled
2023-11-17 14:12:46 -08:00
EOF
exit 1
}
2023-11-17 12:30:24 -08:00
oldpwd="$(pwd)"
cd "$(dirname "$0")"
2023-12-01 15:46:43 -08:00
converter="./builddir/bmxconverter"
2023-11-17 14:12:46 -08:00
prebuilt=0
images=()
bpps=()
2023-11-17 14:12:46 -08:00
resize=()
2023-12-01 15:46:43 -08:00
palettes=()
generate_palette=1
2023-11-17 14:12:46 -08:00
enable_defaults=1
dither=1
reverse=1
enable_compression=1
2023-11-17 14:12:46 -08:00
enable_reverse=1
enable_dither=1
2023-12-01 15:46:43 -08:00
enable_probe=1
debug_flags=""
2023-11-17 12:30:24 -08:00
outdir="testout"
2023-12-07 13:04:14 -08:00
reverse_dir_set=0
reversedir=""
OPTIONS=$(getopt -o "b:hp:i:r:no:d:p:cCDRSO" --long "help,use-program:,input-image:,output-bpp:,resize:,no-defaults,output-dir:,debug:,no-reverse,no-dither,no-compress,palette-file,no-generate-palette,no-probe,color,no-color,reverse-dir" -- "$@")
2023-11-17 14:12:46 -08:00
if [ $? != 0 ]; then
echo "Getopt error."
2023-11-17 14:12:46 -08:00
usage
fi
eval set -- "$OPTIONS"
while [ -n "$1" ]; do
case "$1" in
2023-12-07 13:04:14 -08:00
-O|--reverse-dir)
reversedir="$2"
reverse_dir_set=1
shift 2
;;
2023-11-17 14:12:46 -08:00
-o|--output-dir)
outdir="$2"
shift 2
;;
-h|--help)
usage
;;
-p|--use-program)
converter="$2"
prebuilt=1
shift 2
;;
-i|--input-image)
images+="$2"
shift 2
;;
-b|--output-bpp)
bpp+="$2"
shift 2
;;
-r|--resize)
resize+="$2"
2023-11-17 14:12:46 -08:00
shift 2
;;
-n|--no-defaults)
enable_defaults=0
shift
;;
2023-12-01 15:46:43 -08:00
-R|--no-reverse)
2023-11-17 14:12:46 -08:00
enable_reverse=0
shift
;;
2023-12-01 15:46:43 -08:00
-D|--no-dither)
2023-11-17 14:12:46 -08:00
enable_dither=0
shift
;;
2023-12-01 15:46:43 -08:00
-S|--no-compress)
enable_compression=0
shift
;;
-d|--debug)
debug_flags="${debug_flags}$2"
shift 2
;;
2023-12-01 15:46:43 -08:00
-p|--palette-file)
palettes+="$2"
shift 2
;;
--no-generate-palette)
generate_palette=0
shift
;;
--no-probe)
enable_probe=0
shift
;;
-c|--color)
color=1
shift
;;
-C|--no-color)
color=0
shift
;;
2023-11-17 14:12:46 -08:00
--)
shift
break
;;
*)
echo "Invalid option: $0"
2023-11-17 14:12:46 -08:00
usage
;;
esac
done
2023-12-07 13:04:14 -08:00
if bool_false $reverse_dir_set; then
reversedir="$outdir/reverse"
fi
if bool_true $generate_palette; then
2023-12-01 15:46:43 -08:00
palettes+=""
fi
2023-12-07 13:04:14 -08:00
if bool_true $enable_defaults; then
2023-12-01 15:46:43 -08:00
images+=("TEST.png" "PACK.png" "CAT.jpg")
bpps+=(1 2 4 8)
2023-12-07 13:04:14 -08:00
resize+=("64x64" "320x240" "640x480")
palettes+=("DPAL.BIN")
fi
if bool_true $enable_reverse; then
mkdir -p $reversedir
2023-11-17 14:12:46 -08:00
fi
2023-12-07 13:04:14 -08:00
if bool_false $prebuilt; then
2023-11-17 14:12:46 -08:00
meson setup builddir
meson compile -C builddir || exit $?
fi
2023-12-01 15:46:43 -08:00
bold() {
if bool_false "$color"; then
return
fi
if bool_true "$tput_avail"; then
tput bold
else
printf "\033[1m"
fi
}
italic() {
if bool_false "$color"; then
return
fi
if bool_true "$tput_avail"; then
tput enter_italics_mode
else
printf "\033[3m"
fi
}
setfgcolor() {
if bool_false "$color"; then
return
fi
if bool_true "$tput_avail"; then
tput setaf "$1"
else
2023-12-07 13:04:14 -08:00
printf "\033[38;5;%sm" "$1"
2023-12-01 15:46:43 -08:00
fi
}
setbgcolor() {
if bool_false "$color"; then
return
fi
if bool_true "$tput_avail"; then
tput setab "$1"
else
2023-12-07 13:04:14 -08:00
printf "\033[48;5;%sm" "$1"
2023-12-01 15:46:43 -08:00
fi
}
resetcolor() {
if bool_false "$color"; then
return
fi
if bool_true "$tput_avail"; then
tput sgr0
else
printf "\033[0m"
fi
}
2023-12-07 13:04:14 -08:00
extension() {
len="$(printf "%s" "$1" | sed 's/[^.]*\.[^.]*/./g' | wc -c)"
printf "%s" "$1" | cut -d. -f$(($len+1))
}
2023-11-17 14:12:46 -08:00
mkdir -p "$outdir"
run() {
2023-12-01 15:46:43 -08:00
converter="$1"
shift
infile="$1"
shift
outfile="$1"
shift
reverse="$1"
shift
2023-12-07 13:04:14 -08:00
infile_for_id=$(basename $infile)
outfile_for_id=$(basename $outfile)
2023-12-01 15:46:43 -08:00
extra_flags_run=()
2023-12-07 13:04:14 -08:00
extra_flags_probe=()
if bool_true "$reverse"; then
extra_flags_run+=( "-reverse" )
else
extra_flags_probe+=( "-reverse" )
fi
id="convert_${infile_for_id}_${outfile_for_id}"
2023-12-01 15:46:43 -08:00
if [ -n "$reverse" ]; then
2023-12-07 13:04:14 -08:00
id="${id}_reverse"
2023-12-01 15:46:43 -08:00
fi
2023-12-07 13:04:14 -08:00
setfgcolor $WHITE
2023-12-01 15:46:43 -08:00
bold
2023-12-07 13:04:14 -08:00
printf "Running test %s\n" "$id"
printf "Command: %s\n" "$converter -in $infile -out $outfile ${extra_flags_run[*]} $*"
2023-12-01 15:46:43 -08:00
resetcolor
2023-12-07 13:04:14 -08:00
setfgcolor $GRAY
2023-12-01 15:46:43 -08:00
tests=$(($tests+1))
"$converter" -in "$infile" -out "$outfile" "${extra_flags_run[@]}" "$@"
if [ $? -ne 0 ]; then
setfgcolor 1
2023-12-07 13:04:14 -08:00
printf "Test $id failed.\n"
2023-12-01 15:46:43 -08:00
resetcolor
failed=$(($failed+1))
2023-12-07 13:04:14 -08:00
failed_ids+=($id)
2023-12-01 15:46:43 -08:00
else
2023-12-07 13:04:14 -08:00
setfgcolor $GREEN
printf "Test $id succeeded!\n"
resetcolor
2023-12-01 15:46:43 -08:00
succeeded=$(($succeeded+1))
fi
resetcolor
if [ $enable_probe -ne 0 ]; then
2023-12-07 13:04:14 -08:00
id="probe_${outfile_for_id}"
if [ -z "$reverse" ]; then
id="${id}_pc"
fi
setfgcolor $BLUE
2023-12-01 15:46:43 -08:00
bold
2023-12-07 13:04:14 -08:00
printf "Running test %s\n" "$id"
2023-12-01 15:46:43 -08:00
printf "Probing %s...\n" "$outfile"
resetcolor
2023-12-07 13:04:14 -08:00
setfgcolor $GRAY
printf "Command: %s\n" "$converter -in $infile -probe ${extra_flags_probe[*]}"
"$converter" -in "$outfile" -probe "${extra_flags_probe[@]}"
2023-12-01 15:46:43 -08:00
resetcolor
tests=$(($tests+1))
if [ $? -ne 0 ]; then
2023-12-07 13:04:14 -08:00
setfgcolor $RED
printf "Test $id failed.\n"
2023-12-01 15:46:43 -08:00
resetcolor
failed=$(($failed+1))
2023-12-07 13:04:14 -08:00
failed_ids+=($id)
2023-12-01 15:46:43 -08:00
else
2023-12-07 13:04:14 -08:00
setfgcolor $GREEN
printf "Test $id succeeded!\n"
resetcolor
2023-12-01 15:46:43 -08:00
succeeded=$(($succeeded+1))
fi
fi
}
2023-11-17 12:30:24 -08:00
for img in "${images[@]}"; do
for bpp in "${bpps[@]}"; do
2023-11-17 14:12:46 -08:00
for size in "${resize[@]}"; do
for compressflag in -compress ""; do
2023-12-01 15:46:43 -08:00
for palette in "${palettes[@]}"; do
width="$(echo -n "$size" | cut -dx -f1)"
height="$(echo -n "$size" | cut -dx -f2)"
2023-12-07 13:04:14 -08:00
ext="$(extension "$img")"
ext_upper="$(printf "%s" "$ext" | tr '[:lower:]' '[:upper:]')"
name="$(basename -s ".$ext" "$img")"
name="$(printf "%s.%s.%sPX.%sB" "$name" "$ext_upper" "$width" "$bpp")"
2023-12-01 15:46:43 -08:00
extraflags=()
if [ -n "$palette" ]; then
extraflags+=( "-palette-file" "$palette" )
2023-12-07 13:04:14 -08:00
name+=".PL-$(basename -s ".$(extension "$palette")" "$palette" | tr '[:lower:]' '[:upper]')"
fi
2023-12-01 15:46:43 -08:00
if [ -n "$compressflag" ]; then
if [ $enable_compression -eq 0 ]; then
continue
fi
extraflags+=( "$compressflag" )
name+=".C"
fi
2023-12-07 13:04:14 -08:00
run "$converter" "$img" "$outdir/$name.BMX" false "${extraflags[@]}" -bpp "$bpp" -resize "$width" "$height" -border 15 0 15 -debug "$debug_flags"
if bool_true $enable_dither; then
run "$converter" "$img" "$outdir/$name.D.BMX" false "${extraflags[@]}" -bpp "$bpp" -resize "$width" "$height" -dither -border 15 0 15 -debug "$debug_flags"
2023-12-01 15:46:43 -08:00
fi
2023-12-07 13:04:14 -08:00
if bool_true $enable_reverse; then
run "$converter" "$outdir/$name.BMX" "$reversedir/$name.PNG" true "${extraflags[@]}" -resize "$width" "$height" -debug "$debug_flags"
if bool_true $enable_dither; then
run "$converter" "$outdir/$name.D.BMX" "$reversedir/$name.D.PNG" true "${extraflags[@]}" -resize "$width" "$height" -dither -debug "$debug_flags"
2023-12-01 15:46:43 -08:00
fi
fi
2023-12-01 15:46:43 -08:00
done
done
2023-11-17 12:30:24 -08:00
done
done
done
2023-12-07 13:04:14 -08:00
badcolor=$RED
goodcolor=$GREEN
okcolor=$YELLOW
success_good=90
success_ok=50
percent_out_of_tests() {
if [ "$tests" -eq 0 ]; then
printf "100"
else
printf "%s" "$((($1*100)/$tests))"
fi
}
invert_percent() {
printf "%s" "$((100-$1))"
}
success_percent="$(percent_out_of_tests $succeeded)"
failed_percent="$(percent_out_of_tests $failed)"
set_color_by_good_percentage() {
if [ "$1" -ge $success_good ]; then
setfgcolor "$goodcolor"
elif [ "$1" -ge $success_ok ]; then
setfgcolor "$okcolor"
else
setfgcolor "$badcolor"
fi
}
set_color_by_bad_percentage() {
set_color_by_good_percentage "$(invert_percent $1)"
}
bold
setfgcolor $WHITE
printf "%s total test cases, " "$tests"
set_color_by_bad_percentage "$failed_percent"
printf "%s failed (%s%%) " "$failed" "$failed_percent"
setfgcolor $WHITE
printf "and "
set_color_by_good_percentage "$success_percent"
printf "%s succeeded (%s%%)." "$succeeded" "$success_percent"
resetcolor
printf "\n"
if [ "$failed" -gt 0 ]; then
setfgcolor $RED
printf "Failing tests:\n"
for test in "${failed_ids[@]}"; do
printf "Test '%s'\n"
done
resetcolor
fi
2023-11-17 12:30:24 -08:00
cd "$oldpwd"