#!/bin/sh
set -eu

# Build only the unmodified upstream tests. Resolve libyuv headers and the
# shared library from the installed packages, not from a built source tree.
# build-needed rebuilds with nocheck and therefore does not provide tests.
WORKDIR="${AUTOPKGTEST_TMP}"
GTEST_SRC_DIR=/usr/src/googletest/googletest
TEST_BINARY="${WORKDIR}/libyuv_unittest"

c++ -std=c++17 -pthread -DHAVE_JPEG \
    -I"${GTEST_SRC_DIR}" -I"${GTEST_SRC_DIR}/include" \
    unit_test/*.cc "${GTEST_SRC_DIR}/src/gtest-all.cc" \
    -lyuv -ljpeg -o "${TEST_BINARY}"

dpkg-query -W -f='${binary:Package} ${Version}\n' libyuv0 libyuv-dev
ldd "${TEST_BINARY}" | tee "${WORKDIR}/linkage.txt"
grep -q 'libyuv\.so\.0 => /' "${WORKDIR}/linkage.txt"
"${TEST_BINARY}" --gtest_output="xml:${WORKDIR}/results.xml"
grep -Eq '<testsuites[^>]* tests="[1-9][0-9]*"' "${WORKDIR}/results.xml"
