2022-01-16 16:27:22 +01:00
#!/usr/bin/env sh
# Copyright (c) 2018 - 2022, Martin Scheidt (ISC license)
# Permission to use, copy, modify, and/or distribute this file for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
# Halt on error
set -e
2022-02-09 01:49:11 +01:00
## -- pass getopts
2022-01-16 16:27:22 +01:00
2022-02-26 18:29:20 +01:00
usage( ) { echo "Usage: testing.sh [-q]" ; }
2022-01-16 16:27:22 +01:00
2022-02-09 01:49:11 +01:00
verbose = 1
2022-01-16 16:27:22 +01:00
2022-02-09 01:49:11 +01:00
while getopts ":q" opt; do
case ${ opt } in
q ) verbose = 0
; ;
\? ) usage
exit 1
; ;
esac
done
2022-01-16 16:27:22 +01:00
2022-02-09 19:15:59 +01:00
## -- variables
2022-02-26 18:29:20 +01:00
# colors
Red = "\033[0;31m"
Green = "\033[0;32m"
Reset = "\033[0;m"
# directory
2022-02-09 19:15:59 +01:00
TESTDIR = "../test"
2022-02-26 18:29:20 +01:00
if [ -d ../tikz-trackschematic ] ; then
cd test/
fi
2022-02-09 19:15:59 +01:00
2022-01-16 16:27:22 +01:00
## -- commands
2022-02-09 01:49:11 +01:00
2022-01-16 16:27:22 +01:00
check_tex_distro( ) {
# check for latexmk
status = 0
command -v pdflatex >/dev/null 2>& 1 || status = 1
if [ $status = 0 ] ; then
2022-02-09 19:15:59 +01:00
if [ $verbose = 1 ] ; then
2022-02-09 01:49:11 +01:00
echo "pdflatex found"
fi
2022-01-16 16:27:22 +01:00
return 0
fi
2022-02-09 01:49:11 +01:00
echo "Program 'pdflatex' not found."
echo "Be sure to have texlive or mactex installed!"
2022-01-16 16:27:22 +01:00
exit 1
}
2022-02-09 02:11:02 +01:00
check_trackschematic( ) {
# check for tikz-trackschematic
status = 0
TEXMFLOCAL = $( kpsewhich --var-value TEXMFLOCAL)
2022-02-09 17:25:15 +01:00
DEVDIR = "tex/latex/local/tikz-trackschematic-dev"
2022-02-09 02:11:02 +01:00
2022-02-09 19:15:59 +01:00
ls $TEXMFLOCAL /$DEVDIR /tikz-trackschematic-dev.sty >> /dev/null 2>& 1 || status = 1
2022-02-09 02:11:02 +01:00
if [ $status = 0 ] ; then
2022-02-09 19:15:59 +01:00
if [ $verbose = 1 ] ; then
2022-02-09 14:09:41 +01:00
echo "tikz-trackschematic-dev found"
2022-02-09 02:11:02 +01:00
fi
return 0
fi
2022-02-09 14:09:41 +01:00
echo "Library 'tikz-trackschematic-dev' not found."
echo "Be sure to have tikz-trackschematic-dev installed!"
2022-02-09 02:11:02 +01:00
exit 1
}
2022-01-16 16:27:22 +01:00
check_imagemagick( ) {
2022-02-09 02:11:02 +01:00
# check for ImageMagick/compare
2022-01-16 16:27:22 +01:00
status = 0
command -v compare >/dev/null 2>& 1 || status = 1
if [ $status = 0 ] ; then
2022-02-09 19:15:59 +01:00
if [ $verbose = 1 ] ; then
2022-02-09 01:49:11 +01:00
echo "compare found"
fi
2022-01-16 16:27:22 +01:00
return 0
fi
2022-02-09 01:49:11 +01:00
echo "Program 'compare' not found."
2022-02-09 02:11:02 +01:00
echo "Be sure to have ImageMagick installed!"
2022-01-16 16:27:22 +01:00
exit 1
}
2022-02-09 19:15:59 +01:00
## -- checking system
2022-01-16 16:27:22 +01:00
check_tex_distro
2022-02-09 02:11:02 +01:00
check_trackschematic
2022-01-16 16:27:22 +01:00
check_imagemagick
2022-02-26 18:29:20 +01:00
if [ ! -d $TESTDIR ] ; then
echo " ${ Red } Do not find test directory! ${ Reset } "
exit 1
fi
2022-01-16 16:27:22 +01:00
2022-02-09 19:15:59 +01:00
if [ "`echo -n`" = "-n" ] ; then
n = ""
c = "\c"
else
n = "-n"
c = ""
fi
2022-02-26 18:29:20 +01:00
## -- Testing
mkdir -p .tex
test_status = 0
# Start with an empty List:
FAILED = ""
2022-01-16 16:27:22 +01:00
2022-02-09 19:15:59 +01:00
if [ $verbose = 1 ] ; then
echo "==========="
echo "Comparison of the expected appearance with the freshly created."
echo "-----------"
fi
for TEST in ` ls $TESTDIR /*.tex` ; do
2022-02-26 18:29:20 +01:00
# setup
2022-02-09 19:15:59 +01:00
FILE = $( basename " $TEST " ) # remove path
NAME = ${ FILE %.* } # remove extension
2022-02-26 18:29:20 +01:00
add_to_list = 0
#
2022-02-09 19:15:59 +01:00
if [ $verbose = 1 ] ; then
2022-02-26 18:29:20 +01:00
echo " $NAME test: "
2022-02-09 01:49:11 +01:00
fi
2022-02-26 18:29:20 +01:00
#
#
## TeX build
#
exit_code_pdflatex = 0
pdflatex -output-directory= .tex -interaction= batchmode -halt-on-error ${ NAME } .tex >> /dev/null 2>& 1 || exit_code_pdflatex = 1
# understanding TeX statistics:
# -> https://tex.stackexchange.com/questions/26208/components-of-latexs-memory-usage
# TOP=$(grep -n "Here is how much of TeX's memory you used:" .tex/${NAME}.log | cut -d: -f1)
# BOTTOM=$(($(grep -n "stack positions out of" .tex/${NAME}.log | cut -d: -f1) + 1))
# awk "NR>$TOP&&NR<$BOTTOM" .tex/${NAME}.log > .tex/${NAME}.statistics.log
memory_usage = $( grep "words of memory out of" .tex/${ NAME } .log | cut -d " " -f2)
memory_usage = $(( $memory_usage / 1000 ))
if [ $exit_code_pdflatex = 0 ] ; then
if [ $verbose = 1 ] ; then
echo $n " - ${ Green } build succesful ${ Reset } : $c "
echo " ${ memory_usage } k of memory used. "
# cat .tex/${NAME}.statistics.log
fi
else
test_status = 1
add_to_list = 1
if [ $verbose = 1 ] ; then
echo " - ${ Red } build failed ${ Reset } . "
fi
2022-02-10 15:22:38 +01:00
fi
2022-02-26 18:29:20 +01:00
#
#
## compare images
#
# compare metrics
# AE: absolute error count, number of different pixels (-fuzz affected)
# DSSIM: structural dissimilarity index
# FUZZ: mean color distance
# MAE: mean absolute error (normalized), average channel error distance
# MEPP: mean error per pixel (normalized mean error, normalized peak error)
# MSE: mean error squared, average of the channel error squared
# NCC: normalized cross correlation
# PAE: peak absolute (normalized peak absolute)
# PHASH: perceptual hash for the sRGB and HCLp colorspaces.
# PSNR: peak signal to noise ratio
# RMSE: root mean squared (normalized root mean squared)
# SSIM: structural similarity index
#
exit_code_compare = 0
compare -metric RMSE -colorspace RGB .tex/${ NAME } .pdf ${ NAME } _expected.pdf NULL: >> /dev/null 2>& 1 || exit_code_compare = 1
if [ $exit_code_compare = 0 ] ; then
if [ $verbose = 1 ] ; then
echo " - ${ Green } comparison succesful ${ Reset } . "
fi
else
test_status = 1
add_to_list = 1
if [ $verbose = 1 ] ; then
echo " - ${ Red } comparison failed ${ Reset } . "
fi
fi
#
#
## if a test failed add to list
if [ $add_to_list = 1 ] ; then
2022-02-27 01:19:56 +01:00
if [ -z " $FAILED " ] ; then
2022-02-26 18:29:20 +01:00
# first item
FAILED = " $NAME "
else
FAILED = " $FAILED , $NAME "
fi
2022-02-09 01:49:11 +01:00
fi
2022-01-16 17:43:56 +01:00
done
2022-01-16 16:27:22 +01:00
2022-02-26 18:29:20 +01:00
## -- finishing
if [ $test_status = 0 ] ; then
if [ $verbose = 1 ] ; then
echo "-----------"
echo " ${ Green } All tests passed! ${ Reset } "
fi
exit 0
else
if [ $verbose = 1 ] ; then
echo "-----------"
echo " ${ Red } The following tests failed: ${ FAILED } ! ${ Reset } "
else
echo " ${ Red } Some or all tests failed! ${ Reset } "
fi
exit 1
2022-02-09 01:49:11 +01:00
fi
2022-02-26 18:29:20 +01:00
EOF