sots-engine/tests/mars_parse/build_and_run.sh

60 lines
2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Self-contained build + test for src/mars/parse (plain g++, no CMake needed).
#
# tests/mars_parse/build_and_run.sh
#
# Always runs the unit tests. When SOTS_DATA_DIR points at an extracted
# sots.gob tree it also parses every brace-block and .effect file there and,
# if the reference Python parsers are reachable (SOTS_RE_PARSERS or
# ~/sots-re/verify/parsers), cross-checks each parse against them.
#
# Env: CXX (g++), PYTHON (python3), BUILD_DIR (tests/mars_parse/build),
# SOTS_DATA_DIR, SOTS_RE_PARSERS.
set -euo pipefail
HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT=$(cd "$HERE/../.." && pwd)
BUILD=${BUILD_DIR:-$HERE/build}
CXX=${CXX:-g++}
PYTHON=${PYTHON:-python3}
FLAGS=(-std=c++17 -O2 -Wall -Wextra -Wpedantic -Werror -I"$ROOT/src" -I"$HERE")
LIB=("$ROOT/src/mars/parse/blocks.cpp" "$ROOT/src/mars/parse/effect.cpp" "$ROOT/src/mars/parse/value.cpp")
mkdir -p "$BUILD"
echo "== building unit tests"
"$CXX" "${FLAGS[@]}" "${LIB[@]}" "$HERE/canon.cpp" "$HERE/test_main.cpp" \
"$HERE/test_value.cpp" "$HERE/test_blocks.cpp" "$HERE/test_effect.cpp" \
-o "$BUILD/unit_tests"
echo "== running unit tests"
"$BUILD/unit_tests"
if [ -z "${SOTS_DATA_DIR:-}" ]; then
echo "== SOTS_DATA_DIR not set: skipping real-data oracle test"
exit 0
fi
if [ ! -d "$SOTS_DATA_DIR" ]; then
echo "== SOTS_DATA_DIR=$SOTS_DATA_DIR is not a directory" >&2
exit 1
fi
echo "== building oracle_check"
"$CXX" "${FLAGS[@]}" "${LIB[@]}" "$HERE/canon.cpp" "$HERE/oracle_check.cpp" -o "$BUILD/oracle_check"
ORACLE="$BUILD/oracle"
rm -rf "$ORACLE"
echo "== running reference parsers (dump.py)"
set +e
"$PYTHON" "$HERE/oracle/dump.py" "$SOTS_DATA_DIR" "$ORACLE" ${SOTS_RE_PARSERS:+--parsers "$SOTS_RE_PARSERS"}
rc=$?
set -e
if [ $rc -eq 3 ]; then
echo "== reference parsers not found: parsing real data without cross-check"
exec "$BUILD/oracle_check" "$SOTS_DATA_DIR"
elif [ $rc -ne 0 ]; then
echo "== dump.py failed (rc=$rc)" >&2
exit $rc
fi
echo "== cross-checking against the oracle"
exec "$BUILD/oracle_check" "$SOTS_DATA_DIR" "$ORACLE"