#! /bin/bash
## $Id: copy-check 1989 2005-03-25 02:38:13Z ensc $

# Copyright (C) 2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# 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; version 2 of the License.
#  
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

set -e

dir=$(mktemp -d ${TMP:-/var/tmp}/copy-check.XXXXXX)
trap "rm -rf $dir" EXIT

COPY=${libinternaltestsuitedir:-$(dirname "$0")}/copy

test -x "$COPY"

function execWrap()
{
    nr=$1
    msg=$2
    shift 2

    "$@" || {
	rc=$?
	echo $"Test $nr '$msg' failed with code $rc"
	return $rc
    }

    return 0
}

function doit()
{
    execWrap "$nr" "copying"   $COPY $dir/$nr-in $dir/$nr-out
    execWrap "$nr" "comparing" cmp   $dir/$nr-in $dir/$nr-out
}

## Usage: execCheck <nr> [<dd-args>*]
function execCheck()
{
    local nr=$1
    shift

    dd if=/dev/urandom of=$dir/$nr-in "$@" &>/dev/null

    doit $nr
}

LANG=C

### Check 1: small file copy
execCheck 01 bs=10 count=1

### Check 2: PAGESIZEd file copy
execCheck 02 bs=4096 count=1

### Check 3: PAGESIZE+1 file copy
execCheck 03 bs=4097 count=1

### Check 3a: really large file copy
test x"$ensc_use_expensive_tests" != xyes ||
    execCheck 03a bs=$[ 64 * 1024*1024 + 23 ] count=1

### Check 4: small sparse file copy
execCheck 04 bs=10   count=1 seek=1

### Check 5: large sparse file copy
execCheck 05 bs=4097 count=1 seek=1

### Check 6: large sparse file copy
execCheck 06 bs=4098 count=1 seek=1

### Check 7: large sparse file copy
execCheck 07 bs=4099 count=1 seek=1

### Check 8: large sparse file copy
execCheck 08 bs=4100 count=1 seek=1

### Check 8a: really large sparse file copy
test x"$ensc_use_expensive_tests" != xyes ||
    execCheck 08a bs=$[ 64 * 1024*1024 + 23 ] count=1 seek=1

### Check 9: huge sparse file copy
test x"$ensc_use_expensive_tests" != xyes ||
    execCheck 09 bs=1024 count=1 seek=$[ 1024*1024 * 5 ]

test x"$ensc_test_debug" != xyes || {
    ls -l $dir/* 
    du    $dir/*
}
