#!/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/checkForMemoryLeaks
##	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 valgrind on a number of examples to detect possible memory leaks

function callValgrind {
	echo -n "Checking for memory leaks in $2 $3... "

	valgrind --tool=memcheck --leak-check=yes --error-exitcode=42 -q $2 $3 > dummy.txt;
	retVal=$?;
	rm -rf dummy.txt;

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


cd ..;
make examples testing;
cd testing;

counter=0;

# run examples in /examples
callValgrind $counter ../bin/example1;
callValgrind $counter ../bin/example1a;
callValgrind $counter ../bin/example1b;
callValgrind $counter ../bin/example2;
callValgrind $counter ../bin/example3;
callValgrind $counter ../bin/example3b;
callValgrind $counter ../bin/example4;
callValgrind $counter ../bin/example5;
callValgrind $counter ../bin/exampleLP;
callValgrind $counter ../bin/qrecipe;
callValgrind $counter ../bin/qrecipeSchur;

# run examples in /testing ...
callValgrind $counter ../bin/test_matrices;
callValgrind $counter ../bin/test_matrices2;
callValgrind $counter ../bin/test_indexlist;
callValgrind $counter ../bin/test_example6;
callValgrind $counter ../bin/test_example7;
callValgrind $counter ../bin/test_infeasible1;
callValgrind $counter ../bin/test_sebastien1;
callValgrind $counter ../bin/test_vanBarelsUnboundedQP;
callValgrind $counter ../bin/test_janick1;
callValgrind $counter ../bin/test_janick2;
callValgrind $counter ../bin/test_constraintProduct1;
callValgrind $counter ../bin/test_constraintProduct2;
callValgrind $counter ../bin/test_guessedWS1;
callValgrind $counter ../bin/test_externalChol1;
callValgrind $counter ../bin/test_runAllOqpExamples;


# ... including testbench with different settings
callValgrind $counter ../bin/test_bench Odd; # default dense
callValgrind $counter ../bin/test_bench Ods; # default sparse
callValgrind $counter ../bin/test_bench Omd; # MPC dense
callValgrind $counter ../bin/test_bench Ors; # reliable dense


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
