using LinearAcoustics
using Base.Test

#testing fractional delay
env = AcEnv(8e3,1)
d = 1e-1*π/2
Nt = round(Int64,0.2*env.Fs)
a = zeros(Nt)
a[1] = 1.
s = zeros(Nt)
s[1] = 1.

s2 =  zeros(s)
s2 = LinearAcoustics.fdelay!(d,s2,a,s,env)

t = linspace(0,Nt/env.Fs,Nt)
using PyPlot
plot(t,s)
plot(t,s2)
plot(d,0, "r*")

#testing tesm
env = AcEnv(8e3)
Nt = round(Int64,0.05*env.Fs)
xs =  SphFib([0.;0.;0.],1.5,5)
xr =  SphFib([0.;0.;0.],1.,5)
s = zeros(Nt,xs.N)
s[1:150,:] = 1

p = tesm(s,xs,[0.;0.;0.],Nt,env)
#testing tesm with base
p2 = tesm(s,randn(Nt),xs,[0.;0.;0.],Nt,env)
p2 = tesm(s,randn(Nt),xs,xr,Nt,env)
s = zeros(Nt)
p2 = tesm(s,randn(Nt),[1.;1.;1.],[0.;0.;0.],Nt,env)

@test norm(p[120]-xs.N*1/(4*π*1.5))<1e-7


# testing time reverse
xs =  SphFib([0.;0.;0.],2.,5)
xr =  SphFib([0.;0.;0.],1.,3)
p = randn(Nt,xr.N)
s = randn(Nt,xs.N)

@test vecdot(tesm(p,xs,xr,Nt,env,true),s)-vecdot(tesm(s,xs,xr,Nt,env),p) <1e-8
b = randn(Nt)
# testing time reverse with base
@test vecdot(tesm(p,b,xs,xr,Nt,env,true),s)-vecdot(tesm(s,b,xs,xr,Nt,env),p) <1e-8


xs =  SphFib([0.;0.;0.],2.,500)
xr =  SphFib([0.;0.;0.],1.,12)
s = randn(Nt,xs.N)
@time tesm(s,b,xs,xr,Nt,env)
@time tesm(s,xs,xr,Nt,env)
return

