(root)/
libredwg-0.13/
programs/
alive.test
#!/bin/sh
# alive.test
#
# Copyright (C) 2010 Thien-Thi Nguyen
# Copyright (C) 2018-2023 Free Software Foundation, Inc.
#
# This program is free software, licensed under the terms of the GNU
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Thien-Thi Nguyen
# Modified: Reini Urban

# Commentary:

# This is a provisional test until something better comes along.
# It checks "aliveness" and the program returning success, and not
# correctness or performance.
#
# It relies on the TESTS_ENVIRONMENT Makefile.am variable to set
# valid `PROGS'.  Each prog in PROGS is run against a set of example DWGs,
# and if *not* successful the verbose output from -v2 is saved in a
# file whose name is DWG-PROG.log.
#
# If there are no problems, exit successfully.  Otherwise display
# the count of problem runs, the list of *.log files and exit with failure.
# DATADIR can be set to support testing from a separate build dir.

# Code:

PROGS=${PROGS:-"dwgread dwgbmp dwg2dxf dwg2SVG dwglayers dwggrep dwgrewrite dxf2dwg dwgwrite"}
[ -z "$DATADIR" ] && DATADIR="../../test/test-data"
top_builddir=".."
# artefact for "shellcheck"
[ -z "$top_builddir" ] && echo $top_builddir
TESTS_ENVIRONMENT=""
DISABLE_JSON=""
DISABLE_WRITE="0"
EXEEXT=""
WRITE_REVS="r1.4 r2.0 r2.10 r2.6 r9 r10 r11 r13 r14 r2000"
#READ_REVS="r1.2 r1.4 r2.0 r2.10 r2.21 r2.22 r2.4 r2.5 r2.6 r9 r10 r11 r13 r14 r2000 r2004 r2010 r2013 r2018"

