!******************************************************************************!
! Testing for 12-bit SJ Data Type conversion to other types
! Also tests FCALC of SJ, but THIN and LIST are NOT supported
! Note: Thisn test should be run with and without the /NONATIVE switch
!
! Arguments: TL (must be even)
!
! @bug      NXM-3894  12-bit integer Midas Data Type needed
! @since    4.1.2
! @author:  Jeanette McCall
!******************************************************************************!
startmacro L:TL[2]
  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 NONATIVE    nonative GET DEF=FALSE SDEF=TRUE  ! run with all Java

  res savedUseNative env.usenative
  if nonative then
    env set usenative |
  else
    env set usenative |IO|Math|Cast|Convert|Other|
  endif

  res t1000sj       jeff1000sj.tmp
  res t1000sifromsj jeff1000sifromsj.tmp
  res t1000slfromsj jeff1000slfromsj.tmp
  res t1000sxfromsj jeff1000sxfromsj.tmp
  res t1000sbfromsj jeff1000sbfromsj.tmp
  res t1000sdfromsj jeff1000sdfromsj.tmp
  res t1000sffromsj jeff1000sffromsj.tmp
  res t1000snfromsj jeff1000snfromsj.tmp
  res t1000sjfromsi jeff1000sjfromsi.tmp
  res t1000si       jeff1000si.tmp
  res t1000sl       jeff1000sl.tmp
  res t1000sx       jeff1000sx.tmp
  res t1000sd       jeff1000sd.tmp
  res t1000sf       jeff1000sf.tmp
  ! FCALC supported
  res t1000sjfcalc     jeff1000sjfcalc.tmp
  res t1000sifcalc     jeff1000sifcalc.tmp
  res t1000siofsjfcalc jeff1000siofsjfcalc.tmp
  ! THIN currently unsupported for SJ
  !res t1000sjthin     jeff1000sjthin.tmp
  !res t1000sithin     jeff1000sithin.tmp
  !res t1000siofsjthin jeff1000siofsjthin.tmp

  res  defaultAmp 0x270
  call createAndNoopFrom12Bit defaultAmp true
  call checkResults

  if interAct then
    set instructions "Tests SJ DataType\n\n&
                      This needs to be run with and without the NONATIVE switch\n\n&
                      Currently NONATIVE = ^{nonative}\n\n&
                      Please check the automated results in the terminal."

    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 12 /title
      gcontrol lval   amp "WAVEFORM amplitude" P1=defaultAmp P2=0x10 P3=0x7000 P4=0x10
      gcontrol button test "" "Check Results"

      list2/id=list1000si t1000sifromsj
      list2/id=list1000sl t1000slfromsj
      list2/id=list1000sx t1000sxfromsj
      list2/id=list1000sb t1000sbfromsj
      list2/id=list1000sd t1000sdfromsj
      list2/id=list1000sf t1000sffromsj
      list2/id=listfcalc  t1000sjfcalc

      plot/id=plotsj t1000sj

      shellgui/id=statusshell/attach
      info "status of 1000 SJ"
      status  t1000sj
      info "status of 1000 SI created from SJ"
      status  t1000sifromsj
      info "status of 1000 SL created from SJ"
      status  t1000slfromsj
      info "status of 1000 SX created from SJ"
      status  t1000sxfromsj
      info "status of 1000 SB created from SJ"
      status  t1000sbfromsj
      info "status of 1000 SD created from SJ"
      status  t1000sdfromsj
      info "status of 1000 SF created from SJ"
      status  t1000sffromsj
      info "status of 1000 SN created from SJ"
      status  t1000snfromsj
      info "DATALIST of 1000 SJ"
      datalist t1000sj /nc=16

    pipe off

  endif

  erase t1000sj t1000si t1000sl t1000sx t1000sd t1000sf
  erase t1000sifromsj t1000slfromsj t1000sxfromsj t1000sbfromsj t1000sdfromsj
  erase t1000sffromsj t1000snfromsj t1000sjfromsi
  erase t1000sjfcalc t1000sifcalc t1000siofsjfcalc
  !erase t1000sjthin t1000sithin t1000siofsjthin

  env set usenative savedUseNative
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 "AMP" then
    call createAndNoopFrom12Bit msg.data
    call updateList2
  elseif msg.name eqs "TEST" then
    call checkResults
  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


