#!/bin/sh
#
# Run instruction testcase and compare results.
#
# Jani Salonen <salojan@goto10.co>
#

export LC_ALL=C

if [ "${2}" = "" ]; then
	echo ""
	echo "Usage ${0} PROGRAM TESTCASE"
	echo ""
	echo "To produce PROGRAM:"
	echo ""
	echo " $ ./build/ano tests/instruction/t_basic_set.ano > engine/dsl_ano.h"
	echo " $ make"
	echo " $ ${0} PROGRAM basic"
	echo ""

	exit 0
fi

# Temporary file for testcase result
#
_o="/tmp/$$.rc"

for i in ${2}; do
	if [ ! -f "tests/instruction/t_${i}_set.rc" ]; then
		echo "${0}: Failed to find test result template for testcase ${i}." 1>&2

		continue
	fi

	# Check testcase binary
	#
	_v=$(./"${1}" -V | grep '^@ANO_SCRIPT_NAME:' | awk '{print $2}')

	if [ "${_v}" != "test_${i}_inst" ]; then
		echo "${0}: Binary script name ${_v} is not expected test_${i}_inst." 1>&2

		continue
	fi

	# Run testcases
	#
	./"${1}" > "${_o}"

	# Check if diff --color works
	#
	_c="--color=always"

	diff ${_c} /dev/null /dev/null > /dev/null 2>&1

	if [ $? != 0 ]; then
		_c=""
	fi

	diff ${_c} \
		--side-by-side \
		--suppress-common-lines \
		"tests/instruction/t_${i}_set.rc" "${_o}"

	_r=$?

	# Check testcase result
	#
	rm -f "${_o}"

	if [ "${_r}" != 0 ]; then
		echo " ! Test case ${i} failed."
	fi
done

#
exit 0
