#!/bin/sh -e

pkgname=OpenMx
debname=r-cran-openmx

if [ "$AUTOPKGTEST_TMP" = "" ] ; then
    AUTOPKGTEST_TMP=`mktemp -d /tmp/${debname}-test.XXXXXX`
    trap "rm -rf $AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi
cd $AUTOPKGTEST_TMP
cp -a /usr/share/doc/$debname/tests $AUTOPKGTEST_TMP
gunzip -r *
cd tests

hostarch=$(dpkg-architecture -qDEB_HOST_ARCH)
# Ignore single test on armhf and arm64
if [ "$hostarch" = "armhf" ] ; then
cat <<EOT
Clarify issue with upstream or armhf porter team:
=================================================
> library(OpenMx)
To take full advantage of multiple cores, use:
  mxOption(key='Number of Threads', value=parallel::detectCores()) #now
  Sys.setenv(OMP_NUM_THREADS=parallel::detectCores()) #before library(OpenMx)
> 
> test_check("OpenMx")
terminate called after throwing an instance of 'std::runtime_error'
  what():  Problem in dVnames mapping
Aborted

EOT
exit 0
fi
if [ "$hostarch" = "arm64" -o "$hostarch" = "i386" ] ; then
  sed -i -e '/^test_that."RAM"/,/^})/d' \
         -e '/^test_that."mediation"/,/^})/d' \
         -e '/^test_that."LISREL"/,/^})/d' \
         -e '/^test_that."probit+poisson ML+WLS"/,/^})/d' testthat/test-discrete.R
  if [ "$hostarch" = "i386" ] ; then
    sed -i -e '/^expect_error(mxRun(wlsTest4)/,$d' testthat/test-wls-binary.R
  fi
fi

if [ "$hostarch" = "i386" -o "$hostarch" = "ppc64el" ] ; then
  rm -f testthat/test-cor.R
fi

for testfile in *.R; do
    echo "BEGIN TEST $testfile"
    LC_ALL=C.UTF-8 R --no-save < $testfile
done

