!******************************************************************************!
!  Purpose: Test the SINKFILE primitive
!  Author: Jay Hallam
!******************************************************************************!
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)

  files/list=files/nameonly AUX=1 ext=tmp
  ! Clean up any upper case file names that will match the lower case names in this test
  foreach name inlist files
    if name EQSS "TFTAB1_"
      erase "^name"
    endif
  endfor

  set tfTab {}
  set tfTab.IN "test_sinkfile_input"
  set tfTab.OUT "test_sinkfile_out"
  set tfTab.OUT_NOOP "test_sinkfile_out_noop"
  set tfTab.OUT_RT "test_sinkfile_rtfile"

  waveform out=tfTab.IN ELEM=1000

  sinkfile tfTab.IN tfTab.OUT
  noop     tfTab.IN tfTab.OUT_NOOP
  assert/text="^reg.main.name: IN = OUT (NOOP)" tfTab.IN FEQ/B tfTab.OUT

  ramp myramp1 elem=1000000 FORM=CF UNITS=1 DELTA=10
  info "YOU SHOULD SEE 9 INFO MSG'S BELOW FOR SPAWNING A NEW FILE"
  sinkfile/CUT=ELEMENTS myramp1 tfTab1_ 0 100000 cutOptions={DIGITS=10,NAMESCHEME=HMS,NEWFILEMSG=""}
  call test_verifyElementFiles

  if baseline isTRUE then
    set testDurationTime 8.0
    set testCutSeconds   3.0
    info "^reg.main.name: processing for ~^{testDurationTime} seconds..."
    pipe on
      waveform _wave form=cf 1e9 shape=sin
      sinkfile/CUT=CLOCK_TIME _wave tfTab2_ 0 ^testCutSeconds cutOptions={NAMESCHEME=STD}
      set this.TIMER(0) testDurationTime

    pipe off
    call test_verifyTimeFiles
  endif

  if /NOERASE isFALSE then
    foreach key INTABLE tfTab
      if tfTab.^key FEXISTS erase tfTab.^Key
    endfor
  else
    info "^reg.main.name: temp files not erased"
  endif

  FORALL files1 erase #
  ERASE files1
  erase myramp1

  if baseline isTRUE then
    FORALL files2 erase #
    erase files2
  endif

  if interAct isTRUE then
    %test_sinkfile_msg ! call the interactive test
    %test_sinkfile_rt  ! call the test of the circ buffer
  endif

endmacro

!******************************************************************************!
procedure processMessage
  if msg.name EQS "MAIN" then
    pipe stop
  elseif msg.name EQS "TIMER" then
    pipe stop
  endif
return

!******************************************************************************!
procedure test_verifyElementFiles
!
!  Test each file has the expected number of elements.
!
  files/f=files1/nameonly/exclude=CWD tfTab1* TMP
  stat files1.tmp size=nsize
  res count 0
  do i 0 nsize-1 1
    result s FILE(files1).getDataTable(^i)
    res testSize 0
    stat ^s.f1 size=testSize
    if ^testSize EQ 100000
      calc count count 1 +
    endif
  enddo
  assert/text="^reg.main.name: All files have the expected number of elements" ^count EQ 10
return

!******************************************************************************!
procedure test_verifyTimeFiles
!
!  Test that based on the timer the correct number of files is created.
!
  files/f=files2/nameonly/exclude=CWD tfTab2* TMP
  stat files2.tmp size=nsize
  calc expSize testDurationTime testCutSeconds / CEIL
  assert/text="^reg.main.name: The expected number of files is ^expSize, got ^nsize" ^nsize EQ expSize
return
