#!/bin/bash
# Usage: gencontrol-cpptest.sh [SEARCH_ROOT]
#   SEARCH_ROOT: directory to scan for test binaries (default: /usr/lib/libtorch-test/).
# Emitted Test-Command paths always reference the installed location
# (/usr/lib/libtorch-test/) regardless of SEARCH_ROOT.
set -e
SEARCH_ROOT="${1:-/usr/lib/libtorch-test/}"
INSTALL_PATH="/usr/lib/libtorch-test"

echo "# Generating Autopkgtest Test Cases for the C++ Testing Programs"
FILES=( $(find "${SEARCH_ROOT}" -type f -executable | LC_ALL=C sort) )
echo "# Found" ${#FILES[@]} "tests"
echo "#"

# Add the test names that are known to fail or flaky
PERMISSIVE_LIST=(
inline_container_test
pow_test
native_test
op_registration_test
vulkan_api_test
)

for (( i = 0; i < ${#FILES[@]}; i++ )); do
	BASENAME="$(basename ${FILES[$i]})"
	echo "# C++ test $((${i}+1))/${#FILES[@]}"
	if echo ${PERMISSIVE_LIST[@]} | grep -o "${BASENAME}" >/dev/null 2>/dev/null; then
		echo "Test-Command: ${INSTALL_PATH}/${BASENAME} || true"
	else
		echo "Test-Command: ${INSTALL_PATH}/${BASENAME}"
	fi
	echo "Depends: build-essential, ninja-build, libtorch-dev, libtorch-test"
	echo "Features: test-name=$((${i}+1))_of_${#FILES[@]}__cpptest__${BASENAME}"
	echo "Restrictions: allow-stderr"
	echo ""
done
