#!./common/bats/bin/bats
# -*-sh-*-

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

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

PKG=LIKWID
module=likwid
library=liblikwid
header=likwid.h
rpm=likwid-${LMOD_FAMILY_COMPILER}${DELIM}

check_rms

setup() {
	OUTPUT="$(mktemp)"
}

teardown() {
	rm -f "${OUTPUT}"
}

@test "[perf-tools/LIKWID] Verify $PKG module is loaded and matches rpm version ($LMOD_FAMILY_COMPILER)" {
	module list "${module}" | grep "1) ${module}" >&"${OUTPUT}" || exit 1
	run grep "${module}" "${OUTPUT}"
	assert_success

	# check version against rpm
	local version
	version="$(rpm -q --queryformat='%{VERSION}\n' "$rpm")"
	run cat "${OUTPUT}"
	assert_output "  1) ${module}/$version"
}

@test "[perf-tools/LIKWID] Verify module ${PKG}_DIR is defined and exists ($LMOD_FAMILY_COMPILER)" {
	DIR=${PKG}_DIR

	if [ -z "${!DIR}" ]; then
		flunk "${PKG}_DIR directory not defined"
	fi

	if [ ! -d "${!DIR}" ]; then
		flunk "directory ${!DIR} does not exist"
	fi
}

@test "[perf-tools/LIKWID] Verify module ${PKG}_BIN is defined and exists ($LMOD_FAMILY_COMPILER)" {
	BIN=${PKG}_BIN

	if [ -z "${!BIN}" ]; then
		flunk "${PKG}_BIN directory not defined"
	fi

	if [ ! -d "${!BIN}" ]; then
		flunk "directory ${!BIN} does not exist"
	fi
}

@test "[perf-tools/LIKWID] Verify availability/execution of likwid-perfctr binary ($LMOD_FAMILY_COMPILER)" {
	run which likwid-perfctr
	assert_success

	likwid-perfctr -a >&"${OUTPUT}"

	run ls -l "${OUTPUT}"
	assert_success

	if [ "$(wc -l <"${OUTPUT}")" -lt "3" ]; then
		flunk "no counters available"
	fi
}

# ----------
# Man pages
# ----------

@test "[perf-tools/LIKWID] Verify availability of man page for likwid_avail ($LMOD_FAMILY_COMPILER)" {
	DIR=${PKG}_DIR

	if [ -z "${!DIR}" ]; then
		flunk "${PKG}_DIR directory not defined"
	fi

	if [ ! -d "${!DIR}" ]; then
		flunk "directory ${!DIR} does not exist"
	fi

	run man -w likwid-perfctr
	assert_success
	assert_output "${!DIR}/man/man1/likwid-perfctr.1"
}

# ----------
# Lib Tests
# ----------

@test "[perf-tools/LIKWID] Verify module ${PKG}_LIB is defined and exists ($LMOD_FAMILY_COMPILER)" {
	LIB=${PKG}_LIB

	if [ -z "${!LIB}" ]; then
		flunk "${PKG}_LIB directory not defined"
	fi

	if [ ! -d "${!LIB}" ]; then
		flunk "directory ${!LIB} does not exist"
	fi
}

@test "[perf-tools/LIKWID] Verify dynamic library available in ${PKG}_LIB ($LMOD_FAMILY_COMPILER)" {
	LIB=${PKG}_LIB

	if [ -z "${!LIB}" ]; then
		flunk "${PKG}_LIB directory not defined"
	fi

	if [ ! -s "${!LIB}/${library}.so" ]; then
		flunk "${library}.so does not exist"
	fi
}

@test "[perf-tools/LIKWID] Verify static library is not present in ${PKG}_LIB ($LMOD_FAMILY_COMPILER)" {
	LIB=${PKG}_LIB

	if [ -z "${!LIB}" ]; then
		flunk "${PKG}_LIB directory not defined"
	fi

	if [ -e "${!LIB}/${library}.a" ]; then
		flunk "${library}.a exists when not expecting it"
	fi
}

# --------------
# Include Tests
# --------------

@test "[perf-tools/LIKWID] Verify module ${PKG}_INC is defined and exists ($LMOD_FAMILY_COMPILER)" {
	INC=${PKG}_INC

	if [ -z "${!INC}" ]; then
		flunk "${PKG}_INC directory not defined"
	fi

	if [ ! -d "${!INC}" ]; then
		flunk "directory ${!INC} does not exist"
	fi
}

@test "[perf-tools/LIKWID] Verify header file is present in ${PKG}_INC ($LMOD_FAMILY_COMPILER)" {
	INC=${PKG}_INC

	if [ -z "${!INC}" ]; then
		flunk "${PKG}_INC directory not defined"
	fi

	if [ ! -s "${!INC}/${header}" ]; then
		flunk "${header} file does not exist"
	fi
}
