module testetautils
	use testHelper
	use kdefs, only : TINY
	use rcmdefs
	use conversion_module, only : almmax,almmin
	use RCM_mod_subs
	use torcm_mod
	use etautils

	implicit none

	contains

	@before
	subroutine setup()
	end subroutine setup

	@after
	subroutine teardown()
	end subroutine teardown

	!Helpers
	subroutine initAlams()
		real (rp) :: itimei,itimef
		integer(iprec) :: nstep
		type(XML_Input_T) :: xmlInp
		INTEGER(iprec) :: ierr

		itimei = 0
		itimef = 60
		nstep = 0

		xmlInp = New_XML_Input('cmriD.xml','Kaiju/Voltron',.true.)

		! Get RCM to read rcmconfig.h5 and store in alamc
		call setFactors(6.371E6_rprec)
		call allocate_conversion_arrays (isize,jsize,kcsize)
		call RCM(itimei, itimef, nstep, 0_iprec)
		call RCM(itimei, itimef, nstep, 1_iprec)
		call Read_alam (kcsize, alamc, ikflavc, fudgec, almdel, almmax, almmin, iesize, ierr)
	end subroutine initAlams

	@test
	subroutine testDPetaDP()
		real (rp) :: etas(kcsize)
		real (rp) :: vm
		real (rp) :: Do_rc ,Po_rc
		real (rp) :: Df_rc,Df_psph,Pf_rc

		real(rp) :: OKerr
		character(len=strLen) :: checkMessage

		real(rp) :: D_err, P_err

		OKerr = 2e-2_rp

		! 5 keV at L=10
		Do_rc = 0.5 *1E6! [1/cc -> 1/m^3]
		Po_rc = 0.5 *1E-9! [nPa -> Pa]
		vm = 2.262

		call initAlams()

		call DP2eta(Do_rc,Po_rc,vm,etas,doRescaleO=.false.,doKapO=.false.)
		call eta2DP(etas,vm,Df_rc,Df_psph,Pf_rc)

		D_err = (Do_rc - Df_rc)/Do_rc
		P_err = (Po_rc - Pf_rc)/Po_rc

		write(*,*) 'D_err=',D_err*100,'%'
		write(*,*) 'P_err=',P_err*100,'%'

		write (checkMessage,'(A,I0,A)') "D->eta->D' error > ",OKerr*100,"%"
		@assertLessThanOrEqual(abs(D_err), OKerr, checkMessage)

		write (checkMessage,'(A,I0,A)') "D->eta->D' error > ",OKerr*100,"%"
		@assertLessThanOrEqual(abs(P_err), OKerr, checkMessage)

	end subroutine testDPetaDP

end module testetautils