!******************************************************************************!
! Create files with different formats and convert them to SJ
!******************************************************************************!
procedure createAndNoopFrom12Bit L:AMPLITUDE Z:updateNibble[false]
    if updateNibble then
      waveform t1000sj FORM="SJ" AMP=0x270 ELEM=512
      noop t1000sj t1000snfromsj{FORM="SN"} /TL=^TL
    endif
    waveform t1000sj FORM="SJ" AMP=^AMPLITUDE ELEM=512
    waveform t1000si FORM="SI" AMP=^AMPLITUDE ELEM=512
    waveform t1000sl FORM="SL" AMP=^AMPLITUDE ELEM=512
    waveform t1000sx FORM="SX" AMP=^AMPLITUDE ELEM=512
    waveform t1000sd FORM="SD" AMP=^AMPLITUDE ELEM=512
    waveform t1000sf FORM="SF" AMP=^AMPLITUDE ELEM=512
    noop t1000sj t1000sifromsj{FORM="SI"} /TL=^TL
    noop t1000sj t1000slfromsj{FORM="SL"} /TL=^TL
    noop t1000sj t1000sxfromsj{FORM="SX"} /TL=^TL
    noop t1000sj t1000sbfromsj{FORM="SB"} /TL=^TL
    noop t1000sj t1000sdfromsj{FORM="SD"} /TL=^TL
    noop t1000sj t1000sffromsj{FORM="SF"} /TL=^TL
    noop t1000si t1000sjfromsi{FORM="SJ"} /TL=^TL
    ! FCALC of SJ supported
    fcalc t1000sjfcalc t1000sj 10 +
    fcalc t1000sifcalc t1000si 10 +
    noop t1000sjfcalc t1000siofsjfcalc{FORM="SI"} /TL=^TL
    ! THIN of SJ currently unsupported
    !thin t1000sj t1000sjthin INC=2
    !thin t1000si t1000sithin INC=2
    !noop t1000sjthin t1000siofsjthin{FORM="SI"} /TL=^TL

return

!******************************************************************************!
! Update LIST2 values
!******************************************************************************!
procedure updateList2

  message send list1000si ,,"OPENFILE" ,, t1000sifromsj
  message send list1000sl ,,"OPENFILE" ,, t1000slfromsj
  message send list1000sx ,,"OPENFILE" ,, t1000sxfromsj
  message send list1000sb ,,"OPENFILE" ,, t1000sbfromsj
  message send list1000sd ,,"OPENFILE" ,, t1000sdfromsj
  message send list1000sf ,,"OPENFILE" ,, t1000sffromsj
  message send listfcalc  ,,"OPENFILE" ,, t1000sjfcalc

return

!******************************************************************************!
! Check results
!******************************************************************************!
procedure checkResults
  assert/text="^reg.main.name: SI matches SJ within expected tolerance" t1000sj      FEQ/D/T t1000sjfromsi    /DELTA=0x10
  assert/text="^reg.main.name: SI from SJ matches SI WAVEFORM"          t1000si      FEQ/D/T t1000sifromsj    /DELTA=0x10
  assert/text="^reg.main.name: SL from SJ matches SL WAVEFORM"          t1000sl      FEQ/D/T t1000slfromsj    /DELTA=0x10
  assert/text="^reg.main.name: SX from SJ matches SX WAVEFORM"          t1000sx      FEQ/D/T t1000sxfromsj    /DELTA=0x10
  assert/text="^reg.main.name: SD from SJ matches SD WAVEFORM"          t1000sd      FEQ/D/T t1000sdfromsj    /DELTA=0x10
  assert/text="^reg.main.name: SF from SJ matches SF WAVEFORM"          t1000sf      FEQ/D/T t1000sffromsj    /DELTA=0x10
  assert/text="^reg.main.name: FCALC of SJ WAVEFORM"                    t1000sifcalc FEQ/D/T t1000siofsjfcalc /DELTA=0x10
  !assert/text="^reg.main.name: THIN of SJ WAVEFORM"                    t1000sithin  FEQ/D/T t1000siofsjthin  /DELTA=0x10
return
