!******************************************************************************!
! Test case description/purpose: FFT Restart Changes NAVG
! This will test that the value for NAVG is not changed when restarts occur.
! The fix prevents any corruption of the fft.navg field.
!
! Arguments: None
!
! @bug      NXM-3120 FFT Restart Changes NAVG
! @since    NeXtMidas 3.9.0
! @author:  Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

   set format sf
   set sr 1e4
   set over 0
   set sfreq 1e3
   set amp 1e4
   set noiseamp 1e2
   set fsize 512         ! default frame size
   set numelem 300k
   set freq 1e3
   set count 0
   set orig_navg 8
   invoke tcur nxm.sys.lib.Time.current()

    pipe on
      waveform/id=wave/throttle=REAL _wave format elem=numelem cos freq=sfreq amp=amp delta=1/sr start=^tcur /TL=^fsize
      FFT,IN1=_wave,OUT1=_fft,NFFT=^fsize,WIN=HANN,OVER=0,NAVG=^orig_navg /psd
      nxm.sys.test.msgsender/id=msgsend/msgid=MAIN IN=_wave NAME=FFTRESTART RATE=10 DATA="Time to restart the FFT" /TL=^fsize
    pipe off

endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "FFTRESTART" then
    calc count count 1 +
    if count eq 30 then
      assert/text="^reg.main.name: navg was not affected by resstart" TRUE ISTRUE
      pipe stop
    endif
    if freq eq 1e3 then
      res freq 2e3
    else
      res freq 1e3
    endif
    set reg.wave.freq  ^freq
    set reg.wave.state "RESTART"

    res navg reg.fft.navg

    if navg neq orig_navg
      assert/text="^reg.main.name: navg (^{navg}) was not affected by restart (original navg:^{orig_navg})" navg eq ^orig_navg
      pipe stop
    endif
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return