function [ok,globalerr,res]=maxtest(ni,x,u,d,Dd,c,r,f,tol,ad,al,ar,bctype)
disp('test')
adcopy=ad;
h=x(2:ni+1)-x(1:ni);
ndf=ni-1+sum(bctype);
%
%computing residual
%
for i=1:ni;
res(i)=f(i)+(Dd(i)-c(i))*(u(i+1)-u(i))/h(i)-r(i)*(u(i+1)+u(i))/2;
res(i)=abs(res(i));
end
%
cint=1/8; %interpolation constant
%
%loop over test points to determine cstab
%
cstab=0;
nitp=3;
index=[1:max(1.01,ndf/(nitp+1)):ndf];
index=round(index);
index=[index,ndf];
si=size(index,2);
for i=1:si;
%
%setting up and solving dual problem(s)
%
delta=zeros(ndf,1);
delta(index(i))=delta(index(i))+1;
%
for i=2:ndf;
ad(i)=ad(i)-al(i-1)*ar(i-1)/ad(i-1);
delta(i)=delta(i)-delta(i-1)*ar(i-1)/ad(i-1);
end
z(ndf)=delta(ndf)/ad(ndf);
for i=ndf-1:-1:1;
delta(i)=delta(i)-delta(i+1)*al(i)/ad(i+1);
z(i)=delta(i)/ad(i);
end
%
if bctype(1)==0
z=[0,z];    %given boundary value
end
if bctype(2)==0
z=[z,0];    %given boundary value
end
%
%computing D2z and cstab
%
Dz=(z(2:ni+1)-z(1:ni))./h;
DDz=abs(Dz(2:ni)-Dz(1:ni-1));
DDz=[DDz(1),DDz,DDz(ni-1)];
DDz=(DDz(2:ni+1)+DDz(1:ni))/2;
DDz=DDz./h;
cstab=max(cstab,sum(DDz.*h));
%
ad=adcopy;   %restoring diagonal in stiffness matrix
end
%
cstab
res=cstab*cint*res;
globalerr=max((h.^2).*res);
%
%setting ok flag
%
if globalerr<tol
ok=1;
else
ok=-1;
end
