! Tests for the MAXMIN command
!
! @author  Jay Hallam
! @author  J. Czechowski IV
!
startmacro

  call setDatDir

  call test_known_file               ! Tests with a known file
  call test_wave "temp_maxmin.tmp"   ! Tests with a file created in the standard write AUX
  call test_wave "mywave{AUX=RAM}"   ! Tests added for Bug 1088 (MAXMIN: can not access RAM aux...)
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

! Compare use of known files against X-Midas 3.8.X QRC
! 
!   X-Midas> maxmin fftdemo.prm
!    Maximum/abscissa/index  :   259.271820068       9.765625000000E-03       1044.
!    Minimum/abscissa/index  :  3.858375549316E-02  -0.449707031250            103.
!   X-Midas> maxmin apenny.prm
!    Maximum/abscissa/index  :   127.000000000       0.000000000000E+00      10881.
!    Minimum/abscissa/index  :  -127.000000000       0.000000000000E+00          1.
procedure test_known_file
  maxmin IN=^{datDir}.fftdemo.prm MAXOUT=fftmax   MINOUT=fftmin
  maxmin IN=^{datDir}.apenny.prm  MAXOUT=pennymax MINOUT=pennymin

  assert/text="MAXMIN: SIN stats for fftdemo.prm, MAX = ^fftmax" fftmax EQT 259.271820068
  assert/text="MAXMIN: SIN stats for fftdemo.prm, MIN = ^fftmin" fftmin EQT 3.858375549316E-02

  assert/text="MAXMIN: SIN stats for apenny.prm, MAX = ^pennymax" pennymax EQ 127  ! exact (SB data)
  assert/text="MAXMIN: SIN stats for apenny.prm, MIN = ^pennymin" pennymin EQ -127 ! exact (SB data)
return


! Tests with a simple sine wave
procedure test_wave U:testfile
  result numElements 10000

  waveform testfile FORM=SD SHAPE=SIN ELEM=numElements

  maxmin/quiet testfile MAXOUT=max MINOUT=min

  assert/text="MAXMIN: SIN stats for ^{testfile}, MAX = ^max" max EQT 1.0
  assert/text="MAXMIN: SIN stats for ^{testfile}, MIN = ^min" min EQT -1.0

  rem max min
  erase testfile
return