#!/bin/bash
set -e

pkg=stopt-examples

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # Double quote below to expand the temporary directory variable now versus
  # later is on purpose.
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/c++/unit "${AUTOPKGTEST_TMP}"

# We are going to run some tests in different families of tests, building them
# with the right flags and running them afterwards.

cd "${AUTOPKGTEST_TMP}/unit/cdf"
g++ -Wall -O3 -I/usr/include/eigen3 -o testFastCDF testFastCDF.cpp -lboost_chrono -lboost_random -lboost_timer -lboost_unit_test_framework -lstopt
./testFastCDF
if [ $? -eq 0 ]; then
    echo "Test testFastCDF PASSED"
else
    echo "Test testFastCDF FAILED"
fi

cd "${AUTOPKGTEST_TMP}/unit/grids"
g++ -Wall -O3 -I/usr/include/eigen3 -o testGridsLegendre testGridsLegendre.cpp -lboost_unit_test_framework -lgeners -lstopt
./testGridsLegendre
if [ $? -eq 0 ]; then
    echo "Test testGridsLegendre PASSED"
else
    echo "Test testGridsLegendre FAILED"
fi

cd "${AUTOPKGTEST_TMP}/unit/regression"
g++ -Wall -O3 -I/usr/include/eigen3 -o testLaplacianKernel testLaplacianKernel.cpp -lboost_chrono -lboost_random -lboost_timer -lboost_unit_test_framework -lstopt
./testLaplacianKernel
if [ $? -eq 0 ]; then
    echo "Test testLaplacianKernel PASSED"
else
    echo "Test testLaplacianKernel FAILED"
fi

cd "${AUTOPKGTEST_TMP}/unit/sparse"
g++ -Wall -O3 -I/usr/include/eigen3 -o testHierarchizationBound testHierarchizationBound.cpp -lboost_unit_test_framework -lstopt
./testHierarchizationBound
if [ $? -eq 0 ]; then
    echo "Test testHierarchizationBound PASSED"
else
    echo "Test testHierarchizationBound FAILED"
fi
