!------------------------------------------------------------------------------!
! Tests for COMPARE command.
! NOTE: Should also run "confidence DSP" since may of the DSP test cases exercise COMPARE.
!
! Author : Jay Hallam / Neon Ngo
!------------------------------------------------------------------------------!
startmacro S:func[""]
  result D:threshold 1e-5  ! JGS -- why so high??
  call setDatDir

  if func NEQS "" then
    call test_^{func}
    stop
  endif

  call test_type1000
  call test_type2000
  call test_type3000
  call test_type5000
  call test_type6000
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

!------------------------------------------------------------------------------!
! generic testing for all file types
! 1. checks max/min (infile1) with expMax/expMin
! 2. checks compare against self (infile1)
! 3. checks compare infile1 with infile2 with expMaxDiff/expMinDiff
!------------------------------------------------------------------------------!
subroutine basic_test s:filetype fn:infile1 f:expMax f:expMin fn:infile2 f:expMaxDiff f:expMinDiff
  remove themax themin
  compare infile1 max_out=themax min_out=themin
  assert/text="^reg.main.name: type ^filetype MAX_OUT==^expMax (^themax)" themax rexists AND themax eq expMax
  assert/text="^reg.main.name: type ^filetype MIN_OUT==^expMin (^themin)" themin rexists AND themin eq expMin

  remove themax themin thrCmpResult
  compare infile1 infile1 themax themin result=thrCmpResult
  assert/text="^reg.main.name: type ^filetype (with same file) MAX_OUT==0 (^themax)" themax rexists AND themax eq 0
  assert/text="^reg.main.name: type ^filetype (with same file) MIN_OUT==0 (^themin)" themin rexists AND themin eq 0
  assert/text="^reg.main.name: type ^filetype (with same file) RESULT==1 (^thrCmpResult)" thrCmpResult rexists AND thrCmpResult eq 1

  remove themax themin thrCmpResult
  compare infile1 infile2 max_out=themax min_out=themin result=thrCmpResult
  assert/text="^reg.main.name: type ^filetype (with other file) MAX_OUT==^expMaxDiff (^themax)" themax rexists AND themax eqt expMaxDiff
  assert/text="^reg.main.name: type ^filetype (with other file) MIN_OUT==^expMinDiff (^themin)" themin rexists AND themin eqt expMinDiff
  assert/text="^reg.main.name: type ^filetype (with other file) RESULT==0 (^thrCmpResult)" thrCmpResult rexists AND thrCmpResult eq 0
return

!------------------------------------------------------------------------------!
procedure test_type1000
  call basic_test "1000 #1" ^{datDir}.sinewave.prm  1.0000013 -1.0000013 ^{datDir}.fftdemo.prm  37.403133 -40.0801
  call basic_test "1000 #2" ^{datDir}.fftdemo.prm  38.27944  -39.831413  ^{datDir}.sinewave.prm 40.0801   -37.403133

  WAVEFORM,OUT=test_compare_amp1,FORM=SF,ELEM=100,SHAPE=SIN,AMP=1,FREQ=.01
  WAVEFORM,OUT=test_compare_amp3,FORM=SF,ELEM=100,SHAPE=SIN,AMP=3,FREQ=.01

  remove themax themin mindiff maxdiff
  compare test_compare_amp1 max_out=themax min_out=themin
  calc minDiff 1 themin + ABS
  calc maxDiff 1 themax - ABS
  assert/text="^reg.main.name: compare 1 scalar float file with zero" &
       maxdiff LE threshold AND minDiff LE threshold

  remove themax themin mindiff maxdiff
  compare test_compare_amp1 test_compare_amp3 max_out=themax min_out=themin
  calc minDiff 2 themin + ABS
  calc maxDiff 2 themax - ABS
  assert/text="^reg.main.name: compare 2 scalar float files" &
       maxdiff LE threshold AND minDiff LE threshold

  erase test_compare_amp1
  erase test_compare_amp3
return

!------------------------------------------------------------------------------!
procedure test_type2000
  call basic_test "2000 #1" ^{datDir}.apenny.prm  127  -127 ^{datDir}.alaska.prm 766.0  -128.0
  call basic_test "2000 #2" ^{datDir}.alaska.prm 4267 -7079 ^{datDir}.apenny.prm 306.0 -1022.0
return

!------------------------------------------------------------------------------!
procedure test_type3000
  res testfile1 "^{datDir}.test3000_eeei.prm"
  res testfile2 "ram:test_compare3000.tmp"
  res testfile3 "^{datDir}.test3000_ieee.prm"

  noop testfile1 testfile2
  res  file(testfile2).data(0,"ABSC") 20.0
  res  file(testfile2).data(0,"ORD")  -3.0
  call basic_test "3000 #1" testfile1 16.0  1.0 testfile2 19.0  -4.0
  call basic_test "3000 #2" testfile2 20.0 -3.0 testfile1  4.0 -19.0

  remove themax themin thrCmpResult
  compare testfile1 testfile3 max_out=themax min_out=themin result=thrCmpResult
  assert/text="^reg.main.name: type 3000 (with similar file) MAX_OUT (^themax)" themax rexists AND themax eq 0
  assert/text="^reg.main.name: type 3000 (with similar file) MIN_OUT (^themin)" themin rexists AND themin eq 0
  assert/text="^reg.main.name: type 3000 (with similar file) RESULT==1 (^thrCmpResult)" thrCmpResult rexists AND thrCmpResult eq 1

  erase testfile2
return

!------------------------------------------------------------------------------!
procedure test_type5000
  res testfile1 "^{datDir}.sv1.prm"
  res testfile2 "^{datDir}.sv2.prm"

  call basic_test "5000 #1" testfile1 3672750.5 -3.479262E7  testfile2 2.0299538E7 -1.5621862E7
  call basic_test "5000 #2" testfile2 7320929.0 -3.9527892E7 testfile1 1.5621862E7 -2.0299538E7
return

!------------------------------------------------------------------------------!
procedure test_type6000
  res testfile1 "^{datDir}.test6000.prm"
  res testfile2 "ram:test_compare6000.tmp"

  noop testfile1 testfile2
  res  file(testfile2).data(0,"CC")   9
  res  file(testfile2).data(0,"FF") -36

  call basic_test "6000 #1" testfile1 678.0   0.345 testfile2   3.5 -272.0
  call basic_test "6000 #2" testfile2 678.0 -36.0   testfile1 272.0   -3.5

  erase testfile2
return

