! Tests for the MERGEFILE command.
!
! Author:  J. Czechowski IV
!
startmacro

  call setDatDir

  call test_merge_twice ^{datDir}.sinewave.prm      ! Type 1000
  call test_merge_twice ^{datDir}.fftdemo.prm       ! Type 1000
  call test_merge_twice ^{datDir}.apenny.prm        ! Type 2000
  call test_merge_twice ^{datDir}.testxy3000.prm    ! Type 3000
  call test_merge_twice ^{datDir}.coneflags5000.prm ! Type 5000
  call test_replay
  call test_merge_bad_fs
endmacro

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

procedure test_merge_twice FN:fname
  status fname TYPE=type SIZE=oldSize

  calc L:newSize oldSize 2 *

  mergefile fname ram:myfile
  assert/text="Merge one Type ^type file to a new file" fname feq/s/d/h ram:myfile
  
  mergefile fname ram:myfile
  status ram:myfile SIZE=size
  assert/text="Merge one Type ^type file with another (new size=^size)" size eq newSize
  
  noop ram:myfile(:^{oldSize}) ram:file1
  noop ram:myfile(^{oldSize}:) ram:file2
  assert/text="Merge one Type ^type file with another (first half)"  fname feq/s/d ram:file1
  assert/text="Merge one Type ^type file with another (second half)" fname feq/s/d ram:file2

  erase/quiet/warn=n ram:myfile
return

procedure test_replay
  erase/quiet/warn=n ram:myfile
  mergefile/REPLAY=4 ^{datDir}.testxy3000.prm ram:myfile
  calc size FILE(^{datDir}.testxy3000.prm).size 4 *
  assert/text="Merge one Type 3000 file to a new file, /REPLAY=4 (size=^{size})" FILE(ram:myfile).size eq ^size

  DO index 0 ^size-1 1
    calc term index 6 mod
    res value FILE(ram:myfile).getDataTable(^index)
    res expected FILE(^{datDir}.testxy3000.prm).getDataTable(^term)
    
    foreach key INTABLE expected
      assert/text="Checking new Type 3000 file index=^{index} ^{key}=^{expected.^{key}}" &
                            ^{expected.^{key}} eqs ^{value.^{key}}
    endfor
  ENDDO
return

! This tests Bug 1173 (MERGEFILE not producing correct number of elements): Merge two files with
! incompatible frame size.
!
! Do not change the /TL=, FS=, ELEM=, or SIZE= since these numbers are carefully
! picked to cause a rounding error in DataFile that is a subject of this bug.
procedure test_merge_bad_fs
  res L:frame 160000    ! <-- DO NOT CHANGE THIS
  res L:elem  0         ! <-- DO NOT CHANGE THIS
  res L:xfer  32768/4   ! <-- DO NOT CHANGE THIS

  waveform ram:temp1000 shape=const amp=1 form=SF elem=^frame
  headermod/create ram:temp2000 FS=^frame SIZE=^elem
  
  ! Use /WARN=NO since this case would produce a warning...
  mergefile/warn=NO/TL=^xfer ram:temp1000{TYPE=2000} ram:temp2000 ! Sets FS=1
  
  status ram:temp2000 SIZE=size
  assert/text="Merge two Type 2000 files with different FS=" size eq 1.0
  
  erase ram:temp1000 ram:temp2000
return
