#!/bin/ksh
#
# Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
#
#       Dump the same holey file a random number of times, each time
#       using one less extent structure than the previous time.  
#	The grep and diff code checks to make sure that you get the
#	same answer each time, no matter how many extents you use.

typeset -i offset
typeset -i length
typeset -i max

# Verify correct usage
if [[ $# != 2 ]] 
then print "usage:  ${0##*/} bindir testdir"
     exit 1
fi

# Check bindir for needed programs.
if [[ ! ( -r "$1/wf" ) ]]
then print "Aborting: necessary program wf is not in $1/."
     if [[ ! ( -r "$1/dump_allocinfo" ) ]]
     then print "          necessary program dump_allocinfo is also missing."
     fi
     exit 1
fi
if [[ ! ( -r "$1/dump_allocinfo" ) ]]
then print "Aborting: necessary program dump_allocinfo is not in $1/."
     exit 1
fi

print "Multiple-buffer-sizes test of get_allocinfo beginning..."

RANDOM=$SECONDS
offset=0
length=$RANDOM
filename=DMAPI_alloc_test_2
((max = $RANDOM/256))
count=$max

# Holey file creation: put about $max holes into the file.
while (( count > 0 ))
do 
	$1/wf -l $length -L $offset -b 512 $2/$filename > /dev/null
	(( offset = RANDOM * 512 + offset + length )) 
	(( length = RANDOM ))
	(( count = count - 1 ))
done
count=$max

# Now count down from $max to 1 and try dump_allocinfo with a buffer that
# can hold that many dm_extent_t structures.
while [[ $count > 0 ]]
do
        $1/dump_allocinfo -n $count $2/$filename | grep '\[' > $2/x.$count
        if (( count < max ))
        then diff $2/x.$count $2/x.$max > /dev/null
	     if [[ $? != 0 ]]
	     then print "ERROR in trial #$count:"
		  diff $2/x.$count $2/x.$max
	     fi
	     rm $2/x.$count
        fi
        (( count = count - 1 ))
done

rm $2/x.$max
rm $2/$filename

print "Test complete."
