!******************************************************************************
! Tests the COPYHEADER command. 
!
! @author  Nick Gabrovsek 
! @since   NeXtMidas 3.5.0
!******************************************************************************
startmacro/SEDITLEGACY=false
  set  fnprefix "copyheader_^{env.pid}"
  res t:tests {TEXT="COPYHEADER"}
  call testStat "test1000_ieee.prm"         !1000 type
  call testStat "world.prm"                 !1999 type
  call testStat "test2000_tcprec.prm"       !2000 type
  call testSplit "test3000_eeei.prm"        !3000 type
  call test5000 "ellipse5000.prm"           !5000 type
  call test5000 "test6000.prm"               !6000 type
  assert/group=tests/summary
endmacro

!*****************************************************************************************************
procedure openFile S:file
  res newname ^{fnprefix}_^{file}.tmp
  copyheader ^file ^newname
  file open/d type ^file  
  res tname type.type 
return

!*****************************************************************************************************
procedure testStat S:file I:open
  call openFile file
  statistics ^file table=old
  statistics ^newname table=new
  !Test the data has been zero-ed out.   
  assert/group=tests/text="^tname: max  = 0"      new.max  eqt 0
  assert/group=tests/text="^tname: min  = 0"      new.min  eqt 0
  if tname EQS "1999" then
    assert/group=tests/text="^tname: sum  = 0"    new.sum  eqt (0.0,0.0)
    assert/group=tests/text="^tname: mean = 0"    new.mean eqt (0.0,0.0)
  else
    assert/group=tests/text="^tname: mean = 0"    new.mean eqt 0
    assert/group=tests/text="^tname: sum  = 0"    new.sum  eqt 0
  endif

  !Test the other header information is the same.
  assert/group=tests/text="^tname: Header Info"   ^file EQF/H/S ^newname

  file close type
  erase/all ^newname
return

!*****************************************************************************************************
procedure testSplit S:file
  call openFile file
  res splitroot ram:^{fnprefix}_split_  ! Since 3.7.2 put in ram to avoid issues running on Windows
  split newname splitroot                         !Split the type 3000 file into many type 1000 files
  files ^{splitroot}* /list=store /nameonly
  res del ^newname
  foreach f INLIST store
    call testStat f                               !Test each type 1000 file normally.
    erase/all f
  endfor
  erase/all ^del
return

!*****************************************************************************************************
procedure test5000 S:File
  call openFile file

  file open temp ^newname
  invoke D:size=temp.getsize()
  res size size-1
  res I:i 0
  while i LT size
    invoke t:datatable=temp.getdatatable(i)
    res i i+1
    foreach val INTABLE/V datatable
      foreach v in val
        !say "^tname:^val = ^v"
        assert/group=tests/text="^tname:^val:^v  = 0"    v eqt 0
      endfor
    endfor
  endwhile
  assert/group=tests/text="^tname: Header Info"   ^file EQF/H/S ^newname
  erase/all ^newname

return
