#!/bin/sh

# Author: Lance Lin <LQi254@protonmail.com>
#         Pierre Gruet <pgt@debian.org>
# Date: 18 February 2022
#       3 May 2022
# File: run-unit-test
# Purpose: run a simple-unit test for the ngs-sdk package
#
# Details:
# Run the tests after building them against the libs in the -dev package.

set -e

pkg=ngs-sdk

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a ngs-sdk/test/ "${AUTOPKGTEST_TMP}"
cd "${AUTOPKGTEST_TMP}/test"

# Building the test-engine lib
cd test_engine
g++ -c -I/usr/include/ngs/unix/generic -Wall -fpic *.cpp
g++ -shared -o libtest_engine.so *.o

# Building the test file against the lib and the test-engine lib.
cd ../ngs-test
g++ -I../../ -I/usr/include/ngs/unix/generic -L../test_engine -Wall -o test main.cpp -ltest_engine -lngs-c++ -lngs-adapt-c++

# Running the tests and collecting the results in outputTest
LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:../test_engine" ./test > outputTest

# Now outputTest should contain something like
# "Test cases run: 107
# Test cases passed: 107"
# So we remove everything before the colons, remove duplicates and check there
# remains only one line in the file (i.e. the count of passing tests is the
# same as the count of tests).
sed 's/.*: //' outputTest | uniq | wc -l | grep -q "1"
if [ $? -eq 0 ]; then
  echo "All tests PASSED"
fi
