#!/usr/bin/env bash

##
##	This file is part of qpOASES.
##
##	qpOASES -- An Implementation of the Online Active Set Strategy.
##	Copyright (C) 2007-2015 by Hans Joachim Ferreau, Andreas Potschka,
##	Christian Kirches et al. All rights reserved.
##
##	qpOASES is free software; you can redistribute it and/or
##	modify it under the terms of the GNU Lesser General Public
##	License as published by the Free Software Foundation; either
##	version 2.1 of the License, or (at your option) any later version.
##
##	qpOASES is distributed in the hope that it will be useful,
##	but WITHOUT ANY WARRANTY; without even the implied warranty of
##	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
##	See the GNU Lesser General Public License for more details.
##
##	You should have received a copy of the GNU Lesser General Public
##	License along with qpOASES; if not, write to the Free Software
##	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
##



##
##	Filename:  testing/runUnitTests
##	Author:    Hans Joachim Ferreau
##	Version:   3.2
##	Date:      2014-2015

# defining colors for output
red='\e[0;31m'
green='\e[0;32m'
NC='\e[0m' # No Color

# runs a number of examples to detect malfunctioning of the code

function runTest {
	echo -n "Running unit test $2 $3... "

	$2 $3 > dummy.txt;
	retVal=$?;
	rm -rf dummy.txt;

	if [ $retVal == 0 ]; then
		echo -e "${green}passed!${NC}"
	else
		if [ $retVal == 99 ]; then
			echo "problem data missing!"
		else
			echo -e "${red}failed!${NC}"
			counter=$[counter+1];
		fi
	fi
}


cd ..;
make testing;
cd testing;

counter=0;

# run unit tests
runTest $counter ../bin/test_matrices;
runTest $counter ../bin/test_matrices2;
runTest $counter ../bin/test_matrices3;
runTest $counter ../bin/test_indexlist;

runTest $counter ../bin/test_example1;
runTest $counter ../bin/test_example1a;
runTest $counter ../bin/test_example1b;
runTest $counter ../bin/test_example2;
runTest $counter ../bin/test_example4;
runTest $counter ../bin/test_example5;
runTest $counter ../bin/test_exampleLP;
runTest $counter ../bin/test_qrecipe;
runTest $counter ../bin/test_qrecipeSchur;
runTest $counter ../bin/test_infeasible1;
runTest $counter ../bin/test_hs268;

runTest $counter ../bin/test_example6;
runTest $counter ../bin/test_example7;
runTest $counter ../bin/test_sebastien1;
runTest $counter ../bin/test_vanBarelsUnboundedQP;
runTest $counter ../bin/test_janick1;
runTest $counter ../bin/test_janick2;
runTest $counter ../bin/test_constraintProduct1;
runTest $counter ../bin/test_constraintProduct2;
runTest $counter ../bin/test_guessedWS1;
runTest $counter ../bin/test_externalChol1;
runTest $counter ../bin/test_gradientShift;
runTest $counter ../bin/test_runAllOqpExamples;

runTest $counter ../bin/test_bench Odd;
runTest $counter ../bin/test_bench Ods;
#runTest $counter ../bin/test_bench Ord;
#runTest $counter ../bin/test_bench Ors;
runTest $counter ../bin/test_bench Omd;
#runTest $counter ../bin/test_bench Oms;


if [ $counter == 0 ]; then
	echo -e "${green}All available tests passed successfully!${NC}"
else
	echo -e "${red}$counter test(s) failed!${NC}"
	exit 1
fi
