#!/bin/sh

# Test vtwrite, vtread

. ./testlib

RDIR=$TMP.rdir

cleanup(){
   [ -d $RDIR ] && {
      rm -f $RDIR/* 
      rmdir $RDIR
   }
}

mkdir -p $RDIR

OPTS="-r 48000 -sa=0.3.f=1000 -T2010-01-01_23:59:55,+30"

# f8
rm -f $RDIR/1001*
vtgen $OPTS | vtwrite -G10 $RDIR

ns=`vtread $RDIR | sample_count_sr - 1`
test "$ns" -eq $((48000 * 30))
result "t1 $ns"

# i4
rm -f $RDIR/1001*
vtgen $OPTS -- -,i4 | vtwrite -G10 $RDIR

ns=`vtread $RDIR | sample_count_sr - 1`
test "$ns" -eq $((48000 * 30))
result "t2 $ns"

# i2
rm -f $RDIR/1001*
vtgen $OPTS -- -,i2 | vtwrite -G10 $RDIR

# All data
ns=`vtread $RDIR | sample_count_sr - 1`
test "$ns" -eq $((48000 * 30))
result "t3 $ns"

# Last block in one file

ns=`vtread -T2010-01-01_23:59:59.8,+0.18 $RDIR | sample_count_sr - 1`
test "$ns" -eq 8641
result "t4a $ns"

# Last block, running on to next file

ns=`vtread -T2010-01-01_23:59:59.1,+6.8 $RDIR | sample_count_sr - 1`
test "$ns" -eq 326400
result "t4b $ns"

# Request end timestamp beyond available data

ns=`vtread -T2010-01-01_23:59:59.1,+50 $RDIR | sample_count_sr - 1`
test "$ns" -ge 1243199 -a "$ns" -le 1243200
result "t4c $ns"

# Request start timestamp earlier than first data available

ns=`vtread -T2010-01-01_23:59:50,+10 $RDIR | sample_count_sr - 1`
test "$ns" -eq 240001
result "t4d $ns"

# Last block, where start timestamp later than next file date

ns=`vtread -T2010-01-02_00:00:00.05,+1 $RDIR | sample_count_sr - 1`
test "$ns" -eq 48000
result "t4e $ns"

# Start end end timestamps withing same data block

ns=`vtread -T2010-01-01_23:59:55.05,+10e-3 $RDIR | sample_count_sr - 1`
test "$ns" -eq 480
result "t4f $ns"

# Test break in stored data

OPTS="-r 48000 -sa=0.3.f=1000 -T2010-01-02_00:00:40,+10"
vtgen $OPTS -- -,i2 | vtwrite -G10 $RDIR

ns=`vtread $RDIR | sample_count_sr - 1`
test "$ns" -eq $((48000 * 40))
result "t5a $ns"

# Begin request in the break

ns=`vtread -T2010-01-02_00:00:35,+6 $RDIR | sample_count_sr - 1`
test "$ns" -eq 48001
result "t5b $ns"

# End request in the break

ns=`vtread -T2010-01-02_00:00:21,+10 $RDIR | sample_count_sr - 1`
test "$ns" -eq 191999
result "t5c $ns"

cleanup


