startmacro
!------------------------------------------------------------------------------!
!  Test the file trimmers
!
!  Dependencies
!    1) noop command
!    2) status command
!
!  Set up the file names
!
!   Author : J. Hallam
!
!------------------------------------------------------------------------------!

! Only change aux.dat when dealing the specialty case of dat being at nxm/sys/test/dat
! Setting aux.dat when run in nextopt does not behave as expected - NXM-3809 since NeXtMidas 4.1.1
call setDatDir
if datDirSlash eqs "nxm/sys/test/dat" then
  res origDatAux "^{aux.dat}"
  invoke nm_root nxm.sys.lib.Shell.getNmRoot()
  res aux.dat "^{nm_root}^{datDirSlash}"
endif

result infile "temp_test_trimmers_input"
result outfiles {index="test_trimmers_index",&
abscissa="test_trimmers_abscissa",relative="test_trimmers_relative",withqual="test_trimmers_and_quals"}

! Create a test file (in 3.3.4: added xdelta != 1.0 for more accurate abscissa trimming test)
waveform out=infile elem=100 delta=0.1

! Test the index (n:m) trimmer
call test_index infile outfiles.index

! Test the relative (n:+m) trimmer
call test_relative infile outfiles.relative

! Test the abscissa (~OFFSET:~NELS) trimmer
call test_abscissa infile outfiles.abscissa

! Test trimmers with qualifiers like (~n:~m){AUX=DAT}
if "^{reg.shell.class}" eqs "class nxm.sys.libxm.XmShell" then
  info "^reg.main.name: from X-Midas, bypassing trimmers {AUX=DAT} test"
else
  call test_trimmers_with_qualifiers "apenny.prm" outfiles.withqual
endif
! Clean up test files
erase infile
foreach testtype INTABLE outfiles
  result tempfile ^outfiles.^testtype
  if tempfile FEXISTS erase/quiet tempfile
endfor

if origDatAux rexists then res aux.dat "^origDatAux"  ! restore original AUX.DAT

endmacro

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
procedure test_index s:infile s:outfile
!
!  A test for index addressing trimmers
!
noop in=infile(0:10) out=outfile
status outfile size=size

ASSERT/TEXT="TRIMMERS: test index, file size" size eq 10

ASSERT/TEXT="TRIMMERS: test index, file elements " &
         file(infile).data(0) EQ file(outfile).data(0) &
     AND file(infile).data(9) EQ file(outfile).data(9)

erase "^outfile"

! Misuse trimmers to create a zero-length file
remove size
noop in=infile(1:1) out=outfile
status outfile size=size

ASSERT/TEXT="TRIMMERS: test index, zero-length file trim (1:1)" size eq 0

return
!******************************************************************************!
procedure test_relative s:infile s:outfile
!
!  A test for relative addressing trimmers
!
noop in=infile(0:+10) out=outfile

status outfile size=size

ASSERT/TEXT="TRIMMERS: test relative, file size" size eq 10

ASSERT/TEXT="TRIMMERS: test relative, file elements" &
         file(infile).data(0) EQ file(outfile).data(0) &
     AND file(infile).data(9) EQ file(outfile).data(9)

return
!******************************************************************************!
procedure test_abscissa s:infile s:outfile
!
!  A test for relative addressing trimmers
!
status infile xd=xdelta
result startValue xdelta*1
result endValue xdelta*11
noop in=infile(~startValue:~endValue) out=outfile

status outfile size=size

ASSERT/TEXT="TRIMMERS: test abscissa, file size "/stat=status size eq 10

ASSERT/TEXT="TRIMMERS: test abscissa, file elements " &
         file(infile).data(1)  EQ file(outfile).data(0) &
     AND file(infile).data(10) EQ file(outfile).data(9)

return

!******************************************************************************!
procedure test_trimmers_with_qualifiers s:ttwq_infile s:outfile
  noop in=ttwq_infile(0:10){aux=dat} out=outfile
  set size FILE(outfile).size
  ASSERT/TEXT="TRIMMERS: trimmers with qualifiers, file size "/stat=status size eq 10

return
