## THIS IS AN AUTOMATICALLY GENERATED FILE --- DO NOT EDIT ---
## instead modify buildtests.sh to generate the tests that you want.
##
## sptest
##
##    run preset sparse tests.  All should pass.
function [passes,tests] = sptest
  disp('writing test output to sptest.log');
  test('sptest','normal','sptest.log');
endfunction


# ==============================================================


# ==============================================================


%!test # segfault test from edd@debian.org
%! n = 510;
%! sparse(kron((1:n)', ones(n,1)), kron(ones(n,1), (1:n)'), ones(n)); 

%% segfault tests from Fabian@isas-berlin.de
%!assert(spinv(sparse([1,1;1,1+i])),[1-1i,1i;1i,-1i],10*eps);
%!error spinv( sparse( [1,1;1,1]   ) );
%!error spinv( sparse( [0,0;0,1]   ) );
%!error spinv( sparse( [0,0;0,1+i] ) );
%!error spinv( sparse( [0,0;0,0]   ) );
%!error splu( sparse( [1,1;1,1]   ) );
%!error splu( sparse( [0,0;0,1]   ) );
%!error splu( sparse( [0,0;0,1+i] ) );
%!error splu( sparse( [0,0;0,0]   ) );
%!test
%! [l,u]=splu(sparse([1,1;1,1+i]));
%! assert(l,sparse([1,2,2],[1,1,2],1),10*eps);
%! assert(u,sparse([1,1,2],[1,2,2],[1,1,1i]),10*eps);

%% error handling in constructor
%!error sparse(1,[2,3],[1,2,3]);
%!error sparse([1,1],[1,1],[1,2],3,3,'bogus');
%!error sparse([1,3],[1,-4],[3,5],2,2);
%!error sparse([1,3],[1,-4],[3,5i],2,2);
%!error sparse(-1,-1,1);

# ==============================================================

%!shared bf
%!test bf=realmin;
%% Make sure newly introduced zeros get eaten
%!assert(nnz(sparse([bf,bf,1]).^realmax),1);
%!assert(nnz(sparse([1,bf,bf]).^realmax),1);
%!assert(nnz(sparse([bf,bf,bf]).^realmax),0);

%!assert(nnz(sparse([bf;bf;1]).^realmax),1);
%!assert(nnz(sparse([1;bf;bf]).^realmax),1);
%!assert(nnz(sparse([0.5;bf;bf]).^realmax),0);

%!assert(nnz(sparse([bf,bf,1])*realmin),1);
%!assert(nnz(sparse([1,bf,bf])*realmin),1);
%!assert(nnz(sparse([bf,bf,bf])*realmin),0);

%!assert(nnz(sparse([bf;bf;1])*realmin),1);
%!assert(nnz(sparse([1;bf;bf])*realmin),1);
%!assert(nnz(sparse([bf;bf;bf])*realmin),0)

%!test bf=realmin+realmin*1i;
%% Make sure newly introduced zeros get eaten
%!assert(nnz(sparse([bf,bf,1]).^realmax),1);
%!assert(nnz(sparse([1,bf,bf]).^realmax),1);
%!assert(nnz(sparse([bf,bf,bf]).^realmax),0);

%!assert(nnz(sparse([bf;bf;1]).^realmax),1);
%!assert(nnz(sparse([1;bf;bf]).^realmax),1);
%!assert(nnz(sparse([0.5;bf;bf]).^realmax),0);

%!assert(nnz(sparse([bf,bf,1])*realmin),1);
%!assert(nnz(sparse([1,bf,bf])*realmin),1);
%!assert(nnz(sparse([bf,bf,bf])*realmin),0);

%!assert(nnz(sparse([bf;bf;1])*realmin),1);
%!assert(nnz(sparse([1;bf;bf])*realmin),1);
%!assert(nnz(sparse([bf;bf;bf])*realmin),0)

%!assert(nnz(sparse([-1,realmin,realmin]).^1.5),1);
%!assert(nnz(sparse([-1,realmin,realmin,1]).^1.5),2);

%!assert(nnz(sparse(1,1,0)),0); # Make sure scalar v==0 doesn't confuse matters
%!assert(nnz(sparse(eye(3))*0),0);
%!assert(nnz(sparse(eye(3))-sparse(eye(3))),0);

%!assert(sparse(eye(3))/0,sparse([1:3],[1:3],Inf));


# ==============================================================

%!shared as,af,bx,bf
%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];
%!test bf=3;
%!test as=sparse(af);
%!test bx=bf;
%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%!test bx=bf=bf+1i;
%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)


# ==============================================================

