#=============================================================================
#  
#  testcase_functions - testcase functions for ../bin/semaphore
#  semaphore - a Korn shell implementation of a counting semaphore
#  Copyright (C) 2004 Intel Corporation
#  
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
#  
#  This program 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 General Public License
#  for more details.
#  
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, see http://www.gnu.org/copyleft/gpl.html or
#  write to:
#  
#  The Free Software Foundation, Inc.,
#  59 Temple Place
#  Suite 330, Boston, MA 02111-1307
#  USA
#  
#  Author:
#  
#  John Spurgeon
#  5200 NE Elam Young Parkway
#  Hillsboro, OR 97124
#  
#  john.p.spurgeon@intel.com
#  
#=============================================================================

test_case()
{
	print "COMMAND: $@"
	$@
}

test_case_3()
{
	local semaphore=test-3
	test_case semaphore -I 2 $semaphore
	test_case semaphore -l $semaphore
	test_case semaphore -P $semaphore
	test_case semaphore -l $semaphore
	test_case semaphore -P $semaphore
	test_case semaphore -l $semaphore
	test_case semaphore -P $semaphore &
	sleep 5
	test_case semaphore -V $semaphore
	test_case semaphore -V $semaphore
	test_case semaphore -V $semaphore
	test_case semaphore -l $semaphore
}

test_case_2()
{
	local semaphore=test-2

	test_case semaphore -P $semaphore
	test_case semaphore -l $semaphore
	test_case semaphore -t 0 -P $semaphore
	test_case semaphore -t 0 -s 2 -P $semaphore
	test_case semaphore -t 1 -P $semaphore
	test_case semaphore -t 1 -s 2 -P $semaphore
	test_case semaphore -t 2 -P $semaphore
	test_case semaphore -t 2 -s 2 -P $semaphore
	test_case semaphore -l $semaphore
	test_case semaphore -V $semaphore
	test_case semaphore -V $semaphore
	test_case semaphore -l $semaphore
}

test_case_1()
{
	local semaphore=test-1

	test_case semaphore -w
	test_case semaphore -r
	test_case semaphore -I 1 $semaphore
	test_case semaphore -l $semaphore
	test_case semaphore -r $semaphore
	test_case semaphore -l $semaphore
	test_case semaphore -l
}

test_semaphore()
{
	local NUM_TESTS=3
	test_case_nums=$@

	if [[ -z $test_case_nums ]]
	then
		print "There are $NUM_TESTS test cases in addition to test case 0."
	elif [[ 0 = $test_case_nums ]]
	then
		test_case_num=1
		while (( $test_case_num <= $NUM_TESTS ))
		do
			if is_integer $test_case_num
			then
				if (( $test_case_num <= $NUM_TESTS ))
				then
					test_case=test_case_$test_case_num
					$test_case
					print -n "Press Enter to continue ..."
					read line
				fi
			fi
			test_case_num=$(($test_case_num+1))
		done
	else
		for test_case_num in $test_case_nums
		do
			if is_integer $test_case_num
			then
				if (( $test_case_num <= $NUM_TESTS ))
				then
					test_case=test_case_$test_case_num
					$test_case
					print -n "Press Enter to continue ..."
					read line
				fi
			fi
		done
	fi
}
