!******************************************************************************!
! 1. Test launching many realtime plots (either line of raster)
! 2. Test the {MAXOUTLETS=} qualifier on a pipe to set max number of pipe readers
!    (which currently is still limited to upper MAX of 31).
! 3. Test's PANEL/GRID={..} switch
! 4. Test registry performance.
! 5. Added testing of PX (percentage based autoGrid from Jeff NXM-4029) in 4.1.3
!
! @author:  Neon Ngo
!******************************************************************************!
startmacro L:numPlots[60]
  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)
  switch "SRATE"       sr       GET DEF=8000
  switch "SFREQ"       sfreq    GET DEF=sr/10
  switch "PLOTTYPE"    plottype GET DEF="LINE"
  switch "PLOTFS"      fs4plot  GET DEF=128

  if interAct then
    set instructions "There should be ^numPlots realtime ^plottype PLOTs with data flowing in all of them.\n\n&
       The plot widths should vary slightly 75%, 125% back and forth."
    set numCols 10
    calc numRows numPlots numCols /
    set maxPipeReaders 31 ! current Pipe implementation's upper max

    pipe on
      if numCols EQ 10 then
        panel/msgid=MAIN/controls=gc/setup/grid={nx=^numCols,ny=^numRows,name=GP,title="plot",px="(0.075,0.125,0.075,0.125,0.075,0.125,0.075,0.125,0.075,0.125,0.075,0.125)"}
      else
        panel/msgid=MAIN/controls=gc/setup/grid={nx=^numCols,ny=^numRows,name=GP,title="plot"}
      endif
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text  idtext "Instructions" instructions 24 8 /title

      calc maxOpenTime 0.05 numPlots *
      timer set
      do ii 1 numPlots
        calc/type=L jj ii maxPipeReaders 1 + / 1 +
        if reg.wave^{jj} nrexists then
          waveform/id=wave^jj/throttle=real _wave^{jj}{maxOutlets=^maxPipeReaders} SI infinity white freq=sfreq amp=1000 delta=1/sr
        endif
        plot/nice/id=GP^{ii} _wave^{jj}{fs=^fs4plot} type=plottype axis=frame
      enddo
      timer elapse elapseTime
      assert/text="^reg.main.name: performance of opening many primitives (^elapseTime sec)" elapseTime <= maxOpenTime
    pipe off

  endif
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
  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
