!******************************************************************************!
! Initial testing for 12-bit CJ Data Type conversion to other types
! Note: This test should be run with and without the /NONATIVE switch
!
! Arguments: TL
!
! @bug      NXM-3894  12-bit integer Midas Data Type needed
! @since    4.1.2
! @author:  Jeanette McCall
!******************************************************************************!
startmacro L:TL[1]
  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 t1000cj       jeff1000cj.tmp
  res t1000cifromcj jeff1000cifromcj.tmp
  res t1000clfromcj jeff1000clfromcj.tmp
  res t1000cxfromcj jeff1000cxfromcj.tmp
  res t1000cbfromcj jeff1000cbfromcj.tmp
  res t1000cdfromcj jeff1000cdfromcj.tmp
  res t1000cffromcj jeff1000cffromcj.tmp
  res t1000cnfromcj jeff1000cnfromcj.tmp
  res t1000cjfromci jeff1000cjfromci.tmp
  res t1000ci       jeff1000ci.tmp
  res t1000cl       jeff1000cl.tmp
  res t1000cx       jeff1000cx.tmp
  res t1000cd       jeff1000cd.tmp
  res t1000cf       jeff1000cf.tmp
  ! THIN of CJ
  res t1000cjthin     jeff1000cjthin.tmp
  res t1000cithin     jeff1000cithin.tmp
  res t1000ciofcjthin jeff1000ciofcjthin.tmp

  res  defaultAmp 0x80
  call createAndNoopFrom12Bit defaultAmp true
  call checkResults

  if interAct then
    set instructions "Tests CJ 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=list1000cj t1000cj
      list2/id=list1000ci t1000cifromcj
      list2/id=list1000cl t1000clfromcj
      list2/id=list1000cx t1000cxfromcj
      list2/id=list1000cb t1000cbfromcj
      list2/id=list1000cd t1000cdfromcj
      list2/id=list1000cf t1000cffromcj
      list2/id=list1000cn t1000cnfromcj

      shellgui/id=statusshell/attach
      say "status of 1000 CJ"
      status  t1000cj
      say "status of 1000 CI created from CJ"
      status  t1000cifromcj
      say "status of 1000 CL created from CJ"
      status  t1000clfromcj
      say "status of 1000 CX created from CJ"
      status  t1000cxfromcj
      say "status of 1000 CB created from CJ"
      status  t1000cbfromcj
      say "status of 1000 CD created from CJ"
      status  t1000cdfromcj
      say "status of 1000 CF created from CJ"
      status  t1000cffromcj
      say "status of 1000 CN created from CJ"
      status  t1000cnfromcj
    pipe off

  endif

  erase t1000cj t1000ci t1000cl t1000cx t1000cd t1000cf
  erase t1000cifromcj t1000clfromcj t1000cxfromcj t1000cbfromcj t1000cdfromcj
  erase t1000cffromcj t1000cnfromcj t1000cjfromci
  erase t1000cjthin t1000cithin t1000ciofcjthin

  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 t1000cj FORM="CJ" AMP=0x80 ELEM=512
      noop t1000cj t1000cnfromcj{FORM="CN"} /TL=^TL
    endif
    waveform t1000cj FORM="CJ" AMP=^AMPLITUDE ELEM=512
    waveform t1000ci FORM="CI" AMP=^AMPLITUDE ELEM=512
    waveform t1000cl FORM="CL" AMP=^AMPLITUDE ELEM=512
    waveform t1000cx FORM="CX" AMP=^AMPLITUDE ELEM=512
    waveform t1000cd FORM="CD" AMP=^AMPLITUDE ELEM=512
    waveform t1000cf FORM="CF" AMP=^AMPLITUDE ELEM=512
    noop t1000cj t1000cifromcj{FORM="CI"} /TL=^TL
    noop t1000cj t1000clfromcj{FORM="CL"} /TL=^TL
    noop t1000cj t1000cxfromcj{FORM="CX"} /TL=^TL
    noop t1000cj t1000cbfromcj{FORM="CB"} /TL=^TL
    noop t1000cj t1000cdfromcj{FORM="CD"} /TL=^TL
    noop t1000cj t1000cffromcj{FORM="CF"} /TL=^TL
    noop t1000ci t1000cjfromci{FORM="CJ"} /TL=^TL
    ! THIN of CJ currently
    thin t1000cj t1000cjthin INC=2
    thin t1000ci t1000cithin INC=2
    noop t1000cjthin t1000ciofcjthin{FORM="CI"} /TL=^TL

return

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

  message send list1000cj ,,"OPENFILE" ,, t1000cj
  message send list1000ci ,,"OPENFILE" ,, t1000cifromcj
  message send list1000cl ,,"OPENFILE" ,, t1000clfromcj
  message send list1000cx ,,"OPENFILE" ,, t1000cxfromcj
  message send list1000cb ,,"OPENFILE" ,, t1000cbfromcj
  message send list1000cd ,,"OPENFILE" ,, t1000cdfromcj
  message send list1000cf ,,"OPENFILE" ,, t1000cffromcj

return

!******************************************************************************!
! Check results
!******************************************************************************!
procedure checkResults
  assert/text="^reg.main.name: CI matches CJ within expected tolerance" t1000cj FEQ/D/T t1000cjfromci /DELTA=0x10
  assert/text="^reg.main.name: CI from CJ matches CI WAVEFORM"          t1000ci FEQ/D/T t1000cifromcj /DELTA=0x10
  assert/text="^reg.main.name: CL from CJ matches CL WAVEFORM"          t1000cl FEQ/D/T t1000clfromcj /DELTA=0x10
  assert/text="^reg.main.name: CX from CJ matches CX WAVEFORM"          t1000cx FEQ/D/T t1000cxfromcj /DELTA=0x10
  assert/text="^reg.main.name: CD from CJ matches CD WAVEFORM"          t1000cd FEQ/D/T t1000cdfromcj /DELTA=0x10
  assert/text="^reg.main.name: CF from CJ matches CF WAVEFORM"          t1000cf FEQ/D/T t1000cffromcj /DELTA=0x10
  assert/text="^reg.main.name: THIN of CJ WAVEFORM"                     t1000cithin FEQ/D/T t1000ciofcjthin /DELTA=0x10
return