%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];
%!test bf=[0,1-1i,0,0;2+1i,0,0,0;3-1i,2+3i,0,0];
%!test as=sparse(af);
%!test bx=bf;
%% Unary matrix tests (uses af,as)
%!assert(is_sparse(as),1)
%!assert(is_sparse(af),0)
%!assert(is_real_sparse(af),0)
%!assert(is_complex_sparse(af),0)
%!assert(spsum(as),sum(af))
%!assert(spsum(as,1),sum(af,1))
%!assert(spsum(as,2),sum(af,2))
%!assert(as==as)
%!assert(as==af)
%!assert(af==as)
%!test
%! [ii,jj,vv,nr,nc] = spfind(as);
%! assert(af,full(sparse(ii,jj,vv,nr,nc)));
%!assert(nnz(as),sum(af(:)!=0))
%!assert(nnz(as),nnz(af))
%!assert(is_real_sparse(spabs(as)))
%!assert(is_real_sparse(spreal(as)))
%!assert(is_real_sparse(spimag(as)))
%!assert(is_sparse(as.'))
%!assert(is_sparse(as'))
%!assert(is_sparse(-as))
%!assert(!is_sparse(~as))
%!assert(spabs(as),abs(af))
%!assert(spreal(as),real(af))
%!assert(spimag(as),imag(af))
%!assert(as.', af.');
%!assert(as',  af');
%!assert(-as, -af);
%!assert(~as, ~af); # fails on 2.1.52
%!error [i,j]=size(af);as(i-1,j+1);
%!error [i,j]=size(af);as(i+1,j-1);
%!test
%! [Is,Js,Vs] = spfind(as);
%! [If,Jf,Vf] = find(af);
%! assert(Is,If);
%! assert(Js,Jf);
%! assert(Vs,Vf);
%!error as(0,1);
%!error as(1,0);
%!assert(spfind(as),find(af))
%!test
%! [i,j,v] = spfind(as);
%! [m,n] = size(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!test
%! [i,j,v,m,n] = spfind(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!assert(issparse(sphcat(as,as)));
%!assert(issparse(spvcat(as,as)));
%!assert(sphcat(as,as), [af,af]);
%!assert(spvcat(as,as), [af;af]);
%!assert(sphcat(as,as,as), [af,af,af]);
%!assert(spvcat(as,as,as), [af;af;af]);

%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%% Matrix-matrix operators (uses af,as,bx,bf)
%!assert(as*bx',af*bf')
%!assert(bx'*as,bf'*af)
%!assert(as/bx,af/bf,100*eps)
%!test error("assert(bx\\as,bf\\af,100*eps) # \\ seg-faults for non-square");

%!test bx=sparse(bf);
%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%% Matrix-matrix operators (uses af,as,bx,bf)
%!assert(as*bx',af*bf')
%!assert(bx'*as,bf'*af)
%!assert(as/bx,af/bf,100*eps)
%!test error("assert(bx\\as,bf\\af,100*eps) # \\ seg-faults for non-square");


# ==============================================================

%!test bf=real(bf);
%!test as=sparse(af);
%!test bx=bf;
%% Unary matrix tests (uses af,as)
%!assert(is_sparse(as),1)
%!assert(is_sparse(af),0)
%!assert(is_real_sparse(af),0)
%!assert(is_complex_sparse(af),0)
%!assert(spsum(as),sum(af))
%!assert(spsum(as,1),sum(af,1))
%!assert(spsum(as,2),sum(af,2))
%!assert(as==as)
%!assert(as==af)
%!assert(af==as)
%!test
%! [ii,jj,vv,nr,nc] = spfind(as);
%! assert(af,full(sparse(ii,jj,vv,nr,nc)));
%!assert(nnz(as),sum(af(:)!=0))
%!assert(nnz(as),nnz(af))
%!assert(is_real_sparse(spabs(as)))
%!assert(is_real_sparse(spreal(as)))
%!assert(is_real_sparse(spimag(as)))
%!assert(is_sparse(as.'))
%!assert(is_sparse(as'))
%!assert(is_sparse(-as))
%!assert(!is_sparse(~as))
%!assert(spabs(as),abs(af))
%!assert(spreal(as),real(af))
%!assert(spimag(as),imag(af))
%!assert(as.', af.');
%!assert(as',  af');
%!assert(-as, -af);
%!assert(~as, ~af); # fails on 2.1.52
%!error [i,j]=size(af);as(i-1,j+1);
%!error [i,j]=size(af);as(i+1,j-1);
%!test
%! [Is,Js,Vs] = spfind(as);
%! [If,Jf,Vf] = find(af);
%! assert(Is,If);
%! assert(Js,Jf);
%! assert(Vs,Vf);
%!error as(0,1);
%!error as(1,0);
%!assert(spfind(as),find(af))
%!test
%! [i,j,v] = spfind(as);
%! [m,n] = size(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!test
%! [i,j,v,m,n] = spfind(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!assert(issparse(sphcat(as,as)));
%!assert(issparse(spvcat(as,as)));
%!assert(sphcat(as,as), [af,af]);
%!assert(spvcat(as,as), [af;af]);
%!assert(sphcat(as,as,as), [af,af,af]);
%!assert(spvcat(as,as,as), [af;af;af]);

%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%% Matrix-matrix operators (uses af,as,bx,bf)
%!assert(as*bx',af*bf')
%!assert(bx'*as,bf'*af)
%!assert(as/bx,af/bf,100*eps)
%!test error("assert(bx\\as,bf\\af,100*eps) # \\ seg-faults for non-square");

%!test bx=sparse(bf);
%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%% Matrix-matrix operators (uses af,as,bx,bf)
%!assert(as*bx',af*bf')
%!assert(bx'*as,bf'*af)
%!assert(as/bx,af/bf,100*eps)
%!test error("assert(bx\\as,bf\\af,100*eps) # \\ seg-faults for non-square");


# ==============================================================

%!test af=real(af);
%!test as=sparse(af);
%!test bx=bf;
%% Unary matrix tests (uses af,as)
%!assert(is_sparse(as),1)
%!assert(is_sparse(af),0)
%!assert(is_real_sparse(af),0)
%!assert(is_complex_sparse(af),0)
%!assert(spsum(as),sum(af))
%!assert(spsum(as,1),sum(af,1))
%!assert(spsum(as,2),sum(af,2))
%!assert(as==as)
%!assert(as==af)
%!assert(af==as)
%!test
%! [ii,jj,vv,nr,nc] = spfind(as);
%! assert(af,full(sparse(ii,jj,vv,nr,nc)));
%!assert(nnz(as),sum(af(:)!=0))
%!assert(nnz(as),nnz(af))
%!assert(is_real_sparse(spabs(as)))
%!assert(is_real_sparse(spreal(as)))
%!assert(is_real_sparse(spimag(as)))
%!assert(is_sparse(as.'))
%!assert(is_sparse(as'))
%!assert(is_sparse(-as))
%!assert(!is_sparse(~as))
%!assert(spabs(as),abs(af))
%!assert(spreal(as),real(af))
%!assert(spimag(as),imag(af))
%!assert(as.', af.');
%!assert(as',  af');
%!assert(-as, -af);
%!assert(~as, ~af); # fails on 2.1.52
%!error [i,j]=size(af);as(i-1,j+1);
%!error [i,j]=size(af);as(i+1,j-1);
%!test
%! [Is,Js,Vs] = spfind(as);
%! [If,Jf,Vf] = find(af);
%! assert(Is,If);
%! assert(Js,Jf);
%! assert(Vs,Vf);
%!error as(0,1);
%!error as(1,0);
%!assert(spfind(as),find(af))
%!test
%! [i,j,v] = spfind(as);
%! [m,n] = size(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!test
%! [i,j,v,m,n] = spfind(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!assert(issparse(sphcat(as,as)));
%!assert(issparse(spvcat(as,as)));
%!assert(sphcat(as,as), [af,af]);
%!assert(spvcat(as,as), [af;af]);
%!assert(sphcat(as,as,as), [af,af,af]);
%!assert(spvcat(as,as,as), [af;af;af]);

%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%% Matrix-matrix operators (uses af,as,bx,bf)
%!assert(as*bx',af*bf')
%!assert(bx'*as,bf'*af)
%!assert(as/bx,af/bf,100*eps)
%!test error("assert(bx\\as,bf\\af,100*eps) # \\ seg-faults for non-square");

%!test bx=sparse(bf);
%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%% Matrix-matrix operators (uses af,as,bx,bf)
%!assert(as*bx',af*bf')
%!assert(bx'*as,bf'*af)
%!assert(as/bx,af/bf,100*eps)
%!test error("assert(bx\\as,bf\\af,100*eps) # \\ seg-faults for non-square");


# ==============================================================

%!test bf=bf+1i*(bf~=0);
%!test as=sparse(af);
%!test bx=bf;
%% Unary matrix tests (uses af,as)
%!assert(is_sparse(as),1)
%!assert(is_sparse(af),0)
%!assert(is_real_sparse(af),0)
%!assert(is_complex_sparse(af),0)
%!assert(spsum(as),sum(af))
%!assert(spsum(as,1),sum(af,1))
%!assert(spsum(as,2),sum(af,2))
%!assert(as==as)
%!assert(as==af)
%!assert(af==as)
%!test
%! [ii,jj,vv,nr,nc] = spfind(as);
%! assert(af,full(sparse(ii,jj,vv,nr,nc)));
%!assert(nnz(as),sum(af(:)!=0))
%!assert(nnz(as),nnz(af))
%!assert(is_real_sparse(spabs(as)))
%!assert(is_real_sparse(spreal(as)))
%!assert(is_real_sparse(spimag(as)))
%!assert(is_sparse(as.'))
%!assert(is_sparse(as'))
%!assert(is_sparse(-as))
%!assert(!is_sparse(~as))
%!assert(spabs(as),abs(af))
%!assert(spreal(as),real(af))
%!assert(spimag(as),imag(af))
%!assert(as.', af.');
%!assert(as',  af');
%!assert(-as, -af);
%!assert(~as, ~af); # fails on 2.1.52
%!error [i,j]=size(af);as(i-1,j+1);
%!error [i,j]=size(af);as(i+1,j-1);
%!test
%! [Is,Js,Vs] = spfind(as);
%! [If,Jf,Vf] = find(af);
%! assert(Is,If);
%! assert(Js,Jf);
%! assert(Vs,Vf);
%!error as(0,1);
%!error as(1,0);
%!assert(spfind(as),find(af))
%!test
%! [i,j,v] = spfind(as);
%! [m,n] = size(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!test
%! [i,j,v,m,n] = spfind(as);
%! x = sparse(i,j,v,m,n);
%! assert(x,as);
%!assert(issparse(sphcat(as,as)));
%!assert(issparse(spvcat(as,as)));
%!assert(sphcat(as,as), [af,af]);
%!assert(spvcat(as,as), [af;af]);
%!assert(sphcat(as,as,as), [af,af,af]);
%!assert(spvcat(as,as,as), [af;af;af]);

%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%% Matrix-matrix operators (uses af,as,bx,bf)
%!assert(as*bx',af*bf')
%!assert(bx'*as,bf'*af)
%!assert(as/bx,af/bf,100*eps)
%!test error("assert(bx\\as,bf\\af,100*eps) # \\ seg-faults for non-square");

%!test bx=sparse(bf);
%% Elementwise binary tests (uses as,af,bx,bf)
%!assert(as==bx,af==bf)
%!assert(bx==as,bf==af)
%!assert(as!=bx,af!=bf)
%!assert(bx!=as,bf!=af)
%!assert(as+bx,af+bf)
%!assert(bx+as,bf+af)
%!assert(as-bx,af-bf)
%!assert(bx-as,bf-af)
%!assert(as.*bx,af.*bf)
%!assert(bx.*as,bf.*af)
%!assert(as./bx,af./bf,100*eps)
%!assert(bx.\as,bf.\af,100*eps)
%!test
%! sv = as.^bx;
%! fv = af.^bf;
%! idx = find(af~=0);
%! assert(sv(:)(idx),fv(:)(idx),100*eps)

%% Matrix-matrix operators (uses af,as,bx,bf)
%!assert(as*bx',af*bf')
%!assert(bx'*as,bf'*af)
%!assert(as/bx,af/bf,100*eps)
%!test error("assert(bx\\as,bf\\af,100*eps) # \\ seg-faults for non-square");


# ==============================================================

%!test af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];
%!test bf=[0,1-1i,0,0;2+1i,0,0,0;3-1i,2+3i,0,0];
%!test ;# invertible matrix
%! bf=af'*bf+max(abs([af(:);bf(:)]))*sparse(eye(columns(as)));
%! bx=sparse(bf);

%!test ;# permuted LU
%! [L,U] = splu(bx);
%! assert(L*U,bx,1e-10);

%!test ;# simple LU + permutations
%! [L,U,pr,pc] = splu(bx);
%! assert(pr'*L*U*pc,bx,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# inverse
%! assert(spinv(bx)*bx,eye(rows(bx)),1e-10);

%!test error("assert(bx\\as,bf\\af,100*eps); # failing");
%!test error("assert(bf\\as,bf\\af,100*eps); # failing");
%!test error("assert(bx\\af,bf\\af,100*eps); # failing");


# ==============================================================

%!test bf=real(bf);
%!test ;# permuted LU
%! [L,U] = splu(bx);
%! assert(L*U,bx,1e-10);

%!test ;# simple LU + permutations
%! [L,U,pr,pc] = splu(bx);
%! assert(pr'*L*U*pc,bx,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# inverse
%! assert(spinv(bx)*bx,eye(rows(bx)),1e-10);

%!test error("assert(bx\\as,bf\\af,100*eps); # failing");
%!test error("assert(bf\\as,bf\\af,100*eps); # failing");
%!test error("assert(bx\\af,bf\\af,100*eps); # failing");


# ==============================================================

%!test af=real(af);
%!test ;# permuted LU
%! [L,U] = splu(bx);
%! assert(L*U,bx,1e-10);

%!test ;# simple LU + permutations
%! [L,U,pr,pc] = splu(bx);
%! assert(pr'*L*U*pc,bx,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# inverse
%! assert(spinv(bx)*bx,eye(rows(bx)),1e-10);

%!test error("assert(bx\\as,bf\\af,100*eps); # failing");
%!test error("assert(bf\\as,bf\\af,100*eps); # failing");
%!test error("assert(bx\\af,bf\\af,100*eps); # failing");


# ==============================================================

%!test bf=bf+1i*(bf~=0);
%!test ;# permuted LU
%! [L,U] = splu(bx);
%! assert(L*U,bx,1e-10);

%!test ;# simple LU + permutations
%! [L,U,pr,pc] = splu(bx);
%! assert(pr'*L*U*pc,bx,1e-10);
%! # triangularity
%! [i,j,v]=spfind(L);
%! assert(i-j>=0);
%! [i,j,v]=spfind(U);
%! assert(j-i>=0);

%!test ;# inverse
%! assert(spinv(bx)*bx,eye(rows(bx)),1e-10);

%!test error("assert(bx\\as,bf\\af,100*eps); # failing");
%!test error("assert(bf\\as,bf\\af,100*eps); # failing");
%!test error("assert(bx\\af,bf\\af,100*eps); # failing");


# ==============================================================

%!shared r,c,m,n,fsum,funiq
%!test
%! r=[1,1,2,1,2,3];
%! c=[2,1,1,1,2,1];
%! m=n=0;


# ==============================================================

%!shared ridx,cidx,idx,as,af
%!test
%! af=[1+1i,2-1i,0,0;0,0,0,3+2i;0,0,0,4];
%! ridx=[1,3]; cidx=[2,3];
%!test as=sparse(af);

%% Point tests
%!test idx=ridx(:)+rows(as)*(cidx(:)-1);
%!assert(issparse(as(idx))); # not yet implemented
%!assert(as(idx),af(idx));
%!assert(as(idx'),af(idx')); # not yet implemented
%!assert(as([idx,idx]),af([idx,idx])); # not yet implemented
%!assert(as(reshape([idx;idx],[1,length(idx),2])),af([idx',idx']));

%% Slice tests
%!assert(as(ridx,cidx),af(ridx,cidx))
%!assert(as(ridx,:), af(ridx,:))
%!assert(as(:,cidx), af(:,cidx))
%!assert(as(:,:), af(:,:))

%% Test 'end' keyword
%!assert(as(end),af(end)) # not yet implemented
%!assert(as(1,end), af(1,end)) # not yet implemented
%!assert(as(end,1), af(end,1)) # not yet implemented
%!assert(as(end,end), af(end,end))
%!assert(as(2:end,2:end), af(2:end,2:end))
%!assert(as(1:end-1,1:end-1), af(1:end-1,1:end-1))
%!test af=real(af);
%!test as=sparse(af);

%% Point tests
%!test idx=ridx(:)+rows(as)*(cidx(:)-1);
%!assert(issparse(as(idx))); # not yet implemented
%!assert(as(idx),af(idx));
%!assert(as(idx'),af(idx')); # not yet implemented
%!assert(as([idx,idx]),af([idx,idx])); # not yet implemented
%!assert(as(reshape([idx;idx],[1,length(idx),2])),af([idx',idx']));

%% Slice tests
%!assert(as(ridx,cidx),af(ridx,cidx))
%!assert(as(ridx,:), af(ridx,:))
%!assert(as(:,cidx), af(:,cidx))
%!assert(as(:,:), af(:,:))

%% Test 'end' keyword
%!assert(as(end),af(end)) # not yet implemented
%!assert(as(1,end), af(1,end)) # not yet implemented
%!assert(as(end,1), af(end,1)) # not yet implemented
%!assert(as(end,end), af(end,end))
%!assert(as(2:end,2:end), af(2:end,2:end))
%!assert(as(1:end-1,1:end-1), af(1:end-1,1:end-1))

# ==============================================================

