# File     : test_support
# Purpose  : support for GYRE test scripts

# Get the platform architecture

get_arch ()
{

    local hardware=`uname -m`
    local os=`uname -s`

    echo -n "${hardware}-${os}"

}

# Run GYRE

run_gyre ()
{

    local exec=$1
    local file=$2
    local label=$3

    if [ -n "$label" ]; then
	echo -n "TEST $label"
    fi

    $exec $file > /dev/null
    retval=$?

    if [ $retval -ne 0 ]; then
	echo " ...failed during execution of $exec"
    fi

    return $retval

}

# Check output

check_output ()
{

    local relerr=$1
    local fields=$2
    local file_a=$3
    local file_b=$4

    if [ ! -e "$file_b" ]; then

        local arch=$(get_arch)

    	file_b="ref/${file_a}.${arch}"

	if [ ! -e "$file_b" ]; then
	    file_b="ref/${file_a}"
	fi

    fi

    if [ -z "$relerr" ]; then
	ndiff -quiet -fields $fields $file_a $file_b
	retval=$?
    else
	ndiff -quiet -relerr $relerr -fields $fields $file_a $file_b
	retval=$?
    fi	

    if [ $retval -ne 0 ]; then
	echo " ...failed when comparing $file_a and $file_b"
    fi

    return $retval

}