i=0
rm -f ./*-dxf*.log ./*-read,.log ./*-write.log ./*-json.log ./*-rw.log
rm -f ./*-bmp.log ./*-grep.log ./*-layers.log ./*-SVG.log ./*-ps.log ./*-svg2.log

#just check that it does not crash
run ()
{
    prog="$1"
    dwg="$2"
    addarg="$3"

    bprog="$(basename "$prog" .exe)"
    bdwg="$(basename "$dwg" .dwg)"
    ddwg="$(dirname "$dwg")"
    if [ "$ddwg" != "." ]; then
        bdwg="$bdwg"_"$ddwg"
    fi
    bprog1=$(echo "$bprog" | sed -e's,^dwg2,,')
    bprog1=$(echo "$bprog1" | sed -e's,^dwg,,')
    if [ -n "$addarg" ]; then
        out=$(echo "$addarg" | sed -e 's,--as=,,')
        log="$bdwg-$out-$bprog1.log"
    else
        log="$bdwg-$bprog1.log"
    fi
    arg="${DATADIR}/$dwg.dwg"
    rm "./$log" 2>/dev/null
    case $bprog in
    dwglayers)  ;;
    dwggrep)    case $dwg in
                    r11/*) arg="-i CONTINUOUS $arg" ;;
                    r*/*)  arg="-i FOO $arg"        ;;
                    *)     arg="-i tekst $arg"      ;;
                esac
                ;;
    dwg2dxf)    if [ -n "$addarg" ]; then
                    arg="-y $addarg -o $bdwg-$out.dxf $arg"
                else
                    arg="-y $arg"
                fi
                ;;
    dwgread)    if [ -z "$DISABLE_JSON" ]; then
                    if [ -n "$addarg" ]; then
                        arg="-v2 $addarg -o $bdwg-$out.json $arg"
                    else
                        arg="-v2 -o $bdwg.json $arg"
                    fi
                else
                    arg="-v2 $arg"
                fi
                ;;
    dwgwrite)   if [ -z "$DISABLE_JSON" ]; then
                    if [ -n "$addarg" ]; then
                        arg="-y -v2 $addarg -o $bdwg-$out.dwg $bdwg.json"
                    else
                        arg="-y -v2 $bdwg.json"
                    fi
                else
                    if [ -n "$addarg" ]; then
                        arg="-y -v2 $addarg -o $bdwg-$out.dwg $dwg.dxf"
                    else
                        arg="-y -v2 $dwg.dxf"
                    fi
                fi
                ;;
    dxfwrite)   if [ -z "$DISABLE_JSON" ]; then
                    if [ -n "$addarg" ]; then
                        arg="-y -v2 $addarg -o $bdwg-$out.dxf $bdwg.json"
                    else
                        arg="-y -v2 $bdwg.json"
                    fi
                else
                    if [ -n "$addarg" ]; then
                        arg="-y -v2 $addarg -o $bdwg-$out.dxf $bdwg.dwg"
                    else
                        arg="-y -v2 $dwg.dwg"
                    fi
                fi
                # no pre-r2 dxf yet
                if [ "$ddwg" = "r1.4" ]; then
                    return
                fi
                ;;
    dxf2dwg)    if [ -n "$addarg" ]; then
                    arg="-y -v2 $addarg -o $bdwg-$out.dwg ${DATADIR}/$dwg.dxf"
                else
                    arg="-y -v2 ${DATADIR}/$dwg.dxf"
                fi
                if [ ! -f "${DATADIR}/$dwg.dxf" ]; then
                    return
                fi
                # no pre-r2 dxf yet
                case $dwg in
                    r1.4/* ) return ;;
                esac
                ;;
    dwgrewrite) if [ -n "$addarg" ]; then
                    arg="-v2 $addarg $arg"
                else
                    arg="-v2 $arg"
                fi
                ;;
    dwgfuzz)    if [ -n "$DISABLE_WRITE" ]; then
                    arg="-dwg $arg"
                else
                    arg="-rw $arg"
                fi
                ;;
    dwgadd)     arg="-o dwgadd.dwg $DATADIR/../../examples/dwgadd.example"
                ;;
    esac
    if [ -n "$VALGRIND" ] && [ -n "$LOG_COMPILER" ]; then
        TESTPROG="$LOG_COMPILER $LOG_FLAGS"
        log="$TEST_SUITE_LOG"
    elif [ -n "" ]; then
        TESTPROG=""
    elif [ -n "$TESTS_ENVIRONMENT" ]; then
        TESTPROG="$TESTS_ENVIRONMENT ${top_builddir}/libtool --mode=execute"
    else
        # libtool only if shared
        TESTPROG="${top_builddir}/libtool --mode=execute"
    fi
    # indxf still leaks, GH #151
    if test "$bprog" = "dxf2dwg" -a -n ""
    then
       TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
    fi
    # load_dwg r2000|r13|r14 leaks
    if test "$bprog" = "load_dwg" -a -n ""
    then
        case $bdwg in
            *_2000 | example_r14 | example_r13 )
                TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
                ;;
            example_201*) # TABLESTYLE handles
                TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
                ;;
        esac
    fi
    if test "$bprog" = "dwgfuzz" -a -n ""
    then
       TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
    fi
    if test "$bprog" = "dwgadd" -a -n ""
    then
       TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
    fi
    if test "$bprog" = "dwgrewrite" -a -n ""
    then
        case $bdwg in
            example_201*) # TABLESTYLE handles
                TESTPROG="env ASAN_OPTIONS=detect_leaks=0 $TESTPROG"
                ;;
        esac
    fi
    echo "$prog $arg 2>$log"
    # shellcheck disable=SC2086
    if $TESTPROG ./$prog $arg >"$log" 2>&1
    then
        rm "./$log"
    else
        todo=
        case $bprog in
            dwgwrite)
                case "$(uname -s)" in
                    CYGWIN*) todo=1 ;;
                    *) ;;
                esac
                ;;
            dwgrewrite)
                case "$(uname -s)" in
                    MINGW*) test -f core && rm core
                            todo=1
                            ;;
                    *) ;;
                esac
                ;;
            *)  ;;
        esac

        if [ -z $todo ]
        then
            i=$((i+1))
            echo "$prog failure $?"
            tail -n100 "$log"
        else
            tmpnum=$((i+1))
            echo "$(basename "$0"): TODO $prog $tmpnum failures"
            ls -l "$log"
            tail -n100 "$log"
        fi
    fi
}

DATA="sample_2000 example_2000 example_2004 example_2007 example_2010"
DATA="$DATA example_2013 example_2018 example_r14 example_r13 r1.4/entities"
TODO="r11/ACEB10 r11/entities-3d r10/entities r2.10/entities r2.6/entities"
[ "$DISABLE_WRITE" = "1" ] || DATA="$DATA $TODO"
if [ -n "$CHECK_MINIMAL" ]; then
    DATA="sample_2000 example_2000 example_2004 example_2007 example_2010 example_2018"
fi

clean ()
{
  for d in $DATA; do
      b="$(basename "$d")"
      rm "./$b.dxf" 2>/dev/null
      rm "./$b.svg" 2>/dev/null
      rm "./$b.ps" 2>/dev/null
      rm "./$b.bmp" 2>/dev/null
      rm "./$b.json" 2>/dev/null
      rm "./$b-rewrite.dwg" 2>/dev/null
      rm "./$b-dwgwrite.dwg" 2>/dev/null
      rm "./${b}_new.dwg" 2>/dev/null
  done
  rm "./entities*.dwg" 2>/dev/null
  rm "./entities*.dxf" 2>/dev/null
  rm "./entities*.json" 2>/dev/null
  rm "./entities.dwg" 2>/dev/null
  rm "./entities-3d.dwg" 2>/dev/null
  rm "./entities-3d_r11.dwg" 2>/dev/null
  rm "./entities-3d_r11.json" 2>/dev/null
  rm "./ACEB10.dwg" 2>/dev/null
  rm "./ACEB10.dxf" 2>/dev/null
  rm "./ACEB10_*.dxf" 2>/dev/null
  rm "./ACEB10_*.json" 2>/dev/null
}

for prog in $PROGS ; do
    case $prog in
    *dwgadd*)
        run "$prog" "."
        ;;
    *)  for dwg in $DATA; do
            run "$prog" "$dwg"
        done
        ;;
    esac
done

# dwgread converts to json here, but has no --as yet
# still disabled, see branch work/2004
if false; then
for prog in dwgwrite dwgrewrite dxf2dwg dwg2dxf dxfwrite ; do
    for dwg in $DATA; do
        for r in $WRITE_REVS; do
            run "$prog${EXEEXT}" "$dwg" "--as=$r"
        done
    done
done
fi

case $PROGS in
    *dwggrep*)
        echo "$TESTPROG dwggrep${EXEEXT} -i -c tekst ${DATADIR}/example_*.dwg"
        n="$($TESTPROG "./dwggrep${EXEEXT}" -i -c tekst "${DATADIR}"/example_*.dwg | tr -d "\r")"
        if test "$n" != "24"; then
            i=$((i+1))
            echo "dwggrep failure $?"
            echo "result $n, expected 24"
        fi
esac

#run dwgread r11/ACEL10

# TODO: check generated DXF's against original DXF's
# skip the run part if dwg2dxf is part of bin_PROGRAMS (i.e. being installed)
# rather see the dxf.test roundtrip test
# only in programs, not examples:
if [ -f cmp_dxf.pl ] && [ -e dwg2dxf ]; then
    for dwg in $DATA; do
        case $dwg in
            r11/*)   ;; # cannot outdxf preR13 yet
            r10/*)   ;; # cannot outdxf preR13 yet
            r2*/*)   ;; # cannot outdxf preR13 yet
            r1.4/*)  ;; # cannot outdxf preR13 yet
            *)       run "dwg2dxf${EXEEXT}" "$dwg" ;;
        esac
        #if [ -f ${DATADIR}/$dwg.dxf ]; then
        #    base="$(basename "$dwg" .dwg)"
        #    perl filt_dxf.pl ${DATADIR}/$dwg.dxf > $base.dxf.orig
        #    perl filt_dxf.pl $base.dxf > $base.dxf.new
        #    diff -bu  $base.dxf.orig $base.dxf.new | perl cmp_dxf.pl
        #    rm $base.dxf.orig $base.dxf.new 2>/dev/null
        #fi
    done
fi

if test "0" = "$i" ; then
    clean
    exit 0
else
    echo "$(basename "$0"): $i failures"
    ls -l ./*.log
    exit 1
fi

# alive.test ends here