startmacro
!------------------------------------------------------------------------------!
! Tests for POLYFIT command.
!
!   Author : J. Hallam
!   Version: $Id: test_polyfit.mm,v 1.6 2009/02/18 16:54:51 jgs Exp $
!
!------------------------------------------------------------------------------!


set tfTab {}
set tfTab.in "test_polyfit_in"
set tfTab.out "test_polyfit_out"
set tfTab.fit "test_polyfit_fit"
set tfTab.err "test_polyfit_err"

fname LABEL=masterFit AREA=TEST NAME=polyfit_xmidas_master ext=prm opt=sys

waveform tfTab.in ELEM=4k SHAPE=COSINE
set order 1
polyfit,IN=tfTab.in,ORDER=1,COEF=coef,OUT=tfTab.out,FIT=tfTab.fit,ERR=tfTab.err

set numCoefMissing 0
do i 0 order
  assert/stat=status/text="^reg.main.name: coef(^i)" coef(^i) REXISTS
  if status isFalse then
    calc numCoefMissing numCoefMissing 1 +
  endif
enddo

if numCoefMissing EQ 0 then
  ! Check to see if the coeficients match 
  assert/text="^reg.main.name: coef(0)" coef(0) EQ 0.0026594092386937975
  assert/text="^reg.main.name: coef(1)" coef(1) EQ -1.7687724733359164E-6
else
  warning "^reg.main.name: ^numCoefMissing coefficients are missing, &
           bypassing some tests"
endif

! Compare the NeXtMidas polyfit output with a file generated by the
! X-Midas MPOLY command.
assert/text="^reg.main.name: FIT output SIZE = MASTER SIZE" FILE(tfTab.fit).size EQ FILE(masterFit).size
assert/text="^reg.main.name: FIT output = MASTER" tfTab.fit FEQ/H/D masterFit

foreach key INTABLE tfTab
  if tfTab.^key FEXISTS erase tfTab.^key
endfor

endmacro
