using LinearAcoustics
using Base.Test

#testing pwdm
env = AcEnv(8e3)
Nt = round(Int64,0.06*env.Fs)
r = 2.5
xs =  SphFib([3.;3.;3.],r,25)
xr =  SphFib([3.;3.;3.],0.99*r,25)
#xr =  [0. 0. 0.; 0. 100. 0.]'
t = linspace(0,Nt/env.Fs,Nt)
d1 = 80
s = exp(-(t-d1/env.Fs).^2/1e-4)
s = repmat(s,1,xs.N)
#s = zeros(s)
#s = ones(Nt-ceil(Int64,2*r/env.c*env.Fs)-1,xs.N)

Nx = 100
xr = LinearGrid([3.;3.;3.],[2*r],Nx)
p = pwdm(fft(s,1),xs,xr,Nt,env)
S = fft(s,1)
@time p = pwdm(S,xs,xr,Nt,env)
@time p = pwdm(S,xs,xr,Nt,env)


Nf = floor(Int,Nt/2)+1
ωc = collect(2*π/env.c*linspace(0,env.Fs,Nt))
d = xr.pos.-xs.xc
p2 = zeros(p)
@time LinearAcoustics.pwdm!(p,S,xs.kd,d,ωc,xs.r,Nf,xs.N,xr.N)
@time LinearAcoustics.pwdm!(p,S,xs.kd,d,ωc,xs.r,Nf,xs.N,xr.N)
return

p = hifft(p,Nt)
##using PyPlot
##imshow(p)
@test norm(maximum(p[:])-xs.N)<1e-2

Nf = floor(Int64,Nt/2)+1
p = randn(Nf,xr.N)+im*randn(Nf,xr.N)
s = randn(Nf,xs.N)+im*randn(Nf,xs.N)
b = randn(Nf)+im*randn(Nf)
#testing adjoint
@test norm(vecdot(pwdm(p,xs,xr,Nt,env,true),s)-vecdot(p,pwdm(s,xs,xr,Nt,env))) <1e-8
#testing adjoint with base
@test norm(vecdot(pwdm(p,b,xs,xr,Nt,env,true),s)-vecdot(p,pwdm(s,b,xs,xr,Nt,env))) <1e-8

