function testtjsr(flag)
% testtjsr( [flag] )
% Demonstrates how to use the tjsr-packge and tests if the tjsr-package works correctly
% if flag is 1, then the functions in the package are tested.
%
% See also: testsubdivision, tjsr
%
% Written by: tommsch, 2018

%#ok<*NOPRT>
%#ok<*ASGLU>
%#ok<*NASGU>

if(nargin>=1 && flag); selftesttjsr; return; end;

errorstring=vprintf('Either start the function with argument <1> or read the source code of this file.\n'); 
return; 
2; %#ok<UNRCH>

%To see how the tjsr package works, see also testsubdivision

end

function selftesttjsr

    errorstring='';
    
    vprintf('The tjsr-package will be tested.\n','cpr',[.1 .4 .1]);
    vprintf('If there is some error or un-commented output, the test fails.\n\n','cpr',[.1 .4 .1]);
    
    addcombination({[1 1 2],[1]});
    
    binarymatrix(2, 2, 2^5+2^3+2^2+2^1);
    
    if(~isequal(blockjsr([1 0],[.5 2]),[.5 2])); errorstring=vprintf('blockjsr() is broken.\n\n','cpr','err','str',errorstring); end;
    
    if(~islogical(chooseval(randn(1,100),[10 50]))); errorstring=vprintf('chooseval() is broken.\n\n','cpr','err','str',errorstring); end;
    
    figure;
    C=codecapacity({[1 1 0],[1 -1 0]},'verbose',2);
    vprintf('There should be text output.\n\n','cpr',[.1 .4 .1]);
    vprintf('FIGURE: A graph may be plotted.\n\n','cpr',[.4 .2 0]);
    
    val=computepolytopenorm([1 1; 3 0]', [1 0; 0 1]', 1, 0, 2);
    if(norm(val-[2 3])>10e-12); errorstring=vprintf('computepolytopenorm() is broken.\n\n','cpr','err','str',errorstring); end;    
    
    daubechiesmatrix(2);
    vprintf('There should be text output.\n\n','cpr',[.1 .4 .1]);
    
    estimatejsr({randn(2), randn(2)})
    vprintf('There should be two numbers text output.\n\n','cpr',[.1 .4 .1]);
    
    [normval,~]=estimatepolytopenorm(randn(3,10), randn(3,10), [], 11);
    
    [ c, nc, info]=findsmp({[1 -1; 3 -2], [1 3; -1 -1]},'maxsmpdepth',15);
    vprintf('There should be text output.\n\n','cpr',[.1 .4 .1]);
    if(~isequal(c{1}, [1;2]) && ~isequal(c{1}, [2;1])); errorstring=vprintf('findsmp() broken.\n\n','cpr','err','str',errorstring); end;    
    
    val=intersectinterval([4 5],[1 4],[-inf 4]);
    if(~isequal(val,[4 4])); errorstring=vprintf('intersectinterval() broken.\n\n','cpr','err','str',errorstring); end;    
    
    [M,B]=invariantsubspace({[1 1 1; 1 0 1; 0 1 0], [1 -1 0; 0 2 0; 1 1 2]},'verbose',0);
    if(size(M,2)~=3); errorstring=vprintf('invariantsubspace() broken.\n\n','cpr','err','str',errorstring); end;    
    
    [M,v0,v0s,mult,oo]=leadingeigenvector({[1 1 ; 0 1],[0 0; 1 0]},{[1 2]',[1]'},'verbose',0);
    
    %this tests makeorderinggraph
    tjsr(tgallery('rand_rho',3,2,100),'plot','tree','autoextravertex',0);
    vprintf('FIGURE: Figure 1 should change to a graph.\n\n','cpr',[.4 .2 0]);
    figure;
    findsmp(tgallery('rand_rho',2,2,10),'maxsmpdepth',10,'plot','tree','N',2)
    vprintf('FIGURE: There should be a graph plotted.\n\n','cpr',[.4 .2 0]);
    figure;
    oo=[1 1 1 2 1 2 2 2 1 1;0 2 1 0 2 1 2 1 1 2;0 0 0 0 1 0 0 1 2 1]; makeorderinggraph(oo,'lableedge');
    vprintf('FIGURE: There should be a graph plotted.\n\n','cpr',[.4 .2 0]);
    
    
    val=makepositive([ 0 -1 2]);
    if(~isequal(val, [0 1 -2])); errorstring=vprintf('makepositive() broken.\n\n','cpr','err','str',errorstring); end;    
    
    figure;
    [chain, MST]=partitionatepolytope(randn(2,100),1,1); 
    vprintf('There should be text output.\n','cpr',[.1 .4 .1]);
    vprintf('FIGURE: There may be a graph plotted.\n\n','cpr',[.4 .2 0]);

    preprocessmatrix({[-1 2; 2 3],[-1 2; 2 3]});
    
    val=reducelength({[2 1 2 2 1 2],[3 1]});
    if(~isequal(val,{[1 2 2],[1 3]})); errorstring=vprintf('reducelength() broken.\n\n','cpr','err','str',errorstring); end;    
    
    val=removecombination({[1 1 2],[1]});
    if(size(val,2)~=2 || ~searchincellarray(2,val,1) || ~searchincellarray(1,val,1)); errorstring=vprintf('removecombination() is broken.\n\n','cpr','err','str',errorstring); end;    
   
    val=tbuildproduct_fast({2 4 5},[1 0 3]);
    if(~isequal(val,10)); errorstring=vprintf('tbuildproduct_fast() is broken.\n\n','cpr','err','str',errorstring); end;
    
    %testtjsr %this file
    
    A=tgallery('rand_rho',3,2,100);
    
    figure;
    [JSR,info]=tjsr(A,'plot','polytope','proof',1,'verbose',1);
    vprintf('There should be a lot of text output, probably with red color too.\n','cpr',[.1 .4 .1]);
    vprintf('There should be no error which sounds serious.\n','cpr',[.1 .4 .1]);
    vprintf('FIGURE: There should be a polytope plotted.\n\n','cpr',[.4 .2 0]);
    if(info.info.errorcode~=-10 || numel(JSR)>1); errorstring=vprintf('tjsr() may be broken. Test tjsr() again yourself.\n\n','cpr','err','str',errorstring); end;
    
    %tjsr_ %this files are not tested
    
    val=unique(cell2mat(tliftproduct({2 3 5}, 2)));
    if(~isequal(val, [1 2 3 4 5 6 9 10 15 25])); errorstring=vprintf('tliftproduct() broken.\n\n','cpr','err','str',errorstring); end;
    
    val=tliftsemidefinite({[1 1;0 1] [1 0; 1 0]}, 2);
    if(~isequal(val, {[1 4 4 2 4 1;0 1 2 1 3 1;0 0 1 0 2 1;0 0 0 1 2 1;0 0 0 0 1 1;0 0 0 0 0 1]; [ones(6,1) zeros(6,5)]})); errorstring=vprintf('tliftsemidefinite() broken.\n\n','cpr','err','str',errorstring); end;
    
    if(norm(trho(A{1})-1)>10e-12); errorstring=vprintf('trho() is broken.\n\n','cpr','err','str',errorstring); end;
    
    %Gurobi functions
    try
        gurobi_setup;% test gurobi
        vprintf('There should be a lot of text output.\n\n','cpr',[.1 .4 .1]);
    catch
        errorstring=vprintf('Gurobi is broken / not installed.\n\n','cpr','err','str',errorstring);
    end
    
    %Sedumi functions
    try
        load arch0
        [x,y,info] = sedumi(At,b,c,K);
        vprintf('There should be a lot of text output.\n\n','cpr',[.1 .4 .1]);
    catch
        errorstring=vprintf('SeDuMi is broken / not installed.\n\n','cpr','err','str',errorstring);
    end
    
    %JSR-toolbox functions
    try
        jsr({randn(2),randn(2)});
        vprintf('There should be text output.\n\n','cpr',[.1 .4 .1]);
    catch
        errorstring=vprintf('JSR-toolbox is broken / not installed.\n\n','cpr','err','str',errorstring);
    end
    
    if(~isempty(errorstring));
        vprintf(errorstring,'cpr','err');
    end
end

function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing. 

