!******************************************************************************!
! Tests that the timeline size with GROW_AS_NEEDED will only expand to
! nxm.sys.lib.TimeLine.DEF_MAX_ALLOC, but a higher timeline size can be
! specified directly (ex: \TLL=1.2M)
!
! Note: This could be adjusted to pass in all the values, rather than use
! a reference file.  Creation for the reference files does not take log
! and allows for the possibility of saving files with these timelines 
!
! Arguments: None
!
! @bug      NXM-3041 TimeLine Issue #2 Insufficient Timeline Entries
! @since    3.7.3
! @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)

  ! Large temporary files created for this test
  res referenceFileOver1M referenceFileOver1M_3041.tmp
  res referenceFileUnder1M referenceFileUnder1M_3041.tmp

  ! Specs from these file used for timeline creation
  res referenceFile nxm.sys.test.test_fftout_timeline.prm
  waveform referenceFileOver1M FORM=CF ELEM=2.2M 
  headermod referenceFileOver1M TC=2017:05:05:11:50:00
  waveform referenceFileUnder1M FORM=SF ELEM=950K
  headermod referenceFileUnder1M TC=2017:05:05:10:50:00
    
  ! Spacing for timeline entries in elements
  ! spacing 1: entries for elements 0,1,2,...
  res spacingEachElement 1
  ! spacing 2: entries for elements 0,2,4,...
  res spacingEveryOther 2
  
  invoke growAsNeeded nxm.sys.lib.TimeLine.GROW_AS_NEEDED
  invoke maxGrowAsNeeded nxm.sys.lib.TimeLine.DEF_MAX_ALLOC

  !
  ! TimeLine creator that will help in testing the other TimeLine issues
  ! (NXM-3041, NXM-3042)
  ! Note: These have been used to manually test TestTimelineCreator methods
  !
  ! create timeline with evenly spaced entries
  call testWithEvenlySpacedEntries referenceFile spacingEveryOther growAsNeeded
  call testWithEvenlySpacedEntries referenceFileUnder1M spacingEachElement growAsNeeded
  call testWithEvenlySpacedEntries referenceFileOver1M spacingEveryOther growAsNeeded
  call testWithEvenlySpacedEntries referenceFileOver1M spacingEveryOther 1.5M

  ! erase the huge temporary files created for this test
  erase referenceFileUnder1M referenceFileOver1M


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: Interactive tests for STATUS" msg.data eqss "PASS"
    pipe stop
  else
    say "^reg.main.name: 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 a TimeLine with evenly spaced entries using
! TestTimelineCreator.timelineByElement
!******************************************************************************!
procedure testWithEvenlySpacedEntries fn:referenceFile L:spacing L:tll

  !---------------------------------------------------------------------
  ! Get values from reference file
  !---------------------------------------------------------------------
  
  ! Get number elements and delta
  status referenceFile SIZE=L:numElements XDELTA=D:delta

  ! Get bytes per element and Start Time
  file open/d refFileTag referenceFile
  ! Get bytes per element
  invoke L:bpe refFileTag.getBPE()
  ! Get Start Time
  invoke starttime refFileTag.getTime()
  file close refFileTag
  
  !---------------------------------------------------------------------
  ! Set Element Offsets and time adjustment for timeline entries
  !---------------------------------------------------------------------
  res adjustment 0.1
  
  ! Create single entry array with offset entry interval
  ! (KeyObject invoke methods treat varargs as an array) 
  res offsetEntries (^spacing)
  invoke oneOffsetArray=nxm.sys.test.TestTimelineCreator.getIntArray(1)
  invoke ,, offsetEntries.toArray(oneOffsetArray,0)

  !---------------------------------------------------------------------
  ! Create TimeLine with evenly spaced entries
  !---------------------------------------------------------------------

  invoke timeline_e=nxm.sys.test.TestTimelineCreator.timelineByElement(tll,delta,bpe,starttime,adjustment,numElements,oneOffsetArray)
  !res timeline_e
  invoke tllist timeline_e.list(7)
  !res tllist
  calc expectedNumEntries numElements spacing /
  invoke expectedNumEntries java.lang.Math.ceil(expectedNumEntries)
  if tll EQ growAsNeeded THEN
    if expectedNumEntries GT maxGrowAsNeeded THEN
      set expectedNumEntries maxGrowAsNeeded
    endif
  elseif expectedNumEntries GT tll THEN
    set expectedNumEntries tll
  endif
  invoke actualNumEntries timeline_e.getSize()
  assert/text="^reg.main.name: TimeLine expansion: ^numElements elem, alloc=^tll size(exp/act):^{expectedNumEntries}/^{actualNumEntries}" actualNumEntries eq expectedNumEntries

  !---------------------------------------------------------------------
  ! Create Table with these TimeLine entries
  !---------------------------------------------------------------------
  invoke timelinetbl=timeline_e.toTable(12,bpe)
  !res timelinetbl

return

!******************************************************************************!
! Create a TimeLine from a Table
! TestTimelineCreator.timelineFromTable
!
! KEY is element offset as String and VALUE is Time as String
!
!******************************************************************************!
procedure createFromTable fn:referenceFile t:timelinetbl U:timeline_result

  !---------------------------------------------------------------------
  ! Get values from reference file
  !---------------------------------------------------------------------

  ! Get number elements and delta
  status referenceFile SIZE=L:numElements XDELTA=D:delta

  ! Get bytes per element and Start Time
  file open/d filetag referenceFile
  ! Get bytes per element
  invoke L:bpe filetag.getBPE()
  file close filetag

  !---------------------------------------------------------------------
  ! Create TimeLine from a Table
  !---------------------------------------------------------------------
  invoke timeline_result=nxm.sys.test.TestTimelineCreator.timelineFromTable(delta,bpe,timelinetbl)
  !res timeline_result
  !invoke tllist timeline_result.list(7)
  !res tllist

return
