#!/usr/bin/env -S bats --report-formatter junit --formatter tap
# -*-sh-*-

load ./common/test_helper_functions || exit 1
source ./common/functions || exit 1

if [ -s ./common/TEST_ENV ];then
    source ./common/TEST_ENV
fi

check_rms
rm=$RESOURCE_MANAGER

NODES=2
TASKS=`tasks_count 8`
ARGS=8
LAST_TASK_INDEX=`echo "$TASKS -1" | bc`

export TAU_TRACE=0 TAU_CALLPATH=0 TAU_PROFILE=1 TAU_METRICS=GET_TIME_OF_DAY
unset OMP_NUM_THREADS

@test "[libs/TAU] MPI C binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s C_mpi_test ];then
	flunk "C_mpi_test binary does not exist"
    fi

    run_mpi_binary "tau_exec ./C_mpi_test" "" $NODES $TASKS
    assert_success

    run ls profile.$LAST_TASK_INDEX.0.0
    assert_success

    rm -rf profile*
}

@test "[libs/TAU] MPI C++ binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -z "$SIMPLE_CI" ];then
        skip "Not supported on SIMPLE_CI"
    fi
    if [ "x$ARCH" == "xx86_64" ];then
        export TAU_METRICS=PAPI_LD_INS
    fi
    if [ ! -s CXX_mpi_test ];then
    flunk "CXX_mpi_test binary does not exist"
    fi

    run_mpi_binary ./run_CXX_mpi_test.sh $ARGS $NODES $TASKS
    assert_success

    ls profile.$LAST_TASK_INDEX.0.0
    assert_success

    rm -rf profile*
}

@test "[libs/TAU] Hybrid OpenMP/MPI C binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -z "$SIMPLE_CI" ];then
        skip "Not supported on SIMPLE_CI"
    fi
    if [ "$LMOD_FAMILY_COMPILER" == "intel" ];then
	skip "intel"
    fi
    if [ "x$ARCH" == "xx86_64" ];then
        export TAU_METRICS=PAPI_L1_DCM
    fi
    if [ ! -s C_hybrid_test ];then
	flunk "C_hybrid_test binary does not exist"
    fi

    run_mpi_binary ./run_C_hybrid_test.sh $ARGS $NODES $TASKS
    assert_success

    # test above runs with OMP_NUM_THREADS=4 on 8 MPI tasks
    ls profile.7.0.3
    assert_success

    rm -rf profile*
}

@test "[libs/TAU] Serial FORTRAN binary runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {

    export TAU_TRACE=1 TAU_PROFILE=0
    if [ ! -s F_test ];then
	flunk "F_test binary does not exist"
    fi

    run_serial_binary "tau_exec ./F_test"
    assert_success

    ls tautrace.0.0.0.trc
    assert_success

    rm -f events.0.edf
    rm -f tautrace.0.0.0.trc
}
