!******************************************************************************!
! Test support for specifying prescale information the file qualifier
!
! @bug      NXM-3078 PLOT: Does not display zero-padded Pre-D data
! @since    3.9.2
! @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)
  switch DEBUG       debug    GET DEF=FALSE SDEF=TRUE

  switch RANDOM      randomPercent GET DEF=100   SDEF=10    ! get percentage of random frames/blocks to use

  if debug then
    res debugswitch "/DEBUG=PRESCALE"   ! todo use this in all test cases
  else
    res debugswitch ""
  endif

  res scaletbl_outside {} ! outside pipe
  res scaletbl_pipe     {} ! inside pipe

  if randomPercent lt 21 then
    res possibleWarn "WARN: View Y Min and Y Max both set to ..."
    info "When doing only a random sampling for scaling, it is 'okay' to see '^{possibleWarn}' as it does not always result in good scale values"
  endif

  res zero_padded_data ram:data
  res psd_file_name nxm.sys.test.test2000_psd.prm

  ! create the 1000 data
  call generate1000data
  ! create copies so that there are no race conditions for each test
  file copy/f zero_padded_data zero_padded_data2
  file copy/f zero_padded_data zero_padded_data3

  if interAct then
    set instructions "A large zero padded 1000 file is plotted with plot/all in various scenarios\n\n&
                      Left Column - outside a pipe\n\n&
                      Right Column - inside a pipe\n\n&
                      Top Right  - no prescaling\n             &
                                     (bad)\n\n&
                      The Rest - use prescaling\n           & 
                                     (good & all match)"
   res testingOutsidePipe true
   if testingOutsidePipe then
   ! **********  1000 FILE TESTING - OUTSIDE A PIPE************************
   ! Show what it is like without the fix
   plot/id=outnofix/all/bg zero_padded_data{LAYER={COLOR=RED}}
   ! Use external pre-scale to save scale values in both a scale table and default KEYWORD
   prescaler/random=^randomPercent IN=zero_padded_data SCALETABLE=scaletbl_outside   ! making 2 separate commands until timing issues fixed
   prescaler/random=^randomPercent IN=zero_padded_data /SCALEKW
   ! plot based on the default keyword
   plot/id=outkw^debugswitch/all/bg FILE=zero_padded_data{PRESCALE=KW,LAYER={COLOR=PINK}}
   ! plot based on the scale table
   plot/id=outext^debugswitch/all/bg FILE=zero_padded_data{PRESCALE={TYPE=EXT,SCALETABLE=scaletbl_outside},LAYER={COLOR=AQUA}}

   ! Use plot's internal prescaler and make a copy (and check the copy)
   plot/id=outinternalcopy^debugswitch/all/bg FILE=zero_padded_data{PRESCALE={OUTCOPY=plot_makes_copy.tmp},LAYER={COLOR=TEAL}}
   pause 0.1
   assert/text="^reg.main.name: plot internal prescaler makes a copy with correct header" zero_padded_data FEQ/H plot_makes_copy.tmp
   assert/text="^reg.main.name: plot internal prescaler makes a copy with correct data" zero_padded_data FEQ/D plot_makes_copy.tmp

   endif

   ! **********  1000 FILE TESTING - INSIDE A PIPE************************
   set this.timer(0) 1
   pipe on
     panel/msgid=MAIN/setup/controls=gc
     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 20 /title

     noop zero_padded_data3 _zero_padded_in

     ! external prescaler in pipe sets setuptable
     prescaler/random=^randomPercent IN=_zero_padded_in OUT=ram:prescale_out SCALETABLE=scaletbl_pipe
     plot/id=pipeext^debugswitch/all ram:prescale_out{PRESCALE={TYPE=EXTERNAL,SCALETABLE=scaletbl_pipe},LAYER={COLOR=WHITE}}
     ! without prescaling when reading from file in pipe
     plot/id=pipebad/all^debugswitch ram:prescale_out{LAYER={COLOR=YELLOW}} 

     ! internal prescaler on piped data
     ! plot/id=pipeinternal2^debugswitch/all _zero_padded_in{PRESCALE={TYPE=KW,RANDOM=15,OUTCOPY=ram:prescale_out},LAYER={COLOR=GREEN,COLOR=ORANGE}}
     ! closer to Jay's scenario - in pipe section but actual file
     plot/id=pipeinternal^debugswitch/all zero_padded_data3{PRESCALE={TYPE=KEYWORD,RANDOM=20},LAYER={COLOR=BLUE}}

   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"
    message FUNC=send ID=outnofix NAME=EXIT
    message FUNC=send ID=outkw NAME=EXIT
    message FUNC=send ID=outext NAME=EXIT
    message FUNC=send ID=outinternalcopy NAME=EXIT
    pause 0.1
    pipe stop
  elseif msg.name eqs "TIMER" then
    set this.timer(0) 0
    !say "message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
    res pane1 reg.win.outnofix
    invoke ,, pane1.attach(reg.outnofix,"OUTNOFIX")
    res pane2 reg.win.outkw
    invoke ,, pane2.attach(reg.outkw,"OUTKW")
    res pane3 reg.win.outext
    invoke ,, pane3.attach(reg.outext,"OUTEXT")
    res pane4 reg.win.outinternalcopy
    invoke ,, pane4.attach(reg.outinternalcopy,"OUTINTERNALCOPY")
  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

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure generate1000data
    ! Generate 0-padded type 1000 data
    waveform ram:f1 FORM=CI ELEM=67500  SHAPE=CONST AMP=0  !DR value 67500
    waveform ram:f2 FORM=CI ELEM=30500  SHAPE=CONST AMP=0  !DR value 30500
    waveform ram:f3 FORM=CI ELEM=672000 SHAPE=CONST AMP=1  !DR value 672000
    waveform ram:f4 FORM=CI ELEM=30000  SHAPE=CONST AMP=0  !DR value 30000
    waveform ram:f5 FORM=CI ELEM=7000   SHAPE=CONST AMP=1  !DR value 7000
    
    mergefile ram:f1 zero_padded_data
    mergefile ram:f2 zero_padded_data
    mergefile ram:f3 zero_padded_data
    mergefile ram:f4 zero_padded_data
    mergefile ram:f5 zero_padded_data

return
